-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.py
127 lines (109 loc) · 5.08 KB
/
watcher.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import sys
sys.path.append("../jetson_tools/")
import misctools
import webcam
import cv2 # made with cv 3.2.0-dev
import numpy as np
import select
import time
import sys
import v4l2capture # can be found here : https://github.com/gebart/python-v4l2capture/blob/master/capture_picture.py
if __name__ == "__main__":
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE,SIG_DFL)
nNumCamera = 0
if len(sys.argv) > 1:
if sys.argv[1][0] == '-':
print( "syntax: %s <camera_num (default: %s)>" % (sys.argv[0] ),nNumCamera)
exit(-1)
else:
nNumCamera = int(sys.argv[1])
webcam.list_video_device()
wcam = webcam.WebCam(strDeviceName = "/dev/video%d" % nNumCamera );
im = wcam.getImage();
im = wcam.getImage();
strWindowName = "camera %d" % nNumCamera
nCptFrame = 0
begin = time.time()
aLastTimeAndName = []
strPrevName = ""
imPrev = im[:]
im[:]=(255,255,255) # first one => refresh at first!
timeLastOutputtedHtml = time.time()
strCurrentImageInHtml = ""
strPrevImageInHtml = ""
bPreviousWasDifferent = False # you need to update once more, as the image in the html is always the previews one (for refresh blinking avoidance)
# center of image
xc1 = int(im.shape[1]*1/4)
xc2 = int(im.shape[1]*3/4)
yc1 = int(im.shape[0]*1/4)
yc2 = int(im.shape[0]*3/4)
while( 1 ):
im = wcam.getImage(bVerbose=False);
if not im is None:
if 0:
cv2.imshow(strWindowName,im)
key = cv2.waitKey(1)
#~ print key
if key == 27: break
nCptFrame += 1
if nCptFrame > 100:
duration = time.time() - begin
print( "fps: %5.3f" % (float(nCptFrame)/duration) )
nCptFrame = 0
begin = time.time()
if 1:
rDiff = misctools.mse(im,imPrev, bDenoise=True)
rDiffCenter = misctools.mse(im[yc1:yc2,xc1:xc2],imPrev[yc1:yc2,xc1:xc2], bDenoise=True)
rAvgColor = im.mean()
imPrev = im
print("DBG: rDiff: %5.2f, rDiffCenter: %5.2f, color: %5.2f" % (rDiff,rDiffCenter, rAvgColor) )
livedataFilename = "/var/www/html/data/liveData"
#~ livedataFilename = "/var/www/html/data/notify.asp"
bRewriteHtml = False
# 4 types de lumiere, regit la couleur:
# - nuit avec juste tele: 4
# - juste sam: 14
# - sombre avec juste salon: 35
# - en journee: ??
# avec bDenoise=True
# rDiff: 70/ 60/ 130/ 700
# rDiffCenter: 70/ 105/ 130/ 700
if (rAvgColor < 40 and (rDiff > 250 or rDiffCenter > 150) ) \
or (rAvgColor > 40 and (rDiff > 300 or rDiffCenter > 200) ) \
:
bPreviousWasDifferent = True
print("DBG: writing image...")
# write image and update liveData for html server
# write image and update liveData for html server
strImageName = "%s.jpg" % misctools.getFilenameFromTime() #time.time()
strTotalImageName = "/var/www/html/data/" + strImageName
cv2.imwrite( strTotalImageName, im,[int(cv2.IMWRITE_JPEG_QUALITY), 80] )
strCurrentTime = misctools.getTimeStamp()
aLastTimeAndName.append([strCurrentTime,strImageName])
aLastTimeAndName = aLastTimeAndName[-9:] # nbr image per pages
bRewriteHtml = True
time.sleep(0.5) # don't refresh too often !
else:
bPreviousWasDifferent = False
time.sleep(0.2)
if time.time() - timeLastOutputtedHtml > 120.:
bRewriteHtml = True
if bRewriteHtml or bPreviousWasDifferent:
print("DBG: generating webpage...")
#~ generateHtml(aLastName, bReverse)
file = open(livedataFilename, "wt")
#file.write("<IMG SRC=./data/%s></IMG>" % strImageName )
file.write("<table><tr>")
nCpt = 0
print("aLastTimeAndName:%s"%aLastTimeAndName)
for s,f in aLastTimeAndName:
file.write("<td><IMG SRC=./data/%s width=640></IMG><br><center>%s</td>" % (f, s) )
if (nCpt % 3) == 2:
file.write("</tr><tr>")
nCpt += 1
file.write("</tr></table><font size=-10>last computed: %s</font>" % misctools.getTimeStamp() )
file.write("<!--end-->" )
file.close()
timeLastOutputtedHtml = time.time()
cv2.imwrite( "/tmp/last.jpg", im )