40 lines
1.0 KiB
Python
Raw Permalink Normal View History

2024-12-11 09:54:57 +02:00
import cv2
from flask import Flask, Response
import threading
app = Flask(__name__)
# Kamera ja kehykset globaalina
camera = cv2.VideoCapture(0)
current_frame = None
def capture_frames():
global camera, current_frame
while True:
success, frame = camera.read()
if success:
current_frame = cv2.imencode('.jpg', frame)[1].tobytes()
# Käynnistä taustasäie kameran pitämiseksi päällä
frame_thread = threading.Thread(target=capture_frames, daemon=True)
frame_thread.start()
def generate_frames():
global current_frame
while True:
if current_frame is not None:
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + current_frame + b'\r\n')
@app.route('/video_feed')
def video_feed():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/')
def index():
return "<h1>Live Stream</h1><img src='/video_feed' width='640'>"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)