Skip to content

Instantly share code, notes, and snippets.

@5shekel
Last active April 9, 2016 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5shekel/998a572599a5147c3927 to your computer and use it in GitHub Desktop.
Save 5shekel/998a572599a5147c3927 to your computer and use it in GitHub Desktop.
whats running on the space.telavivmakers.org
#!/usr/bin/python
import numpy
import cv2, sys
import Image
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from SocketServer import ThreadingMixIn
import StringIO
import time
import datetime
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
pass
cap=None
class CamHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
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)
#####
# this draws a rect around motion
#for c in result:
# (x, y, w, h) = cv2.boundingRect(c)
# cv2.rectangle(imgRGB, (x, y), (x + w, y + h), (0, 255, 0), 2)
#cv2.putText(imgRGB, "{}".format(text), (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
ts = datetime.datetime.now().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")
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():
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment