如何使用Python将OpenCV输出发送到浏览器? [英] How to send OpenCV output to browser with python?

查看:135
本文介绍了如何使用Python将OpenCV输出发送到浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有开放式cv的简单python脚本,它可以接收视频并使用YOLO对其进行对象检测.我的问题是,如何将输出作为实时流显示到我的网站上.
这是python代码,保存到output.avi.

I have a simple python script with open cv, which takes in a video and does object detection on it using YOLO. My question is, how can I display the output to my website as a live stream.
Here is the python code, saving to output.avi.

import cv2
from darkflow.net.build import TFNet
import numpy as np
import time
import pafy

options = {
    'model': 'cfg/tiny-yolo.cfg',
    'load': 'bin/yolov2-tiny.weights',
    'threshold': 0.2,
    'gpu': 0.75
}

tfnet = TFNet(options)
colors = [tuple(255 * np.random.rand(3)) for _ in range(10)]

capture = cv2.VideoCapture()
capture.open("rtmp://888888888888888")

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

#capture = cv2.VideoCapture(url)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)

while True:
    stime = time.time()
    ret, frame = capture.read()
    if ret:
        results = tfnet.return_predict(frame)


        for color, result in zip(colors, results):
            if result['label'] == 'person':
                tl = (result['topleft']['x'], result['topleft']['y'])
                br = (result['bottomright']['x'], result['bottomright']['y'])
                label = result['label']
                confidence = result['confidence']
                text = '{}: {:.0f}%'.format(label, confidence * 100)
                frame = cv2.rectangle(frame, tl, br, color, 5)
                frame = cv2.putText(
                    frame, text, tl, cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 0, 0), 2)
        out.write(frame)
        cv2.imshow('frame', frame)
        print('FPS {:.1f}'.format(1 / (time.time() - stime)))
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

capture.release()
out.release()
cv2.destroyAllWindows()

推荐答案

您可以使用ffmpeg或GStreamer通过本地网络流式传输图像,而无需使用文件播放器来显示流,而无需写入文件.或者,您可以使用简单的Flask服务器和html页面来执行以下操作: https://blog.miguelgrinberg.com/post/video-streaming-with-flask

Instead of writing into a file you can stream the images over your local network using ffmpeg or GStreamer and use some player to show the stream. Or you can use a simple flask server and a html page to do that see here: https://blog.miguelgrinberg.com/post/video-streaming-with-flask

这篇关于如何使用Python将OpenCV输出发送到浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆