Spaces:
Runtime error
Runtime error
File size: 7,505 Bytes
10bfc0b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
import os
import cv2
import matplotlib.pyplot as plt
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 cpu_warning(device):
if device == "cpu":
print("Using CPU for face enhancer. If you have a GPU, you can set device='cuda' to speed up the process. You can also set enhance=False to skip the enhancement.")
def swap_n_show(img1_fn, img2_fn, app, swapper,
plot_before=False, plot_after=True, enhance=False, enhancer='REAL-ESRGAN 2x',device="cpu"):
validate_image(img1_fn)
validate_image(img2_fn)
img1 = cv2.imread(img1_fn)
img2 = cv2.imread(img2_fn)
if plot_before:
fig, axs = plt.subplots(1, 2, figsize=(10, 5))
axs[0].imshow(img1[:,:,::-1])
axs[0].axis('off')
axs[1].imshow(img2[:,:,::-1])
axs[1].axis('off')
plt.show()
# Do the swap
face1 = app.get(img1)[0]
face2 = app.get(img2)[0]
img1_ = img1.copy()
img2_ = img2.copy()
if plot_after:
img1_ = swapper.get(img1_, face1, face2, paste_back=True)
img2_ = swapper.get(img2_, face2, face1, paste_back=True)
if enhance:
cpu_warning(device)
model, model_runner = load_face_enhancer_model(enhancer,device)
img1_ = model_runner(img1_, model)
img2_ = model_runner(img2_, model)
fig, axs = plt.subplots(1, 2, figsize=(10, 5))
axs[0].imshow(img1_[:,:,::-1])
axs[0].axis('off')
axs[1].imshow(img2_[:,:,::-1])
axs[1].axis('off')
plt.show()
return img1_, img2_
def swap_n_show_same_img(img1_fn,
app, swapper,
plot_before=False,
plot_after=True, enhance=False, enhancer='REAL-ESRGAN 2x',device="cpu"):
validate_image(img1_fn)
img1 = cv2.imread(img1_fn)
if plot_before:
fig, ax = plt.subplots(1, 1, figsize=(10, 5))
ax.imshow(img1[:,:,::-1])
ax.axis('off')
plt.show()
# Do the swap
faces = app.get(img1)
face1, face2 = faces[0], faces[1]
img1_ = img1.copy()
if plot_after:
img1_ = swapper.get(img1_, face1, face2, paste_back=True)
img1_ = swapper.get(img1_, face2, face1, paste_back=True)
if enhance:
cpu_warning(device)
model, model_runner = load_face_enhancer_model(enhancer,device)
img1_ = model_runner(img1_, model)
fig, ax = plt.subplots(1, 1, figsize=(10, 5))
ax.imshow(img1_[:,:,::-1])
ax.axis('off')
plt.show()
return img1_
def swap_face_single(img1_fn, img2_fn, app, swapper,
plot_before=False, plot_after=True, enhance=False, enhancer='REAL-ESRGAN 2x',device="cpu"):
validate_image(img1_fn)
validate_image(img2_fn)
img1 = cv2.imread(img1_fn)
img2 = cv2.imread(img2_fn)
if plot_before:
axs = plt.subplots(1, 2, figsize=(10, 5))
axs[0].imshow(img1[:,:,::-1])
axs[0].axis('off')
axs[1].imshow(img2[:,:,::-1])
axs[1].axis('off')
plt.show()
# Do the swap
face1 = app.get(img1)[0]
face2 = app.get(img2)[0]
img1_ = img1.copy()
if plot_after:
img1_ = swapper.get(img1_, face1, face2, paste_back=True)
if enhance:
cpu_warning(device)
model, model_runner = load_face_enhancer_model(enhancer,device)
img1_ = model_runner(img1_, model)
# Save the image
output_fn = os.path.join('outputs', os.path.basename(img1_fn))
cv2.imwrite(output_fn, img1_)
print(f'Image saved to {output_fn}')
return img1_
def fine_face_swap(img1_fn, img2_fn, app, swapper,enhance=False, enhancer='REAL-ESRGAN 2x',device="cpu"):
img1 = cv2.imread(img1_fn)
facesimg1 = app.get(img1)
total_faces_img1 = len(facesimg1)
if total_faces_img1 > 1:
print(f'{total_faces_img1} faces detected')
fig, axs = plt.subplots(1, total_faces_img1, figsize=(12, 5))
for i, face in enumerate(facesimg1):
bbox = face['bbox']
bbox = [int(b) for b in bbox]
axs[i].imshow(img1[bbox[1]:bbox[3],bbox[0]:bbox[2],::-1])
axs[i].axis('off')
axs[i].set_title(f'Face {i+1}')
plt.suptitle('Select a face to swap')
plt.show()
else:
print(f'{total_faces_img1} face detected')
bbox = facesimg1[0]['bbox']
bbox = [int(b) for b in bbox]
plt.imshow(img1[bbox[1]:bbox[3],bbox[0]:bbox[2],::-1])
plt.axis('off')
plt.title('Face 1')
plt.show()
# Select a face from img1
face_idximg1 = int(input(f'Enter face number (1-{total_faces_img1}): '))
if face_idximg1 < 1 or face_idximg1 > total_faces_img1:
raise ValueError(f'Invalid face number {face_idximg1}')
face = facesimg1[face_idximg1-1]
bbox = face['bbox']
bbox = [int(b) for b in bbox]
face_img = img1[bbox[1]:bbox[3],bbox[0]:bbox[2],::-1]
plt.imshow(face_img)
plt.axis('off')
plt.title(f'Face {face_idximg1}')
plt.suptitle('Selected face')
plt.show()
img2 = cv2.imread(img2_fn)
facesimg2 = app.get(img2)
total_faces_img2 = len(facesimg2)
if total_faces_img2 > 1:
print(f'{total_faces_img2} faces detected')
fig, axs = plt.subplots(1, total_faces_img2, figsize=(12, 5))
for i, face in enumerate(facesimg2):
bbox = face['bbox']
bbox = [int(b) for b in bbox]
axs[i].imshow(img2[bbox[1]:bbox[3],bbox[0]:bbox[2],::-1])
axs[i].axis('off')
axs[i].set_title(f'Face {i+1}')
plt.suptitle('Select a face to swap')
plt.show()
else:
print(f'{total_faces_img2} face detected')
bbox = facesimg2[0]['bbox']
bbox = [int(b) for b in bbox]
plt.imshow(img2[bbox[1]:bbox[3],bbox[0]:bbox[2],::-1])
plt.axis('off')
plt.title('Face 1')
plt.show()
# Select a face from img2
face_idximg2 = int(input(f'Enter face number (1-{total_faces_img2}): '))
if face_idximg2 < 1 or face_idximg2 > total_faces_img2:
raise ValueError(f'Invalid face number {face_idximg2}')
face = facesimg2[face_idximg2-1]
bbox = face['bbox']
bbox = [int(b) for b in bbox]
face_img = img2[bbox[1]:bbox[3],bbox[0]:bbox[2],::-1]
plt.imshow(face_img)
plt.axis('off')
plt.title(f'Face {face_idximg2}')
plt.suptitle('Selected face')
plt.show()
# source face
face1 = app.get(img1)[face_idximg1-1]
face2 = app.get(img2)[face_idximg2-1]
img1_ = img1.copy()
img1_ = swapper.get(img1_, face1, face2, paste_back=True)
if enhance:
cpu_warning(device)
model, model_runner = load_face_enhancer_model(enhancer,device)
img1_ = model_runner(img1_, model)
# Save the image
output_fn = os.path.join('outputs', os.path.basename(img1_fn))
cv2.imwrite(output_fn, img1_)
print(f'Image saved to {output_fn}')
return img1_ |