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

时间:2013-07-16 16:30:24

标签: 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)

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

1 个答案:

答案 0 :(得分:13)

我有同样的问题,在我的情况下,我是从视频文件中读取但它应该工作。使用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)