Created
October 10, 2024 07:00
-
-
Save ckocyigit/c75e6987babb494801ec6d3161743a4b to your computer and use it in GitHub Desktop.
python stream webcam over http
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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