Skip to content

Instantly share code, notes, and snippets.

@youqingkui
Last active January 31, 2018 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save youqingkui/b4f88e3b607ed5e3239e645ef0a4dfac to your computer and use it in GitHub Desktop.
Save youqingkui/b4f88e3b607ed5e3239e645ef0a4dfac to your computer and use it in GitHub Desktop.
pi图片流处理
import cv2
import urllib
import numpy as np
stream=urllib.urlopen('http://192.168.31.249:8080/video')
bytes=''
while True:
bytes+=stream.read(1024)
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.CV_LOAD_IMAGE_COLOR)
cv2.imshow('i',i)
if cv2.waitKey(1) ==27:
exit(0)
import cv2
import requests
import numpy as np
r = requests.get('http://192.168.31.249:8080/video', stream=True)
if(r.status_code == 200):
bytes = bytes()
for chunk in r.iter_content(chunk_size=1024):
bytes += chunk
a = bytes.find(b'\xff\xd8')
b = bytes.find(b'\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)
else:
print("Received unexpected status code {}".format(r.status_code))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment