From 272685979d8d91d637e4945e8c3a9bfab12abaa2 Mon Sep 17 00:00:00 2001 From: Max McKelvey Date: Tue, 14 Nov 2023 10:43:15 -0800 Subject: [PATCH] added working motion detection from framegrab --- api/gl_process.py | 12 +++++++++++- app/detectors/page.tsx | 10 +++++++--- components/EditDetectorOverlay.tsx | 21 +++++++++++++++++++-- utils/types.ts | 2 ++ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/api/gl_process.py b/api/gl_process.py index 2958495..734349a 100644 --- a/api/gl_process.py +++ b/api/gl_process.py @@ -7,6 +7,7 @@ import multiprocessing from api.notifications import send_notifications import logging +import framegrab def frame_to_base64(frame) -> str: # encode image as jpeg @@ -47,9 +48,14 @@ def run_process(idx: int, logger: logging.Logger, detector: dict, api_key: str, delay = lambda: time.sleep(poll_delay) cycle_time = 30 + motion = None + if trigger_type == "motion": # TODO: implement - raise ValueError(f"Trigger type [{trigger_type}] not yet supported.") + if detector["config"]["cycle_time"] < poll_delay: + poll_delay = detector["config"]["cycle_time"] + cycle_time = detector["config"]["cycle_time"] + motion = framegrab.motion.MotionDetector(detector["config"]["motion_percent"], detector["config"]["motion_threshold"]) elif trigger_type == "time": if detector["config"]["cycle_time"] < poll_delay: poll_delay = detector["config"]["cycle_time"] @@ -87,6 +93,10 @@ def run_process(idx: int, logger: logging.Logger, detector: dict, api_key: str, logger.warn("Frame recieved as None.") continue + if motion is not None and not motion.motion_detected(frame): + time.sleep(cycle_time) + continue + # send to groundlight query = gl.submit_image_query(det, frame, 0) # default wait is 30s diff --git a/app/detectors/page.tsx b/app/detectors/page.tsx index 29040c9..04d4574 100644 --- a/app/detectors/page.tsx +++ b/app/detectors/page.tsx @@ -101,7 +101,7 @@ export default function Home() { {group[0].name} @@ -153,8 +153,12 @@ export default function Home() {
{detector.config.pin}
Pin Active State:
{detector.config.pin_active_state == 1 ? "HIGH" : "LOW"}
-
{"Cycle Time:"}
-
{detector.config.cycle_time + "s"}
+
{"Cycle Time:"}
+
{detector.config.cycle_time + "s"}
+
{"Frame Percent:"}
+
{(detector.config.cycle_time ? detector.config.cycle_time * 10 : 100) + "%"}
+
{"Threshold:"}
+
{detector.config.cycle_time}
diff --git a/components/EditDetectorOverlay.tsx b/components/EditDetectorOverlay.tsx index bc72ac1..f48c7e6 100644 --- a/components/EditDetectorOverlay.tsx +++ b/components/EditDetectorOverlay.tsx @@ -15,6 +15,8 @@ export const EditDetectorOverlay = ({ detector, detectors, index, startWithNew, const [cycleTime, setCycleTime] = useState(detector.config.cycle_time); const [pin, setPin] = useState(detector.config.pin); const [pinActiveState, setPinActiveState] = useState(detector.config.pin_active_state); + const [motionPercent, setMotionPercent] = useState(detector.config.motion_percent || 0.3); + const [motionThreshold, setMotionThreshold] = useState(detector.config.motion_threshold || 50); useEffect(() => { setNewDetector(startWithNew || false); @@ -70,15 +72,28 @@ export const EditDetectorOverlay = ({ detector, detectors, index, startWithNew,
Trigger Type:
- setTriggerType(e)} /> + setTriggerType(e)} />
{ - triggerType === "time" && + (triggerType === "time" || triggerType === "motion") &&
Cycle Time:
setCycleTime(parseInt(e.target.value))} min={0} />
} + { + triggerType === "motion" && +
+
+
Motion Percent:
+ setMotionPercent(parseFloat(e.target.value))} min={0} /> +
+
+
Motion Threshold:
+ setMotionThreshold(parseInt(e.target.value))} min={0} /> +
+
+ } { triggerType === "pin" &&
@@ -109,6 +124,8 @@ export const EditDetectorOverlay = ({ detector, detectors, index, startWithNew, pin, pin_active_state: pinActiveState, notifications: detector.config.notifications, + motion_percent: motionPercent, + motion_threshold: motionThreshold, } }, index: index, diff --git a/utils/types.ts b/utils/types.ts index 0fa74b8..b401397 100644 --- a/utils/types.ts +++ b/utils/types.ts @@ -41,6 +41,8 @@ type DetConfType = { pin?: number; pin_active_state?: PinState; notifications?: NotificationOptionsType; + motion_percent?: number; + motion_threshold?: number; }; type DetType = {