-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfinal_cam.py
More file actions
73 lines (54 loc) · 1.8 KB
/
Copy pathfinal_cam.py
File metadata and controls
73 lines (54 loc) · 1.8 KB
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
import cv2
import requests
interval = 50
def movement(img, total, iteration):
height = img.shape[0]
width = img.shape[1]
for i in range(0, height):
for j in range(0, width):
pixel = img[i][j]
if (pixel != 0):
total += 1
if(iteration == interval):
score = 4 * (total - 11100000)
score = (score / 4260000) * 100
if (score >= 100):
score = 100
elseif (score <= 0):
score = 0
total = 0
iteration = 0
print(score)
url = "http://localhost:5000/send-camera"
myobj = {"cameraValue": score}
requests.post(url, myobj)
iteration += 1
return total, iteration
cv2.namedWindow("cam")
vc = cv2.VideoCapture(0) # opens default camera (could also select a file)
if vc.isOpened(): # try to get the first frame
rval, firstFrame = vc.read()
else:
rval = False
print("Playback error!")
firstGray = cv2.cvtColor(firstFrame, cv2.COLOR_BGR2GRAY)
# to get rid of gaussian noise
firstGray = cv2.GaussianBlur(firstGray, (5, 5), 0)
iteration = 0
total = 0
while rval:
rval, frame = vc.read() # reads frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0) # to get rid of gaussian noise
change = cv2.absdiff(firstGray, gray)
cv2.imshow("cam", change)
key = cv2.waitKey(20)
if key == 27: # exit stream on ESC
break
rval, frame = vc.read() # reads frame
firstGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
firstGray = cv2.GaussianBlur(firstGray, (5, 5), 0)
total, iteration = movement(change, total, iteration)
# do something with score ^^
vc.release() # cleans up
cv2.destroyWindow("cam")