lopesdri commited on
Commit
03a517d
·
1 Parent(s): b314b38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -14
app.py CHANGED
@@ -3,33 +3,35 @@ import cv2
3
  import numpy as np
4
  import gradio as gr
5
 
6
- # load model
7
  model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
8
 
9
- # set model parameters
10
- model.conf = 0.25 # NMS confidence threshold
11
- model.iou = 0.45 # NMS IoU threshold
12
- model.agnostic = False # NMS class-agnostic
13
- model.multi_label = False # NMS multiple labels per box
14
- model.max_det = 1000 # maximum number of detections per image
15
 
16
 
17
  def detect(img):
18
 
19
- # perform inference
20
  results = model(img, size=640)
21
 
22
- # inference with test time augmentation
23
- results = model(img, augment=True)
24
- # parse results
25
  predictions = results.pred[0]
26
  boxes = predictions[:, :4] # x1, y1, x2, y2
27
  scores = predictions[:, 4]
28
  categories = predictions[:, 5]
 
 
 
 
 
 
 
 
29
 
30
- # save image as numpy array
31
- return results.numpy()
32
- # show detection bounding boxes on image
33
 
34
  img = gr.inputs.Image(shape=(192, 192))
35
 
 
3
  import numpy as np
4
  import gradio as gr
5
 
6
+
7
  model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
8
 
9
+
10
+ model.conf = 0.25
11
+ model.iou = 0.45
12
+ model.agnostic = False
13
+ model.multi_label = False
14
+ model.max_det = 1000
15
 
16
 
17
  def detect(img):
18
 
19
+
20
  results = model(img, size=640)
21
 
 
 
 
22
  predictions = results.pred[0]
23
  boxes = predictions[:, :4] # x1, y1, x2, y2
24
  scores = predictions[:, 4]
25
  categories = predictions[:, 5]
26
+ dfResults = results.pandas().xyxy[0]
27
+ return drawRectangles(image, dfResults[['xmin', 'ymin', 'xmax','ymax']].astype(int))
28
+
29
+ def drawRectangles(image, dfResults):
30
+ for index, row in dfResults.iterrows():
31
+ print( (row['xmin'], row['ymin']))
32
+ image = cv2.rectangle(image, (row['xmin'], row['ymin']), (row['xmax'], row['ymax']), (255, 0, 0), 2)
33
+ return image
34
 
 
 
 
35
 
36
  img = gr.inputs.Image(shape=(192, 192))
37