Spaces:
Running
Running
Added Uploading
Browse files- app.py +70 -4
- app_old.py +59 -0
app.py
CHANGED
@@ -2,6 +2,8 @@ import os
|
|
2 |
import faiss
|
3 |
import gradio as gr
|
4 |
from helpers import *
|
|
|
|
|
5 |
|
6 |
detector = load_detector()
|
7 |
model = load_model()
|
@@ -20,8 +22,9 @@ for r, _, f in os.walk(os.getcwd() + "/images"):
|
|
20 |
source_faces = []
|
21 |
for img in source_imgs:
|
22 |
try:
|
23 |
-
faces, id = extract_faces(detector, img)
|
24 |
-
source_faces.append(faces[id])
|
|
|
25 |
except Exception as e:
|
26 |
print(f"Skipping {img}, {e}")
|
27 |
|
@@ -45,7 +48,7 @@ def find_names(image):
|
|
45 |
recognition(imgs, ids, names, source_faces, d, source_imgs)
|
46 |
return ",".join(names), "Recognition.jpg"
|
47 |
|
48 |
-
|
49 |
find_names,
|
50 |
gr.Image(type="filepath"),
|
51 |
["text", gr.Image(type = "filepath")],
|
@@ -55,5 +58,68 @@ demo = gr.Interface(
|
|
55 |
]
|
56 |
)
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
if __name__ == "__main__":
|
59 |
-
|
|
|
|
2 |
import faiss
|
3 |
import gradio as gr
|
4 |
from helpers import *
|
5 |
+
import shutil
|
6 |
+
from PIL import Image
|
7 |
|
8 |
detector = load_detector()
|
9 |
model = load_model()
|
|
|
22 |
source_faces = []
|
23 |
for img in source_imgs:
|
24 |
try:
|
25 |
+
# faces, id = extract_faces(detector, img)
|
26 |
+
# source_faces.append(faces[id])
|
27 |
+
source_faces.append(Image.open(img))
|
28 |
except Exception as e:
|
29 |
print(f"Skipping {img}, {e}")
|
30 |
|
|
|
48 |
recognition(imgs, ids, names, source_faces, d, source_imgs)
|
49 |
return ",".join(names), "Recognition.jpg"
|
50 |
|
51 |
+
detect = gr.Interface(
|
52 |
find_names,
|
53 |
gr.Image(type="filepath"),
|
54 |
["text", gr.Image(type = "filepath")],
|
|
|
58 |
]
|
59 |
)
|
60 |
|
61 |
+
|
62 |
+
def upload_files(files):
|
63 |
+
if not os.path.exists(os.path.join(os.getcwd(), "temp")):
|
64 |
+
os.mkdir(os.path.join(os.getcwd(), "temp"))
|
65 |
+
for file in files:
|
66 |
+
shutil.move(file.name, os.path.join(os.getcwd(), "temp", file.name.split('\\')[-1]))
|
67 |
+
return None, "Uploaded!"
|
68 |
+
|
69 |
+
with gr.Blocks() as upload:
|
70 |
+
gr.Markdown("# Select Images to Upload and click Upload")
|
71 |
+
with gr.Row():
|
72 |
+
input = gr.Files(file_types=[".jpg", ".jpeg", ".png"], label="Upload images")
|
73 |
+
# input = gr.Image(type="filepath")
|
74 |
+
output = gr.Textbox()
|
75 |
+
upload_btn = gr.Button(label="Upload")
|
76 |
+
upload_btn.click(upload_files, inputs=[input], outputs=[input, output])
|
77 |
+
|
78 |
+
def load_image():
|
79 |
+
global i, imgs
|
80 |
+
images = os.listdir(os.path.join(os.getcwd(), "temp"))
|
81 |
+
imgs = []
|
82 |
+
for image in images:
|
83 |
+
faces, id = extract_faces(detector, os.path.join(os.getcwd(), "temp", image))
|
84 |
+
# imgs.append(Image.open(os.path.join(os.getcwd(), "temp", image)))
|
85 |
+
imgs.append(faces[id])
|
86 |
+
return imgs[0], "Loaded!"
|
87 |
+
|
88 |
+
def save_img(label):
|
89 |
+
global i, imgs
|
90 |
+
imgs[i].save(os.path.join(os.getcwd(), "images", f"{label}.jpg"))
|
91 |
+
i+=1
|
92 |
+
if i < len(imgs):
|
93 |
+
return imgs[i], "Saved!"
|
94 |
+
else:
|
95 |
+
clear()
|
96 |
+
return None, "Finished!"
|
97 |
+
|
98 |
+
def clear():
|
99 |
+
global i, imgs
|
100 |
+
i = 0
|
101 |
+
imgs = None
|
102 |
+
shutil.rmtree(os.path.join(os.getcwd(), "temp"))
|
103 |
+
return None, None
|
104 |
+
|
105 |
+
i = 0
|
106 |
+
imgs = None
|
107 |
+
|
108 |
+
with gr.Blocks() as annotate:
|
109 |
+
output = gr.Image(type="pil", label="Image")
|
110 |
+
input = gr.Textbox(label = "Enter Label")
|
111 |
+
with gr.Row():
|
112 |
+
next_btn = gr.Button(label="Next")
|
113 |
+
next_btn.click(load_image, inputs=[], outputs=[output, input])
|
114 |
+
save_btn = gr.Button(label="Save")
|
115 |
+
save_btn.click(save_img, inputs=[input], outputs=[output, input])
|
116 |
+
clear_btn = gr.Button(label="Clear")
|
117 |
+
clear_btn.click(clear, inputs=[], outputs=[output, input])
|
118 |
+
|
119 |
+
tabbed_interface = gr.TabbedInterface(
|
120 |
+
[upload, annotate, detect],
|
121 |
+
["Upload", "Annotate", "Attendance"],
|
122 |
+
)
|
123 |
if __name__ == "__main__":
|
124 |
+
tabbed_interface.launch()
|
125 |
+
|
app_old.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import faiss
|
3 |
+
import gradio as gr
|
4 |
+
from helpers import *
|
5 |
+
|
6 |
+
detector = load_detector()
|
7 |
+
model = load_model()
|
8 |
+
|
9 |
+
source_imgs = []
|
10 |
+
for r, _, f in os.walk(os.getcwd() + "/images"):
|
11 |
+
for file in f:
|
12 |
+
if (
|
13 |
+
(".jpg" in file.lower())
|
14 |
+
or (".jpeg" in file.lower())
|
15 |
+
or (".png" in file.lower())
|
16 |
+
):
|
17 |
+
exact_path = r + "/" + file
|
18 |
+
source_imgs.append(exact_path)
|
19 |
+
|
20 |
+
source_faces = []
|
21 |
+
for img in source_imgs:
|
22 |
+
try:
|
23 |
+
faces, id = extract_faces(detector, img)
|
24 |
+
source_faces.append(faces[id])
|
25 |
+
except Exception as e:
|
26 |
+
print(f"Skipping {img}, {e}")
|
27 |
+
|
28 |
+
source_embeddings = get_embeddings(model, source_faces)
|
29 |
+
|
30 |
+
def find_names(image):
|
31 |
+
imgs, _ = extract_faces(detector, image)
|
32 |
+
for i, face in enumerate(imgs):
|
33 |
+
if(face.size[0] * face.size[1] < 1000):
|
34 |
+
del imgs[i]
|
35 |
+
|
36 |
+
embeds = get_embeddings(model, imgs)
|
37 |
+
d = np.zeros((len(source_embeddings), len(embeds)))
|
38 |
+
for i, s in enumerate(source_embeddings):
|
39 |
+
for j, t in enumerate(embeds):
|
40 |
+
d[i][j] = findCosineDistance(s, t)
|
41 |
+
ids = np.argmin(d, axis = 0)
|
42 |
+
names = []
|
43 |
+
for i in ids:
|
44 |
+
names.append(source_imgs[i].split("/")[-1].split(".")[0])
|
45 |
+
recognition(imgs, ids, names, source_faces, d, source_imgs)
|
46 |
+
return ",".join(names), "Recognition.jpg"
|
47 |
+
|
48 |
+
demo = gr.Interface(
|
49 |
+
find_names,
|
50 |
+
gr.Image(type="filepath"),
|
51 |
+
["text", gr.Image(type = "filepath")],
|
52 |
+
examples = [
|
53 |
+
os.path.join(os.path.dirname(__file__), "examples/group1.jpg"),
|
54 |
+
os.path.join(os.path.dirname(__file__), "examples/group2.jpg")
|
55 |
+
]
|
56 |
+
)
|
57 |
+
|
58 |
+
if __name__ == "__main__":
|
59 |
+
demo.launch()
|