vp = Path(args.video_path) assert vp.is_file(), '{} not a file'.format(vp) cap = cv2.VideoCapture(str(vp)) assert cap.isOpened(), 'Cannot open video file!' cv2.namedWindow('yolo', cv2.WINDOW_NORMAL) while True: # Decode ret, frame = cap.read() if not ret: break # Inference tic = time.perf_counter() dets = yolo.detect_get_box_in([frame], box_format='ltrb') toc = time.perf_counter() print('infer duration: {:0.3f}s'.format(toc - tic)) dets = dets[0] # Drawing show_frame = frame.copy() for det in dets: ltrb, conf, clsname = det l, t, r, b = ltrb cv2.rectangle(show_frame, (int(l), int(t)), (int(r), int(b)), (255, 255, 0)) cv2.putText(show_frame, '{}:{:0.2f}'.format(clsname, conf), (l, b), cv2.FONT_HERSHEY_DUPLEX, fontScale=1,
od = YOLO( bgr=True, score=od_thresh, batch_size=bs, model_path='model_data/pp_reanchored_best_val.h5', anchors_path='model_data/PP_ALL_anchors.txt', classes_path='model_data/PP_classes.txt' ) remainder = len(pp) print('Forward passes...') for start_idx in tqdm(range(0, len(pp), bs)): if remainder < bs: bs = remainder od.regenerate(bs) images, impaths = pp.pull_images(start_idx, bs) remainder -= len(impaths) results = od.detect_get_box_in(images, box_format='ltrb') det_dict = {} for i, result in enumerate(results): impath = impaths[i] for det in result: box_info, score, pred_cls = det pred_cls_id = od.class_names.index(pred_cls) + 1 l,t,r,b = box_info this_array = np.array([l,t,r,b,score]) if pred_cls_id not in det_dict: det_dict[pred_cls_id] = {} if impath not in det_dict[pred_cls_id]: det_dict[pred_cls_id][impath] = this_array else: det_dict[pred_cls_id][impath] = np.vstack([det_dict[pred_cls_id][impath], this_array])
# Note that all images of a batch needs to be same size for the post-processing to make sense. for impath in ip.rglob('*'): if impath.suffix in ['.png', '.jpg', '.jpeg']: img = cv2.imread(str(impath)) if first_size is None: first_size = img.shape[:2] else: img = cv2.resize(img, first_size[::-1]) images.append(img) image_paths.append(impath) else: raise Exception('Path given does not exist') # Inference tic = time.perf_counter() all_dets = yolo.detect_get_box_in(images, box_format='ltrb') toc = time.perf_counter() print('infer duration: {:0.3f}s'.format(toc - tic)) # Drawing for img, imgpath, dets in zip(images, image_paths, all_dets): show_img = img.copy() for det in dets: ltrb, conf, clsname = det l, t, r, b = ltrb cv2.rectangle(show_img, (int(l), int(t)), (int(r), int(b)), (255, 255, 0)) cv2.putText(show_img, '{}:{:0.2f}'.format(clsname, conf), (l, b), cv2.FONT_HERSHEY_DUPLEX, fontScale=1,