Skip to content

Commit

Permalink
Add image validator
Browse files Browse the repository at this point in the history
Signed-off-by: kiranpranay <[email protected]>
  • Loading branch information
KiranPranay committed Jul 29, 2023
1 parent 27bec49 commit e9ff5c4
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions faceswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,22 @@

from face_enhancer import load_face_enhancer_model

def validate_image(img):
if not os.path.exists(img):
raise ValueError(f'Image {img} does not exist')
# check if img is a valid image file
if not os.path.isfile(img):
raise ValueError(f'Image {img} is not a valid image file')
# validate it to be jpg jpeg, png formats
if not img.lower().endswith(('.jpg', '.jpeg', '.png')):
raise ValueError(f'Image {img} is not a valid image file')

def swap_n_show(img1_fn, img2_fn, app, swapper,
plot_before=False, plot_after=True, enhance=False, enhancer='REAL-ESRGAN 2x'):

validate_image(img1_fn)
validate_image(img2_fn)

img1 = cv2.imread(img1_fn)
img2 = cv2.imread(img2_fn)

Expand Down Expand Up @@ -42,6 +55,8 @@ def swap_n_show_same_img(img1_fn,
app, swapper,
plot_before=False,
plot_after=True, enhance=False, enhancer='REAL-ESRGAN 2x'):

validate_image(img1_fn)
img1 = cv2.imread(img1_fn)

if plot_before:
Expand Down Expand Up @@ -69,6 +84,9 @@ def swap_n_show_same_img(img1_fn,
def swap_face_single(img1_fn, img2_fn, app, swapper,
plot_before=False, plot_after=True, enhance=False, enhancer='REAL-ESRGAN 2x'):

validate_image(img1_fn)
validate_image(img2_fn)

img1 = cv2.imread(img1_fn)
img2 = cv2.imread(img2_fn)

Expand Down
Binary file added images/Anushka.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed images/bramhi.jpg
Binary file not shown.
Binary file added images/keerthi.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed images/modi.jpg
Binary file not shown.
Binary file modified images/result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
swapper = insightface.model_zoo.get_model('inswapper/inswapper_128.onnx', download=False, download_zip=False)

# Load images
img1_fn = 'images/bramhi.jpg'
img2_fn = 'images/modi.jpg'
img1_fn = 'images/Anushka.jpg'
img2_fn = 'images/keerthi.jpg'

# Swap faces between two images
# swap_n_show(img1_fn, img2_fn, app, swapper)
Expand Down

0 comments on commit e9ff5c4

Please sign in to comment.