我有一个网络摄像头,我可以从中捕获视频:
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) and 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
我想要做的是将frame
发送到Web API(HTTP),接收响应图像并显示该图像。
我是openCV的新手。你能告诉我我该怎么办?
答案 0 :(得分:1)
试试这个(取自真实的项目)。显然会跳过某些部分(身份验证,响应验证)。希望它会给你一个很好的把握。
import cv2
from PIL import Image
from six import StringIO
import requests
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) and 0xFF == ord('q'):
frame_im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
pil_im = Image.fromarray(frame_im)
stream = StringIO()
pil_im.save(stream, format="JPEG")
stream.seek(0)
img_for_post = stream.read()
files = {'image': img_for_post}
response = requests.post(
url='/api/path-to-your-endpoint/',
files=files
)
break
cap.release()
cv2.destroyAllWindows()