cv2读取视频并通过post上传

cv2_post.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import cv2
import requests
import os


def video_post(files):
response = requests.post("http://127.0.0.1:5000/upload",
data=None,
files=files,
verify=False,
stream=True)
print("picture upload state: ", response.status_code)


def read_video_show(videoName):
'''
opencv 读取视频

cv2.VideoCapture(filename) #读取本地视频
cv2.VideoCapture(index) #获取摄像头
'''

# 1. 读取视频
cap = cv2.VideoCapture(videoName) # 若参数为0, 则是本地摄像头

# 2. 判断读的视频流是否成功
while cap.isOpened(): # 当成功时

# 3. 获取每帧图像
ret, frame = cap.read() # 若获取成功,ret为True,否则为False;frame是图像
img_path = os.path.join(os.getcwd(), "results", "result.png")
cv2.imwrite(img_path, frame)
files = {'file': open(img_path, 'rb')}
video_post(files)

if ret: # 成功获取图像
cv2.imshow('frame', frame) # 两个参数,一个是展示画面的名字,一个是像素内容
key = cv2.waitKey(25) # 停留25ms,当为0的时候则堵塞在第一帧不会继续下去
if key == ord(' ') or key == ord('q'): # 当键入空格或者q时,则退出while循环
break

cap.release() # 释放视频
cv2.destroyAllWindows() # 释放所有显示图像的窗口


if __name__ == "__main__":
read_video_show("static/video.mp4")