Skip to content

Instantly share code, notes, and snippets.

@vb100
Last active September 24, 2017 18:21
Show Gist options
  • Save vb100/316e200af55e59fe3b13e21c2d8efe22 to your computer and use it in GitHub Desktop.
Save vb100/316e200af55e59fe3b13e21c2d8efe22 to your computer and use it in GitHub Desktop.
Motion detection application that recognize motion by We camera and record motion time stamp to a HTML file in an interactive chart.
import cv2, time
first_frame = None
video=cv2.VideoCapture(0)
while True:
check, frame = video.read()
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray=cv2.GaussianBlur(gray,(21,21),0)
if first_frame is None:
first_frame = gray
continue #continue the While loop
delta_frame = cv2.absdiff(first_frame, gray)
thresh_frame = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1] #Tuple
thresh_frame = cv2.dilate(thresh_frame, None, iterations=2)
#Find the contours
(_,cnts,_) = cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in cnts:
if cv2.contourArea(contour) < 1000:
continue
(x, y, w, h) = cv2.boundingRect(contour)
cv2.rectangle(frame, (x, y), (x+w, y+h), (0,255,0), 3)
cv2.imshow("Gray frame", gray)
cv2.imshow("Delta frame", delta_frame)
cv2.imshow("Threshold frame", thresh_frame)
cv2.imshow("Color frame", frame)
key=cv2.waitKey(1)
print(gray)
print(delta_frame)
if key == ord('q'):
break
video.release()
cv2.destroyAllWindows
import cv2, time
#1. Create an object. Zero for external camera
video=cv2.VideoCapture(0)
#7. Play the video (Indenting)
#8. a variable
a=0
while True:
a = a + 1
#3. Create a frame object
check, frame = video.read()
print(check)
print(frame) #Reresenting image
#6. Converting to Graycolor
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#time.sleep(3)
#4. Show the frame
#cv2.imshow("Capturing", frame)
#6.1.
cv2.imshow("Capturing", gray)
#5. For press any key to out (miliseconds)
#cv2.waitKey(0)
#7. For playing
key=cv2.waitKey(1)
if key == ord('q'):
break
print(a)
#2. Shut down the camera
video.release()
cv2.destroyAllWindows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment