OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 # Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
3 # | 3 # |
4 # Use of this source code is governed by a BSD-style license | 4 # Use of this source code is governed by a BSD-style license |
5 # that can be found in the LICENSE file in the root of the source | 5 # that can be found in the LICENSE file in the root of the source |
6 # tree. An additional intellectual property rights grant can be found | 6 # tree. An additional intellectual property rights grant can be found |
7 # in the file PATENTS. All contributing project authors may | 7 # in the file PATENTS. All contributing project authors may |
8 # be found in the AUTHORS file in the root of the source tree. | 8 # be found in the AUTHORS file in the root of the source tree. |
9 | 9 |
10 import optparse | 10 import optparse |
(...skipping 30 matching lines...) Expand all Loading... | |
41 'will assume we can find it in barcode_tools/' | 41 'will assume we can find it in barcode_tools/' |
42 'relative to this directory.')) | 42 'relative to this directory.')) |
43 parser.add_option('--ffmpeg_path', type='string', | 43 parser.add_option('--ffmpeg_path', type='string', |
44 help=('The path to where the ffmpeg executable is located. ' | 44 help=('The path to where the ffmpeg executable is located. ' |
45 'If omitted, it will be assumed to be present in the ' | 45 'If omitted, it will be assumed to be present in the ' |
46 'PATH with the name ffmpeg[.exe].')) | 46 'PATH with the name ffmpeg[.exe].')) |
47 parser.add_option('--zxing_path', type='string', | 47 parser.add_option('--zxing_path', type='string', |
48 help=('The path to where the zxing executable is located. ' | 48 help=('The path to where the zxing executable is located. ' |
49 'If omitted, it will be assumed to be present in the ' | 49 'If omitted, it will be assumed to be present in the ' |
50 'PATH with the name zxing[.exe].')) | 50 'PATH with the name zxing[.exe].')) |
51 parser.add_option('--stats_file', type='string', default='stats.txt', | 51 parser.add_option('--stats_file_ref', type='string', default='stats_ref.txt', |
52 help=('Path to the temporary stats file to be created and ' | |
53 'used. Default: %default')) | |
54 parser.add_option('--stats_file_test', type='string', | |
55 default='stats_test.txt', | |
52 help=('Path to the temporary stats file to be created and ' | 56 help=('Path to the temporary stats file to be created and ' |
mandermo
2016/12/21 16:42:06
Maybe --stats_file_test should be called --stats_f
kjellander_webrtc
2017/01/02 20:01:48
Good point, although it's not as clean.
I suggest
| |
53 'used. Default: %default')) | 57 'used. Default: %default')) |
54 parser.add_option('--yuv_frame_width', type='int', default=640, | 58 parser.add_option('--yuv_frame_width', type='int', default=640, |
55 help='Width of the YUV file\'s frames. Default: %default') | 59 help='Width of the YUV file\'s frames. Default: %default') |
56 parser.add_option('--yuv_frame_height', type='int', default=480, | 60 parser.add_option('--yuv_frame_height', type='int', default=480, |
57 help='Height of the YUV file\'s frames. Default: %default') | 61 help='Height of the YUV file\'s frames. Default: %default') |
58 options, _ = parser.parse_args() | 62 options, _ = parser.parse_args() |
59 | 63 |
60 if not options.ref_video: | 64 if not options.ref_video: |
61 parser.error('You must provide a path to the reference video!') | 65 parser.error('You must provide a path to the reference video!') |
62 if not os.path.exists(options.ref_video): | 66 if not os.path.exists(options.ref_video): |
63 parser.error('Cannot find the reference video at %s' % options.ref_video) | 67 parser.error('Cannot find the reference video at %s' % options.ref_video) |
64 | 68 |
65 if not options.test_video: | 69 if not options.test_video: |
66 parser.error('You must provide a path to the test video!') | 70 parser.error('You must provide a path to the test video!') |
67 if not os.path.exists(options.test_video): | 71 if not os.path.exists(options.test_video): |
68 parser.error('Cannot find the test video at %s' % options.test_video) | 72 parser.error('Cannot find the test video at %s' % options.test_video) |
69 | 73 |
70 if not options.frame_analyzer: | 74 if not options.frame_analyzer: |
71 parser.error('You must provide the path to the frame analyzer executable!') | 75 parser.error('You must provide the path to the frame analyzer executable!') |
72 if not os.path.exists(options.frame_analyzer): | 76 if not os.path.exists(options.frame_analyzer): |
73 parser.error('Cannot find frame analyzer executable at %s!' % | 77 parser.error('Cannot find frame analyzer executable at %s!' % |
74 options.frame_analyzer) | 78 options.frame_analyzer) |
75 return options | 79 return options |
76 | 80 |
81 def DecodeBarcodesInVideo(options, path_to_decoder, video, stat_file): | |
82 # Run barcode decoder on the test video to identify frame numbers. | |
83 png_working_directory = tempfile.mkdtemp() | |
84 cmd = [ | |
85 sys.executable, | |
86 path_to_decoder, | |
87 '--yuv_file=%s' % video, | |
88 '--yuv_frame_width=%d' % options.yuv_frame_width, | |
89 '--yuv_frame_height=%d' % options.yuv_frame_height, | |
90 '--stats_file=%s' % stat_file, | |
91 '--png_working_dir=%s' % png_working_directory, | |
92 ] | |
93 if options.zxing_path: | |
94 cmd.append('--zxing_path=%s' % options.zxing_path) | |
95 if options.ffmpeg_path: | |
96 cmd.append('--ffmpeg_path=%s' % options.ffmpeg_path) | |
97 | |
98 # On Windows, sometimes the inherited stdin handle from the parent process | |
99 # fails. Workaround this by passing null to stdin to the subprocesses. | |
100 null_filehandle = open(os.devnull, 'r') | |
phoglund
2016/12/12 12:46:20
Create a helper which you invoke
...Popen(cmd, st
mandermo
2016/12/21 16:42:06
Done.
| |
101 | |
102 barcode_decoder = subprocess.Popen(cmd, stdin=null_filehandle, | |
103 stdout=sys.stdout, stderr=sys.stderr) | |
104 barcode_decoder.wait() | |
105 | |
106 shutil.rmtree(png_working_directory) | |
107 if barcode_decoder.returncode != 0: | |
108 print 'Failed to run barcode decoder script.' | |
109 return 1 | |
110 return 0 | |
77 | 111 |
78 def main(): | 112 def main(): |
79 """The main function. | 113 """The main function. |
80 | 114 |
81 A simple invocation is: | 115 A simple invocation is: |
82 ./webrtc/tools/barcode_tools/compare_videos.py | 116 ./webrtc/tools/barcode_tools/compare_videos.py |
83 --ref_video=<path_and_name_of_reference_video> | 117 --ref_video=<path_and_name_of_reference_video> |
84 --test_video=<path_and_name_of_test_video> | 118 --test_video=<path_and_name_of_test_video> |
85 --frame_analyzer=<path_and_name_of_the_frame_analyzer_executable> | 119 --frame_analyzer=<path_and_name_of_the_frame_analyzer_executable> |
86 | 120 |
87 Notice that the prerequisites for barcode_decoder.py also applies to this | 121 Notice that the prerequisites for barcode_decoder.py also applies to this |
88 script. The means the following executables have to be available in the PATH: | 122 script. The means the following executables have to be available in the PATH: |
89 * zxing | 123 * zxing |
90 * ffmpeg | 124 * ffmpeg |
91 """ | 125 """ |
92 options = _ParseArgs() | 126 options = _ParseArgs() |
93 | 127 |
94 if options.barcode_decoder: | 128 if options.barcode_decoder: |
95 path_to_decoder = options.barcode_decoder | 129 path_to_decoder = options.barcode_decoder |
96 else: | 130 else: |
97 path_to_decoder = os.path.join(SCRIPT_DIR, 'barcode_tools', | 131 path_to_decoder = os.path.join(SCRIPT_DIR, 'barcode_tools', |
98 'barcode_decoder.py') | 132 'barcode_decoder.py') |
99 | 133 |
134 if DecodeBarcodesInVideo(options, path_to_decoder, | |
135 options.ref_video, options.stats_file_ref) != 0: | |
136 return 1 | |
137 if DecodeBarcodesInVideo(options, path_to_decoder, | |
138 options.test_video, options.stats_file_test) != 0: | |
139 return 1 | |
140 | |
100 # On Windows, sometimes the inherited stdin handle from the parent process | 141 # On Windows, sometimes the inherited stdin handle from the parent process |
101 # fails. Work around this by passing null to stdin to the subprocesses. | 142 # fails. Workaround this by passing null to stdin to the subprocesses. |
102 null_filehandle = open(os.devnull, 'r') | 143 null_filehandle = open(os.devnull, 'r') |
103 | 144 |
104 # Run barcode decoder on the test video to identify frame numbers. | |
105 png_working_directory = tempfile.mkdtemp() | |
106 cmd = [ | |
107 sys.executable, | |
108 path_to_decoder, | |
109 '--yuv_file=%s' % options.test_video, | |
110 '--yuv_frame_width=%d' % options.yuv_frame_width, | |
111 '--yuv_frame_height=%d' % options.yuv_frame_height, | |
112 '--stats_file=%s' % options.stats_file, | |
113 '--png_working_dir=%s' % png_working_directory, | |
114 ] | |
115 if options.zxing_path: | |
116 cmd.append('--zxing_path=%s' % options.zxing_path) | |
117 if options.ffmpeg_path: | |
118 cmd.append('--ffmpeg_path=%s' % options.ffmpeg_path) | |
119 barcode_decoder = subprocess.Popen(cmd, stdin=null_filehandle, | |
120 stdout=sys.stdout, stderr=sys.stderr) | |
121 barcode_decoder.wait() | |
122 | |
123 shutil.rmtree(png_working_directory) | |
124 if barcode_decoder.returncode != 0: | |
125 print 'Failed to run barcode decoder script.' | |
126 return 1 | |
127 | |
128 # Run frame analyzer to compare the videos and print output. | 145 # Run frame analyzer to compare the videos and print output. |
129 cmd = [ | 146 cmd = [ |
130 options.frame_analyzer, | 147 options.frame_analyzer, |
131 '--label=%s' % options.label, | 148 '--label=%s' % options.label, |
132 '--reference_file=%s' % options.ref_video, | 149 '--reference_file=%s' % options.ref_video, |
133 '--test_file=%s' % options.test_video, | 150 '--test_file=%s' % options.test_video, |
134 '--stats_file=%s' % options.stats_file, | 151 '--stats_file_ref=%s' % options.stats_file_ref, |
152 '--stats_file_test=%s' % options.stats_file_test, | |
135 '--width=%d' % options.yuv_frame_width, | 153 '--width=%d' % options.yuv_frame_width, |
136 '--height=%d' % options.yuv_frame_height, | 154 '--height=%d' % options.yuv_frame_height, |
137 ] | 155 ] |
138 frame_analyzer = subprocess.Popen(cmd, stdin=null_filehandle, | 156 frame_analyzer = subprocess.Popen(cmd, stdin=null_filehandle, |
139 stdout=sys.stdout, stderr=sys.stderr) | 157 stdout=sys.stdout, stderr=sys.stderr) |
140 frame_analyzer.wait() | 158 frame_analyzer.wait() |
141 if frame_analyzer.returncode != 0: | 159 if frame_analyzer.returncode != 0: |
142 print 'Failed to run frame analyzer.' | 160 print 'Failed to run frame analyzer.' |
143 return 1 | 161 return 1 |
144 | 162 |
145 return 0 | 163 return 0 |
146 | 164 |
147 if __name__ == '__main__': | 165 if __name__ == '__main__': |
148 sys.exit(main()) | 166 sys.exit(main()) |
OLD | NEW |