2
여러 클라이언트가 브라우저를 통해 웹캠에 액세스하려고합니다. 나는 다음과 같은 소스 코드를 시도 :웹캠 플라스크를 사용하여 라이브 스트리밍
main.py :
#!/usr/bin/env python
from flask import Flask, render_template, Response
# emulated camera
from webcamvideostream import WebcamVideoStream
import cv2
app = Flask(__name__, template_folder='C:\coding\streamingserver\templates')
@app.route('/')
def index():
"""Video streaming home page."""
return render_template('streaming.html')
def gen(camera):
"""Video streaming generator function."""
while True:
frame = camera.read()
ret, jpeg = cv2.imencode('.jpg', frame)
# print("after get_frame")
if jpeg is not None:
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n')
else:
print("frame is none")
@app.route('/video_feed')
def video_feed():
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(gen(WebcamVideoStream().start()),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5010, debug=True, threaded=True)
webcamvideostream.py :
# import the necessary packages
from threading import Thread
import cv2
class WebcamVideoStream:
def __init__(self, src=0):
# initialize the video camera stream and read the first frame
# from the stream
print("init")
self.stream = cv2.VideoCapture(src)
(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):
print("start thread")
# start the thread to read frames from the video stream
t = Thread(target=self.update, args=())
t.daemon = True
t.start()
return self
def update(self):
print("read")
# keep looping infinitely until the thread is stopped
while True:
# if the thread indicator variable is set, stop the thread
if self.stopped:
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
이 작동 - 스레드가 중지되지 않습니다 것을 제외하고 .. 그래서 난 내 브라우저를 새로 고침하는 경우 또는 스트림에 액세스하는 다른 탭을 열면 스레드 수가 증가합니다. 어디에서 정지 기능을 호출해야할지 모르겠다. 누군가 나를 도울 수 있습니까?
최저
, 한나