import cv2 import urllib.request import numpy as np import sys host = "192.168.1.109:8080" #在这里记得修改IP,否则是无法调用的,刚刚浏览器输入的地址 if len(sys.argv)>1: host = sys.argv[1] hoststr = 'http://' + host + '/?action=stream' print('Streaming ' + hoststr) print('Print Esc to quit') stream=urllib.request.urlopen(hoststr) bytes=bytes() while True: bytes+=stream.read(1024) a = bytes.find(b'xffxd8') b = bytes.find(b'xffxd9') if a!=-1 and b!=-1: jpg = bytes[a:b+2] bytes= bytes[b+2:] #flags = 1 for color image i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),flags=1) # print i.shape cv2.imshow("wjw",i) if cv2.waitKey(1) & 0xFF == ord('q'): exit(0)
import cv2 import urllib.request import numpy as np import sys import base64 import time import os host = "192.168.1.109:8080" #在这里记得修改IP,否则是无法调用的,刚刚浏览器输入的地址 if len(sys.argv)>1: host = sys.argv[1] hoststr = 'http://' + host + '/?action=stream' print('Streaming ' + hoststr) print('Print Esc to quit') stream=urllib.request.urlopen(hoststr) bytes=bytes() while True: bytes+=stream.read(1024) a = bytes.find(b'xffxd8') b = bytes.find(b'xffxd9') if a!=-1 and b!=-1: jpg = bytes[a:b+2] bytes= bytes[b+2:] #flags = 1 for color image i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),flags=1) # print i.shape cv2.imshow("wjw",i) if cv2.waitKey(1) & 0xFF == ord('q'): exit(0) name = str(time.time()) p = '/home/wjw/Desktop/123/'+name+'.jpg' # 判断目录是否存在 if not os.path.exists(os.path.split(p)[0]): # 目录不存在创建,makedirs可以创建多级目录 os.makedirs(os.path.split(p)[0]) try: # 保存数据到文件 with open(p, 'wb') as f: f.write(jpg) print('保存成功') except Exception as e: print('保存失败', e)