AgeGuesser / app.py
onipot
detect cleanup
b2de2a8
raw
history blame
3.76 kB
import gradio as gr
from PIL import Image,ImageDraw, ImageFont
import sys
import torch
from util import Detection
import os
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")
roboto_font = ImageFont.truetype("Roboto-Regular.ttf")
def run_yolo(img):
img_path = img.name
img0 = Image.open(img_path).convert("RGB")
draw = ImageDraw.Draw(img0)
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,
exist_ok=True, nosave=True, save_txt=False, source=img_path, project=None, name=None)
detections : list[Detection] = []
for k, (bboxes, img) in enumerate(predictions):
for i, bbox in enumerate(bboxes):
det = Detection(
(k+1)*(i+1),
bbox["xmin"],
bbox["ymin"],
bbox["xmax"],
bbox["ymax"],
bbox["conf"],
bbox["class"],
bbox["class"],
img0.size
)
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))
if len(same) == 0:
detections.append(det)
draw.rectangle(((det.xmin, det.ymin), (det.xmax, det.ymax)), fill=None, outline=(255,255,255))
draw.rectangle(((det.xmin, det.ymin - 10), (det.xmax, det.ymin)), fill=(255,255,255))
draw.text((det.xmin, det.ymin - 10), det.class_name, fill=(0,0,0), font=roboto_font)
return img0
inputs = gr.inputs.Image(type='filepath', label="Input Image")
outputs = gr.outputs.Image(type="pil", label="Output Image")
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, inputs, outputs, title=title, description=description, article=article, examples=examples, theme="huggingface").launch(enable_queue=True)