diff --git a/inference_gfpgan.py b/inference_gfpgan.py index a426cfc7..45ac84f7 100644 --- a/inference_gfpgan.py +++ b/inference_gfpgan.py @@ -74,40 +74,42 @@ def main(): print(f'Processing {img_name} ...') basename, ext = os.path.splitext(img_name) input_img = cv2.imread(img_path, cv2.IMREAD_COLOR) + + # raise an exception with if statement + if input_img != None: + # restore faces and background if necessary + cropped_faces, restored_faces, restored_img = restorer.enhance( + input_img, has_aligned=args.aligned, only_center_face=args.only_center_face, paste_back=args.paste_back) - # restore faces and background if necessary - cropped_faces, restored_faces, restored_img = restorer.enhance( - input_img, has_aligned=args.aligned, only_center_face=args.only_center_face, paste_back=args.paste_back) + # save faces + for idx, (cropped_face, restored_face) in enumerate(zip(cropped_faces, restored_faces)): + # save cropped face + save_crop_path = os.path.join(args.save_root, 'cropped_faces', f'{basename}_{idx:02d}.png') + imwrite(cropped_face, save_crop_path) + # save restored face + if args.suffix is not None: + save_face_name = f'{basename}_{idx:02d}_{args.suffix}.png' + else: + save_face_name = f'{basename}_{idx:02d}.png' + save_restore_path = os.path.join(args.save_root, 'restored_faces', save_face_name) + imwrite(restored_face, save_restore_path) + # save comparison image + cmp_img = np.concatenate((cropped_face, restored_face), axis=1) + imwrite(cmp_img, os.path.join(args.save_root, 'cmp', f'{basename}_{idx:02d}.png')) - # save faces - for idx, (cropped_face, restored_face) in enumerate(zip(cropped_faces, restored_faces)): - # save cropped face - save_crop_path = os.path.join(args.save_root, 'cropped_faces', f'{basename}_{idx:02d}.png') - imwrite(cropped_face, save_crop_path) - # save restored face - if args.suffix is not None: - save_face_name = f'{basename}_{idx:02d}_{args.suffix}.png' - else: - save_face_name = f'{basename}_{idx:02d}.png' - save_restore_path = os.path.join(args.save_root, 'restored_faces', save_face_name) - imwrite(restored_face, save_restore_path) - # save comparison image - cmp_img = np.concatenate((cropped_face, restored_face), axis=1) - imwrite(cmp_img, os.path.join(args.save_root, 'cmp', f'{basename}_{idx:02d}.png')) + # save restored img + if restored_img is not None: + if args.ext == 'auto': + extension = ext[1:] + else: + extension = args.ext - # save restored img - if restored_img is not None: - if args.ext == 'auto': - extension = ext[1:] - else: - extension = args.ext - - if args.suffix is not None: - save_restore_path = os.path.join(args.save_root, 'restored_imgs', - f'{basename}_{args.suffix}.{extension}') - else: - save_restore_path = os.path.join(args.save_root, 'restored_imgs', f'{basename}.{extension}') - imwrite(restored_img, save_restore_path) + if args.suffix is not None: + save_restore_path = os.path.join(args.save_root, 'restored_imgs', + f'{basename}_{args.suffix}.{extension}') + else: + save_restore_path = os.path.join(args.save_root, 'restored_imgs', f'{basename}.{extension}') + imwrite(restored_img, save_restore_path) print(f'Results are in the [{args.save_root}] folder.')