def detect(cfgfile, weightfile, imgfile): m = Darknet(cfgfile) m.print_network() m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) num_classes = 80 if num_classes == 20: namesfile = 'data/voc.names' elif num_classes == 80: namesfile = 'data/coco.names' else: namesfile = 'data/names' use_cuda = 0 if use_cuda: m.cuda() # img = Image.open(imgfile).convert('RGB') # sized = img.resize((m.width, m.height)) w_im = imgfile.shape[1] h_im = imgfile.shape[0] boxes = np.array(do_detect(m, imgfile, 0.5, 0.4, use_cuda)) boxes[:, 0] *= w_im boxes[:, 1] *= h_im boxes[:, 2] *= w_im boxes[:, 3] *= h_im return boxes
def detect_cv2(cfgfile, weightfile, imgfile): import cv2 m = Darknet(cfgfile) m.print_network() m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) if use_cuda: m.cuda() img = cv2.imread(imgfile) sized = cv2.resize(img, (m.width, m.height)) sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB) for i in range(2): start = time.time() boxes = do_detect(m, sized, 0.5, m.num_classes, 0.4, use_cuda) finish = time.time() if i == 1: print('%s: Predicted in %f seconds.' % (imgfile, (finish - start))) class_names = load_class_names(namesfile) plot_boxes_cv2(img, boxes, savename='predictions.jpg', class_names=class_names)
def detect_cv2_camera(cfgfile, weightfile): import cv2 m = Darknet(cfgfile) m.print_network() m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) if use_cuda: m.cuda() cap = cv2.VideoCapture(0) # cap = cv2.VideoCapture("./test.mp4") print("Starting the YOLO loop...") while True: ret, img = cap.read() sized = cv2.resize(img, (m.width, m.height)) sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB) start = time.time() boxes = do_detect(m, sized, 0.5, num_classes, 0.4, use_cuda) finish = time.time() print('Predicted in %f seconds.' % (finish - start)) class_names = load_class_names(namesfile) result_img = plot_boxes_cv2(img, boxes, savename=None, class_names=class_names) cv2.imshow('Yolo demo', result_img) cv2.waitKey(1) cap.release()
def detect(cfgfile, weightfile, imgfile): m = Darknet(cfgfile) m.print_network() m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) num_classes = 80 if num_classes == 20: namesfile = 'data/voc.names' elif num_classes == 80: namesfile = 'data/coco.names' else: namesfile = 'data/names' use_cuda = 0 if use_cuda: m.cuda() img = Image.open(imgfile).convert('RGB') sized = img.resize((m.width, m.height)) for i in range(2): start = time.time() boxes = do_detect(m, sized, 0.5, 0.4, use_cuda) finish = time.time() if i == 1: print('%s: Predicted in %f seconds.' % (imgfile, (finish - start))) class_names = load_class_names(namesfile) plot_boxes(img, boxes, 'predictions.jpg', class_names)
def detect_imges(cfgfile, weightfile, imgfile_list=['data/dog.jpg', 'data/giraffe.jpg']): m = Darknet(cfgfile) m.print_network() m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) if use_cuda: m.cuda() imges = [] imges_list = [] for imgfile in imgfile_list: img = Image.open(imgfile).convert('RGB') imges_list.append(img) sized = img.resize((m.width, m.height)) imges.append(np.expand_dims(np.array(sized), axis=0)) images = np.concatenate(imges, 0) for i in range(2): start = time.time() boxes = do_detect(m, images, 0.5, num_classes, 0.4, use_cuda) finish = time.time() if i == 1: print('%s: Predicted in %f seconds.' % (imgfile, (finish - start))) class_names = load_class_names(namesfile) for i, (img, box) in enumerate(zip(imges_list, boxes)): plot_boxes(img, box, 'predictions{}.jpg'.format(i), class_names)
def detect(cfgfile, weightfile, imgfile): # 根据 配置文件 初始化网络 m = Darknet(cfgfile) # 打印网络框架信息(每层网络结构、卷积核数、输入特征图尺度及通道数、输出特征图尺度及通道数) m.print_network() # 加载 模型权重 m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) # 默认使用 coco类别 namesfile = 'data/coco.names' # 默认CPU use_cuda = 0 if use_cuda: m.cuda() # 读取 测试图片并转为 RGB通道 img = Image.open(imgfile).convert('RGB') # 测试图像 调整尺度,以便输入网络 sized = img.resize((m.width, m.height)) # 统计第二次运行结果 的时间更稳定,更具代表性 for i in range(2): start = time.time() #默认CPU boxes = do_detect(m, sized, 0.5, 0.4, use_cuda) finish = time.time() if i == 1: print('%s: Predicted in %f seconds.' % (imgfile, (finish - start))) # 加载类别名称,为 bbox打类别标签 class_names = load_class_names(namesfile) # 将bbox及类别 绘制到 测试图像并保存 plot_boxes(img, boxes, 'img/predictions.jpg', class_names)
def init_model(transform): parser = argparse.ArgumentParser() parser.add_argument("--confidence", dest="confidence", help="Object Confidence to filter predictions", default=0.25) parser.add_argument("--nms_thresh", dest="nms_thresh", help="NMS Threshhold", default=0.4) parser.add_argument("--reso", dest='reso', help= "Input resolution of the network. Increase to increase accuracy. Decrease to increase speed", default="160", type=str) args, unknown = parser.parse_known_args() cfgfile = "./cfg/yolov4.cfg" weightsfile = "./weights/yolov4.pth" confidence = float(args.confidence) nms_thesh = float(args.nms_thresh) CUDA = torch.cuda.is_available() num_classes = 80 # bbox_attrs = 5 + num_classes class_names = load_class_names("./data/coco.names") model = Darknet(cfgfile) model.load_weights(weightsfile) if CUDA: model.cuda() model.eval() return (model, class_names,CUDA), None
def detect_cv2(cfgfile, weightfile, imgfile, outfile): import cv2 m = Darknet(cfgfile) m.print_network() m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) if use_cuda: m.cuda() num_classes = m.num_classes if num_classes == 20: namesfile = 'data/voc.names' elif num_classes == 80: namesfile = 'data/coco.names' else: namesfile = 'D:/work_source/CV_Project/datasets/xi_an_20201125/all/names_xi_an_20201125.txt' class_names = load_class_names(namesfile) img = cv2.imread(imgfile) # print('demo pic size:', img.shape) sized = cv2.resize(img, (m.width, m.height)) # print('demo pic resize to:',sized.shape) sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB) boxes = [] for i in range(2): start = time.time() boxes = do_detect(m, sized, 0.4, 0.6, use_cuda) finish = time.time() if i == 1: print('%s: Predicted in %f seconds.' % (imgfile, (finish - start))) plot_boxes_cv2(img, boxes[0], savename=outfile, class_names=class_names)
def load_model(): m = Darknet(CFG) m.load_weights(WEIGHTS) if use_cuda: m.cuda() return m
def detect_skimage(cfgfile, weightfile, imgfile): from skimage import io from skimage.transform import resize m = Darknet(cfgfile) m.print_network() m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) if m.num_classes == 20: namesfile = 'data/voc.names' elif m.num_classes == 80: namesfile = 'data/coco.names' else: namesfile = 'data/names' use_cuda = 1 if use_cuda: m.cuda() img = io.imread(imgfile) sized = resize(img, (m.width, m.height)) * 255 for i in range(2): start = time.time() boxes = do_detect(m, sized, 0.5, 0.4, use_cuda) finish = time.time() if i == 1: print('%s: Predicted in %f seconds.' % (imgfile, (finish - start))) class_names = load_class_names(namesfile) plot_boxes_cv2(img, boxes, savename='predictions.jpg', class_names=class_names)
def detect(cfgfile, weightfile, imgfile): m = Darknet(cfgfile) m.load_state_dict(torch.load(weightfile)) # m.print_network() # m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) num_classes = 20 if num_classes == 20: namesfile = 'data/voc.names' elif num_classes == 80: namesfile = 'data/coco.names' else: namesfile = 'data/names' use_cuda = 1 if use_cuda: m.cuda() input_img = cv2.imread(imgfile) orig_img = Image.open(imgfile) start = time.time() boxes,scale = do_detect(m, input_img, 0.5, 0.4, use_cuda) finish = time.time() print('%s: Predicted in %f seconds.' % (imgfile, (finish - start))) class_names = load_class_names(namesfile) plot_boxes(orig_img, boxes, 'predictions.jpg', class_names,scale=scale)
def detect_cv2_camera(cfgfile, weightfile, videofile): import cv2 m = Darknet(cfgfile) m.print_network() m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) if use_cuda: m.cuda() # cap = cv2.VideoCapture(0) cap = cv2.VideoCapture(videofile) # cap.set(3, 1280) # cap.set(4, 720) print("Starting the YOLO loop...") num_classes = m.num_classes if num_classes == 20: namesfile = 'data/voc.names' elif num_classes == 80: namesfile = 'data/coco.names' else: namesfile = 'data/x.names' class_names = load_class_names(namesfile) conf_thresh = 0.2 detect_fps = 5 detect_interval_msec = 1000 / detect_fps next_detect_msec = 0 # Save original image assert os.path.isdir('/track_data/img') while True: ret = cap.grab() if not ret: break video_msec = cap.get(cv2.CAP_PROP_POS_MSEC) if video_msec > next_detect_msec: next_detect_msec += detect_interval_msec ret, img = cap.retrieve() sized = cv2.resize(img, (m.width, m.height)) sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB) start = time.time() boxes = do_detect(m, sized, conf_thresh, 0.6, use_cuda) finish = time.time() print('Predicted in %f seconds.' % (finish - start)) cv2.imwrite(f'/track_data/img/{video_msec:010.2f}.jpg', img) # Save detection result image plot_boxes_cv2(img, boxes[0], savename=f'/track_data/detect/{video_msec:010.2f}', class_names=class_names) cap.release()
def load_model(model_config_file, weight_file, frame_size): model = Darknet(model_config_file, inference=True) checkpoint = torch.load( weight_file, map_location=torch.device('cuda')) model.load_state_dict(checkpoint['state_dict']) model.eval() model.cuda() return model
def detect_cv2(cfgfile, weightfile, imgfile): import cv2 m = Darknet(cfgfile) m.print_network() m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) if use_cuda: m.cuda() num_classes = m.num_classes if num_classes == 20: namesfile = 'data/voc.names' elif num_classes == 80: namesfile = 'data/coco.names' else: namesfile = '/home/dreamer/Private/ObjectDetection/yolo-series/darknet-A-version/yolov4-rongwen20201203.names' class_names = load_class_names(namesfile) img = cv2.imread(imgfile) sized = cv2.resize(img, (m.width, m.height)) #=============================================== # rh = 608.0 # rw = 608.0 # h, w = img.shape[:2] # ratio = min(rh / h, rw / w) # # re_img = cv2.resize(img, (int(w * ratio), int(h * ratio))) # pad_board = np.zeros([int(rh), int(rw), 3], np.uint8) # if w > h: # pad_board[int(rh / 2 - h * ratio / 2): int(rh / 2 + h * ratio / 2), :] = re_img # else: # pad_board[:, int(rw / 2 - w * ratio / 2):int(rw / 2 + w * ratio / 2)] = re_img # # pad_board = pad_board.astype(np.float32) # # pad_board /= 255.0 # sized = cv2.cvtColor(pad_board, cv2.COLOR_BGR2RGB) # =============================================== # img_in = np.transpose(img_in, (2, 0, 1)).astype(np.float32) # img_in = np.expand_dims(img_in, axis=0) # img_in /= 255.0 # for i in range(2): start = time.time() boxes = do_detect(m, sized, 0.03, 0.45, use_cuda) finish = time.time() if i == 1: print('%s: Predicted in %f seconds.' % (imgfile, (finish - start))) plot_boxes_cv2(img, boxes[0], savename='predictions.jpg', class_names=class_names)
def detect_cv2(cfgfile, weightfile, imgfile): import cv2 m = Darknet(cfgfile) m.print_network() m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) if use_cuda: m.cuda() num_classes = m.num_classes if num_classes == 20: namesfile = 'data/voc.names' elif num_classes == 80: namesfile = 'data/coco.names' else: namesfile = 'data/x.names' class_names = load_class_names(namesfile) cap = cv2.VideoCapture('1.mp4') w = int(cap.get(3)) h = int(cap.get(4)) fourcc = cv2.VideoWriter_fourcc(*'MJPG') out = cv2.VideoWriter('output_1_3.avi', fourcc, 15, (w, h)) list_file = open('detection_rslt.txt', 'w') frame_index = 0 min_time = 10.0 max_time_1 = 0.0 max_time_2 = 0.0 avg_time = 0.0 while True: frame_index += 1 start = time.time() ret, img = cap.read() if not ret: break sized = cv2.resize(img, (m.width, m.height)) sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB) # for i in range(2): boxes = do_detect(m, sized, 0.4, 0.6, use_cuda) plot_boxes_cv2(img, boxes[0], class_names=class_names, out=out) finish = time.time() infer_time = finish - start min_time = min(min_time, infer_time) max_time_2 = max(max_time_2, infer_time) max_time_1 = max(max_time_1, infer_time) if max_time_1 != max_time_2 else infer_time avg_time += infer_time print('{}: Predicted in {} seconds.'.format(imgfile, infer_time)) cap.release() print('min : {}\n' 'max : {}\n' 'avg : {}'.format(min_time, max_time_1, avg_time / frame_index))
def init_darknet(cfgfile, weightfile): global m , use_cuda m = Darknet(cfgfile) m.print_network() m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) if use_cuda: m.cuda()
def detect_cv2_camera(cfgfile, weightfile): import cv2 m = Darknet(cfgfile) # mot_tracker = Sort() m.print_network() m.load_weights(weightfile) if args.torch: m.load_state_dict(torch.load(weightfile)) else: m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) if use_cuda: m.cuda() # cap = cv2.VideoCapture(0) cap = cv2.VideoCapture('rtsp://192.168.1.75:8554/mjpeg/1') # cap = cv2.VideoCapture("./test.mp4") cap.set(3, 1280) cap.set(4, 720) print("Starting the YOLO loop...") num_classes = m.num_classes if num_classes == 20: namesfile = 'data/voc.names' elif num_classes == 80: namesfile = 'data/coco.names' else: namesfile = 'data/x.names' class_names = load_class_names(namesfile) while True: ret, img = cap.read() sized = cv2.resize(img, (m.width, m.height)) sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB) # piling = Image.fromarray(sized) start = time.time() boxes = do_detect(m, sized, 0.4, 0.6, use_cuda) if boxes is not None: # tracked_object = mot_tracker.update(tensorQ) finish = time.time() print('Predicted in %f seconds.' % (finish - start)) result_img = plot_boxes_cv2(img, boxes[0], savename=None, class_names=class_names) cv2.imshow('Yolo demo', result_img) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
def call_yolov4(cfgfile='../yolov4.cfg', weightfile='../yolov4.weights', use_cuda=True): m = Darknet(cfgfile) m.load_weights(weightfile) if use_cuda: m.cuda().eval() else: m.eval() return m
def detect_cv2(cfgfile, weightfile, imgfile): import cv2 m = Darknet(cfgfile) m.print_network() m.load_weights(weightfile) if args.torch: m.load_state_dict(torch.load(weightfile)) else: m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) if use_cuda: m.cuda() num_classes = m.num_classes if num_classes == 20: namesfile = 'data/voc.names' elif num_classes == 80: namesfile = 'data/coco.names' else: namesfile = 'data/x.names' class_names = load_class_names(namesfile) while True: val = input("\n numero da imagem: ") pred_init_time = time.time() named_file = "../fotos_geladeira_4/opencv_frame_" + val + ".png" print(named_file) img = cv2.imread(named_file) # img = cv2.imread(imgfile) sized = cv2.resize(img, (m.width, m.height)) sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB) for i in range(2): start = time.time() boxes = do_detect(m, sized, 0.4, 0.6, use_cuda) finish = time.time() if i == 1: print('%s: Predicted in %f seconds.' % (imgfile, (finish - start))) plot_boxes_cv2(img, boxes[0], savename='predictions.jpg', class_names=class_names) count_total_in_image(boxes[0], class_names) print("\n Total inference time {0} seconds".format(time.time() - pred_init_time))
def detect_cv2_camera(cfgfile, weightfile): import cv2 m = Darknet(cfgfile) m.print_network() if args.torch: m.load_state_dict(torch.load(weightfile)) else: m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) if use_cuda: m.cuda() cap = cv2.VideoCapture(0) # cap = cv2.VideoCapture("./test.mp4") cap.set(3, 1280) cap.set(4, 720) print("Starting the YOLO loop...") num_classes = m.num_classes if num_classes == 20: namesfile = 'data/voc.names' elif num_classes == 80: namesfile = 'data/coco.names' else: namesfile = 'data/x.names' class_names = load_class_names(namesfile) while True: ret, img = cap.read() sized = cv2.resize(img, (m.width, m.height)) sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB) start = time.time() boxes = do_detect(m, sized, 0.4, 0.6, use_cuda) finish = time.time() print('Predicted in %f seconds.' % (finish - start)) result_img = plot_boxes_cv2(img, boxes[0], savename=None, class_names=class_names) cv2.imshow('Yolo demo', result_img) cv2.waitKey(1) cap.release()
def load_network(cfgfile, weightfile): m = Darknet(cfgfile) m.print_network() m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) if use_cuda: m.cuda() num_classes = m.num_classes if num_classes == 20: namesfile = 'data/voc.names' elif num_classes == 80: namesfile = 'data/coco.names' else: namesfile = 'data/x.names' class_names = load_class_names(namesfile) return m, class_names
def detect(json_dir, video_dir, save_dir): starttime = timeit.default_timer() Path(save_dir).mkdir(parents=True, exist_ok=True) cfgfile = config['detector']['cfgfile'] weightfile = config['detector']['weightfile'] model = Darknet(cfgfile) model.load_weights(weightfile) model.cuda() class_names = config['detector']['originclassnames'] cam_datas = get_list_data(json_dir) for cam_data in cam_datas: cam_name = cam_data['camName'] roi_poly = Polygon(cam_data['shapes'][0]['points']) video_path = os.path.join(video_dir, cam_name + '.mp4') video_cap = cv2.VideoCapture(video_path) num_frames = int(video_cap.get(cv2.CAP_PROP_FRAME_COUNT)) imgs = [] for i in tqdm(range(num_frames), desc='Extracting {}'.format(cam_name)): success, img = video_cap.read() img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) imgs.append(img) boxes = detect_yolo(model, class_names, imgs, cam_name, config['detector']['batchsize']) # remove bboxes out of MOI if config['remove_not_intersec_moi']: boxes = [ check_intersect_box(box_list, roi_poly) for box_list in boxes ] if save_dir: filepath = os.path.join(save_dir, cam_name) boxes = np.array(boxes) np.save(filepath, boxes) endtime = timeit.default_timer() print('Detect time: {} seconds'.format(endtime - starttime))
def detect_cv2_img(cfgfile, weightfile, img_file): m = Darknet(cfgfile) m.print_network() m.load_weights(weightfile) # print('Loading weights from %s... Done!' % (weightfile)) if use_cuda: m.cuda() img = cv2.imread(img_file) sized = cv2.resize(img, (m.width, m.height)) sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB) boxes = do_detect(m, sized, 0.4, 0.6, use_cuda) boxes_darknet_format = to_darknet_format(boxes, img.shape[0], img.shape[1]) print(boxes[0]) print(boxes_darknet_format) return boxes[0]
def load_model(opts, frame_size): cfg_file_path = opts.model_config_dir + \ "/yolov4_" + str(frame_size) + ".cfg" model = Darknet(cfg_file_path, inference=True) weight_file = os.path.join( opts.weights_dir, "yolov4_{}.pth".format(frame_size)) checkpoint = torch.load( weight_file, map_location='cuda:{}'.format(opts.gpu_id)) model.load_state_dict(checkpoint['state_dict']) model.eval() if not opts.no_cuda: model.cuda(opts.gpu_id) # Zero grad for parameters for param in model.parameters(): param.grad = None return model
def detect(cfgfile, weightfile, imgfile): m = Darknet(cfgfile) checkpoint = torch.load(weightfile) model_dict = m.state_dict() pretrained_dict = checkpoint keys = [] for k, v in pretrained_dict.items(): keys.append(k) i = 0 for k, v in model_dict.items(): if v.size() == pretrained_dict[keys[i]].size(): model_dict[k] = pretrained_dict[keys[i]] i = i + 1 m.load_state_dict(model_dict) # m.load_state_dict(torch.load(weightfile)) # m.print_network() # m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) namesfile = 'data/mydata.names' use_cuda = 1 if use_cuda: m.cuda() input_img = cv2.imread(imgfile) # orig_img = Image.open(imgfile).convert('RGB') start = time.time() boxes, scale = do_detect(m, input_img, 0.5, 0.4, use_cuda) finish = time.time() print('%s: Predicted in %f seconds.' % (imgfile, (finish - start))) class_names = load_class_names(namesfile) # draw_boxes(input_img,boxes,scale=scale) plot_boxes_cv2(input_img, boxes, 'predictions1.jpg', class_names=class_names, scale=scale)
def detect_cv2(cfgfile, weightfile, img): m = Darknet(cfgfile) m.print_network() m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) if use_cuda: m.cuda() num_classes = m.num_classes if num_classes == 20: namesfile = 'data/voc.names' elif num_classes == 80: namesfile = 'data/coco.names' else: namesfile = 'data/x.names' class_names = load_class_names(namesfile) sized = cv2.resize(img, (m.width, m.height)) sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB) start = time.time() boxes = do_detect(m, sized, 0.4, 0.6, use_cuda) objects = [] for i, box in enumerate(boxes[0]): dic = {} dic['kind'] = class_names[box[6]] dic['confidence'] = box[4] dic.update(get_bbox_coordinates(img, box)) cropped_img = crop_box(img, box) dic['feature'] = extract_feature(cropped_img) objects.append(dic) finish = time.time() print('Predicted in %f seconds.' % (finish - start)) plot_boxes_cv2(img, boxes[0], savename='predictions.jpg', class_names=class_names) return({'objects': objects})
def detect_cv2(cfgfile, weightfile, imgfile): import cv2 m = Darknet(cfgfile) # 创建 Darknet 模型对象 m m.print_network() # 打印网络结构信息 m.load_weights(weightfile) # 加载网络权重值 在 tools/darknet2pytorch.py 函数中 print('Loading weights from %s... Done!' % (weightfile)) if use_cuda: m.cuda() # 如果使用 cuda,则将模型对象拷贝至显存 num_classes = m.num_classes if num_classes == 20: namesfile = 'data/voc.names' elif num_classes == 80: namesfile = 'data/coco.names' else: namesfile = 'data/x.names' class_names = load_class_names(namesfile) # 加载类别名 # 如果用 PIL 打开图像 # img = Image.open(imgfile).convert('RGB') # sized = img.resize((m.width, m.height)) img = cv2.imread(imgfile) cv2.imwrite('./debug/img.jpg', img) # print(m.width, m.height) # (608, 608) sized = cv2.resize(img, (m.width, m.height)) sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB) for i in range(2): start = time.time() boxes = do_detect(m, sized, 0.4, 0.6, use_cuda) # 做检测,返回的 boxes 是最晚 NMS 后的检测框 finish = time.time() if i == 1: print('%s: Predicted in %f seconds.' % (imgfile, (finish - start))) plot_boxes_cv2(img, boxes[0], savename='./debug/predictions.jpg', class_names=class_names) # raw
def detect(cfgfile, weightfile, imgfile): m = Darknet(cfgfile) m.print_network() m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) if use_cuda: m.cuda() m.eval() img = Image.open(imgfile).convert('RGB') sized = img.resize((m.width, m.height)) start = time.time() num = 1 for i in range(num): boxes = do_detect(m, sized, 0.5, num_classes, 0.4, use_cuda) finish = time.time() print('%s: Predicted in %f seconds.' % (imgfile, (finish - start) / num)) class_names = load_class_names(namesfile) plot_boxes(img, boxes, 'predictions.jpg', class_names)
def detect_BEV_flat(cfgfile, weightfile, imgfile): """Detect elements in BEV map with yolov4_BEV_flat Args: cfgfile (str): Path to .cfg file weightfile (str): Path to .weights file imgfile (str): Path to image on which we want to run BEV detection """ # load model m = Darknet(cfgfile, model_type="BEV_flat") m.print_network() m.load_weights(weightfile, cut_off=54) print("Loading backbone from %s... Done!" % (weightfile)) # push to GPU if use_cuda: m.cuda() # load names namesfile = "names/BEV.names" class_names = load_class_names(namesfile) # read sample image img = cv2.imread(imgfile) sized = cv2.resize(img, (m.width, m.height)) sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB) # create batch sized = np.expand_dims(sized, 0) sized = np.concatenate((sized, sized), 0) # run inference start = time.time() boxes = do_detect(m, sized, 0.4, 0.6, use_cuda) finish = time.time() print("%s: Predicted in %f seconds." % (imgfile, (finish - start)))
cfgfile = "cfg/yolov4.cfg" weightsfile = "weight/yolov4.weights" args = arg_parse() confidence = float(args.confidence) nms_thesh = float(args.nms_thresh) CUDA = torch.cuda.is_available() num_classes = 80 bbox_attrs = 5 + num_classes class_names = load_class_names("data/coco.names") model = Darknet(cfgfile) model.load_weights(weightsfile) if CUDA: model.cuda() model.eval() cap = cv2.VideoCapture(0) assert cap.isOpened(), 'Cannot capture source' frames = 0 start = time.time() while cap.isOpened(): ret, frame = cap.read() if ret: sized = cv2.resize(frame, (model.width, model.height)) sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB) boxes = do_detect(model, sized, 0.5, 0.4, CUDA)