onipot commited on
Commit
b2de2a8
1 Parent(s): cf58338

detect cleanup

Browse files
Files changed (3) hide show
  1. .gitignore +2 -1
  2. app.py +4 -3
  3. yolov5/detect.py +6 -36
.gitignore CHANGED
@@ -1,2 +1,3 @@
1
  __pycache__/
2
- *.sh
 
 
1
  __pycache__/
2
+ *.sh
3
+ *.pt
app.py CHANGED
@@ -1,9 +1,9 @@
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')
@@ -24,7 +24,7 @@ roboto_font = ImageFont.truetype("Roboto-Regular.ttf")
24
 
25
  def run_yolo(img):
26
 
27
- img_path = img.name # ["name"]
28
  img0 = Image.open(img_path).convert("RGB")
29
  draw = ImageDraw.Draw(img0)
30
 
@@ -56,7 +56,8 @@ def run_yolo(img):
56
 
57
  return img0
58
 
59
- inputs = gr.inputs.Image(type='file', label="Input Image")
 
60
  outputs = gr.outputs.Image(type="pil", label="Output Image")
61
 
62
  title = "AgeGuesser"
 
1
  import gradio as gr
2
  from PIL import Image,ImageDraw, ImageFont
3
  import sys
 
4
  import torch
5
  from util import Detection
6
+ import os
7
 
8
  face_model = os.environ.get('FACE_MODEL')
9
  age_model = os.environ.get('AGE_MODEL')
 
24
 
25
  def run_yolo(img):
26
 
27
+ img_path = img.name
28
  img0 = Image.open(img_path).convert("RGB")
29
  draw = ImageDraw.Draw(img0)
30
 
 
56
 
57
  return img0
58
 
59
+
60
+ inputs = gr.inputs.Image(type='filepath', label="Input Image")
61
  outputs = gr.outputs.Image(type="pil", label="Output Image")
62
 
63
  title = "AgeGuesser"
yolov5/detect.py CHANGED
@@ -251,7 +251,7 @@ def predict(
251
  augment=False, # augmented inference
252
  visualize=False, # visualize features
253
  update=False, # update all models
254
- project=ROOT / 'runs/detect', # save results to project/name
255
  name='exp', # save results to project/name
256
  exist_ok=False, # existing project/name ok, do not increment
257
  line_thickness=3, # bounding box thickness (pixels)
@@ -267,20 +267,9 @@ def predict(
267
 
268
  save_dir = None
269
  save_path = None
270
- # save_img = not nosave and not source.endswith('.txt') # save inference images
271
- is_file = Path(source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
272
- is_url = source.lower().startswith(('rtsp://', 'rtmp://', 'http://', 'https://'))
273
- webcam = source.isnumeric() or source.endswith('.txt') or (is_url and not is_file)
274
-
275
- # Directories
276
- if project is not None:
277
- save_dir = increment_path(Path(project) / name, exist_ok=exist_ok) # increment run
278
- (save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True) # make dir
279
 
280
  dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt)
281
- #bs = 1 # batch_size
282
- #vid_path, vid_writer = [None] * bs, [None] * bs
283
-
284
  # Run inference
285
 
286
  dt, seen = [0.0, 0.0, 0.0], 0
@@ -299,7 +288,7 @@ def predict(
299
  dt[0] += t2 - t1
300
 
301
  # Inference
302
- visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False
303
  pred = model(im, augment=augment, visualize=visualize)
304
  t3 = time_sync()
305
  dt[1] += t3 - t2
@@ -308,34 +297,16 @@ def predict(
308
  pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)
309
  dt[2] += time_sync() - t3
310
 
311
- # Second-stage classifier (optional)
312
- # pred = utils.general.apply_classifier(pred, classifier_model, im, im0s)
313
-
314
  # Process predictions
315
  preds = []
316
  for i, det in enumerate(pred): # per image
317
- seen += 1
318
- if webcam: # batch_size >= 1
319
- p, im0, frame = path[i], im0s[i].copy(), dataset.count
320
- s += f'{i}: '
321
- else:
322
- p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0)
323
-
324
- p = Path(p) # to Path
325
 
326
- #s += '%gx%g ' % im.shape[2:] # print string
327
- # gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh
328
- #imc = im0.copy() if save_crop else im0 # for save_crop
329
- #annotator = Annotator(im0, line_width=line_thickness, example=str(names))
330
  if len(det):
331
  # Rescale boxes from img_size to im0 size
332
  det[:, :4] = scale_coords(im.shape[2:], det[:, :4], im0.shape).round()
333
 
334
- # Print results
335
- """ for c in det[:, -1].unique():
336
- n = (det[:, -1] == c).sum() # detections per class
337
- s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string """
338
-
339
  for *xyxy, conf, cls in reversed(det):
340
  face = im0[int(xyxy[1]):int(xyxy[3]),int(xyxy[0]):int(xyxy[2])]
341
  face_img = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
@@ -344,8 +315,7 @@ def predict(
344
  y = age_model(im)
345
 
346
  age = y[0]
347
- print(age)
348
- # xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
349
  preds.append({"class": str(int(age)), "xmin": int(xyxy[0]), "ymin": int(xyxy[1]), "xmax": int(xyxy[2]),"ymax": int(xyxy[3]), "conf": float(conf)})
350
 
351
  yield preds, save_path
 
251
  augment=False, # augmented inference
252
  visualize=False, # visualize features
253
  update=False, # update all models
254
+ project=None, # save results to project/name
255
  name='exp', # save results to project/name
256
  exist_ok=False, # existing project/name ok, do not increment
257
  line_thickness=3, # bounding box thickness (pixels)
 
267
 
268
  save_dir = None
269
  save_path = None
 
 
 
 
 
 
 
 
 
270
 
271
  dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt)
272
+
 
 
273
  # Run inference
274
 
275
  dt, seen = [0.0, 0.0, 0.0], 0
 
288
  dt[0] += t2 - t1
289
 
290
  # Inference
291
+ visualize = False
292
  pred = model(im, augment=augment, visualize=visualize)
293
  t3 = time_sync()
294
  dt[1] += t3 - t2
 
297
  pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)
298
  dt[2] += time_sync() - t3
299
 
 
 
 
300
  # Process predictions
301
  preds = []
302
  for i, det in enumerate(pred): # per image
 
 
 
 
 
 
 
 
303
 
304
+ p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0)
305
+
 
 
306
  if len(det):
307
  # Rescale boxes from img_size to im0 size
308
  det[:, :4] = scale_coords(im.shape[2:], det[:, :4], im0.shape).round()
309
 
 
 
 
 
 
310
  for *xyxy, conf, cls in reversed(det):
311
  face = im0[int(xyxy[1]):int(xyxy[3]),int(xyxy[0]):int(xyxy[2])]
312
  face_img = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
 
315
  y = age_model(im)
316
 
317
  age = y[0]
318
+
 
319
  preds.append({"class": str(int(age)), "xmin": int(xyxy[0]), "ymin": int(xyxy[1]), "xmax": int(xyxy[2]),"ymax": int(xyxy[3]), "conf": float(conf)})
320
 
321
  yield preds, save_path