-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli_video.py
147 lines (124 loc) · 5.27 KB
/
cli_video.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/python3
__author__ = "Igor Kim"
__credits__ = ["Igor Kim"]
__maintainer__ = "Igor Kim"
__email__ = "[email protected]"
__status__ = "Development"
__date__ = "05/2019"
__license__ = "MIT"
import os, sys, argparse, time, math, logging
import pandas as pd
import multiprocessing as mp
import recognizer, consts, cv_helpers
DEFAULT_PADDING = 20
def mp_job(fnames, q, padding=0, scaling_factor=1, debug=False):
for f in fnames:
has_value, has_none, result = recognizer.process_one_image(f, None, padding, scaling_factor, debug)
q.put(result)
def append_result(res, data):
for k in res:
data[k].append(res[k])
def extract_frames(input_path, output_folder, n_proc):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
n_frames, fps = cv_helpers.get_video_n_frames(input_path)
if fps == 0:
fps = 1
n_proc = 1
logger.warning("Could not detect video FPS, fallback to 1 thread")
n_jobs = n_frames
procs = [mp.Process(target=cv_helpers.get_video_frames,
args=(input_path, output_folder, None, None)) for i in range(n_proc)]
else:
frames = list(range(0, n_frames, fps))
_, middle_frame = cv_helpers.get_video_frame(input_path, frames[math.floor(len(frames)/2)])
n_jobs = len(frames)
p_index = list(range(n_jobs))
chunk_size = int(math.ceil(n_jobs/n_proc))
res = [frames[i:i+chunk_size] for i in range(0, n_jobs, chunk_size)]
n_proc = len(res)
procs = [mp.Process(target=cv_helpers.get_video_frames,
args=(input_path, output_folder, res[i], middle_frame)) for i in range(n_proc)]
start = time.time()
for p in procs:
p.start()
logger.info("Extracting %d frames in %d thread(s)"%(n_jobs, n_proc))
for p in procs:
p.join()
logger.info("Extracted %d in %.2f s"%(n_jobs, time.time() - start))
def parse_folder(folder, output_csv, n_proc, scaling_factor):
data = {}
for k in consts.EXPECTED_KEYS:
data[k] = []
data["file"] = []
logger.info("Parsing folder %s"%folder)
frames_fn = []
for f in os.listdir(folder):
if f.endswith(".png"):
frames_fn.append(os.path.join(folder, f))
q = mp.Queue()
n_jobs = len(frames_fn)
p_index = list(range(n_jobs))
chunk_size = int(math.ceil(n_jobs/n_proc))
res = [frames_fn[i:i+chunk_size] for i in range(0, n_jobs, chunk_size)]
n_proc = len(res)
start = time.time()
procs = [mp.Process(target=mp_job, args=(res[i], q, DEFAULT_PADDING,
scaling_factor)) for i in range(n_proc)]
for p in procs:
p.start()
logger.info("Starting text recognition in %d thread(s)"%n_proc)
for p in procs:
p.join()
while not q.empty():
append_result(q.get(), data)
logger.info("Processed %d frames in %.2f s"%(n_jobs, time.time() - start))
df = pd.DataFrame(data=data)
df = df.groupby("timestamp").first().sort_values(by=["timestamp"]).reset_index()
df.to_csv(output_csv)
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input", required=True, type=str, help="Path to video file")
ap.add_argument("-n", "--n-proc", type=int, default=4, help="Number of cores for multiprocessing")
ap.add_argument("-e", "--skip-extracting", action='store_true', help="Skip extracting images")
ap.add_argument("-p", "--skip-parsing", action='store_true', help="Skip parsing images")
ap.add_argument("-s", "--scaling-factors", type=str, default="5,4,3,2", help="Scaling factor to resize for Tesseract")
ap.add_argument("-d", "--debug-level", type=str, default="info", help="Debug level")
ap.add_argument("-f", "--debug-file", type=str, default="", help="Output logs to file")
ap.add_argument("-o", "--silent", action='store_true', help="Do not output logs to STDOUT")
args = vars(ap.parse_args())
debug_level = logging.WARNING
new_debug_level = "warning" if args["debug_level"] is None else str(args["debug_level"]).lower()
if new_debug_level.startswith("deb"):
debug_level = logging.DEBUG
elif new_debug_level.startswith("inf"):
debug_level = logging.INFO
elif new_debug_level.startswith("warn"):
debug_level = logging.WARNING
elif new_debug_level.startswith("err"):
debug_level = logging.ERROR
elif new_debug_level.startswith("crit"):
debug_level = logging.CRITICAL
handlers = []
if not args["silent"]:
handlers.append(logging.StreamHandler())
if args["debug_file"] and len(args["debug_file"]) > 0:
if not os.path.exists(args["debug_file"]):
os.makedirs(args["debug_file"])
handlers.append(logging.FileHandler(args["debug_file"]))
logging.basicConfig(
level=debug_level,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt="%Y-%m-%d %H:%M:%S",
handlers=handlers)
logger = logging.getLogger('')
if not os.path.exists(args["input"]) or not os.path.isfile(args["input"]):
logger.critical("File %s not found, exiting"%args["input"])
sys.exit()
scaling_factors = list(map(int, args["scaling_factors"].split(",")))
n_proc = args["n_proc"]
output_folder = os.path.splitext(args["input"])[0]
output_csv = output_folder + ".csv"
if not args["skip_extracting"]:
extract_frames(args["input"], output_folder, n_proc)
if not args["skip_parsing"]:
parse_folder(output_folder, output_csv, n_proc, scaling_factors)