Python EEL,将数据从Python缓慢传输到javascript(视频流)

2024-09-30 22:20:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在html页面上显示一个视频流,下面是与此任务相关的部分代码

main.py

from camera import VideoCamera

def gen(camera):
    global flag_cam
    flag_cam = True
    while flag_cam:
        frame = camera.get_frame()
        yield frame

@eel.expose
def video_feed():
    x = VideoCamera()
    y = gen(x)

    
    for each in y:
        blob = base64.b64encode(each)
        blob = "data:image/jpeg;base64," + blob.decode("utf-8")
        # print(len(blob))     # 1 000 000 for ipcam 1080p, for webcam 170 000
        eel.updateImageSrc(blob)()    #THIS IS SLOW

@eel.expose
def stop_cam():
    global flag_cam
    flag_cam = False

camera.py

import cv2

class VideoCamera(object):
    def __init__(self):
        # self.video = cv2.VideoCapture(0)
        self.video = cv2.VideoCapture("rtsp://username:password@192.168.1.5:554/1")

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

    def get_frame(self):
        success, image = self.video.read()
        if success:
            ret, jpeg = cv2.imencode('.jpg', image)
            return jpeg.tobytes()

script.js

eel.expose(updateImageSrc);
function updateImageSrc(val) { 
    let elem = document.getElementById('bg');
    elem.src = val
}

如果我使用高清分辨率,通过函数updateImageSrc传输数据会将整个程序的速度降低到10 fps。。 有没有其他方法可以完成我的任务,或者可以以某种方式加速updateImageSrc函数


Tags: imageselffordefvideocv2frameblob