Spaces:
Runtime error
Runtime error
sandrocalzada
commited on
Commit
·
bbb8961
1
Parent(s):
8da6d04
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import face_recognition
|
3 |
+
import os
|
4 |
+
import cv2
|
5 |
+
import insightface
|
6 |
+
import pickle
|
7 |
+
from insightface.app import FaceAnalysis
|
8 |
+
from concurrent.futures import ThreadPoolExecutor
|
9 |
+
|
10 |
+
# Initialize your models only once
|
11 |
+
app = FaceAnalysis(name='buffalo_l')
|
12 |
+
app.prepare(ctx_id=0, det_size=(640, 640))
|
13 |
+
swapper = insightface.model_zoo.get_model(os.path.join(os.getcwd(), 'inswapper_128.onnx'), download=False)
|
14 |
+
|
15 |
+
# Load pickle file once and keep it in memory
|
16 |
+
@st.cache(allow_output_mutation=True)
|
17 |
+
def load_data_images():
|
18 |
+
with open(os.path.join(os.getcwd(), 'data_images.pkl'), 'rb') as file:
|
19 |
+
return pickle.load(file)
|
20 |
+
|
21 |
+
data_images = load_data_images()
|
22 |
+
|
23 |
+
def face_swapper(image_background, image_customer):
|
24 |
+
face_customer = app.get(image_customer)[0]
|
25 |
+
faces = app.get(image_background)
|
26 |
+
|
27 |
+
for face in faces:
|
28 |
+
image_background = swapper.get(image_background, face, face_customer, paste_back=True)
|
29 |
+
|
30 |
+
return image_background
|
31 |
+
|
32 |
+
def process(image):
|
33 |
+
images_background_encoding, images_background_contents = data_images['encodings'], data_images['content']
|
34 |
+
image_loaded = face_recognition.load_image_file(image)
|
35 |
+
face_encoding = face_recognition.face_encodings(image_loaded)[0]
|
36 |
+
face_distances = face_recognition.face_distance(images_background_encoding, face_encoding)
|
37 |
+
|
38 |
+
tmp_distance = face_distances[0]
|
39 |
+
tmp_content = images_background_contents[0]
|
40 |
+
for face_distance, images_background_content in zip(face_distances[1:], images_background_contents[1:]):
|
41 |
+
if tmp_distance > face_distance:
|
42 |
+
tmp_distance = face_distance
|
43 |
+
tmp_content = images_background_content
|
44 |
+
|
45 |
+
output_image = face_swapper(tmp_content, image_loaded)
|
46 |
+
return output_image
|
47 |
+
|
48 |
+
image_output = None
|
49 |
+
|
50 |
+
st.title('Change Faces')
|
51 |
+
|
52 |
+
option = st.radio('How would you like to upload your image?', ('File', 'WebCam'), horizontal=True)
|
53 |
+
|
54 |
+
if option=='File':
|
55 |
+
uploaded_file = st.file_uploader('Choose your image', type=['jpg', 'png', 'jpeg'])
|
56 |
+
else:
|
57 |
+
uploaded_file = st.camera_input("Take a picture")
|
58 |
+
|
59 |
+
if uploaded_file is not None:
|
60 |
+
bytes_data = uploaded_file.getvalue()
|
61 |
+
if option=='File':
|
62 |
+
st.image(uploaded_file)
|
63 |
+
|
64 |
+
if st.button('Process'):
|
65 |
+
with ThreadPoolExecutor() as executor:
|
66 |
+
future = executor.submit(process, uploaded_file)
|
67 |
+
image_output = future.result()
|
68 |
+
st.image(image_output)
|
69 |
+
|
70 |
+
if image_output is not None:
|
71 |
+
image_output_to_download = cv2.cvtColor(image_output, cv2.COLOR_BGR2RGB)
|
72 |
+
_, image_output_to_download = cv2.imencode('.jpg', image_output_to_download)
|
73 |
+
st.download_button('Download image', image_output_to_download.tobytes(), file_name=f'output_{uploaded_file.name}')
|