@@ -54,10 +54,7 @@ def slicer(a, chunk_i, chunk_j, two_d=True):
54
54
return c
55
55
56
56
57
- def frameToText (data ):
58
- k = data [0 ]
59
- frame = data [1 ]
60
- opts = data [2 ]
57
+ def frameToText (frame , opts ):
61
58
text = ''
62
59
63
60
frame_gray = cv2 .cvtColor (frame , cv2 .COLOR_BGR2GRAY )
@@ -87,13 +84,17 @@ def frameToText(data):
87
84
tmp3 = [tmp2 [i :i + chunk_size ] for i in range (0 , len (tmp2 ), chunk_size )]
88
85
89
86
text += '\n ' .join (tmp3 )
90
- return ( k , text )
87
+ return text
91
88
92
89
93
- def loadFrame (data ):
94
- ind = data [0 ]
95
- path = data [1 ]
96
- return (ind , cv2 .imread (path ))
90
+ def loadFrame (path ):
91
+ return cv2 .imread (path )
92
+
93
+
94
+ def loadFrameAndConvertToText (data ):
95
+ path = data [0 ]
96
+ opts = data [1 ]
97
+ return frameToText (loadFrame (path ), opts )
97
98
98
99
99
100
def main (args ):
@@ -120,38 +121,24 @@ def main(args):
120
121
opts ['PALETTE' ] = PALETTE
121
122
opts ['mode' ] = args .mode
122
123
123
- frames_unord = []
124
-
125
124
with tempfile .TemporaryDirectory () as tmpdir :
126
125
print (tmpdir )
127
126
ffmpeg .input (filepath ).output (os .path .join (tmpdir , '%d.png' )).run ()
128
127
files = os .listdir (tmpdir )
129
128
files .sort (key = lambda f : int (re .sub ('\D' , '' , f )))
130
129
with mp .Pool (mp .cpu_count ()) as pool :
131
130
with tqdm (total = len (files )) as t :
132
- for res in pool .imap_unordered ( loadFrame , zip (range ( len ( files )), list (map (lambda f : os .path .join (tmpdir , f ), files )))):
133
- frames_unord .append (res )
131
+ for res in pool .imap ( loadFrameAndConvertToText , zip (list (map (lambda f : os .path .join (tmpdir , f ), files )), repeat ( opts ))):
132
+ texts .append (res )
134
133
t .update ()
135
134
136
- frames = sorted (frames_unord )
137
- frames = list (map (lambda p : p [1 ], frames ))
138
-
139
- texts_unord = []
140
-
141
- with mp .Pool (mp .cpu_count ()) as pool :
142
- with tqdm (total = len (frames )) as t :
143
- for res in pool .imap_unordered (frameToText , zip (range (len (frames )), frames , repeat (opts ))):
144
- t .update ()
145
- texts_unord .append (res )
146
-
147
- texts = sorted (texts_unord )
148
- texts = list (map (lambda p : p [1 ], texts ))
149
-
150
135
print ('frames loaded' )
151
136
152
- fps = MediaInfo .parse (filepath ).tracks [0 ].frame_rate
153
- origwidth = frames [0 ].shape [1 ]
154
- origheight = frames [0 ].shape [0 ]
137
+ media_info = [info for info in MediaInfo .parse (
138
+ filepath ).tracks if info .track_type == 'Video' ][0 ]
139
+ fps = media_info .frame_rate
140
+ origwidth = media_info .width
141
+ origheight = media_info .height
155
142
width = ceil (origwidth / N_WIDTH )
156
143
height = ceil (origheight / N_HEIGHT )
157
144
@@ -160,9 +147,7 @@ def main(args):
160
147
f .write ("{},{},{},{},{},{}\n " .format (
161
148
filepath , fps , int (width ), int (height ), origwidth , origheight ))
162
149
f .write ('=====\n ' )
163
- for text in texts :
164
- f .write (text )
165
- f .write ('\n ' )
150
+ f .write ('\n ' .join (texts ))
166
151
167
152
168
153
if __name__ == "__main__" :
0 commit comments