Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
98 lines (78 sloc) 2.91 KB
import cv2
from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
capture = None
class CamHandler(BaseHTTPRequestHandler):
def do_GET(self):
print(self.path)
if self.path.endswith('/stream.mjpg'):
self.send_response(20)
self.send_header('Content-type', 'multipart/x-mixed-replace; boundary=--jpgboundary')
self.end_headers()
while True:
try:
if(frame != None):
pass
r, buf = cv2.imencode(".jpg", frame)
self.wfile.write("--jpgboundary\r\n".encode())
self.end_headers()
self.wfile.write(bytearray(buf))
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>')
self.wfile.write('<img src="http://localhost:9090/stream.mjpg" height="240px" width="320px"/>')
self.wfile.write('</body></html>')
return
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
class WebcamVideoStream:
def __init__(self, src=0):
# initialize the video camera stream and read the first frame
# from the stream
self.stream = cv2.VideoCapture(src)
# self.stream.set(3, 1920)
# self.stream.set(4, 1080)
# self.stream.set(15,-100)
(self.grabbed, self.frame) = self.stream.read()
# initialize the variable used to indicate if the thread should
# be stopped
self.stopped = False
def start(self):
# start the thread to read frames from the video stream
Thread(target=self.update, args=()).start()
return self
def update(self):
# keep looping infinitely until the thread is stopped
while True:
# if the thread indicator variable is set, stop the thread
if self.stopped:
self.stream.release()
return
# otherwise, read the next frame from the stream
(self.grabbed, self.frame) = self.stream.read()
def read(self):
# return the frame most recently read
return self.frame
def stop(self):
# indicate that the thread should be stopped
self.stopped = True
def main():
global capture
ip = ''
capture = cv2.VideoCapture(0)
capture.set(3, 320)
capture.set(4, 240)
try:
server = ThreadedHTTPServer((ip, 9090), CamHandler)
print("starting server")
server.serve_forever()
except KeyboardInterrupt:
capture.release()
server.socket.close()
if __name__ == '__main__':
main()