-
Notifications
You must be signed in to change notification settings - Fork 0
/
motionObjectDetection.py
executable file
·87 lines (67 loc) · 1.74 KB
/
motionObjectDetection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
#-*- coding : utf-8 -*-
import cv2
import time
import datetime
import os
def mkdir(path):
folder = os.path.exists(path)
if not folder :
os.makedirs(path)
print ("--- new folder ... ---")
print ("--- ok ---")
else :
print ("--- there is this folder ---")
def nothing(x):
pass
objectFile = "./motionObject"
mkdir(objectFile)
print ("save folder : ./motionObject")
# Select which camera.
camera = cv2.VideoCapture(0)
time.sleep(2)
background = None
cv2.namedWindow("fps")
cv2.createTrackbar('level', 'fps', 21, 255, nothing)
shot_idx = 0
while True:
text = "No Target"
flat = 0
kerne = cv2.getTrackbarPos('level', 'fps')
if kerne % 2 == 0:
kerne = kerne + 1
(grabbed, frame) = camera.read()
# To grayscale.
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
# Use Gaussian filter.
gray = cv2.GaussianBlur(gray, (kerne, kerne), 0)
if background is None:
background = gray
continue
frameDelta = cv2.absdiff(background, gray)
thresh = cv2.threshold(frameDelta,25,255,cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh,None,iterations=2)
# Contouring.
cnts = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2]
for c in cnts:
# Ignore the smaller rectangular region.
if cv2.contourArea(c) < 100:
continue
flat = 1
# Calculate the bounding box outline.
(x, y , w, h) = cv2.boundingRect(c)
# Circle the box in the current frame.
cv2.rectangle(frame, (x,y), (x+w, y+h),(0,255,0),2)
print ("Find Target")
cv2.imshow("fps",frame)
key = cv2.waitKey(1) & 0xFF
ch = cv2.waitKey(1)
if key == ord("q"):
break
if flat == 1:
fn = './motionObject/shot%d.jpg' % (shot_idx)
cv2.imwrite(fn,frame)
shot_idx += 1
continue
camera.release()
cv2.destroyAllWindows()