Delete app.py
Browse files
app.py
DELETED
|
@@ -1,204 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import argparse
|
| 3 |
-
import glob
|
| 4 |
-
import os
|
| 5 |
-
import random
|
| 6 |
-
import time
|
| 7 |
-
from pathlib import Path
|
| 8 |
-
|
| 9 |
-
import cv2
|
| 10 |
-
import numpy as np
|
| 11 |
-
import torch
|
| 12 |
-
import onnxruntime as ort
|
| 13 |
-
from PIL import Image
|
| 14 |
-
from torchvision import transforms
|
| 15 |
-
from tqdm import tqdm
|
| 16 |
-
|
| 17 |
-
from utils.general import non_max_suppression, scale_coords, letterbox_for_img
|
| 18 |
-
from utils.plots import show_seg_result, plot_one_box, driving_area_mask, lane_line_mask
|
| 19 |
-
from utils.torch_utils import select_device, AverageMeter, time_synchronized
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def detect(path, model):
|
| 23 |
-
# with torch.no_grad():
|
| 24 |
-
parser = argparse.ArgumentParser()
|
| 25 |
-
parser.add_argument('--weights', type=str, default=model + '.onnx', help='model.pth path(s)')
|
| 26 |
-
parser.add_argument('--source', type=str, default=path, help='file/folder ex:inference/images')
|
| 27 |
-
parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
|
| 28 |
-
parser.add_argument('--conf-thres', type=float, default=0.25, help='object confidence threshold')
|
| 29 |
-
parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS')
|
| 30 |
-
parser.add_argument('--device', default='cpu', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
| 31 |
-
parser.add_argument('--save_dir', type=str, default='runs/detect', help='directory to save results')
|
| 32 |
-
parser.add_argument('--original_shape', default=True, help='maintain original shape')
|
| 33 |
-
opt = parser.parse_args()
|
| 34 |
-
|
| 35 |
-
device = select_device(device=opt.device)
|
| 36 |
-
half = device.type != 'cpu' # half precision only supported on CUDA
|
| 37 |
-
|
| 38 |
-
# Load model
|
| 39 |
-
inf_time = AverageMeter()
|
| 40 |
-
nms_time = AverageMeter()
|
| 41 |
-
|
| 42 |
-
ort.set_default_logger_severity(4)
|
| 43 |
-
ort_session = ort.InferenceSession('weights/' + opt.weights)
|
| 44 |
-
|
| 45 |
-
# Set Dataloader
|
| 46 |
-
dataset = LoadImages(opt.source, img_size=opt.img_size)
|
| 47 |
-
|
| 48 |
-
transform = transforms.Compose([
|
| 49 |
-
transforms.ToTensor(),
|
| 50 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 51 |
-
])
|
| 52 |
-
|
| 53 |
-
t0 = time.time()
|
| 54 |
-
for i, (path, img, img_det, vid_cap, shapes) in tqdm(enumerate(dataset), total=len(dataset)):
|
| 55 |
-
img = transform(img).to(device)
|
| 56 |
-
img = img.half() if half else img.float() # uint8 to fp16/32
|
| 57 |
-
if img.ndimension() == 3:
|
| 58 |
-
img = img.unsqueeze(0)
|
| 59 |
-
|
| 60 |
-
img = img.numpy()
|
| 61 |
-
|
| 62 |
-
# Inference
|
| 63 |
-
t1 = time_synchronized()
|
| 64 |
-
if opt.weights == 'mtpnet.onnx':
|
| 65 |
-
det_out, da_seg_out, ll_seg_out = ort_session.run(
|
| 66 |
-
['detect_output', 'driving_area_segmentation', 'lane_line_segmentation'], input_feed={"images": img}
|
| 67 |
-
)
|
| 68 |
-
else:
|
| 69 |
-
det_out, da_seg_out, ll_seg_out = ort_session.run(
|
| 70 |
-
['det_out', 'drive_area_seg', 'lane_line_seg'], input_feed={"images": img}
|
| 71 |
-
)
|
| 72 |
-
t2 = time_synchronized()
|
| 73 |
-
|
| 74 |
-
det_out = torch.from_numpy(det_out).float()
|
| 75 |
-
da_seg_out = torch.from_numpy(da_seg_out).float()
|
| 76 |
-
ll_seg_out = torch.from_numpy(ll_seg_out).float()
|
| 77 |
-
|
| 78 |
-
# Apply NMS
|
| 79 |
-
t3 = time_synchronized()
|
| 80 |
-
det_pred = non_max_suppression(det_out, opt.conf_thres, opt.iou_thres)
|
| 81 |
-
t4 = time_synchronized()
|
| 82 |
-
|
| 83 |
-
height, width = img.shape[2], img.shape[3]
|
| 84 |
-
pad_w, pad_h = shapes[1][1]
|
| 85 |
-
pad_w, pad_h = int(pad_w), int(pad_h)
|
| 86 |
-
|
| 87 |
-
inf_time.update(t2 - t1, img.shape[0])
|
| 88 |
-
nms_time.update(t4 - t3, img.shape[0])
|
| 89 |
-
|
| 90 |
-
da_seg_mask = driving_area_mask(da_seg_out, width, height, pad_w, pad_h, 0.5)
|
| 91 |
-
ll_seg_mask = lane_line_mask(ll_seg_out, width, height, pad_w, pad_h, 0.5)
|
| 92 |
-
img_det = show_seg_result(img_det, (da_seg_mask, ll_seg_mask), batch=0, is_demo=True)
|
| 93 |
-
|
| 94 |
-
det = det_pred[0]
|
| 95 |
-
if len(det):
|
| 96 |
-
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img_det.shape).round()
|
| 97 |
-
for *xyxy, conf, cls in reversed(det):
|
| 98 |
-
label_det_pred = 'Car ' + f'{conf * 100:.2f}' + '%'
|
| 99 |
-
plot_one_box(xyxy, img_det, label=label_det_pred, color=[0, 255, 255], line_thickness=1)
|
| 100 |
-
|
| 101 |
-
if opt.original_shape:
|
| 102 |
-
ori_height = int(640 / shapes[1][0][0])
|
| 103 |
-
ori_width = int(640 / shapes[1][0][1])
|
| 104 |
-
img_det = cv2.resize(img_det, (ori_width, ori_height), interpolation=cv2.INTER_LINEAR)
|
| 105 |
-
|
| 106 |
-
print('Done. (%.3fs)' % (time.time() - t0))
|
| 107 |
-
print('inf : (%.4fs/frame) nms : (%.4fs/frame)' % (inf_time.avg, nms_time.avg))
|
| 108 |
-
|
| 109 |
-
return Image.fromarray(img_det[:, :, ::-1])
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
class LoadImages: # for inference
|
| 113 |
-
def __init__(self, path, img_size=640, stride=32):
|
| 114 |
-
p = str(Path(path)) # os-agnostic
|
| 115 |
-
p = os.path.abspath(p) # absolute path
|
| 116 |
-
if '*' in p:
|
| 117 |
-
files = sorted(glob.glob(p, recursive=True)) # glob
|
| 118 |
-
elif os.path.isdir(p):
|
| 119 |
-
files = sorted(glob.glob(os.path.join(p, '*.*'))) # dir
|
| 120 |
-
elif os.path.isfile(p):
|
| 121 |
-
files = [p] # files
|
| 122 |
-
else:
|
| 123 |
-
raise Exception('ERROR: %s does not exist' % p)
|
| 124 |
-
|
| 125 |
-
img_formats = ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.tiff', '.dng']
|
| 126 |
-
vid_formats = ['.mov', '.avi', '.mp4', '.mpg', '.mpeg', '.m4v', '.wmv', '.mkv']
|
| 127 |
-
images = [x for x in files if os.path.splitext(x)[-1].lower() in img_formats]
|
| 128 |
-
videos = [x for x in files if os.path.splitext(x)[-1].lower() in vid_formats]
|
| 129 |
-
ni, nv = len(images), len(videos)
|
| 130 |
-
|
| 131 |
-
self.img_size = img_size
|
| 132 |
-
self.files = images + videos
|
| 133 |
-
self.nf = ni + nv # number of files
|
| 134 |
-
self.video_flag = [False] * ni + [True] * nv
|
| 135 |
-
self.mode = 'images'
|
| 136 |
-
self.frame = 0
|
| 137 |
-
self.nframes = 0
|
| 138 |
-
if any(videos):
|
| 139 |
-
self.new_video(videos[0]) # new video
|
| 140 |
-
else:
|
| 141 |
-
self.cap = None
|
| 142 |
-
assert self.nf > 0, f'No images or videos found in {p}. ' \
|
| 143 |
-
f'Supported formats are:\nimages: {img_formats}\nvideos: {vid_formats}'
|
| 144 |
-
|
| 145 |
-
def __iter__(self):
|
| 146 |
-
self.count = 0
|
| 147 |
-
return self
|
| 148 |
-
|
| 149 |
-
def __next__(self):
|
| 150 |
-
if self.count == self.nf:
|
| 151 |
-
raise StopIteration
|
| 152 |
-
path = self.files[self.count]
|
| 153 |
-
|
| 154 |
-
if self.video_flag[self.count]:
|
| 155 |
-
# Read video
|
| 156 |
-
self.mode = 'video'
|
| 157 |
-
ret_val, img0 = self.cap.read()
|
| 158 |
-
if not ret_val:
|
| 159 |
-
self.count += 1
|
| 160 |
-
self.cap.release()
|
| 161 |
-
if self.count == self.nf: # last video
|
| 162 |
-
raise StopIteration
|
| 163 |
-
else:
|
| 164 |
-
path = self.files[self.count]
|
| 165 |
-
self.new_video(path)
|
| 166 |
-
ret_val, img0 = self.cap.read()
|
| 167 |
-
h0, w0 = img0.shape[:2]
|
| 168 |
-
|
| 169 |
-
self.frame += 1
|
| 170 |
-
print('\n video %g/%g (%g/%g) %s: ' % (self.count + 1, self.nf, self.frame, self.nframes, path), end='')
|
| 171 |
-
|
| 172 |
-
else:
|
| 173 |
-
# Read image
|
| 174 |
-
self.count += 1
|
| 175 |
-
img0 = cv2.imread(path, cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION) # BGR
|
| 176 |
-
assert img0 is not None, 'Image Not Found ' + path
|
| 177 |
-
print('image %g/%g %s: \n' % (self.count, self.nf, path), end='')
|
| 178 |
-
h0, w0 = img0.shape[:2]
|
| 179 |
-
|
| 180 |
-
# Padded resize
|
| 181 |
-
img0 = cv2.resize(img0, (1280, 720), interpolation=cv2.INTER_LINEAR)
|
| 182 |
-
img, ratio, pad = letterbox_for_img(img0, new_shape=self.img_size, auto=False)
|
| 183 |
-
h, w = img.shape[:2]
|
| 184 |
-
shapes = (h0, w0), ((h / h0, w / w0), pad)
|
| 185 |
-
|
| 186 |
-
# Convert
|
| 187 |
-
# img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB
|
| 188 |
-
img = np.ascontiguousarray(img)
|
| 189 |
-
|
| 190 |
-
return path, img, img0, self.cap, shapes
|
| 191 |
-
|
| 192 |
-
def new_video(self, path):
|
| 193 |
-
self.frame = 0
|
| 194 |
-
self.cap = cv2.VideoCapture(path)
|
| 195 |
-
self.nframes = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 196 |
-
|
| 197 |
-
def __len__(self):
|
| 198 |
-
return self.nf # number of files
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
gr.Interface(detect, [gr.Image(type='filepath'), gr.Dropdown(choices=["mtpnet", "yolop"])], gr.Image(type="pil"),
|
| 202 |
-
title="Mtpnet", examples=[["example.jpg", "mtpnet"], ["example2.jpg", "mtpnet"]],
|
| 203 |
-
description="Mtpnet 🚀: demo for multitask panoptic driving perception network").launch()
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|