Spaces:
Running
Running
sandrocalzada
commited on
Commit
•
e70a1d4
1
Parent(s):
5d7d229
Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,31 @@
|
|
1 |
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
import cv2
|
4 |
-
import numpy as np
|
5 |
-
import os
|
6 |
-
import glob
|
7 |
-
import cv2
|
8 |
-
import matplotlib.pyplot as plt
|
9 |
import insightface
|
10 |
from insightface.app import FaceAnalysis
|
11 |
from insightface.data import get_image as ins_get_image
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
# Load your trained model
|
16 |
-
#model = tf.keras.models.load_model('path_to_your_model.h5')
|
17 |
-
|
18 |
-
def predict_gender(image):
|
19 |
-
# Convert image to format expected by your model & preprocess
|
20 |
-
img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
21 |
-
img = cv2.resize(img, (224, 224)) # Example size
|
22 |
-
img = img / 255.0 # Normalizing
|
23 |
-
img = np.expand_dims(img, axis=0)
|
24 |
-
|
25 |
-
prediction = model.predict(img)
|
26 |
-
|
27 |
-
# Assuming binary classification with a single output neuron
|
28 |
-
return "Male" if prediction[0] < 0.5 else "Female"
|
29 |
-
|
30 |
-
|
31 |
def predict(image_in_video, image_in_img):
|
32 |
if image_in_video == None and image_in_img == None:
|
33 |
raise gr.Error("Please capture an image using the webcam or upload an image.")
|
|
|
34 |
image = image_in_video or image_in_img
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
app = FaceAnalysis(name='buffalo_l')
|
38 |
app.prepare(ctx_id=0, det_size=(640, 640))
|
39 |
swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download='FALSE', download_zip= 'FALSE')
|
40 |
|
41 |
-
img = ins_get_image('t1')
|
42 |
-
faces = app.get(img)
|
43 |
-
|
44 |
-
source_face = faces[0]
|
45 |
-
bbox = source_face['bbox']
|
46 |
-
bbox = [int(b) for b in bbox]
|
47 |
-
|
48 |
-
source_face = faces[0]
|
49 |
-
bbox = source_face['bbox']
|
50 |
-
bbox = [int(b) for b in bbox]
|
51 |
-
|
52 |
-
res = img.copy()
|
53 |
-
for face in faces:
|
54 |
-
res = swapper.get(res, face, source_face, paste_back=True)
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
with gr.Blocks() as blocks:
|
59 |
gr.Markdown("### Capture Image Using WebCam or Upload")
|
60 |
|
@@ -82,4 +53,5 @@ with gr.Blocks() as blocks:
|
|
82 |
gr.Examples(fn=predict, examples=[], inputs=[image_in_img, image_in_video], outputs=[image_out])
|
83 |
|
84 |
blocks.queue()
|
85 |
-
blocks.launch()
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import cv2
|
|
|
|
|
|
|
|
|
|
|
3 |
import insightface
|
4 |
from insightface.app import FaceAnalysis
|
5 |
from insightface.data import get_image as ins_get_image
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def predict(image_in_video, image_in_img):
|
8 |
if image_in_video == None and image_in_img == None:
|
9 |
raise gr.Error("Please capture an image using the webcam or upload an image.")
|
10 |
+
|
11 |
image = image_in_video or image_in_img
|
12 |
+
img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
13 |
+
|
14 |
+
faces = app.get(img)
|
15 |
+
source_face = faces[0]
|
16 |
+
bbox = source_face['bbox']
|
17 |
+
bbox = [int(b) for b in bbox]
|
18 |
+
|
19 |
+
res = img.copy()
|
20 |
+
for face in faces:
|
21 |
+
res = swapper.get(res, face, source_face, paste_back=True)
|
22 |
+
|
23 |
+
return res[:, :, [2, 1, 0]] # Convert BGR to RGB for Gradio display
|
24 |
|
25 |
app = FaceAnalysis(name='buffalo_l')
|
26 |
app.prepare(ctx_id=0, det_size=(640, 640))
|
27 |
swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download='FALSE', download_zip= 'FALSE')
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
with gr.Blocks() as blocks:
|
30 |
gr.Markdown("### Capture Image Using WebCam or Upload")
|
31 |
|
|
|
53 |
gr.Examples(fn=predict, examples=[], inputs=[image_in_img, image_in_video], outputs=[image_out])
|
54 |
|
55 |
blocks.queue()
|
56 |
+
blocks.launch()
|
57 |
+
|