chauhuynh90 commited on
Commit
d163e81
·
1 Parent(s): 0a6a2e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -4
app.py CHANGED
@@ -6,8 +6,58 @@ from PIL import ImageFont
6
  import cv2
7
  import numpy as np
8
 
9
- def greet(name):
10
- return "Hello " + name + "!!"
11
 
12
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
13
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  import cv2
7
  import numpy as np
8
 
9
+ model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5n_rebar_kaggle.pt')
 
10
 
11
+ def yolo(im, conf, iou, size=640):
12
+
13
+ mask = np.array(im["mask"])
14
+ mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
15
+ contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
16
+ if contours:
17
+ mask = np.zeros(mask.shape, np.uint8)
18
+ cnt = contours[0]
19
+ mask = cv2.drawContours(mask, [cnt], 0, 255, -1)
20
+ im = np.array(im["image"])
21
+ im = cv2.bitwise_and(im, im, mask=mask)
22
+ im = Image.fromarray(im)
23
+ else:
24
+ im = im["image"]
25
+
26
+ model.conf = conf
27
+ model.iou = iou
28
+ results = model(im, size=size) # custom inference size
29
+ # inference
30
+
31
+ # output_im = Image.fromarray(results.render(labels=False)[0])
32
+ # output_im = results.render(labels=False)[0]
33
+ output_im = np.array(im)
34
+
35
+ pred = results.pandas().xyxy[0]
36
+ counting = pred.shape[0]
37
+ text = f"{counting} objects"
38
+ for index, row in pred.iterrows():
39
+ cv2.circle(output_im, (int((row["xmin"] + row["xmax"]) * 0.5), int((row["ymin"] + row["ymax"]) * 0.5)), int((row["xmax"] - row["xmin"]) * 0.5 * 0.6), (255, 0, 0), -1)
40
+
41
+ return Image.fromarray(output_im), text
42
+
43
+ slider_step = 0.05
44
+ nms_conf = 0.25
45
+ nms_iou = 0.1
46
+ # inputs_image = gr.inputs.Image(type='pil', label="Original Image")
47
+ inputs_image = gr.inputs.Image(tool="sketch", label="Original Image",type="pil")
48
+ inputs_conf = gr.Slider(0, 1, step=slider_step, value=nms_conf, label="Conf Thres")
49
+ inputs_iou = gr.Slider(0, 1, step=slider_step, value=nms_iou, label="IoU Thres")
50
+
51
+ inputs = [inputs_image, inputs_conf, inputs_iou]
52
+
53
+ outputs_image = gr.outputs.Image(type="pil", label="Output Image")
54
+ outputs_text = gr.Textbox(label="Number of objects")
55
+ outputs = [outputs_image, outputs_text]
56
+
57
+ title = "OBJECT COUNTING"
58
+ description = "Object counting demo. Upload an image or click an example image to use. You can select the area to count by drawing a closed area on the input image."
59
+ article = "<p style='text-align: center'>Counting objects in image</a></p>"
60
+ examples = [['S__275668998.jpg']]
61
+
62
+ gr.Interface(yolo, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch(
63
+ debug=True)#, share=True)