source: or ' | |synth [: = [:...]]'
(source = 0, fallback = presets['chess'])
| 166 | |
| 167 | |
| 168 | def create_capture(source = 0, fallback = presets['chess']): |
| 169 | '''source: <int> or '<int>|<filename>|synth [:<param_name>=<value> [:...]]' |
| 170 | ''' |
| 171 | source = str(source).strip() |
| 172 | chunks = source.split(':') |
| 173 | # handle drive letter ('c:', ...) |
| 174 | if len(chunks) > 1 and len(chunks[0]) == 1 and chunks[0].isalpha(): |
| 175 | chunks[1] = chunks[0] + ':' + chunks[1] |
| 176 | del chunks[0] |
| 177 | |
| 178 | source = chunks[0] |
| 179 | try: source = int(source) |
| 180 | except ValueError: pass |
| 181 | params = dict( s.split('=') for s in chunks[1:] ) |
| 182 | |
| 183 | cap = None |
| 184 | if source == 'synth': |
| 185 | Class = classes.get(params.get('class', None), VideoSynthBase) |
| 186 | try: cap = Class(**params) |
| 187 | except: pass |
| 188 | else: |
| 189 | cap = cv2.VideoCapture(source) |
| 190 | if 'size' in params: |
| 191 | w, h = map(int, params['size'].split('x')) |
| 192 | cap.set(cv2.CAP_PROP_FRAME_WIDTH, w) |
| 193 | cap.set(cv2.CAP_PROP_FRAME_HEIGHT, h) |
| 194 | if cap is None or not cap.isOpened(): |
| 195 | print('Warning: unable to open video source: ', source) |
| 196 | if fallback is not None: |
| 197 | return create_capture(fallback, None) |
| 198 | return cap |
| 199 | |
| 200 | if __name__ == '__main__': |
| 201 | import sys |
no test coverage detected