Skip to content

Instantly share code, notes, and snippets.

@flokli
Created June 13, 2016 10:16
Show Gist options
  • Save flokli/33f78badde685d2d3ee8228f0789d33f to your computer and use it in GitHub Desktop.
Save flokli/33f78badde685d2d3ee8228f0789d33f to your computer and use it in GitHub Desktop.
Serve current JPG frame via HTTP get from an MJPG stream
#!/usr/bin/env python3
import cv2
import time
from threading import Thread
from flask import Flask, Response
app = Flask(__name__)
out = None
class CaptureThread:
def __init__(self):
thread = Thread(target=self.run, args=())
thread.daemon = True
thread.start()
def run(self):
global out
cap = cv2.VideoCapture('http://192.168.178.20/video.mjpg')
while True:
ret, frame = cap.read()
if ret:
out = bytearray(cv2.imencode('.jpg', frame)[1])
else:
time.sleep(0.01)
t = CaptureThread()
@app.route('/snapshot.jpg')
def snapshot():
resp = Response(response=out, status=200, mimetype='image/jpg')
return resp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment