**树莓派4B同时调用两个USB摄像头出错,一次只能访问一个,无法同时访问。
**
摄像头连接图
#!/usr/bin/env python3
from flask import Flask, render_template, Response
import cv2
class VideoCamera(object):
def __init__(self, camera_index):
self.video = cv2.VideoCapture(camera_index)
def __del__(self):
self.video.release()
def get_frame(self):
success, image = self.video.read()
if not success:
return None
ret, jpeg = cv2.imencode('.jpg', image)
return jpeg.tobytes()
app = Flask(__name__)
#@app.route('/')
#def index():
# return render_template('index.html')
def gen(camera):
while True:
frame = camera.get_frame()
if frame is None:
continue
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@app.route('/video_feed_1')
def video_feed_1():
return Response(gen(VideoCamera(0)),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/video_feed_2')
def video_feed_2():
return Response(gen(VideoCamera(2)),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, port=5000)
树莓派上运行的代码
单独访问第一个摄像头
单独访问第二个摄像头
在访问第一个摄像头时访问第二个摄像头视频流
报错找不到摄像头。。
如果能成功帮助解决问题,感激不仅可以适当给一些奖励