0

I am working on using Python 3 to take an IP web camera's stream and display it on my computer. The following code only works in python 2.7

import cv2
import urllib.request
import numpy as np

stream=urllib.request.urlopen('http://192.168.0.90/mjpg/video.mjpg')
bytes=''
while True:
    bytes+=stream.read(16384)
    a = bytes.find('\xff\xd8')
    b = bytes.find('\xff\xd9')
    if a!=-1 and b!=-1:
        jpg = bytes[a:b+2]
        bytes= bytes[b+2:]
        i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR)
        cv2.imshow('i',i)
        if cv2.waitKey(1) ==27:
            exit(0)

However when I try it on Python 3 I get the following error

bytes+=stream.read(16384)

TypeError: Can't convert 'bytes' object to str implicitly

This works perfectly in 2.7 but I cannot find a way to get it to work in 3, any ideas?

1

1 Answer 1

1

in python3 str are not single bytes

change it to

bytes=b''

also bytes is a builtin ... you probably should not use that as a variable name

Sign up to request clarification or add additional context in comments.

2 Comments

I tried what you said but now it is throwing this error on line 9 a = bytes.find('\xff\xd8') TypeError: 'str' does not support the buffer interface
a = bytes.find(b'\xff\xd8') anywhere you use bytes you must preface it with a b

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.