-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess_video.py
More file actions
128 lines (103 loc) · 4.06 KB
/
Process_video.py
File metadata and controls
128 lines (103 loc) · 4.06 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import cv2
import Const
import numpy as np
from PIL import Image
from Scan_board import scan_board
from Detect_board import detect_board
def images2pdf(path, option, counter):
image_list = []
img_name = ""
if option == Const.SEPARATE_OPTION:
img_name = 'Part'
elif option == Const.APPEND_OPTION:
img_name = 'Board'
for i in range(1, counter):
image_list.append(Image.open(path + '\\' + img_name + str(i) + '.jpg'))
first_image = image_list.pop(0)
first_image.save(path + '\\PDF_Collection.pdf', save_all=True, append_images=image_list)
def process_video(main_window, source, output_path, scan_option, source_option):
# video source
cap = None
if source_option is Const.RECORD_VIDEO:
source_path = source
try:
cap = cv2.VideoCapture(source_path)
if not cap.isOpened():
raise NameError("The video path is invalid")
except Exception as e:
main_window.source_error(e)
return
elif source_option is Const.CAMERA:
try:
camera_num = int(source)
cap = cv2.VideoCapture(camera_num)
if not cap.isOpened():
raise NameError("The camera number does not exist")
except ValueError:
main_window.source_error("The camera number is invalid")
return
except Exception as e:
main_window.source_error(e)
return
# initialize variables
time_no_write: int = 0
counter: int = 1
is_first_frame: bool = True
is_first_scan: bool = True
is_frame_scanned: bool = True
is_writing: bool
there_more_frame: bool
processed_frame: np.ndarray
frame_after: np.ndarray
original_frame: np.ndarray
frame_before = None
board = None
# read the first frame
there_more_frame, original_frame = cap.read()
while there_more_frame:
processed_frame, is_writing = detect_board(original_frame)
# while write - red circle
if is_writing:
time_no_write = 0
is_frame_scanned = False
cv2.circle(processed_frame, (20, 20), 10, (0, 0, 255), -1)
# while not write - green circle
else:
time_no_write += 1
# cv2.circle(processed_frame, (20, 20), 10, (0, 255, 0), -1)
# first frame
if is_first_frame:
is_first_frame = False
frame_before = processed_frame.copy()
# scan the board
elif time_no_write > Const.FPS and not is_frame_scanned:
frame_after = processed_frame
if scan_option is Const.APPEND_OPTION:
if is_first_scan: # create blank board
board = np.full((frame_before.shape[0], frame_before.shape[1], 3), 125, dtype=np.uint8)
is_first_scan = False
counter, board = scan_board(main_window, output_path, frame_before,
frame_after, counter, scan_option, board)
else: # SEPARATE_OPTION
counter, _ = scan_board(main_window, output_path, frame_before, frame_after, counter, scan_option)
frame_before = frame_after.copy()
is_frame_scanned = True
cv2.circle(processed_frame, (20, 20), 10, (0, 255, 0), -1)
# show the video
main_window.set_board_img(processed_frame.copy())
# read the next frame
there_more_frame, original_frame = cap.read()
if cv2.waitKey(1) and main_window.isStopProcess:
break
# save the last board
if scan_option is Const.APPEND_OPTION:
try:
write_check = cv2.imwrite(output_path + "/Board%d.jpg" % counter, board)
counter += 1
if not write_check:
raise NameError("The output path is invalid - output will not be saved")
except Exception as e:
main_window.output_error(e)
cap.release()
if counter > 1:
images2pdf(output_path, scan_option, counter)