File size: 3,769 Bytes
af771af
0fe9ba3
af771af
 
13f24b5
b2de2a8
af771af
bd1ea7f
 
 
af771af
bd1ea7f
 
af771af
 
 
 
 
 
13f24b5
611c38a
af771af
611c38a
 
c12cb7e
af771af
c12cb7e
af771af
0fe9ba3
c12cb7e
 
 
0fe9ba3
 
af771af
 
bd1ea7f
 
 
c12cb7e
 
bd1ea7f
af771af
 
bd1ea7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c12cb7e
 
 
 
 
 
af771af
 
b2de2a8
c12cb7e
 
 
 
 
 
 
 
 
 
af771af
c12cb7e
 
 
 
cf58338
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c12cb7e
 
 
 
 
af771af
 
c12cb7e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import gradio as gr
from PIL import Image,ImageDraw, ImageFont, ImageOps
import sys
import torch
from util import Detection, load_font
import os

if os.environ.get('FACE_MODEL') is not None:
    face_model = os.environ.get('FACE_MODEL')
    age_model = os.environ.get('AGE_MODEL')

    torch.hub.download_url_to_file(face_model, 'face_model.pt')
    torch.hub.download_url_to_file(age_model, 'age_model.pt')

sys.path.append("./")
sys.path.append("./yolov5")

from yolov5.detect import predict, load_yolo_model


# Load Models
model, stride, names, pt, jit, onnx, engine = load_yolo_model("face_model.pt", imgsz=[320,320])
age_model_ts = torch.jit.load("age_model.pt")

text_box_height = 22

roboto_font = load_font(height_px=text_box_height-2)


def run_yolo(img0, with_random_augs):

    img0 = ImageOps.contain(img0, (640,640))
    img0 = ImageOps.exif_transpose(img0)

    draw = ImageDraw.Draw(img0)

    predictions = predict(age_model_ts, model, 
        stride, imgsz=[320, 320], 
        conf_thres=0.5, iou_thres=0.45,
        source=img0,
        with_random_augs = with_random_augs
    )

    detections : list[Detection] = []
    for k, bbox  in enumerate(predictions):
        det = Detection(
            (k+1),
            bbox["xmin"],
            bbox["ymin"],
            bbox["xmax"],
            bbox["ymax"],
            bbox["conf"],
            bbox["class"],
            bbox["class"],
            img0.size
        )

        detections.append(det)
        draw.rectangle(((det.xmin, det.ymin), (det.xmax, det.ymax)), fill=None, outline=(255,255,255))

        text_length = roboto_font.getlength(bbox["class"])

        rect_center = (det.xmin + det.xmax - text_length) // 2
        draw.rectangle(((rect_center, det.ymin), (rect_center + text_length, det.ymin + text_box_height)), fill=(255,255,255))
        draw.text((rect_center, det.ymin), det.class_name, fill=(0,0,0), font=roboto_font)

    return img0


""" img = Image.open("D:\\Download\\IMG_20220803_153335c2.jpg").convert("RGB")
run_yolo(img)
sys.exit(1) """


def main():
    input = gr.Image(type='pil', label="Input Image")
    outputs = gr.Image(type="pil", label="Output Image", interactive=False)
    
    augment_preds = gr.Checkbox(label="Apply random augmentations")

    title = "AgeGuesser"
    description = "Guess the age of a person from a facial image!"
    article = """
<p>A fully automated system based on YOLOv5 and EfficientNet to perform face detection and age estimation in real-time.</p>
<p><b>Links</b></p>
<ul>
<li>
<a href='https://link.springer.com/chapter/10.1007/978-3-030-89131-2_25'>Springer</a>
</li>
<li>
<a href='https://www.researchgate.net/publication/355777953_Real-Time_Age_Estimation_from_Facial_Images_Using_YOLO_and_EfficientNet'>Paper</a> 
</li>
<li>
<a href='https://github.com/ai-hazard/AgeGuesser-train'>Github</a> 
</li>
</ul>
<p>Credits to my dear colleague <a href='https://www.linkedin.com/in/nicola-marvulli-904270136/'>Dott. Nicola Marvulli</a>, we've developed AgeGuesser together as part of two university exams. (Computer Vision + Deep Learning)</p>
<p>Credits to my dear professors and the <a href='https://sites.google.com/site/cilabuniba/'>CILAB</a> research group</p>
<ul>
<li>
<a href='https://sites.google.com/site/cilabuniba/people/giovanna-castellano'>Prof. Giovanna Castellano</a>
</li>
<li>
<a href='https://sites.google.com/view/gennaro-vessio/home-page'>Prof. Gennaro Vessio</a> 
</li>
</ul>
    """

    examples = [['images/1.jpg'], ['images/2.jpg'], ['images/3.jpg'], ['images/4.jpg'], ['images/5.jpg'], ]

    gr.Interface(run_yolo, [input, augment_preds], outputs, title=title, description=description, article=article, examples=examples, theme="huggingface").launch(enable_queue=True, ) # share=True


main()