You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.1 KiB
Python

import cv2
from motion_detection import detect_motion
from database import log_event
class Camera:
def __init__(self, camera_id, source=0):
self.camera_id = camera_id
self.capture = cv2.VideoCapture(source)
self.is_running = True
def start_stream(self):
background_frame = None
while self.is_running:
ret, frame = self.capture.read()
if not ret:
break
if background_frame is None:
background_frame = frame
moving_objects = detect_motion(frame, background_frame)
if moving_objects:
log_event(self.camera_id, moving_objects)
def get_frame(self):
while self.is_running:
ret, frame = self.capture.read()
if ret:
_, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
def stop_stream(self):
self.is_running = False
self.capture.release()