我正在尝试将我的 flir Lepton 的视频源流式传输到网页。我得到了一些代码,我还没有完全理解。我是烧瓶的新手。但是,我让相机在 VLC 和一个简单的 python 脚本中工作,它捕获 200 帧并将它们保存到视频文件中。正如我在标题中所说,我使用 OpenCV 来完成所有这些工作,但似乎无法“获取”相机。尝试连接到网站时,我得到“无法读取”的打印输出。我还应该补充一点,在尝试相同的事情时,我也遇到了 PiCam 模块的问题,尽管问题不同。
对我来说更神秘的是,相机似乎进入了捕捉状态。在“简单”脚本中捕获视频时,LED 开始闪烁。这是我遇到问题的文件的来源:
from flask import Flask, render_template, Response
import cv2
from flirpy.camera.lepton import Lepton
fcamera = Lepton()
app = Flask(__name__)
camera = cv2.VideoCapture(1) # use 0 for web camera
# for cctv camera use rtsp://username:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp' instead of camera
# for local webcam use cv2.VideoCapture(0)
def gen_frames(): # generate frame by frame from camera
while True:
# Capture frame-by-frame
success, frame = camera.read() # read the camera frame
if not success:
print("could not read")
break
else:
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # concat frame one by one and show result
@app.route('/video_feed')
def video_feed():
#Video streaming route. Put this in the src attribute of an img tag
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/')
def index():
"""Video streaming home page."""
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
这是运行没有问题的简单文件:
from flirpy.camera.lepton import Lepton
import matplotlib.pyplot as plt
import cv2 as cv
from time import sleep
cap = cv.VideoCapture(1)
fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('output.avi', fourcc, 20.0, (int(cap.get(3)), int(cap.get(4))))
i = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
# write the flipped frame
out.write(frame)
if i > 200:
break
i = i + 1
# Release everything if job is finished
cap.release()
out.release()
我应该提到我没有尝试同时使用这些脚本中的任何一个。VLC 也没有运行。
谢谢你的帮助!
javamaster10000