4
4
from glob import glob
5
5
import os
6
6
import argparse
7
+ import json
8
+
9
+ # video file processing setup
10
+ # from: https://stackoverflow.com/a/61927951
11
+ import argparse
12
+ import subprocess
13
+ import sys
14
+ from pathlib import Path
15
+ from typing import NamedTuple
16
+
17
+
18
+ class FFProbeResult (NamedTuple ):
19
+ return_code : int
20
+ json : str
21
+ error : str
22
+
23
+
24
+ def ffprobe (file_path ) -> FFProbeResult :
25
+ command_array = ["ffprobe" ,
26
+ "-v" , "quiet" ,
27
+ "-print_format" , "json" ,
28
+ "-show_format" ,
29
+ "-show_streams" ,
30
+ file_path ]
31
+ result = subprocess .run (command_array , stdout = subprocess .PIPE , stderr = subprocess .PIPE , universal_newlines = True )
32
+ return FFProbeResult (return_code = result .returncode ,
33
+ json = result .stdout ,
34
+ error = result .stderr )
35
+
7
36
8
37
# openpose setup
9
38
from src import model
@@ -34,9 +63,6 @@ def process_frame(frame, body=True, hands=True):
34
63
# https://stackoverflow.com/questions/61036822/opencv-videowriter-produces-cant-find-starting-number-error
35
64
import ffmpeg
36
65
37
- def to8 (img ):
38
- return (img / 256 ).astype ('uint8' )
39
-
40
66
# open specified video
41
67
parser = argparse .ArgumentParser (
42
68
description = "Process a video annotating poses detected." )
@@ -47,28 +73,34 @@ def to8(img):
47
73
video_file = args .file
48
74
cap = cv2 .VideoCapture (video_file )
49
75
50
- # pull video file info
51
- # don't know why this is how it's defined https://stackoverflow.com/questions/52068277/change-frame-rate-in-opencv-3-4-2
52
- input_fps = cap .get (5 )
76
+ # get video file info
77
+ ffprobe_result = ffprobe (args .file )
78
+ info = json .loads (ffprobe_result .json )
79
+ videoinfo = [i for i in info ["streams" ] if i ["codec_type" ] == "video" ][0 ]
80
+ input_fps = videoinfo ["avg_frame_rate" ]
81
+ # input_fps = float(input_fps[0])/float(input_fps[1])
82
+ input_pix_fmt = videoinfo ["pix_fmt" ]
83
+ input_vcodec = videoinfo ["codec_name" ]
53
84
54
85
# define a writer object to write to a movidified file
55
- assert len (video_file .split ("." )) == 2 , \
56
- "file/dir names must not contain extra ."
57
- output_file = video_file .split ("." )[0 ]+ ".processed.avi"
86
+ postfix = info ["format" ]["format_name" ].split ("," )[0 ]
87
+ output_file = "." .join (video_file .split ("." )[:- 1 ])+ ".processed." + postfix
58
88
59
89
60
90
class Writer ():
61
- def __init__ (self , output_file , input_fps , input_framesize , gray = False ):
91
+ def __init__ (self , output_file , input_fps , input_framesize , input_pix_fmt ,
92
+ input_vcodec ):
62
93
if os .path .exists (output_file ):
63
94
os .remove (output_file )
64
95
self .ff_proc = (
65
96
ffmpeg
66
97
.input ('pipe:' ,
67
98
format = 'rawvideo' ,
68
- pix_fmt = 'gray' if gray else 'rgb24' ,
69
- s = '%sx%s' % (input_framesize [1 ],input_framesize [0 ]))
70
- .filter ('fps' , fps = input_fps , round = 'up' )
71
- .output (output_file , pix_fmt = 'yuv420p' )
99
+ pix_fmt = "bgr24" ,
100
+ s = '%sx%s' % (input_framesize [1 ],input_framesize [0 ]),
101
+ r = input_fps )
102
+ .output (output_file , pix_fmt = input_pix_fmt , vcodec = input_vcodec )
103
+ .overwrite_output ()
72
104
.run_async (pipe_stdin = True )
73
105
)
74
106
@@ -86,12 +118,14 @@ def close(self):
86
118
if frame is None :
87
119
break
88
120
89
- if writer is None :
90
- input_framesize = frame .shape [:2 ]
91
- writer = Writer (output_file , input_fps , input_framesize )
92
121
posed_frame = process_frame (frame , body = not args .no_body ,
93
122
hands = not args .no_hands )
94
123
124
+ if writer is None :
125
+ input_framesize = posed_frame .shape [:2 ]
126
+ writer = Writer (output_file , input_fps , input_framesize , input_pix_fmt ,
127
+ input_vcodec )
128
+
95
129
cv2 .imshow ('frame' , posed_frame )
96
130
97
131
# write the frame
0 commit comments