import cv2
from urllib.request import urlopen
import numpy as np
stream = urlopen('http://localhost:8080/?action=stream')
#此處localhost我執行時有改成自己的IP
bytes = ''
while True:
bytes += stream.read(1024)
#此處都會有ERROR,TypeError,改成str(stream.read(1024))才不會ERROR
#但我總覺得不能這樣改
a = bytes.rfind('\xff\xd8')
b = bytes.rfind('\xff\xd9')
if a != -1 and b != -1:
#此處a以及b一直都是-1
jpg = bytes[a:b+2]
bytes = bytes[b+2:]
i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR)
#error: (-215:Assertion failed) !buf.empty() in function 'imdecode_'
cv2.imshow('i', i)
if cv2.waitKey(1) == 27:
exit(0)
這是我在網路上找的python程式碼,要從mjpg-streamer取得畫面並用opencv做後續處理,但是用python3執行後,都會出現這些問題,請問是哪裡出問題了呢?
你的bytes = ''
是字串阿……怎可以跟bytes加在一起?