-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_camera.py
70 lines (54 loc) · 2.32 KB
/
open_camera.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
import cv2
import datetime
import random
import sys
# ------------ This script will be launched if you clicked "Open camera" in "Camera" ------------
def change_date_format(ch):
ch2 = ""
for i in ch:
if (i==':'):
i = ';'
ch2 = ch2+i
return ch2
img = str(sys.argv[1])[1:len(str(sys.argv[1]))-1]
db_num = str(sys.argv[2])[1:len(str(sys.argv[2]))-1]
b = True
while (b):
try:
if (img == '0'):
cap = cv2.VideoCapture(0) # Uncomment this and comment the line above to open your webcam
else:
cap = cv2.VideoCapture(img)
# http://admin:@192.168.22.37/video/mjpg.cgi?profileid=3
ret, frame1 = cap.read()
ret, frame2 = cap.read()
k = 0
L = 0
while (cap.isOpened()):
diff = cv2.absdiff(frame1, frame2) # Find out the difference between the first frame and the second frame
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY) # Gray is easier to find the contours
blur = cv2.GaussianBlur(gray, (5, 5), 0) # To remove the noise
_, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
dilated = cv2.dilate(thresh, None, iterations=3) # Improve the mask
contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # find the contours
for contour in contours:
(x, y, w, h) = cv2.boundingRect(contour) # x, y, width, height
if (cv2.contourArea(contour) < 1000): # Don't draw on small objects
continue
k = k+1
if (k>=20):
datet = str(datetime.datetime.now()) # Get the date
datet = change_date_format(datet)
cv2.imwrite('unprocessed images' + db_num + '/' + str(datet) + ' (' + str(random.randint(1, 999999999)) + ')' + '.jpg', frame1)
L = L+1
k = 0
cv2.imshow("Camera", frame1)
frame1 = frame2
ret, frame2 = cap.read() # To read the next frame
if (cv2.waitKey(40) == 27):
b = False
break
cv2.destroyAllWindows()
cap.release()
except:
continue