Update app.py
Browse files
app.py
CHANGED
|
@@ -5,8 +5,6 @@ import insightface
|
|
| 5 |
from insightface.app import FaceAnalysis
|
| 6 |
from PIL import Image
|
| 7 |
from nudenet import NudeDetector
|
| 8 |
-
from skimage import io
|
| 9 |
-
from skimage.draw import polygon
|
| 10 |
|
| 11 |
# Initialize the FaceAnalysis model
|
| 12 |
app = FaceAnalysis(name='buffalo_l')
|
|
@@ -41,7 +39,19 @@ def check_for_adult_content(image):
|
|
| 41 |
|
| 42 |
return False # No explicit content detected
|
| 43 |
|
| 44 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
def swap_faces(destination_image, source_image):
|
| 46 |
# Load the destination and source images from Streamlit inputs
|
| 47 |
img = cv2.cvtColor(np.array(destination_image), cv2.COLOR_RGB2BGR)
|
|
@@ -60,36 +70,27 @@ def swap_faces(destination_image, source_image):
|
|
| 60 |
res = img.copy()
|
| 61 |
try:
|
| 62 |
for face in faces:
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
return "Memory error: Face swapping operation failed due to memory overload."
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
points = face['bbox'].astype(np.int32) # Bounding box points
|
| 70 |
-
rr, cc = polygon([points[1], points[3], points[3], points[1]],
|
| 71 |
-
[points[0], points[0], points[2], points[2]])
|
| 72 |
-
mask[rr, cc] = 255 # Create mask
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
-
# Apply sharpening for clearer image
|
| 79 |
kernel = np.array([[0, -1, 0],
|
| 80 |
[-1, 5, -1],
|
| 81 |
[0, -1, 0]])
|
| 82 |
-
sharpened_res = cv2.filter2D(
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
# Commented smoothing (blurring) for testing purposes
|
| 86 |
-
smoothed_res = cv2.GaussianBlur(res, (5, 5), 0)
|
| 87 |
|
| 88 |
# Convert the result to RGB for display in Streamlit
|
| 89 |
sharpened_res_rgb = cv2.cvtColor(sharpened_res, cv2.COLOR_BGR2RGB)
|
| 90 |
|
| 91 |
return sharpened_res_rgb
|
| 92 |
-
|
| 93 |
# Streamlit app layout
|
| 94 |
def main():
|
| 95 |
st.set_page_config(page_title="SnapSwap", page_icon=":camera:")
|
|
|
|
| 5 |
from insightface.app import FaceAnalysis
|
| 6 |
from PIL import Image
|
| 7 |
from nudenet import NudeDetector
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Initialize the FaceAnalysis model
|
| 10 |
app = FaceAnalysis(name='buffalo_l')
|
|
|
|
| 39 |
|
| 40 |
return False # No explicit content detected
|
| 41 |
|
| 42 |
+
# Validate ROI to prevent out-of-bound errors
|
| 43 |
+
def validate_roi(roi, image_shape):
|
| 44 |
+
x, y, w, h = roi
|
| 45 |
+
img_h, img_w = image_shape[:2]
|
| 46 |
+
|
| 47 |
+
x = max(0, x)
|
| 48 |
+
y = max(0, y)
|
| 49 |
+
w = min(w, img_w - x)
|
| 50 |
+
h = min(h, img_h - y)
|
| 51 |
+
|
| 52 |
+
return x, y, w, h
|
| 53 |
+
|
| 54 |
+
# Face swapping function with sharpening
|
| 55 |
def swap_faces(destination_image, source_image):
|
| 56 |
# Load the destination and source images from Streamlit inputs
|
| 57 |
img = cv2.cvtColor(np.array(destination_image), cv2.COLOR_RGB2BGR)
|
|
|
|
| 70 |
res = img.copy()
|
| 71 |
try:
|
| 72 |
for face in faces:
|
| 73 |
+
bbox = face['bbox'].astype(int)
|
| 74 |
+
x, y, w, h = validate_roi(bbox, img.shape) # Validate ROI
|
|
|
|
| 75 |
|
| 76 |
+
# Resize source face to match destination face size
|
| 77 |
+
test_face_resized = cv2.resize(test, (w, h))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
res = swapper.get(res, face, test_face_resized, paste_back=True)
|
| 80 |
+
except MemoryError:
|
| 81 |
+
return "Memory error: Face swapping operation failed due to memory overload."
|
| 82 |
|
| 83 |
+
# Apply sharpening for a clearer image
|
| 84 |
kernel = np.array([[0, -1, 0],
|
| 85 |
[-1, 5, -1],
|
| 86 |
[0, -1, 0]])
|
| 87 |
+
sharpened_res = cv2.filter2D(res, -1, kernel)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
# Convert the result to RGB for display in Streamlit
|
| 90 |
sharpened_res_rgb = cv2.cvtColor(sharpened_res, cv2.COLOR_BGR2RGB)
|
| 91 |
|
| 92 |
return sharpened_res_rgb
|
| 93 |
+
|
| 94 |
# Streamlit app layout
|
| 95 |
def main():
|
| 96 |
st.set_page_config(page_title="SnapSwap", page_icon=":camera:")
|