onipot commited on
Commit
af771af
1 Parent(s): 0c2c19f
Files changed (2) hide show
  1. Roboto-Regular.ttf +0 -0
  2. app.py +85 -0
Roboto-Regular.ttf ADDED
Binary file (159 kB). View file
 
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image,ImageDraw, ImageFont
3
+ import sys
4
+ import os
5
+ import torch
6
+ from util import Detection
7
+
8
+ face_model = os.environ.get('FACE_MODEL')
9
+ age_model = os.environ.get('AGE_MODEL')
10
+
11
+ torch.hub.download_url_to_file(face_model, 'face_model.pt')
12
+ torch.hub.download_url_to_file(age_model, 'age_model.pt')
13
+
14
+ sys.path.append("./")
15
+ sys.path.append("./yolov5")
16
+
17
+ age_model_ts = torch.jit.load("age_model.pt")
18
+
19
+ from yolov5.detect import predict, load_yolo_model
20
+
21
+ # Model
22
+
23
+ model, stride, names, pt, jit, onnx, engine = load_yolo_model("face_model.pt", imgsz=[320,320])
24
+
25
+ def run_yolo(img):
26
+
27
+ #img0 = Image.open(img.name).convert("RGB")
28
+ img_path = img.name # ["name"]
29
+ img0 = Image.open(img_path).convert("RGB")
30
+ draw = ImageDraw.Draw(img0)
31
+
32
+ predictions = predict(age_model_ts, model, stride, names, pt, jit, onnx, engine, imgsz=[320, 320], conf_thres=0.5, iou_thres=0.45, save_conf=True,
33
+ exist_ok=True, nosave=True, save_txt=False, source=img_path, project=None, name=None)
34
+
35
+ detections : list[Detection] = []
36
+ for k, (bboxes, img) in enumerate(predictions):
37
+
38
+ #print(bboxes)
39
+ # exp.imgs.append(img_info)
40
+ for i, bbox in enumerate(bboxes):
41
+ det = Detection(
42
+ (k+1)*(i+1),
43
+ bbox["xmin"],
44
+ bbox["ymin"],
45
+ bbox["xmax"],
46
+ bbox["ymax"],
47
+ bbox["conf"],
48
+ bbox["class"],
49
+ bbox["class"],
50
+ img0.size
51
+ )
52
+ same = list(filter(lambda x: x.xmin == det.xmin and x.ymin == det.ymin or ( det.xmin > x.xmin and det.ymin > x.ymin and det.xmax < x.xmax and det.ymax < x.ymax ) or ( det.xmin < x.xmin and det.ymin < x.ymin and det.xmax > x.xmax and det.ymax > x.ymax ) or Detection.get_iou(det, x) > 0.6, detections))
53
+
54
+ if len(same) == 0:
55
+ detections.append(det)
56
+ draw.rectangle(((det.xmin, det.ymin), (det.xmax, det.ymax)), fill=None, outline=(255,255,255))
57
+ draw.rectangle(((det.xmin, det.ymin - 10), (det.xmax, det.ymin)), fill=(255,255,255))
58
+ draw.text((det.xmin, det.ymin - 10), det.class_name, fill=(0,0,0), font=ImageFont.truetype("Roboto-Regular.ttf"))
59
+
60
+ return img0
61
+
62
+ inputs = gr.inputs.Image(type='file', label="Input Image")
63
+ outputs = gr.outputs.Image(type="pil", label="Output Image")
64
+
65
+ title = "AgeGuesser"
66
+ description = "Guess the age of a person from his/her face!"
67
+ article = """A fully automated system based on YOLOv5 and EfficientNet to perform face detection and age estimation in real-time.
68
+
69
+ Links:
70
+ <ul>
71
+ <li>
72
+ <a href='https://link.springer.com/chapter/10.1007/978-3-030-89131-2_25'>Paper</a>
73
+ </li>
74
+ <li>
75
+ <a href='https://www.researchgate.net/publication/355777953_Real-Time_Age_Estimation_from_Facial_Images_Using_YOLO_and_EfficientNet'>Paper</a>
76
+ </li>
77
+ <li>
78
+ <a href='https://github.com/ai-hazard/AgeGuesser-train'>Github</a>
79
+
80
+ </li>
81
+ """
82
+
83
+ examples = [['images/1.jpg'], ['images/2.jpg'], ['images/3.jpg'], ['images/4.jpg'], ['images/5.jpg'], ]
84
+
85
+ gr.Interface(run_yolo, inputs, outputs, title=title, description=description, article=article, examples=examples, theme="huggingface").launch(enable_queue=True)