Spaces:
Runtime error
Runtime error
Commit
·
00b33b8
1
Parent(s):
7d4d504
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,123 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
| 4 |
-
from PIL import ImageDraw
|
| 5 |
-
from PIL import ImageFont
|
| 6 |
import cv2
|
| 7 |
import numpy as np
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
| 4 |
+
# from PIL import ImageDraw
|
| 5 |
+
# from PIL import ImageFont
|
| 6 |
import cv2
|
| 7 |
import numpy as np
|
| 8 |
+
import argparse
|
| 9 |
|
| 10 |
+
def parse_args(known=False):
|
| 11 |
+
parser = argparse.ArgumentParser(description="Object Counting v1.0")
|
| 12 |
+
parser.add_argument(
|
| 13 |
+
"--yolov5_path",
|
| 14 |
+
"-yp",
|
| 15 |
+
default="ultralytics/yolov5",
|
| 16 |
+
type=str,
|
| 17 |
+
help="yolov5 path",
|
| 18 |
+
)
|
| 19 |
+
parser.add_argument(
|
| 20 |
+
"--model_path",
|
| 21 |
+
"-mp",
|
| 22 |
+
default="model/yolov5n_rebar_kaggle.pt",
|
| 23 |
+
type=str,
|
| 24 |
+
help="model path",
|
| 25 |
+
)
|
| 26 |
+
parser.add_argument(
|
| 27 |
+
"--nms_conf",
|
| 28 |
+
"-conf",
|
| 29 |
+
default=0.25,
|
| 30 |
+
type=float,
|
| 31 |
+
help="model NMS confidence threshold",
|
| 32 |
+
)
|
| 33 |
+
parser.add_argument(
|
| 34 |
+
"--nms_iou",
|
| 35 |
+
"-iou",
|
| 36 |
+
default=0.1,
|
| 37 |
+
type=float,
|
| 38 |
+
help="model NMS IoU threshold"
|
| 39 |
+
)
|
| 40 |
+
parser.add_argument(
|
| 41 |
+
"--device",
|
| 42 |
+
"-dev",
|
| 43 |
+
default="cuda:0",
|
| 44 |
+
type=str,
|
| 45 |
+
help="cuda or cpu",
|
| 46 |
+
)
|
| 47 |
+
parser.add_argument("--inference_size", "-isz", default=640, type=int, help="model inference size")
|
| 48 |
+
parser.add_argument("--slider_step", "-ss", default=0.05, type=float, help="slider step")
|
| 49 |
+
|
| 50 |
+
args = parser.parse_known_args()[0] if known else parser.parse_args()
|
| 51 |
+
return args
|
| 52 |
+
|
| 53 |
+
def image_roi(im):
|
| 54 |
+
mask = np.array(im["mask"])
|
| 55 |
+
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
|
| 56 |
+
contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
| 57 |
+
if contours:
|
| 58 |
+
mask = np.zeros(mask.shape, np.uint8)
|
| 59 |
+
cnt = contours[0]
|
| 60 |
+
mask = cv2.drawContours(mask, [cnt], 0, 255, -1)
|
| 61 |
+
im = np.array(im["image"])
|
| 62 |
+
im = cv2.bitwise_and(im, im, mask=mask)
|
| 63 |
+
im = Image.fromarray(im)
|
| 64 |
+
else:
|
| 65 |
+
im = im["image"]
|
| 66 |
+
return im
|
| 67 |
+
|
| 68 |
+
def yolo(im, conf, iou, size):
|
| 69 |
+
|
| 70 |
+
global model
|
| 71 |
+
if im is None:
|
| 72 |
+
print ("No image")
|
| 73 |
+
return None, None
|
| 74 |
+
im = image_roi(im)
|
| 75 |
+
model.conf = conf
|
| 76 |
+
model.iou = iou
|
| 77 |
+
results = model(im, size=size) # custom inference size
|
| 78 |
+
output_im = np.array(im)
|
| 79 |
+
|
| 80 |
+
pred = results.pandas().xyxy[0]
|
| 81 |
+
counting = pred.shape[0]
|
| 82 |
+
text = f"{counting} objects"
|
| 83 |
+
for index, row in pred.iterrows():
|
| 84 |
+
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)
|
| 85 |
+
|
| 86 |
+
return Image.fromarray(output_im), text
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def main(args):
|
| 90 |
+
gr.close_all()
|
| 91 |
+
|
| 92 |
+
global model
|
| 93 |
+
yolo_path = args.yolov5_path
|
| 94 |
+
model_path = args.model_path
|
| 95 |
+
nms_conf = args.nms_conf
|
| 96 |
+
nms_iou = args.nms_iou
|
| 97 |
+
device = args.device
|
| 98 |
+
inference_size = args.inference_size
|
| 99 |
+
slider_step = args.slider_step
|
| 100 |
+
|
| 101 |
+
model = torch.hub.load(yolo_path, 'custom', path=model_path, device=device)
|
| 102 |
+
inputs_image = gr.inputs.Image(tool="sketch", label="Original Image",type="pil")
|
| 103 |
+
inputs_conf = gr.Slider(0, 1, step=slider_step, value=nms_conf, label="Conf Thres")
|
| 104 |
+
inputs_iou = gr.Slider(0, 1, step=slider_step, value=nms_iou, label="IoU Thres")
|
| 105 |
+
inputs_size = gr.Slider(384, 1536, step=128, value=inference_size, label="Inference Size")
|
| 106 |
+
inputs = [inputs_image, inputs_conf, inputs_iou, inputs_size]
|
| 107 |
+
|
| 108 |
+
outputs_image = gr.outputs.Image(type="pil", label="Output Image")
|
| 109 |
+
outputs_text = gr.Textbox(label="Number of objects")
|
| 110 |
+
outputs = [outputs_image, outputs_text]
|
| 111 |
+
|
| 112 |
+
title = "OBJECT COUNTING"
|
| 113 |
+
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."
|
| 114 |
+
article = "<p style='text-align: center'>Counting objects in image</a></p>"
|
| 115 |
+
examples = [['./images/S__275668998.jpg'], ['./images/S__275669003.jpg'], ['./images/S__275669004.jpg']]
|
| 116 |
+
|
| 117 |
+
gr.Interface(fn=yolo, inputs=inputs, outputs=outputs, title=title, description=description, article=article, examples=examples, cache_examples=False, analytics_enabled=False).launch(
|
| 118 |
+
debug=True, share=True)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
args = parse_args()
|
| 123 |
+
main(args)
|