Skip to content

Commit

Permalink
added working motion detection from framegrab
Browse files Browse the repository at this point in the history
  • Loading branch information
max-at-groundlight committed Nov 14, 2023
1 parent 52aee52 commit 2726859
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
12 changes: 11 additions & 1 deletion api/gl_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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

Expand Down
10 changes: 7 additions & 3 deletions app/detectors/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default function Home() {
<a
href={"https://app.groundlight.ai/reef/detectors/" + group[0].id}
target="_blank"
className="text-lg hover:bg-gray-200 hover:text-gray-700 rounded-md px-4 py-1 mr-auto"
className="text-lg hover:bg-gray-200 hover:text-gray-700 rounded-md px-4 py-1 mr-auto flex place-items-center justify-center"
>
{group[0].name}
<ArrowUpRightIcon className="ml-2 w-5 h-5 inline-block" />
Expand Down Expand Up @@ -153,8 +153,12 @@ export default function Home() {
<div className={` ${detector.config.trigger_type != "pin" && "hidden"}`}>{detector.config.pin}</div>
<div className={` ${detector.config.trigger_type != "pin" && "hidden"}`}>Pin Active State:</div>
<div className={` ${detector.config.trigger_type != "pin" && "hidden"}`}>{detector.config.pin_active_state == 1 ? "HIGH" : "LOW"}</div>
<div className={` ${detector.config.trigger_type != "time" && "hidden"}`}>{"Cycle Time:"}</div>
<div className={` ${detector.config.trigger_type != "time" && "hidden"}`}>{detector.config.cycle_time + "s"}</div>
<div className={` ${(detector.config.trigger_type != "time" && detector.config.trigger_type != "motion") && "hidden"}`}>{"Cycle Time:"}</div>
<div className={` ${(detector.config.trigger_type != "time" && detector.config.trigger_type != "motion") && "hidden"}`}>{detector.config.cycle_time + "s"}</div>
<div className={` ${detector.config.trigger_type != "motion" && "hidden"}`}>{"Frame Percent:"}</div>
<div className={` ${detector.config.trigger_type != "motion" && "hidden"}`}>{(detector.config.cycle_time ? detector.config.cycle_time * 10 : 100) + "%"}</div>
<div className={` ${detector.config.trigger_type != "motion" && "hidden"}`}>{"Threshold:"}</div>
<div className={` ${detector.config.trigger_type != "motion" && "hidden"}`}>{detector.config.cycle_time}</div>
</div>
</div>
<div className="w-full h-full flex flex-col place-items-center justify-center gap-4">
Expand Down
21 changes: 19 additions & 2 deletions components/EditDetectorOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export const EditDetectorOverlay = ({ detector, detectors, index, startWithNew,
const [cycleTime, setCycleTime] = useState<number | undefined>(detector.config.cycle_time);
const [pin, setPin] = useState<number | undefined>(detector.config.pin);
const [pinActiveState, setPinActiveState] = useState<number | undefined>(detector.config.pin_active_state);
const [motionPercent, setMotionPercent] = useState<number | undefined>(detector.config.motion_percent || 0.3);
const [motionThreshold, setMotionThreshold] = useState<number | undefined>(detector.config.motion_threshold || 50);

useEffect(() => {
setNewDetector(startWithNew || false);
Expand Down Expand Up @@ -70,15 +72,28 @@ export const EditDetectorOverlay = ({ detector, detectors, index, startWithNew,

<div className="flex gap-2">
<div className="font-bold place-self-center">Trigger Type:</div>
<Dropdown options={["time", "pin", "motion"]} selected={triggerType} setSelected={(e, idx) => setTriggerType(e)} />
<Dropdown options={["time", "motion"]} selected={triggerType} setSelected={(e, idx) => setTriggerType(e)} />
</div>
{
triggerType === "time" &&
(triggerType === "time" || triggerType === "motion") &&
<div className="flex gap-2">
<div className="font-bold place-self-center">Cycle Time:</div>
<input className="border-2 border-gray-300 rounded-md p-2 w-full" type="number" placeholder="Cycle Time" value={cycleTime} onChange={(e) => setCycleTime(parseInt(e.target.value))} min={0} />
</div>
}
{
triggerType === "motion" &&
<div className="flex flex-col gap-2">
<div className="flex gap-2">
<div className="font-bold place-self-center">Motion Percent:</div>
<input className="border-2 border-gray-300 rounded-md p-2 w-full" type="number" placeholder="Motion Percent" value={motionPercent} onChange={(e) => setMotionPercent(parseFloat(e.target.value))} min={0} />
</div>
<div className="flex gap-2">
<div className="font-bold place-self-center">Motion Threshold:</div>
<input className="border-2 border-gray-300 rounded-md p-2 w-full" type="number" placeholder="Motion Threshold" value={motionThreshold} onChange={(e) => setMotionThreshold(parseInt(e.target.value))} min={0} />
</div>
</div>
}
{
triggerType === "pin" &&
<div>
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type DetConfType = {
pin?: number;
pin_active_state?: PinState;
notifications?: NotificationOptionsType;
motion_percent?: number;
motion_threshold?: number;
};

type DetType = {
Expand Down

0 comments on commit 2726859

Please sign in to comment.