From 2aaf4388675887e219b81a3e020b05c71b092eb5 Mon Sep 17 00:00:00 2001 From: Urmi Jana <75936174+Urmi-Jana@users.noreply.github.com> Date: Wed, 19 Oct 2022 00:42:52 +0530 Subject: [PATCH 1/2] Add web cam motion detector --- scripts/WebCam Motion Detector/Graph.html | 85 +++++++++++++++++++ .../WebCam Motion Detector/MotionDetector.py | 69 +++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 scripts/WebCam Motion Detector/Graph.html create mode 100644 scripts/WebCam Motion Detector/MotionDetector.py diff --git a/scripts/WebCam Motion Detector/Graph.html b/scripts/WebCam Motion Detector/Graph.html new file mode 100644 index 0000000..2da6b7e --- /dev/null +++ b/scripts/WebCam Motion Detector/Graph.html @@ -0,0 +1,85 @@ + + + + + + + + + + + Bokeh Plot + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + \ No newline at end of file diff --git a/scripts/WebCam Motion Detector/MotionDetector.py b/scripts/WebCam Motion Detector/MotionDetector.py new file mode 100644 index 0000000..95d4049 --- /dev/null +++ b/scripts/WebCam Motion Detector/MotionDetector.py @@ -0,0 +1,69 @@ +import cv2,time,pandas +from datetime import datetime + +first_frame = None +status_list = [None, None] +time = [] +df = pandas.DataFrame(columns=["Start", "End"]) + +video = cv2.VideoCapture(0) + +while True: + check, frame = video.read() + status = 0 + grey = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) + grey = cv2.GaussianBlur(grey, (21, 21), 0) #blurring for accuracy + + if first_frame is None: #capturing the background or the initial frame + first_frame = grey + continue + + delta_frame = cv2.absdiff(first_frame, grey) #to + + thresh_frame = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1] + #returns a tuple and converts the moving pixels to white + + thresh_frame = cv2.dilate(thresh_frame, None, iterations=2) + + (cnts,_) = cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, + cv2.CHAIN_APPROX_SIMPLE) + + for cont in cnts: + if cv2.contourArea(cont) < 10000: #excluding negligble objects + continue + status = 1 + (x, y, w, h) = cv2.boundingRect(cont) + cv2.rectangle(frame, (x, y), (x+w, y+h), (0,255,0), 3,) + status_list.append(status) + + status_list = status_list[-2:] + + if status_list[-1] == 1 and status_list[-2] == 0: + time.append(datetime.now()) + if status_list[-1] == 0 and status_list[-2] == 1: + time.append(datetime.now()) + cv2.imshow("captured", grey) + cv2.imshow("delta", delta_frame) + cv2.imshow("Threshold", thresh_frame) + cv2.imshow("color frame", frame) + + key = cv2.waitKey(1) + + if key == ord('q'): + if status == 1: + time.append(datetime.now()) + break + print(status) + +print(status_list) +print(time) + +for i in range(0, len(time), 2): + df = df.append({"Start" : time[i], "End": time[i+1]}, ignore_index=True) + +df.to_csv("Times.csv") + + + +video.release() +cv2.destroyAllWindows() \ No newline at end of file From 637d58ca4f8ce51e69a6b7a4261141969a8ed65b Mon Sep 17 00:00:00 2001 From: Urmi Jana <75936174+Urmi-Jana@users.noreply.github.com> Date: Wed, 19 Oct 2022 00:44:40 +0530 Subject: [PATCH 2/2] Create Readme.md --- scripts/WebCam Motion Detector/Readme.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 scripts/WebCam Motion Detector/Readme.md diff --git a/scripts/WebCam Motion Detector/Readme.md b/scripts/WebCam Motion Detector/Readme.md new file mode 100644 index 0000000..ef77f14 --- /dev/null +++ b/scripts/WebCam Motion Detector/Readme.md @@ -0,0 +1,2 @@ +### Web Cam Motion Detector +Download and run the above python code to activate the motion detector.