Spaces:
Runtime error
Runtime error
sandrocalzada
commited on
Commit
·
446556e
1
Parent(s):
8006c7e
Update app.py
Browse files
app.py
CHANGED
@@ -7,10 +7,10 @@ import pickle
|
|
7 |
from insightface.app import FaceAnalysis
|
8 |
from concurrent.futures import ThreadPoolExecutor
|
9 |
|
10 |
-
# Initialize your models
|
11 |
-
app =
|
12 |
-
|
13 |
-
|
14 |
|
15 |
# Load pickle file once and keep it in memory
|
16 |
@st.cache_data
|
@@ -18,7 +18,21 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
def face_swapper(image_background, image_customer):
|
24 |
face_customer = app.get(image_customer)[0]
|
@@ -30,7 +44,8 @@ def face_swapper(image_background, image_customer):
|
|
30 |
return image_background
|
31 |
|
32 |
def process(image):
|
33 |
-
|
|
|
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)
|
@@ -58,14 +73,15 @@ else:
|
|
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
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
69 |
|
70 |
if image_output is not None:
|
71 |
image_output_to_download = cv2.cvtColor(image_output, cv2.COLOR_BGR2RGB)
|
|
|
7 |
from insightface.app import FaceAnalysis
|
8 |
from concurrent.futures import ThreadPoolExecutor
|
9 |
|
10 |
+
# Initialize your models to None
|
11 |
+
app = None
|
12 |
+
swapper = None
|
13 |
+
data_images = None
|
14 |
|
15 |
# Load pickle file once and keep it in memory
|
16 |
@st.cache_data
|
|
|
18 |
with open(os.path.join(os.getcwd(), 'data_images.pkl'), 'rb') as file:
|
19 |
return pickle.load(file)
|
20 |
|
21 |
+
# Lazy initialization of data_images
|
22 |
+
def get_data_images():
|
23 |
+
global data_images
|
24 |
+
if data_images is None:
|
25 |
+
data_images = load_data_images()
|
26 |
+
return data_images
|
27 |
+
|
28 |
+
# Lazy initialization for models
|
29 |
+
def prepare_models():
|
30 |
+
global app, swapper
|
31 |
+
if app is None:
|
32 |
+
app = FaceAnalysis(name='buffalo_l')
|
33 |
+
app.prepare(ctx_id=0, det_size=(640, 640))
|
34 |
+
if swapper is None:
|
35 |
+
swapper = insightface.model_zoo.get_model(os.path.join(os.getcwd(), 'inswapper_128.onnx'), download=False)
|
36 |
|
37 |
def face_swapper(image_background, image_customer):
|
38 |
face_customer = app.get(image_customer)[0]
|
|
|
44 |
return image_background
|
45 |
|
46 |
def process(image):
|
47 |
+
prepare_models() # Prepare models only when required
|
48 |
+
images_background_encoding, images_background_contents = get_data_images()['encodings'], get_data_images()['content']
|
49 |
image_loaded = face_recognition.load_image_file(image)
|
50 |
face_encoding = face_recognition.face_encodings(image_loaded)[0]
|
51 |
face_distances = face_recognition.face_distance(images_background_encoding, face_encoding)
|
|
|
73 |
|
74 |
if uploaded_file is not None:
|
75 |
bytes_data = uploaded_file.getvalue()
|
76 |
+
if option == 'File':
|
77 |
st.image(uploaded_file)
|
78 |
+
|
79 |
if st.button('Process'):
|
80 |
+
with st.spinner('Processing...'):
|
81 |
+
with ThreadPoolExecutor() as executor:
|
82 |
+
future = executor.submit(process, uploaded_file)
|
83 |
+
image_output = future.result()
|
84 |
+
st.image(image_output)
|
85 |
|
86 |
if image_output is not None:
|
87 |
image_output_to_download = cv2.cvtColor(image_output, cv2.COLOR_BGR2RGB)
|