-
Notifications
You must be signed in to change notification settings - Fork 2
/
video_capture.py
86 lines (67 loc) · 2.71 KB
/
video_capture.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import cv2
import time
def video_init(is_2_write=False,save_path=None):
writer = None
cap = cv2.VideoCapture(0)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)#default 480
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)#default 640
# width = 480
# height = 640
# cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
# cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
'''
ref:https://docs.opencv.org/master/dd/d43/tutorial_py_video_display.html
FourCC is a 4-byte code used to specify the video codec.
The list of available codes can be found in fourcc.org.
It is platform dependent. The following codecs work fine for me.
In Fedora: DIVX, XVID, MJPG, X264, WMV1, WMV2. (XVID is more preferable. MJPG results in high size video. X264 gives very small size video)
In Windows: DIVX (More to be tested and added)
In OSX: MJPG (.mp4), DIVX (.avi), X264 (.mkv).
FourCC code is passed as `cv.VideoWriter_fourcc('M','J','P','G')or cv.VideoWriter_fourcc(*'MJPG')` for MJPG.
'''
if is_2_write is True:
#fourcc = cv2.VideoWriter_fourcc('x', 'v', 'i', 'd')
#fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
fourcc = cv2.VideoWriter_fourcc(*'divx')
if save_path is None:
save_path = 'demo.avi'
writer = cv2.VideoWriter(save_path, fourcc, 30, (int(width), int(height)))
return cap,height,width,writer
def video_capture(is_2_write=False,save_path=None):
#----var
frame_count = 0
FPS = "0"
#----video streaming init
cap, height, width, writer = video_init(is_2_write=is_2_write,save_path=save_path)
while (cap.isOpened()):
#----get image
ret, img = cap.read()
if ret is True:
#----image processing
if frame_count == 0:
t_start = time.time()
frame_count += 1
if frame_count >= 10:
FPS = "FPS=%1f" % (10 / (time.time() - t_start))
frame_count = 0
# cv2.putText(影像, 文字, 座標, 字型, 大小, 顏色, 線條寬度, 線條種類)
cv2.putText(img, FPS, (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 3)
#----image display
cv2.imshow("demo by JohnnyAI", img)
#----image writing
if writer is not None:
writer.write(img)
#----'q' key pressed?
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
print("get image failed")
break
#----release
cap.release()
if writer is not None:
writer.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
save_path = r"E:\demo.avi"
video_capture(is_2_write=True, save_path=save_path)