onipot commited on
Commit
13f24b5
1 Parent(s): bd1ea7f

pil image as input

Browse files
Files changed (2) hide show
  1. app.py +12 -10
  2. util.py +17 -2
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  from PIL import Image,ImageDraw, ImageFont, ImageOps
3
  import sys
4
  import torch
5
- from util import Detection
6
  import os
7
 
8
  if os.environ.get('FACE_MODEL') is not None:
@@ -17,16 +17,17 @@ sys.path.append("./yolov5")
17
 
18
  from yolov5.detect import predict, load_yolo_model
19
 
 
20
  # Load Models
21
  model, stride, names, pt, jit, onnx, engine = load_yolo_model("face_model.pt", imgsz=[320,320])
22
  age_model_ts = torch.jit.load("age_model.pt")
23
 
24
- roboto_font = ImageFont.truetype("Roboto-Regular.ttf")
25
 
26
- def run_yolo(img):
27
 
28
- img_path = img
29
- img0 = Image.open(img_path).convert("RGB")
30
 
31
  img0 = ImageOps.contain(img0, (720,720))
32
  img0 = ImageOps.exif_transpose(img0)
@@ -55,15 +56,16 @@ def run_yolo(img):
55
 
56
  detections.append(det)
57
  draw.rectangle(((det.xmin, det.ymin), (det.xmax, det.ymax)), fill=None, outline=(255,255,255))
58
- draw.rectangle(((det.xmin, det.ymin - 10), (det.xmax, det.ymin)), fill=(255,255,255))
59
- draw.text((det.xmin, det.ymin - 10), det.class_name, fill=(0,0,0), font=roboto_font)
60
 
 
61
  return img0
62
 
63
 
64
- # run_yolo("D:\\Download\\IMG_20220803_153335.jpg")
65
-
66
- inputs = gr.inputs.Image(type='filepath', label="Input Image")
67
  outputs = gr.outputs.Image(type="pil", label="Output Image")
68
 
69
  title = "AgeGuesser"
 
2
  from PIL import Image,ImageDraw, ImageFont, ImageOps
3
  import sys
4
  import torch
5
+ from util import Detection, load_font
6
  import os
7
 
8
  if os.environ.get('FACE_MODEL') is not None:
 
17
 
18
  from yolov5.detect import predict, load_yolo_model
19
 
20
+
21
  # Load Models
22
  model, stride, names, pt, jit, onnx, engine = load_yolo_model("face_model.pt", imgsz=[320,320])
23
  age_model_ts = torch.jit.load("age_model.pt")
24
 
25
+ roboto_font = load_font(height_px=18)
26
 
27
+ def run_yolo(img0):
28
 
29
+ #img_path = img
30
+ #img0 = Image.open(img_path).convert("RGB")
31
 
32
  img0 = ImageOps.contain(img0, (720,720))
33
  img0 = ImageOps.exif_transpose(img0)
 
56
 
57
  detections.append(det)
58
  draw.rectangle(((det.xmin, det.ymin), (det.xmax, det.ymax)), fill=None, outline=(255,255,255))
59
+ draw.rectangle(((det.xmin, det.ymin - 20), (det.xmax, det.ymin)), fill=(255,255,255))
60
+ draw.text((det.xmin, det.ymin - 20), det.class_name, fill=(0,0,0), font=roboto_font)
61
 
62
+ # img0.save("img.jpg")
63
  return img0
64
 
65
 
66
+ #run_yolo("D:\\Download\\IMG_20220803_153335c.jpg")
67
+ #sys.exit(1)
68
+ inputs = gr.inputs.Image(type='pil', label="Input Image")
69
  outputs = gr.outputs.Image(type="pil", label="Output Image")
70
 
71
  title = "AgeGuesser"
util.py CHANGED
@@ -1,5 +1,7 @@
1
- class Detection(object):
 
2
 
 
3
 
4
  def __init__(self, id: int, xmin: int, ymin: int, xmax:int, ymax:int, conf: float, class_id:int, class_name:str, orig_img_sz: "tuple[int]") -> None:
5
 
@@ -79,4 +81,17 @@ class Detection(object):
79
  return iou
80
 
81
  def __str__(self) -> str:
82
- return f"[{self.xmin}, {self.ymin}, {self.xmax}, {self.ymax}]"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image,ImageDraw, ImageFont, ImageOps
2
+
3
 
4
+ class Detection(object):
5
 
6
  def __init__(self, id: int, xmin: int, ymin: int, xmax:int, ymax:int, conf: float, class_id:int, class_name:str, orig_img_sz: "tuple[int]") -> None:
7
 
 
81
  return iou
82
 
83
  def __str__(self) -> str:
84
+ return f"[{self.xmin}, {self.ymin}, {self.xmax}, {self.ymax}]"
85
+
86
+
87
+ def load_font(height_px = 20):
88
+
89
+ init_size = 12
90
+ roboto_font = ImageFont.truetype("Roboto-Regular.ttf", size=init_size)
91
+
92
+ while roboto_font.getsize("20")[1] < height_px:
93
+
94
+ init_size += 1
95
+ roboto_font = ImageFont.truetype("Roboto-Regular.ttf", size=init_size)
96
+
97
+ return roboto_font