Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,10 +15,11 @@ if not os.path.exists(inswapper_path):
|
|
| 15 |
swapper = insightface.model_zoo.get_model(inswapper_path, providers=['CPUExecutionProvider'])
|
| 16 |
|
| 17 |
def preprocess_image(img):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
def sharpen_image(img):
|
| 24 |
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
|
|
@@ -27,11 +28,17 @@ def sharpen_image(img):
|
|
| 27 |
|
| 28 |
def swap_faces(src_img, dst_img, blur_strength=3, sharpen=False):
|
| 29 |
try:
|
|
|
|
| 30 |
src = preprocess_image(src_img)
|
| 31 |
dst = preprocess_image(dst_img)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
if not src_faces or not dst_faces:
|
| 37 |
raise ValueError("No faces detected in one of the images.")
|
|
@@ -39,14 +46,18 @@ def swap_faces(src_img, dst_img, blur_strength=3, sharpen=False):
|
|
| 39 |
src_face = src_faces[0]
|
| 40 |
dst_face = dst_faces[0]
|
| 41 |
|
| 42 |
-
|
|
|
|
| 43 |
|
|
|
|
| 44 |
if blur_strength > 0:
|
| 45 |
swapped_img = cv2.GaussianBlur(swapped_img, (blur_strength, blur_strength), 0)
|
| 46 |
|
|
|
|
| 47 |
if sharpen:
|
| 48 |
swapped_img = sharpen_image(swapped_img)
|
| 49 |
|
|
|
|
| 50 |
result = cv2.cvtColor(swapped_img, cv2.COLOR_RGB2BGR)
|
| 51 |
return result
|
| 52 |
except Exception as e:
|
|
|
|
| 15 |
swapper = insightface.model_zoo.get_model(inswapper_path, providers=['CPUExecutionProvider'])
|
| 16 |
|
| 17 |
def preprocess_image(img):
|
| 18 |
+
# Adjust brightness and contrast to improve quality
|
| 19 |
+
alpha = 1.2 # Contrast control
|
| 20 |
+
beta = 10 # Brightness control
|
| 21 |
+
adjusted = cv2.convertScaleAbs(img, alpha=alpha, beta=beta)
|
| 22 |
+
return adjusted
|
| 23 |
|
| 24 |
def sharpen_image(img):
|
| 25 |
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
|
|
|
|
| 28 |
|
| 29 |
def swap_faces(src_img, dst_img, blur_strength=3, sharpen=False):
|
| 30 |
try:
|
| 31 |
+
# Preprocess while preserving color
|
| 32 |
src = preprocess_image(src_img)
|
| 33 |
dst = preprocess_image(dst_img)
|
| 34 |
|
| 35 |
+
# Convert to RGB for face detection
|
| 36 |
+
src_rgb = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
|
| 37 |
+
dst_rgb = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)
|
| 38 |
+
|
| 39 |
+
# Detect faces
|
| 40 |
+
src_faces = app.get(src_rgb)
|
| 41 |
+
dst_faces = app.get(dst_rgb)
|
| 42 |
|
| 43 |
if not src_faces or not dst_faces:
|
| 44 |
raise ValueError("No faces detected in one of the images.")
|
|
|
|
| 46 |
src_face = src_faces[0]
|
| 47 |
dst_face = dst_faces[0]
|
| 48 |
|
| 49 |
+
# Perform face swap using inswapper
|
| 50 |
+
swapped_img = swapper.get(dst_rgb, dst_face, src_face, paste_back=True)
|
| 51 |
|
| 52 |
+
# Post-processing: Apply slight blur for smoother blending
|
| 53 |
if blur_strength > 0:
|
| 54 |
swapped_img = cv2.GaussianBlur(swapped_img, (blur_strength, blur_strength), 0)
|
| 55 |
|
| 56 |
+
# Optional: Apply lightweight sharpening
|
| 57 |
if sharpen:
|
| 58 |
swapped_img = sharpen_image(swapped_img)
|
| 59 |
|
| 60 |
+
# Convert back to BGR for output
|
| 61 |
result = cv2.cvtColor(swapped_img, cv2.COLOR_RGB2BGR)
|
| 62 |
return result
|
| 63 |
except Exception as e:
|