Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import faiss
|
2 |
+
|
3 |
+
detector = get_detector()
|
4 |
+
model = load_model()
|
5 |
+
|
6 |
+
source_imgs = []
|
7 |
+
for r, _, f in os.walk(os.getcwd() + "/images"):
|
8 |
+
for file in f:
|
9 |
+
if (
|
10 |
+
(".jpg" in file.lower())
|
11 |
+
or (".jpeg" in file.lower())
|
12 |
+
or (".png" in file.lower())
|
13 |
+
):
|
14 |
+
exact_path = r + "/" + file
|
15 |
+
source_imgs.append(exact_path)
|
16 |
+
|
17 |
+
source_faces = []
|
18 |
+
for img in source_imgs:
|
19 |
+
source_faces.append(extract_faces(detector, img)[0])
|
20 |
+
source_embeddings = get_embeddings(source_faces)
|
21 |
+
|
22 |
+
index = faiss.IndexFlatL2(4096)
|
23 |
+
index.add(np.array(source_embeddings))
|
24 |
+
|
25 |
+
# set image
|
26 |
+
image = 'group.jpg'
|
27 |
+
|
28 |
+
def find_names(image):
|
29 |
+
imgs = extract_faces(detector, image)
|
30 |
+
embeds = get_embeddings(imgs)
|
31 |
+
D, I = index.search(np.array(embeds), 1)
|
32 |
+
names = ""
|
33 |
+
for i in I:
|
34 |
+
names+=source_imgs[i[0]].split("/")[-1].split(".")[0]
|
35 |
+
names+= "/n"
|
36 |
+
return "\n".join(names)
|
37 |
+
|
38 |
+
demo = gr.Interface(
|
39 |
+
find_names,
|
40 |
+
gr.Image(type="filepath"),
|
41 |
+
"text",
|
42 |
+
examples = [
|
43 |
+
os.path.join(os.path.dirname(__file__), "examples/group1.jpg"),
|
44 |
+
os.path.join(os.path.dirname(__file__), "examples/group2.jpg")
|
45 |
+
]
|
46 |
+
)
|
47 |
+
|
48 |
+
if __name__ == "__main__":
|
49 |
+
demo.launch()
|