-
Notifications
You must be signed in to change notification settings - Fork 9
/
basic_stream.py
58 lines (54 loc) · 1.6 KB
/
basic_stream.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
#!/usr/bin/python
'''
Streaming the openCV to a server.
Adapted from: https://gist.github.com/n3wtron/4624820
'''
import cv2
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import time
capture=None
class CamHandler(BaseHTTPRequestHandler):
def do_GET(self):
print self.path
if self.path.endswith('.mjpg'):
self.send_response(20)
self.send_header('Content-type','multipart/x-mixed-replace; boundary=--jpgboundary')
self.end_headers()
while True:
try:
rc,img = capture.read()
if not rc:
continue
imgRGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
r, buf = cv2.imencode(".jpg",imgRGB)
self.wfile.write("--jpgboundary\r\n")
self.send_header('Content-type','image/jpeg')
self.send_header('Content-length',str(len(buf)))
self.end_headers()
self.wfile.write(bytearray(buf))
self.wfile.write('\r\n')
except KeyboardInterrupt:
break
return
if self.path.endswith('.html') or self.path=="/":
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write('<html><head></head><body style="overflow-x:hidden;overflow-y:hidden">')
self.wfile.write('<img src="http://127.0.0.1:9090/cam.mjpg" height="780px" width="1366px"/>')
self.wfile.write('</body></html>')
return
def main():
global capture
capture = cv2.VideoCapture(0)
capture.set(1366, 320);
capture.set(768, 240);
try:
server = HTTPServer(('',9090),CamHandler)
print "server started"
server.serve_forever()
except KeyboardInterrupt:
capture.release()
server.socket.close()
if __name__ == '__main__':
main()