-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamer.py
90 lines (79 loc) · 2.73 KB
/
streamer.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
#!/usr/bin/python
'''
thieverized from https://gist.github.com/n3wtron/4624820
'''
import cv2
import PIL.Image as Image
from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
import io
import time
import threading
from vision_processing import Vision
# evil global thread shared state
vision = None
class CamHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path.endswith('.mjpg'):
self.send_response(200)
self.send_header(
'Content-type',
'multipart/x-mixed-replace; boundary=--jpgboundary')
self.end_headers()
while True:
try:
img = vision.display
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
jpg = Image.fromarray(imgRGB)
tmpFile = io.BytesIO()
jpg.save(tmpFile, 'JPEG')
self.wfile.write(bytes("--jpgboundary", "utf-8"))
self.send_header('Content-type', 'image/jpeg')
self.send_header('Content-length', str(tmpFile.tell()))
self.end_headers()
jpg.save(self.wfile, 'JPEG')
time.sleep(0.15)
except KeyboardInterrupt:
break
return
if self.path.endswith('.html'):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes('<html><head></head><body>', 'utf-8'))
self.wfile.write(
bytes('<img src="http://127.0.0.1:8080/cam.mjpg"/>',
'utf-8'))
self.wfile.write(bytes('</body></html>', 'utf-8'))
return
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
class ReaderThread(threading.Thread):
def __init__(self):
super().__init__()
global vision
self.stop = False
shape = (240, 320)
vision = self.vision = Vision(shape)
self.vision.mode = 100
def run(self):
with self.vision:
while True:
vision.get_depths()
vision.idepth_stats()
vision.set_display()
time.sleep(0.15)
if self.stop:
break
def main():
reader = ReaderThread()
reader.start()
try:
server = ThreadedHTTPServer(('', 8080), CamHandler)
print ("server started on http://localhost:8080/index.html")
server.serve_forever()
except KeyboardInterrupt:
server.socket.close()
reader.stop = True
if __name__ == '__main__':
main()