Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -84,6 +84,122 @@ def detect(img,model):
|
|
84 |
print(weights)
|
85 |
if weights == 'yolop.pt':
|
86 |
weights = 'End-to-end.pth'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
|
89 |
|
|
|
84 |
print(weights)
|
85 |
if weights == 'yolop.pt':
|
86 |
weights = 'End-to-end.pth'
|
87 |
+
from lib.config import cfg
|
88 |
+
from lib.config import update_config
|
89 |
+
from lib.utils.utils import create_logger, select_device, time_synchronized
|
90 |
+
from lib.models import get_net
|
91 |
+
from lib.dataset import LoadImages, LoadStreams
|
92 |
+
from lib.core.general import non_max_suppression, scale_coords
|
93 |
+
from lib.utils import plot_one_box,show_seg_result
|
94 |
+
from lib.core.function import AverageMeter
|
95 |
+
from lib.core.postprocess import morphological_process, connect_lane
|
96 |
+
from tqdm import tqdm
|
97 |
+
normalize = transforms.Normalize(
|
98 |
+
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
|
99 |
+
)
|
100 |
+
|
101 |
+
transform=transforms.Compose([
|
102 |
+
transforms.ToTensor(),
|
103 |
+
normalize,
|
104 |
+
])
|
105 |
+
model = get_net(cfg)
|
106 |
+
checkpoint = torch.load(opt.weights, map_location= device)
|
107 |
+
model.load_state_dict(checkpoint['state_dict'])
|
108 |
+
model = model.to(device)
|
109 |
+
|
110 |
+
dataset = LoadImages(opt.source, img_size=opt.img_size)
|
111 |
+
bs = 1 # batch_size
|
112 |
+
|
113 |
+
# Get names and colors
|
114 |
+
names = model.module.names if hasattr(model, 'module') else model.names
|
115 |
+
colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))]
|
116 |
+
|
117 |
+
# Run inference
|
118 |
+
t0 = time.time()
|
119 |
+
|
120 |
+
vid_path, vid_writer = None, None
|
121 |
+
img = torch.zeros((1, 3, opt.img_size, opt.img_size), device=device) # init img
|
122 |
+
_ = model(img.half() if half else img) if device.type != 'cpu' else None # run once
|
123 |
+
model.eval()
|
124 |
+
|
125 |
+
|
126 |
+
for i, (path, img, img_det, vid_cap,shapes) in tqdm(enumerate(dataset),total = len(dataset)):
|
127 |
+
img = transform(img).to(device)
|
128 |
+
img = img.half() if half else img.float() # uint8 to fp16/32
|
129 |
+
if img.ndimension() == 3:
|
130 |
+
img = img.unsqueeze(0)
|
131 |
+
# Inference
|
132 |
+
t1 = time_synchronized()
|
133 |
+
det_out, da_seg_out,ll_seg_out= model(img)
|
134 |
+
t2 = time_synchronized()
|
135 |
+
# if i == 0:
|
136 |
+
# print(det_out)
|
137 |
+
inf_out, _ = det_out
|
138 |
+
inf_time.update(t2-t1,img.size(0))
|
139 |
+
|
140 |
+
# Apply NMS
|
141 |
+
t3 = time_synchronized()
|
142 |
+
det_pred = non_max_suppression(inf_out, conf_thres=opt.conf_thres, iou_thres=opt.iou_thres, classes=None, agnostic=False)
|
143 |
+
t4 = time_synchronized()
|
144 |
+
|
145 |
+
nms_time.update(t4-t3,img.size(0))
|
146 |
+
det=det_pred[0]
|
147 |
+
|
148 |
+
save_path = str(opt.save_dir +'/'+ Path(path).name) if dataset.mode != 'stream' else str(opt.save_dir + '/' + "web.mp4")
|
149 |
+
|
150 |
+
_, _, height, width = img.shape
|
151 |
+
h,w,_=img_det.shape
|
152 |
+
pad_w, pad_h = shapes[1][1]
|
153 |
+
pad_w = int(pad_w)
|
154 |
+
pad_h = int(pad_h)
|
155 |
+
ratio = shapes[1][0][1]
|
156 |
+
|
157 |
+
da_predict = da_seg_out[:, :, pad_h:(height-pad_h),pad_w:(width-pad_w)]
|
158 |
+
da_seg_mask = torch.nn.functional.interpolate(da_predict, scale_factor=int(1/ratio), mode='bilinear')
|
159 |
+
_, da_seg_mask = torch.max(da_seg_mask, 1)
|
160 |
+
da_seg_mask = da_seg_mask.int().squeeze().cpu().numpy()
|
161 |
+
# da_seg_mask = morphological_process(da_seg_mask, kernel_size=7)
|
162 |
+
|
163 |
+
|
164 |
+
ll_predict = ll_seg_out[:, :,pad_h:(height-pad_h),pad_w:(width-pad_w)]
|
165 |
+
ll_seg_mask = torch.nn.functional.interpolate(ll_predict, scale_factor=int(1/ratio), mode='bilinear')
|
166 |
+
_, ll_seg_mask = torch.max(ll_seg_mask, 1)
|
167 |
+
ll_seg_mask = ll_seg_mask.int().squeeze().cpu().numpy()
|
168 |
+
# Lane line post-processing
|
169 |
+
#ll_seg_mask = morphological_process(ll_seg_mask, kernel_size=7, func_type=cv2.MORPH_OPEN)
|
170 |
+
#ll_seg_mask = connect_lane(ll_seg_mask)
|
171 |
+
|
172 |
+
img_det = show_seg_result(img_det, (da_seg_mask, ll_seg_mask), _, _, is_demo=True)
|
173 |
+
|
174 |
+
if len(det):
|
175 |
+
det[:,:4] = scale_coords(img.shape[2:],det[:,:4],img_det.shape).round()
|
176 |
+
for *xyxy,conf,cls in reversed(det):
|
177 |
+
label_det_pred = f'{names[int(cls)]} {conf:.2f}'
|
178 |
+
plot_one_box(xyxy, img_det , label=label_det_pred, color=colors[int(cls)], line_thickness=2)
|
179 |
+
|
180 |
+
if dataset.mode == 'images':
|
181 |
+
cv2.imwrite(save_path,img_det)
|
182 |
+
|
183 |
+
elif dataset.mode == 'video':
|
184 |
+
if vid_path != save_path: # new video
|
185 |
+
vid_path = save_path
|
186 |
+
if isinstance(vid_writer, cv2.VideoWriter):
|
187 |
+
vid_writer.release() # release previous video writer
|
188 |
+
|
189 |
+
fourcc = 'mp4v' # output video codec
|
190 |
+
fps = vid_cap.get(cv2.CAP_PROP_FPS)
|
191 |
+
h,w,_=img_det.shape
|
192 |
+
vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*fourcc), fps, (w, h))
|
193 |
+
vid_writer.write(img_det)
|
194 |
+
|
195 |
+
else:
|
196 |
+
cv2.imshow('image', img_det)
|
197 |
+
cv2.waitKey(1) # 1 millisecond
|
198 |
+
im0 = img_det
|
199 |
+
print('Results saved to %s' % Path(opt.save_dir))
|
200 |
+
print('Done. (%.3fs)' % (time.time() - t0))
|
201 |
+
print('inf : (%.4fs/frame) nms : (%.4fs/frame)' % (inf_time.avg,nms_time.avg))
|
202 |
+
|
203 |
|
204 |
|
205 |
|