Spaces:
Running
Running
add global try.
Browse files102 unmodified lines10310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920029 unmodified lines230231232233234235236102 unmodified linesdef inference(image, background_enhance, face_upsample, upscale, codeformer_fidelity):"""Run a single prediction on the model"""# take the default setting for the demohas_aligned = Falseonly_center_face = Falsedraw_box = Falsedetection_model = "retinaface_resnet50"upscale = int(upscale) # covert type to intface_helper = FaceRestoreHelper(upscale,face_size=512,crop_ratio=(1, 1),det_model=detection_model,save_ext="png",use_parse=True,device=device,)bg_upsampler = upsampler if background_enhance else Noneface_upsampler = upsampler if face_upsample else Noneimg = cv2.imread(str(image), cv2.IMREAD_COLOR)if has_aligned:# the input faces are already cropped and alignedimg = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR)face_helper.is_gray = is_gray(img, threshold=5)if face_helper.is_gray:print('Grayscale input: True')face_helper.cropped_faces = [img]else:face_helper.read_image(img)# get face landmarks for each facenum_det_faces = face_helper.get_face_landmarks_5(only_center_face=only_center_face, resize=640, eye_dist_threshold=5)print(f"\tdetect {num_det_faces} faces")# align and warp each faceface_helper.align_warp_face()# face restoration for each cropped facefor idx, cropped_face in enumerate(face_helper.cropped_faces):# prepare datacropped_face_t = img2tensor(cropped_face / 255.0, bgr2rgb=True, float32=True)normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)cropped_face_t = cropped_face_t.unsqueeze(0).to(device)try:with torch.no_grad():output = codeformer_net(cropped_face_t, w=codeformer_fidelity, adain=True)[0]restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))del outputtorch.cuda.empty_cache()except Exception as error:print(f"\tFailed inference for CodeFormer: {error}")restored_face = tensor2img(cropped_face_t, rgb2bgr=True, min_max=(-1, 1))restored_face = restored_face.astype("uint8")face_helper.add_restored_face(restored_face)# paste_backif not has_aligned:# upsample the backgroundif bg_upsampler is not None:# Now only support RealESRGAN for upsampling backgroundbg_img = bg_upsampler.enhance(img, outscale=upscale)[0]else:bg_img = Noneface_helper.get_inverse_affine(None)# paste each restored face to the input imageif face_upsample and face_upsampler is not None:restored_img = face_helper.paste_faces_to_input_image(upsample_img=bg_img,draw_box=draw_box,face_upsampler=face_upsampler,)else:restored_img = face_helper.paste_faces_to_input_image(upsample_img=bg_img, draw_box=draw_box)# save restored imgsave_path = f'output/out.png'imwrite(restored_img, str(save_path))restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)return restored_img, save_pathtitle = "CodeFormer: Robust Face Restoration and Enhancement Network"29 unmodified linesIf you have any questions, please feel free to reach me out at <b>shangchenzhou@gmail.com</b>."""demo = gr.Interface(102 unmodified lines10310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220329 unmodified lines233234235236237238239102 unmodified linesdef inference(image, background_enhance, face_upsample, upscale, codeformer_fidelity):"""Run a single prediction on the model"""try: # global try# take the default setting for the demohas_aligned = Falseonly_center_face = Falsedraw_box = Falsedetection_model = "retinaface_resnet50"upscale = int(upscale) # covert type to intface_helper = FaceRestoreHelper(upscale,face_size=512,crop_ratio=(1, 1),det_model=detection_model,save_ext="png",use_parse=True,device=device,)bg_upsampler = upsampler if background_enhance else Noneface_upsampler = upsampler if face_upsample else Noneimg = cv2.imread(str(image), cv2.IMREAD_COLOR)if has_aligned:# the input faces are already cropped and alignedimg = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR)face_helper.is_gray = is_gray(img, threshold=5)if face_helper.is_gray:print('Grayscale input: True')face_helper.cropped_faces = [img]else:face_helper.read_image(img)# get face landmarks for each facenum_det_faces = face_helper.get_face_landmarks_5(only_center_face=only_center_face, resize=640, eye_dist_threshold=5)print(f"\tdetect {num_det_faces} faces")# align and warp each faceface_helper.align_warp_face()# face restoration for each cropped facefor idx, cropped_face in enumerate(face_helper.cropped_faces):# prepare datacropped_face_t = img2tensor(cropped_face / 255.0, bgr2rgb=True, float32=True)normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)cropped_face_t = cropped_face_t.unsqueeze(0).to(device)try:with torch.no_grad():output = codeformer_net(cropped_face_t, w=codeformer_fidelity, adain=True)[0]restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))del outputtorch.cuda.empty_cache()except Exception as error:print(f"\tFailed inference for CodeFormer: {error}")restored_face = tensor2img(cropped_face_t, rgb2bgr=True, min_max=(-1, 1))restored_face = restored_face.astype("uint8")face_helper.add_restored_face(restored_face)# paste_backif not has_aligned:# upsample the backgroundif bg_upsampler is not None:# Now only support RealESRGAN for upsampling backgroundbg_img = bg_upsampler.enhance(img, outscale=upscale)[0]else:bg_img = Noneface_helper.get_inverse_affine(None)# paste each restored face to the input imageif face_upsample and face_upsampler is not None:restored_img = face_helper.paste_faces_to_input_image(upsample_img=bg_img,draw_box=draw_box,face_upsampler=face_upsampler,)else:restored_img = face_helper.paste_faces_to_input_image(upsample_img=bg_img, draw_box=draw_box)# save restored imgsave_path = f'output/out.png'imwrite(restored_img, str(save_path))restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)return restored_img, save_pathexcept Exception as error:print('global exception', error)return None, Nonetitle = "CodeFormer: Robust Face Restoration and Enhancement Network"29 unmodified linesIf you have any questions, please feel free to reach me out at <b>shangchenzhou@gmail.com</b>."""demo = gr.Interface(
app.py+92-89