-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspacecam
executable file
·122 lines (109 loc) · 3.6 KB
/
spacecam
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
#!/usr/bin/python
import numpy
import random
import cv2, sys
import Image
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from SocketServer import ThreadingMixIn
import StringIO
import time
import datetime
from pytz import timezone
from os.path import exists
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
pass
cap = None
showthis_filename = '/home/tami/TAMI_camera/showthis.jpg'
class CamHandler(BaseHTTPRequestHandler):
def do_GET(self):
if exists(showthis_filename):
self.send_response(200)
self.send_header('Content-type','image/jpeg')
with open(showthis_filename, 'rb') as fd:
img = fd.read()
self.send_header('Content-length', str(len(img)))
self.end_headers()
self.wfile.write(img)
return
self.send_response(200)
self.send_header('Access-Control-Allow-Credentials', 'true')
self.send_header('Access-Control-Allow-Origin', 'true')
self.send_header('Content-type','multipart/x-mixed-replace; boundary=--jpgboundary')
self.end_headers()
avg = None
while True:
try:
rc,img = cap.read()
if not rc:
continue
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#####
frame = cv2.GaussianBlur(gray, (21, 21), 0)
#text = "Unoccupied"
if avg is None:
avg = frame.copy().astype("float")
continue
cv2.accumulateWeighted(frame, avg, 0.5)
myDelta = cv2.absdiff(frame, cv2.convertScaleAbs(avg))
thresh = cv2.threshold(myDelta, 5, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
(cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
result = []
for c in cnts:
if cv2.contourArea(c) >= 900:
result.append(c)
#text = "Occupied"
if result or not ('imgRGB' in locals()):
#####
edge = cv2.blur(gray, (3,3))
edgeThresh = 40
edge = cv2.Canny(edge, edgeThresh, edgeThresh*3, apertureSize=3)
edge = cv2.dilate(edge, numpy.ones((3,3),numpy.uint8))
imgRGB = cv2.bitwise_and(img, img, mask = edge)
imgRGB = cv2.cvtColor(imgRGB, cv2.COLOR_BGR2RGB)
#####
for c in result:
(x, y, w, h) = cv2.boundingRect(c)
imgRGB[y:y+h, x:x+w] = numpy.random.randint(256, size=(h,w,3))
#### DOWN WITH BIG BROTHER ###
#cv2.rectangle(imgRGB, (x, y), (x + w, y + h), (0, 255, 0), 2)
#cv2.rectangle(imgRGB, (x, y), (x + w, y + h), (0, 255, 0), -1)
#cv2.putText(imgRGB, "{}".format(text), (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
ts = datetime.datetime.now(timezone('Asia/Jerusalem')).strftime("%A %d %B %Y %I:%M:%S%p")
cv2.putText(imgRGB, ts, (10, imgRGB.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (255, 0, 0), 1)
#####
jpg = Image.fromarray(imgRGB)
tmpFile = StringIO.StringIO()
jpg.save(tmpFile,'JPEG')
self.wfile.write("--jpgboundary\n")
self.send_header('Content-type','image/jpeg')
self.send_header('Content-length',str(tmpFile.len))
self.end_headers()
jpg.save(self.wfile,'JPEG')
time.sleep(0.05)
except KeyboardInterrupt:
break
return
def main():
random.seed()
global cap
cap = cv2.VideoCapture(0)
MY_FPS = 5
MY_WIDTH = 640
MY_HEIGHT = 480
if (cap.isOpened()):
cap.set(3, MY_WIDTH)
cap.set(4, MY_HEIGHT)
cap.set(cv2.cv.CV_CAP_PROP_FPS, MY_FPS);
else:
exit
global img
try:
server = ThreadedHTTPServer(('',8081),CamHandler)
print "server started"
server.serve_forever()
except KeyboardInterrupt:
cap.release()
server.socket.close()
if __name__ == '__main__':
main()