我从源头安装了opencv(我试过pip和sudo的方法,但没有在客户端的机器上打开摄像头)。所以,我成功地安装了它,并且cv2.so文件被正确地链接到所创建的虚拟环境。即使在导入cv2时也能正常工作。第二天,我停止了EC2实例,因为它是收费的,我发现现在导入cv2时,它抛出了 "没有找到名为cv2的模块"。请指导。
更新1 - 当我运行代码时,它没有出错,但是,当我加载页面时,出现以下错误 - cv2.error: OpenCV(4.2.0) ioopencvmodulesimgcodecssrcloadsave.cpp:877: error: (-215:断言失败)!
我知道imencode收到的是空图像,所以才有上述代码。但代码在本地机器上运行完全正常。当我在ec2上运行它时,就会弹出这个错误。
run.py
from flask import Flask, render_template, Response, url_for
import io
import cv2
app = Flask(__name__)
@app.route('/')
def index():
"""Video streaming home page."""
return render_template('index.html')
def gen():
vc = cv2.VideoCapture(0)
"""Video streaming generator function."""
while True:
read_return_code, frame = vc.read()
encode_return_code, image_buffer = cv2.imencode('.jpg', frame)
io_buf = io.BytesIO(image_buffer)
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + io_buf.read() + b'\r\n')
@app.route('/video_feed')
def video_feed():
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(
gen(),
mimetype='multipart/x-mixed-replace; boundary=frame'
)
# main driver function
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000)
index.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="container" style="width: 90vw; text-align: center;">
<h1 style="font-size: 4vh; color: blue;">Intelligent Camera</h1>
<h4 style="font-size: 2vh;">Best Object Detection Cam</h4>
<img src="{{url_for('video_feed')}}" style="width: calc(70% - 10px); height: calc(60% - 10px); background-color: white; border: double black;">
</div>
</body>
</html>
你可以试试这个命令来安装CV2。
pip install opencv-python
这个错误是告诉你 frame
是 None
.
预热视频流时,测试错误(read_return_code
在您的情况下)。) 根据视频源,您可能会在开始获得有效帧之前获得几个这样的帧。
但我认为你有一个不同的问题。如果你是在ec2上部署,你是如何连接摄像机的?