Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i send cv2.frames to a browser

I am trying to send a webcam image to browser using Python. Now, I send it using the following code:

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)

How can I save an image and reopen an image and convert to base64 with a single statement?

like image 453
Vishnu V Avatar asked Jul 16 '13 16:07

Vishnu V


1 Answers

I had the same problem, in my case I was reading from video file but it should work. Use cv2.imencode() method. See the following code

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)
like image 154
Ubaidah Avatar answered Nov 05 '22 11:11

Ubaidah