Compare among `image_array`, `frame_list` and `origin_frames` and decide whether to save the temp background images.
(image_array, frame_list, origin_frames, output_path,
start, end, img_format, overwrite, num_frames,
read_frames_batch)
| 60 | |
| 61 | |
| 62 | def _prepare_background(image_array, frame_list, origin_frames, output_path, |
| 63 | start, end, img_format, overwrite, num_frames, |
| 64 | read_frames_batch): |
| 65 | """Compare among `image_array`, `frame_list` and `origin_frames` and decide |
| 66 | whether to save the temp background images.""" |
| 67 | if num_frames > 300: |
| 68 | read_frames_batch = True |
| 69 | |
| 70 | frames_folder = None |
| 71 | remove_folder = False |
| 72 | |
| 73 | if isinstance(image_array, np.ndarray): |
| 74 | |
| 75 | image_array = torch.Tensor(image_array) |
| 76 | |
| 77 | if image_array is not None: |
| 78 | if image_array.ndim == 3: |
| 79 | image_array = image_array[None] |
| 80 | if image_array.shape[0] == 1: |
| 81 | image_array = image_array.repeat(num_frames, 1, 1, 1) |
| 82 | frame_list = None |
| 83 | origin_frames = None |
| 84 | image_array = image_array[start:end] |
| 85 | |
| 86 | # check the output path and get the image_array |
| 87 | if output_path is not None: |
| 88 | prepare_output_path(output_path=output_path, |
| 89 | allowed_suffix=['.mp4', 'gif', '.png', '.jpg','.jpeg'], |
| 90 | tag='output video', |
| 91 | path_type='auto', |
| 92 | overwrite=overwrite) |
| 93 | if image_array is None: |
| 94 | # choose in frame_list or origin_frames |
| 95 | # if all None, will use pure white background |
| 96 | if frame_list is None and origin_frames is None: |
| 97 | print( |
| 98 | 'No background provided, will use pure white background.') |
| 99 | elif frame_list is not None and origin_frames is not None: |
| 100 | warnings.warn('Redundant input, will only use frame_list.') |
| 101 | origin_frames = None |
| 102 | |
| 103 | # read the origin frames as array if any. |
| 104 | if frame_list is None and origin_frames is not None: |
| 105 | check_input_path(input_path=origin_frames, |
| 106 | allowed_suffix=['.mp4', '.gif', ''], |
| 107 | tag='origin frames', |
| 108 | path_type='auto') |
| 109 | # if origin_frames is a video, write it as a folder of images |
| 110 | # if read_frames_batch is True, else read directly as an array. |
| 111 | if Path(origin_frames).is_file(): |
| 112 | if read_frames_batch: |
| 113 | frames_folder = osp.join( |
| 114 | Path(output_path).parent, |
| 115 | Path(output_path).name + '_input_temp') |
| 116 | os.makedirs(frames_folder, exist_ok=True) |
| 117 | video_to_images(origin_frames, |
| 118 | frames_folder, |
| 119 | img_format=img_format, |
no test coverage detected