如何将cv2.frames发送到浏览器

Vis*_*u V 6 python sockets opencv

我正在尝试使用Python将网络摄像头图像发送到浏览器.现在,我使用以下代码发送它:

def send_a_frame():
 capture = cv2.VideoCapture(0)
 frame = capture.read()[1]
 cv2.imwrite("im1.png",frame)
 cnt = open("im1.png","rb").read()
 b64 = base64.encodestring(cnt)
 html = "<html><img src='data:image/png;base64,"+base64 +"'></html"
 send(html)
Run Code Online (Sandbox Code Playgroud)

如何保存图像并重新打开图像并使用单个语句转换为base64?

Uba*_*dah 15

我有同样的问题,在我的情况下,我是从视频文件中读取,但它应该工作.使用cv2.imencode()方法.请参阅以下代码

def send_a_frame():
    capture = cv2.VideoCapture(0)
    frame = capture.read()[1]
    cnt = cv2.imencode('.png',frame)[1]
    b64 = base64.encodestring(cnt)
    html = "<html><img src='data:image/png;base64,"+b64 +"'></html"
    send(html)
Run Code Online (Sandbox Code Playgroud)