Ceneksanzak commited on
Commit
c817722
·
1 Parent(s): 9fe8b2c
Files changed (4) hide show
  1. .gitignore +4 -0
  2. app.py +25 -0
  3. detect_faces.py +49 -0
  4. requirements.txt +3 -0
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ venv
2
+ **/__pycache__/
3
+ **/.DS_Store
4
+ **/*.pt
app.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from detect_faces import MAX_OUTPUT, check_and_download_model, detect_faces
3
+
4
+
5
+ def process_image(input_image, box_margin):
6
+ if input_image is None:
7
+ return INITIAL_OUTPUTS
8
+ output_images = detect_faces(input_image, box_margin=box_margin)
9
+ output_images = [gr.Image(image, visible=True) for image in output_images]
10
+ output_images += INITIAL_OUTPUTS[: 100 - len(output_images)]
11
+ return output_images
12
+
13
+
14
+ INITIAL_OUTPUTS = [gr.Image(visible=False) for _ in range(MAX_OUTPUT)]
15
+ outputs = INITIAL_OUTPUTS.copy()
16
+
17
+ iface = gr.Interface(
18
+ fn=process_image,
19
+ inputs=[gr.Image(type="filepath"), gr.Slider(0, 40)],
20
+ outputs=outputs,
21
+ allow_flagging="never",
22
+ live=True,
23
+ )
24
+ check_and_download_model()
25
+ iface.launch()
detect_faces.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ultralytics import YOLO
2
+ import os
3
+ import requests
4
+ from PIL import Image
5
+ from functools import lru_cache
6
+
7
+ MODEL_LINK = (
8
+ "https://github.com/akanametov/yolov8-face/releases/download/v0.0.0/yolov8l-face.pt"
9
+ )
10
+ MODEL_PATH = "yolov8l-face.pt"
11
+ MAX_OUTPUT = 10
12
+
13
+ model = YOLO(MODEL_PATH)
14
+
15
+
16
+ def check_and_download_model():
17
+ if os.path.exists(MODEL_PATH):
18
+ return
19
+ response = requests.get(MODEL_LINK, timeout=60)
20
+ with open(MODEL_PATH, "wb") as f:
21
+ f.write(response.content)
22
+
23
+
24
+ def adjust_box(b, image_shape, margin=10):
25
+ x1, y1, x2, y2 = b
26
+ x1 = max(0, x1 - margin)
27
+ y1 = max(0, y1 - margin)
28
+ x2 = min(image_shape[0], x2 + margin)
29
+ y2 = min(image_shape[1], y2 + margin)
30
+ return (int(x1), int(y1), int(x2), int(y2))
31
+
32
+
33
+ @lru_cache(maxsize=None)
34
+ def predict(path):
35
+ results = model.predict(path)
36
+ orimg = Image.open(path)
37
+ return results, orimg
38
+
39
+
40
+ def detect_faces(image, box_margin=0):
41
+ faces = []
42
+ results, orimg = predict(image)
43
+ for box in results[0].boxes:
44
+ coords = box.xyxy[0]
45
+ coords = adjust_box(coords, orimg.size, margin=box_margin)
46
+ crop = orimg.crop(coords)
47
+ crop = crop.resize((512, 512))
48
+ faces.append(crop)
49
+ return faces
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ ultralytics
3
+ dill