Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -36,153 +36,156 @@ from PIL import Image
|
|
36 |
|
37 |
|
38 |
def detect(img,model):
|
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 |
-
#(
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
img
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
#
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
#
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
|
|
|
|
|
|
186 |
return Image.fromarray(im0[:,:,::-1])
|
187 |
|
188 |
|
|
|
36 |
|
37 |
|
38 |
def detect(img,model):
|
39 |
+
with torch.no_grad():
|
40 |
+
parser = argparse.ArgumentParser()
|
41 |
+
parser.add_argument('--weights', nargs='+', type=str, default=model+".pt", help='model.pt path(s)')
|
42 |
+
parser.add_argument('--source', type=str, default='Inference/', help='source') # file/folder, 0 for webcam
|
43 |
+
parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
|
44 |
+
parser.add_argument('--conf-thres', type=float, default=0.25, help='object confidence threshold')
|
45 |
+
parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS')
|
46 |
+
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
47 |
+
parser.add_argument('--view-img', action='store_true', help='display results')
|
48 |
+
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
|
49 |
+
parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
|
50 |
+
parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
|
51 |
+
parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --class 0, or --class 0 2 3')
|
52 |
+
parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
|
53 |
+
parser.add_argument('--augment', action='store_true', help='augmented inference')
|
54 |
+
parser.add_argument('--update', action='store_true', help='update all models')
|
55 |
+
parser.add_argument('--project', default='runs/detect', help='save results to project/name')
|
56 |
+
parser.add_argument('--name', default='exp', help='save results to project/name')
|
57 |
+
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
|
58 |
+
parser.add_argument('--trace', action='store_true', help='trace model')
|
59 |
+
opt = parser.parse_args()
|
60 |
+
img.save("Inference/test.jpg")
|
61 |
+
source, weights, view_img, save_txt, imgsz, trace = opt.source, opt.weights, opt.view_img, opt.save_txt, opt.img_size, opt.trace
|
62 |
+
save_img = True # save inference images
|
63 |
+
#webcam = source.isnumeric() or source.endswith('.txt') or source.lower().startswith(
|
64 |
+
#('rtsp://', 'rtmp://', 'http://', 'https://'))
|
65 |
+
#print(webcam)
|
66 |
+
# Directories
|
67 |
+
save_dir = Path(increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok)) # increment run
|
68 |
+
(save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True) # make dir
|
69 |
+
|
70 |
+
# Initialize
|
71 |
+
set_logging()
|
72 |
+
device = select_device(opt.device)
|
73 |
+
#print(device)
|
74 |
+
half = device.type != 'cpu' # half precision only supported on CUDA
|
75 |
+
|
76 |
+
# Load model
|
77 |
+
inf_time = AverageMeter()
|
78 |
+
waste_time = AverageMeter()
|
79 |
+
nms_time = AverageMeter()
|
80 |
+
|
81 |
+
# Load model
|
82 |
+
#model = attempt_load(weights, map_location=device) # load FP32 model
|
83 |
+
#stride = int(model.stride.max()) # model stride
|
84 |
+
#imgsz = check_img_size(imgsz, s=stride) # check img_size
|
85 |
+
#print(weights)
|
86 |
+
stride =32
|
87 |
+
model = torch.jit.load(weights,map_location=device)
|
88 |
+
model.eval()
|
89 |
+
|
90 |
+
# Set Dataloader
|
91 |
+
vid_path, vid_writer = None, None
|
92 |
+
dataset = LoadImages(source, img_size=imgsz, stride=stride)
|
93 |
+
|
94 |
+
# Run inference
|
95 |
+
if device.type != 'cpu':
|
96 |
+
model(torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters()))) # run once
|
97 |
+
t0 = time.time()
|
98 |
+
for path, img, im0s, vid_cap in dataset:
|
99 |
+
img = torch.from_numpy(img).to(device)
|
100 |
+
img = img.half() if half else img.float() # uint8 to fp16/32
|
101 |
+
img /= 255.0 # 0 - 255 to 0.0 - 1.0
|
102 |
+
print(img.shape)
|
103 |
+
|
104 |
+
if img.ndimension() == 3:
|
105 |
+
img = img.unsqueeze(0)
|
106 |
+
|
107 |
+
# Inference
|
108 |
+
t1 = time_synchronized()
|
109 |
+
[pred,anchor_grid],seg,ll= model(img)
|
110 |
+
t2 = time_synchronized()
|
111 |
+
|
112 |
+
# waste time: the incompatibility of torch.jit.trace causes extra time consumption in demo version
|
113 |
+
# but this problem will not appear in offical version
|
114 |
+
tw1 = time_synchronized()
|
115 |
+
pred = split_for_trace_model(pred,anchor_grid)
|
116 |
+
tw2 = time_synchronized()
|
117 |
+
|
118 |
+
# Apply NMS
|
119 |
+
t3 = time_synchronized()
|
120 |
+
pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, classes=opt.classes, agnostic=opt.agnostic_nms)
|
121 |
+
t4 = time_synchronized()
|
122 |
+
|
123 |
+
da_seg_mask = driving_area_mask(seg)
|
124 |
+
ll_seg_mask = lane_line_mask(ll)
|
125 |
+
|
126 |
+
print(da_seg_mask.shape)
|
127 |
+
# Process detections
|
128 |
+
for i, det in enumerate(pred): # detections per image
|
129 |
+
|
130 |
+
p, s, im0, frame = path, '', im0s, getattr(dataset, 'frame', 0)
|
131 |
+
|
132 |
+
p = Path(p) # to Path
|
133 |
+
save_path = str(save_dir / p.name) # img.jpg
|
134 |
+
txt_path = str(save_dir / 'labels' / p.stem) + ('' if dataset.mode == 'image' else f'_{frame}') # img.txt
|
135 |
+
s += '%gx%g ' % img.shape[2:] # print string
|
136 |
+
gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh
|
137 |
+
if len(det):
|
138 |
+
# Rescale boxes from img_size to im0 size
|
139 |
+
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()
|
140 |
+
|
141 |
+
# Print results
|
142 |
+
for c in det[:, -1].unique():
|
143 |
+
n = (det[:, -1] == c).sum() # detections per class
|
144 |
+
#s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
|
145 |
+
|
146 |
+
# Write results
|
147 |
+
for *xyxy, conf, cls in reversed(det):
|
148 |
+
if save_txt: # Write to file
|
149 |
+
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
|
150 |
+
line = (cls, *xywh, conf) if opt.save_conf else (cls, *xywh) # label format
|
151 |
+
with open(txt_path + '.txt', 'a') as f:
|
152 |
+
f.write(('%g ' * len(line)).rstrip() % line + '\n')
|
153 |
+
|
154 |
+
if save_img : # Add bbox to image
|
155 |
+
plot_one_box(xyxy, im0, line_thickness=3)
|
156 |
+
|
157 |
+
# Print time (inference)
|
158 |
+
print(f'{s}Done. ({t2 - t1:.3f}s)')
|
159 |
+
show_seg_result(im0, (da_seg_mask,ll_seg_mask), is_demo=True)
|
160 |
+
|
161 |
+
# Save results (image with detections)
|
162 |
+
if save_img:
|
163 |
+
if dataset.mode == 'image':
|
164 |
+
cv2.imwrite(save_path, im0)
|
165 |
+
print(f" The image with the result is saved in: {save_path}")
|
166 |
+
else: # 'video' or 'stream'
|
167 |
+
if vid_path != save_path: # new video
|
168 |
+
vid_path = save_path
|
169 |
+
if isinstance(vid_writer, cv2.VideoWriter):
|
170 |
+
vid_writer.release() # release previous video writer
|
171 |
+
if vid_cap: # video
|
172 |
+
fps = vid_cap.get(cv2.CAP_PROP_FPS)
|
173 |
+
#w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
174 |
+
#h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
175 |
+
w,h = im0.shape[1], im0.shape[0]
|
176 |
+
else: # stream
|
177 |
+
fps, w, h = 30, im0.shape[1], im0.shape[0]
|
178 |
+
save_path += '.mp4'
|
179 |
+
vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
|
180 |
+
vid_writer.write(im0)
|
181 |
+
|
182 |
+
inf_time.update(t2-t1,img.size(0))
|
183 |
+
nms_time.update(t4-t3,img.size(0))
|
184 |
+
waste_time.update(tw2-tw1,img.size(0))
|
185 |
+
print('inf : (%.4fs/frame) nms : (%.4fs/frame)' % (inf_time.avg,nms_time.avg))
|
186 |
+
print(f'Done. ({time.time() - t0:.3f}s)')
|
187 |
+
print(im0.shape)
|
188 |
+
|
189 |
return Image.fromarray(im0[:,:,::-1])
|
190 |
|
191 |
|