Aklavya commited on
Commit
4899c48
·
verified ·
1 Parent(s): 2f95613

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2
3
+ import streamlit as st
4
+ import insightface
5
+ from insightface.app import FaceAnalysis
6
+ from PIL import Image
7
+
8
+ # Initialize the FaceAnalysis model
9
+ app = FaceAnalysis(name='buffalo_l')
10
+ app.prepare(ctx_id=0, det_size=(640, 640)) # Ensure the model runs on the CPU if there's limited GPU
11
+
12
+ # Load the face swapping model (downloading on-demand)
13
+ swapper = insightface.model_zoo.get_model('insightface/inswapper_128.onnx', download=True)
14
+
15
+ # Face swapping function
16
+ def swap_faces(destination_image, source_image):
17
+ # Load the destination and source images from Streamlit inputs
18
+ img = cv2.cvtColor(np.array(destination_image), cv2.COLOR_RGB2BGR)
19
+ test = cv2.cvtColor(np.array(source_image), cv2.COLOR_RGB2BGR)
20
+
21
+ # Detect faces in the destination and source images
22
+ faces = app.get(img)
23
+ test_faces = app.get(test)
24
+
25
+ if not faces or not test_faces:
26
+ return "No faces detected in one or both images."
27
+
28
+ test_face = test_faces[0]
29
+
30
+ # Perform face swapping with error handling
31
+ res = img.copy()
32
+ try:
33
+ for face in faces:
34
+ res = swapper.get(res, face, test_face, paste_back=True)
35
+ except MemoryError:
36
+ return "Memory error: Face swapping operation failed due to memory overload."
37
+
38
+ # Sharpen the output image by 20%
39
+ kernel = np.array([[0, -1, 0],
40
+ [-1, 5, -1],
41
+ [0, -1, 0]])
42
+ sharpened_res = cv2.filter2D(res, -1, kernel)
43
+
44
+ # Convert the result to RGB for display in Streamlit
45
+ sharpened_res_rgb = cv2.cvtColor(sharpened_res, cv2.COLOR_BGR2RGB)
46
+
47
+ return sharpened_res_rgb
48
+
49
+ # Streamlit app layout
50
+ st.title("Face Swapping")
51
+ st.write("Upload a source and a destination image to perform face swapping.")
52
+
53
+ # Upload destination and source images
54
+ destination_image = st.file_uploader("Upload Destination Image", type=["jpg", "png", "jpeg"])
55
+ source_image = st.file_uploader("Upload Source Image", type=["jpg", "png", "jpeg"])
56
+
57
+ if destination_image and source_image:
58
+ # Read images with PIL (Streamlit works with PIL images)
59
+ destination_img = Image.open(destination_image)
60
+ source_img = Image.open(source_image)
61
+
62
+ st.image(destination_img, caption="Destination Image", use_column_width=True)
63
+ st.image(source_img, caption="Source Image", use_column_width=True)
64
+
65
+ # Call the face swapping function
66
+ result = swap_faces(destination_img, source_img)
67
+
68
+ if isinstance(result, str):
69
+ st.error(result) # Display error message if no faces detected or memory error
70
+ else:
71
+ st.image(result, caption="Swapped Face", use_column_width=True)