对于:
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
对于frame, 要把它转化成
fout = open("路径", "rb")
frame = fout.read()
fout.close
要使得2个frame的格式是一样的,如何转换。
目的是因为使用的一个API需要传入二进制文件流,但是摄像头用opencv打开后每帧的对象并不是二进制的。
从numpy.ndarray (图片)到字节流:
x = img.tobytes()
从二进制文件到图片(numpy.ndarray):
img = cv2.imdecode(np.fromstring(x, np.uint8) )
使用 cv2.imencode(".jpg", frame)[1].tobytes() , frame对象就是numpy的ndarray对象。是图片的元数据,如果直接tobytes 写入文件无法打开,需要编码成一种图片文件格式在tobytes 写入文件就行。
import cv2 # opencv-python (3.4.2.16)
import numpy as np # numpy (1.14.5)
import requests
file = requests.get("https://www.baidu.com/img/bd_logo1.png")
img = cv2.imdecode(np.fromstring(file.content, np.uint8), 1) #file.content 是读取的远程文件的字节流
cv2.imwrite('save3.jpg', img) # 保存
从numpy.ndarray (图片)到字节流:
x = img.tobytes()
从二进制文件到图片(numpy.ndarray):
img = cv2.imdecode(np.fromstring(x, np.