forked from Muhamob/Simple-Python-heatmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
70 lines (60 loc) · 1.7 KB
/
test.py
File metadata and controls
70 lines (60 loc) · 1.7 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
import numpy as np
import cv2
# use it if you wonna write video or ffmpeg
# from skvideo.io import FFmpegWriter
start = 1
duration = 10
fps = '30'
cap = cv2.VideoCapture(0)
outfile = 'heatmap.mp4'
while True:
try:
_, f = cap.read()
f = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
f = cv2.GaussianBlur(f, (11, 11), 2, 2)
cnt = 0
res = 0.05*f
res = res.astype(np.float64)
break
except:
print('s')
fgbg = cv2.createBackgroundSubtractorMOG2(history=1, varThreshold=100,
detectShadows=True)
# writer = FFmpegWriter(outfile, outputdict={'-r': fps})
#writer = FFmpegWriter(outfile)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (13, 13))
cnt = 0
sec = 0
while True:
# if sec == duration: break
cnt += 1
if cnt % int(fps) == 0:
print(sec)
sec += 1
ret, frame = cap.read()
if not ret: break
fgmask = fgbg.apply(frame, None, 0.01)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# if cnt == 30: res
gray = cv2.GaussianBlur(gray, (11, 11), 2, 2)
gray = gray.astype(np.float64)
fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_CLOSE, kernel)
fgmask = fgmask.astype(np.float64)
res += (40 * fgmask + gray) * 0.01
res_show = res / res.max()
res_show = np.floor(res_show * 255)
res_show = res_show.astype(np.uint8)
res_show = cv2.applyColorMap(res_show, cv2.COLORMAP_JET)
cv2.imshow('s', res_show)
# if sec < start: continue
# try:
# writer.writeFrame(res_show)
# except:
# writer.close()
# break
k = cv2.waitKey(30) & 0xff
if k == 27:
break
#writer.close()
cap.release()
cv2.destroyAllWindows()