Skip to content

Instantly share code, notes, and snippets.

@ckocyigit
Created October 10, 2024 07:00
Show Gist options
  • Select an option

  • Save ckocyigit/c75e6987babb494801ec6d3161743a4b to your computer and use it in GitHub Desktop.

Select an option

Save ckocyigit/c75e6987babb494801ec6d3161743a4b to your computer and use it in GitHub Desktop.
python stream webcam over http
import cv2
from flask import Flask, Response
app = Flask(__name__)
camera = cv2.VideoCapture(0)
def generate_frames():
while True:
success, frame = camera.read()
if not success:
break
else:
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
print(f"Frame size: {len(frame)} bytes")
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/video_feed')
def video_feed():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment