(uploaded_img, uploaded_audio, width, height, length, seed, facemask_dilation_ratio, facecrop_dilation_ratio, context_frames, context_overlap, cfg, steps, sample_rate, fps, device)
| 139 | return sorted_bboxes[0] |
| 140 | |
| 141 | def process_video(uploaded_img, uploaded_audio, width, height, length, seed, facemask_dilation_ratio, facecrop_dilation_ratio, context_frames, context_overlap, cfg, steps, sample_rate, fps, device): |
| 142 | |
| 143 | if seed is not None and seed > -1: |
| 144 | generator = torch.manual_seed(seed) |
| 145 | else: |
| 146 | generator = torch.manual_seed(random.randint(100, 1000000)) |
| 147 | |
| 148 | #### face musk prepare |
| 149 | face_img = cv2.imread(uploaded_img) |
| 150 | face_mask = np.zeros((face_img.shape[0], face_img.shape[1])).astype('uint8') |
| 151 | det_bboxes, probs = face_detector.detect(face_img) |
| 152 | select_bbox = select_face(det_bboxes, probs) |
| 153 | if select_bbox is None: |
| 154 | face_mask[:, :] = 255 |
| 155 | else: |
| 156 | xyxy = select_bbox[:4] |
| 157 | xyxy = np.round(xyxy).astype('int') |
| 158 | rb, re, cb, ce = xyxy[1], xyxy[3], xyxy[0], xyxy[2] |
| 159 | r_pad = int((re - rb) * facemask_dilation_ratio) |
| 160 | c_pad = int((ce - cb) * facemask_dilation_ratio) |
| 161 | face_mask[rb - r_pad : re + r_pad, cb - c_pad : ce + c_pad] = 255 |
| 162 | |
| 163 | #### face crop |
| 164 | r_pad_crop = int((re - rb) * facecrop_dilation_ratio) |
| 165 | c_pad_crop = int((ce - cb) * facecrop_dilation_ratio) |
| 166 | crop_rect = [max(0, cb - c_pad_crop), max(0, rb - r_pad_crop), min(ce + c_pad_crop, face_img.shape[1]), min(re + r_pad_crop, face_img.shape[0])] |
| 167 | face_img = crop_and_pad(face_img, crop_rect) |
| 168 | face_mask = crop_and_pad(face_mask, crop_rect) |
| 169 | face_img = cv2.resize(face_img, (width, height)) |
| 170 | face_mask = cv2.resize(face_mask, (width, height)) |
| 171 | |
| 172 | ref_image_pil = Image.fromarray(face_img[:, :, [2, 1, 0]]) |
| 173 | face_mask_tensor = torch.Tensor(face_mask).to(dtype=weight_dtype, device="cuda").unsqueeze(0).unsqueeze(0).unsqueeze(0) / 255.0 |
| 174 | |
| 175 | video = pipe( |
| 176 | ref_image_pil, |
| 177 | uploaded_audio, |
| 178 | face_mask_tensor, |
| 179 | width, |
| 180 | height, |
| 181 | length, |
| 182 | steps, |
| 183 | cfg, |
| 184 | generator=generator, |
| 185 | audio_sample_rate=sample_rate, |
| 186 | context_frames=context_frames, |
| 187 | fps=fps, |
| 188 | context_overlap=context_overlap |
| 189 | ).videos |
| 190 | |
| 191 | save_dir = Path("output/tmp") |
| 192 | save_dir.mkdir(exist_ok=True, parents=True) |
| 193 | output_video_path = save_dir / "output_video.mp4" |
| 194 | save_videos_grid(video, str(output_video_path), n_rows=1, fps=fps) |
| 195 | |
| 196 | video_clip = VideoFileClip(str(output_video_path)) |
| 197 | audio_clip = AudioFileClip(uploaded_audio) |
| 198 | final_output_path = save_dir / "output_video_with_audio.mp4" |
no test coverage detected