Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
initialized the database and edited the camera to save to the database
  • Loading branch information
Mubaraq Olayinka committed Jul 21, 2024
2 parents 444a41b + 05275ce commit 93ea0a6
Show file tree
Hide file tree
Showing 3 changed files with 28,678 additions and 3 deletions.
13 changes: 13 additions & 0 deletions ServiceAccountKey.json
@@ -0,0 +1,13 @@
{
"type": "service_account",
"project_id": "only-motion-detector",
"private_key_id": "32ab7946c1ef340abe95c885676fffe99354f047",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCbPydFTdbYai+e\nvekqdNbJLCJCfse6S3PkTShOt71Z7BkiBiWY5Ze7cvrcTdwJy33wN7ElcArDzYcF\nSBlq3HF/xNK5v5YS+Hpf3DVzisUJDroTg7BEtNhjwpAZzC4lfCjINAxJX4yxz9EA\n7Mdj+Eth4w7QDboy3cG0up3AWNdAoDXMGesNiQTJ1KbiP+3PMTE+UlJ5XmUs4tbF\n2lNQ5mPzBn6lq5yNPwGCfVu9u+KuhKFNvZOawNWrCprlFQ1fU0T6Ct43Zze+ewJE\nWu/oia7Fj/w0MuPcJ9oCmXBkB4pIV0ewxdWrV0qrafgnAn+bIheMhlZJ/mpIfnH5\nqAnW0SR5AgMBAAECggEAEWs7Q3IXEnPiS/QQXrclXQUaraNT/aR2/b1lMbqOqwbr\n+v6oO/ORhsgWkElxeu0zgD8qHS0+cqHI7eAdGfm0sm//GjWo57qsNb1GMg9L9Fbh\nV0yF+++JUrLYlaHB5mrRQ0Vea40mWCfH0kwUQzSaIK2Sp+giZZAEKqcVtCiZCPhu\nvJw9pUHBqAiDyXZwAILRTR1kgQp06a1TRUxd7aC5OctiwwRwXR5lKRR9xP/vM8MZ\n8IuvrymUiAxZLlgI1i4wYN7CLgC7ia2rymHsCj9g7KyWdk6kXlSdIctTbqvIYMET\nWRtK8w/HdxfPfugzMV9NEk+LnS/sjkQeihIu7LaAEQKBgQDUkfWKhjqIvyHayeph\nhTk3ReQsiOyOUOG+wVEwRWiOGM2Bt1f+ph52hZWJ37lE5z3uS7d1+ls4f3OednAb\n19AbT2Aa3fA3n4ga5v0KpEsFfBeE9SuyGjtRgmeF27ripQU+uiWz7Jh3DlcuSbHn\nQID5k3YERk6yMAX46lrzYMAiQwKBgQC69wJ1jPGvRbbLp6UDNEkld53zHmOdEDGp\nT2wDRXgejK6MMFDOisAZGCRvu3YxNxyhhTGCZUL/As2rzUAC0BlU3nRBBnWZxrX/\nXISeDdyzLCCr4JLeTxiOlzPqdoB9y79BSUxj5yCutBNw7JjjlzZExPXXTcl3ouof\n593JfaEokwKBgGgs1/75ZXpa4UaOKfV9CttRv08sE3JzpQ6NQzK68VCiSyTFK7S6\nyLchJHYbVHq2MR21scV52nbHz2vbc3nRvEWGEv1Xw0AfSN6WUXCDr9iSRhx84ts4\nC9YYaI+rbd1hLj7dj1isHNRMDOG8cBl6/TpS0EWsBylAa8zfXY9k3QRtAoGBAKLz\nLBqF6BMJ4KKpD2G2jVXxZiGBx/IZLh9RkGNdha6ikHKwnY0WUwzMBN249e85CGQi\nMR9j4DtgFx94eSS6eImGhK4z0gVfsjnJSfBzRnqGqEcFKRxGtbF5SQ89jwmUrbFa\nEBE2XT7GxfbyzuH1ZaAURewL6N6zh/ic3qmBLBevAoGAUohdreRXIXSd1EfQ5p0u\nWLmHQ4nYPTc71Mr0K4vyqCSXHOsFybjG+3i1fHSYTWRqLxq/Za4LRomrloUt6diA\nfig6kw8nK39JQa8A2QrPaLqcwQTv0inpWHPRLieYaUHDQnlx3J8Nxk7eGykyFLm4\nPCqmPQOzT4odBB2ShLoIJlM=\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-7cnbe@only-motion-detector.iam.gserviceaccount.com",
"client_id": "107965334749798166149",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-7cnbe%40only-motion-detector.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}
105 changes: 102 additions & 3 deletions camera.py
Expand Up @@ -3,19 +3,30 @@
import paho.mqtt.client as mqtt
import os
from datetime import datetime
import firebase_admin
from firebase_admin import credentials, storage

# MQTT settings
MQTT_BROKER = "broker.hivemq.com"
MQTT_PORT = 1883
MQTT_TOPIC = "home/camera_motion"

# Firebase settings
FIREBASE_CREDENTIALS_PATH = "ServiceAccountKey.json"
FIREBASE_STORAGE_BUCKET = "only-motion-detector.appspot.com"

# Initialize Firebase Admin SDK
cred = credentials.Certificate(FIREBASE_CREDENTIALS_PATH)
firebase_admin.initialize_app(cred, {'storageBucket': FIREBASE_STORAGE_BUCKET})
bucket = storage.bucket()

# Initialize MQTT client
client = mqtt.Client()
client.connect(MQTT_BROKER, MQTT_PORT)

# Initialize the camera
# Initialize the camera
macbook_camera = cv2.VideoCapture(0)
iphone_camera = cv2.VideoCapture(1)
iphone_camera = cv2.VideoCapture(1)

# Get the video frame width and height for camera
macbook_camera.set(cv2.CAP_PROP_FRAME_WIDTH, 740)
Expand All @@ -24,7 +35,6 @@
iphone_camera.set(cv2.CAP_PROP_FRAME_WIDTH, 740)
iphone_camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)


frame_width_macbook = int(macbook_camera.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height_macbook = int(macbook_camera.get(cv2.CAP_PROP_FRAME_HEIGHT))

Expand Down Expand Up @@ -58,3 +68,92 @@
# Read the first frame from iPhone camera (if available)
ret_iphone, frame1_iphone = iphone_camera.read()
ret_iphone, frame2_iphone = iphone_camera.read()

while True:
# Process MacBook camera frames
if ret_macbook:
# Compute the absolute difference between the two frames
diff_macbook = cv2.absdiff(frame1_macbook, frame2_macbook)
gray_macbook = cv2.cvtColor(diff_macbook, cv2.COLOR_BGR2GRAY)
blur_macbook = cv2.GaussianBlur(gray_macbook, (5, 5), 0)
_, thresh_macbook = cv2.threshold(blur_macbook, 20, 255, cv2.THRESH_BINARY)
dilated_macbook = cv2.dilate(thresh_macbook, None, iterations=3)
contours_macbook, _ = cv2.findContours(dilated_macbook, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

motion_detected_macbook = False
for contour in contours_macbook:
if cv2.contourArea(contour) < 500:
continue
(x, y, w, h) = cv2.boundingRect(contour)
cv2.rectangle(frame1_macbook, (x, y), (x + w, y + h), (0, 255, 0), 2)
motion_detected_macbook = True

# If motion is detected, publish to MQTT and print message
if motion_detected_macbook:
client.publish(MQTT_TOPIC, "Motion Detected (MacBook)")
print("Motion Detected (MacBook)")

# Write the frame to the output file for MacBook camera
out_macbook.write(frame1_macbook)

# Show the frame with contours for MacBook camera
cv2.imshow("Feed (MacBook)", frame1_macbook)

# Update the frames for MacBook camera
frame1_macbook = frame2_macbook
ret_macbook, frame2_macbook = macbook_camera.read()

# Process iPhone camera frames (if available)
if ret_iphone:
# Compute the absolute difference between the two frames (example)
diff_iphone = cv2.absdiff(frame1_iphone, frame2_iphone)
gray_iphone = cv2.cvtColor(diff_iphone, cv2.COLOR_BGR2GRAY)
blur_iphone = cv2.GaussianBlur(gray_iphone, (5, 5), 0)
_, thresh_iphone = cv2.threshold(blur_iphone, 20, 255, cv2.THRESH_BINARY)
dilated_iphone = cv2.dilate(thresh_iphone, None, iterations=3)
contours_iphone, _ = cv2.findContours(dilated_iphone, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

motion_detected_iphone = False
for contour in contours_iphone:
if cv2.contourArea(contour) < 500:
continue
(x, y, w, h) = cv2.boundingRect(contour)
cv2.rectangle(frame1_iphone, (x, y), (x + w, y + h), (0, 255, 0), 2)
motion_detected_iphone = True

# If motion is detected, publish to MQTT and print message
if motion_detected_iphone:
client.publish(MQTT_TOPIC, "Motion Detected (iPhone)")
print("Motion Detected (iPhone)")

# Write the frame to the output file for iPhone camera (if available)
if iphone_camera.isOpened():
out_iphone.write(frame1_iphone)

# Show the frame with contours for iPhone camera (if available)
cv2.imshow("Feed (iPhone)", frame1_iphone)

# Update the frames for iPhone camera (if available)
frame1_iphone = frame2_iphone
ret_iphone, frame2_iphone = iphone_camera.read()

# Exit on 'q' key press
if cv2.waitKey(10) & 0xFF == ord('q'):
break

# Upload to Firebase Cloud Storage
def upload_to_firebase(file_path, blob_name):
blob = bucket.blob(blob_name)
blob.upload_from_filename(file_path)
print(f'File {file_path} uploaded to {blob_name}.')

upload_to_firebase(mp4_video_path_macbook, f'motion_detected_macbook_{timestamp}.mp4')
upload_to_firebase(mp4_video_path_iphone, f'motion_detected_iphone_{timestamp}.mp4')

# Clean up
macbook_camera.release()
out_macbook.release()
if iphone_camera.isOpened():
iphone_camera.release()
out_iphone.release()
cv2.destroyAllWindows()

0 comments on commit 93ea0a6

Please sign in to comment.