gpt4 book ai didi

python - 使用 OpenCV 图像的 Flask 视频流

转载 作者:太空狗 更新时间:2023-10-29 21:41:28 25 4
gpt4 key购买 nike

我正在尝试使用 Flask 来显示 OpenCV 图像流。我正在使用 ROS 和 Zed 立体相机。

问题是 flask 服务器只显示损坏的图像图标。我猜问题出在 gen() 方法中,因为 cv2.imwrite('t.jpg', img) 方法是错误的方法。我对 OpenCV 的经验很少。

Flask 服务器接收到的图像数据是一个 InputArray。我需要一种方法来转换它并在 Flask 服务器中显示图像。

我正在使用 Python 2.7 和 rospy (ROS)。

有什么建议吗?

更新:

ROS节点访问ZED cam的代码:

    #!/usr/bin/env python

# ROS imports
import rospy
from sensor_msgs.msg import Image

# Utils
import numpy as np
import cv2
from cv_bridge import CvBridge, CvBridgeError
import stream


def callback(data):
"""
param data: data from zed/rgb/image_rect_color topic
"""
# convert from ROS sensor_msgs/Image to cv2
bridge = CvBridge()
try:
cv_img = bridge.imgmsg_to_cv2(data, desired_encoding='passthrough')
stream.img = cv_img
except CvBridgeError as e:
print(e)
# show image stream
# cv2.imshow('zed', cv_img)
# cv2.waitKey(3)


def zed_sub():
# initialize ROS node 'zed_sub'
rospy.init_node('zed_sub')
# subscribe to the ROS topic 'zed/rgb/image_rect_color'
rospy.Subscriber('zed/rgb/image_rect_color', Image, callback)
# keep python from exiting until this node is stopped
try:
rospy.spin()
except KeyboardInterrupt:
cv2.destroyAllWindows()


if __name__ == '__main__':
zed_sub()

Flask 服务器的代码:

#!/usr/bin/env python

from flask import Flask, render_template, Response
import cv2


app = Flask(__name__)
HOST = '192.168.1.3' # on-board computer's IP address
PORT = 8080

img = None


@app.route('/')
def index():
return render_template('index.html')


@app.route('/video_feed')
def video_feed():
return Response(gen(), mimetype='multipart/x-mixed-replace; boundary=frame')


def gen():
"""Video streaming generator function."""
global img
while True:
try:
cv2.imwrite('t.jpg', img)
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + open('t.jpg', 'rb').read() + b'\r\n')
except NameError as e:
print(e)


if __name__ == '__main__':
app.run(host=HOST, port=PORT, debug=True, threaded=True)

最佳答案

要访问笔记本电脑网络摄像头以外的摄像头,您可以像这样使用 RTSP 链接rtsp://admin:12345@192.168.1.1:554/h264/ch1/main/av_stream”

在哪里

>        username:admin
> password:12345
> your camera ip address and port
> ch1 is first camera on that DVR

将 cv2.VideoCamera(0) 替换为您相机的链接,这样就可以了

相机.py

import cv2

class VideoCamera(object):
def __init__(self):
# Using OpenCV to capture from device 0. If you have trouble capturing
# from a webcam, comment the line below out and use a video file
# instead.
self.video = cv2.VideoCapture(0)
# If you decide to use video.mp4, you must have this file in the folder
# as the main.py.
# self.video = cv2.VideoCapture('video.mp4')

def __del__(self):
self.video.release()

def get_frame(self):
success, image = self.video.read()
# We are using Motion JPEG, but OpenCV defaults to capture raw images,
# so we must encode it into JPEG in order to correctly display the
# video stream.
ret, jpeg = cv2.imencode('.jpg', image)
return jpeg.tobytes()

主.py

from flask import Flask, render_template, Response
from camera import VideoCamera

app = Flask(__name__)

@app.route('/')
def index():
return render_template('index.html')

def gen(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

@app.route('/video_feed')
def video_feed():
return Response(gen(VideoCamera()),
mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)

那么你可以关注这个博客来提高你的FPS

https://www.pyimagesearch.com/2015/12/21/increasing-webcam-fps-with-python-and-opencv/

关于python - 使用 OpenCV 图像的 Flask 视频流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49939859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com