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?

Community
  • 1
  • 1
  • Answer with more detail and other solutions here : https://stackoverflow.com/questions/21702477/how-to-parse-mjpeg-http-stream-from-ip-camera – qhelaine Jan 28 '23 at 12:06

1 Answers1

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

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • 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` –  Dec 29 '15 at 19:04
  • `a = bytes.find(b'\xff\xd8')` anywhere you use bytes you must preface it with a `b` – Joran Beasley Dec 29 '15 at 19:14