def transform_to_onnx(cfgfile, weightfile, batch_size=1): model = Darknet(cfgfile) model.print_network() model.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) # model.cuda() x = torch.randn((batch_size, 3, model.height, model.width), requires_grad=True) # .cuda() onnx_file_name = "yolov4_{}_3_{}_{}.onnx".format(batch_size, model.height, model.width) # Export the model print('Export the onnx model ...') torch.onnx.export(model, x, onnx_file_name, export_params=True, opset_version=11, do_constant_folding=True, input_names=['input'], output_names=['output'], dynamic_axes=None) print('Onnx model exporting done') return onnx_file_name
def detect_cv2(self, 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 namesfile = 'cfg/ball.names' class_names = load_class_names(namesfile) (img_h, img_w) = imgfile[0].shape[:2] result = dict() for i, img in enumerate(imgfile): #img = cv2.imread(imgfile[i]) 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[0].sort(key=lambda s: s[4], reverse=True) if boxes[0]: for box in boxes[0]: x1 = int(box[0] * img_w) y1 = int(box[1] * img_h) w = int(box[2] * img_w) - x1 h = int(box[3] * img_h) - y1 bbox = (x1, y1, w, h) result['frame'] = i result['bbox'] = bbox self.bbox_set.append(result.copy())
def detect_img_folder(cfgfile, weightfile, imgfolder, specialnms, gpu): import cv2 m = Darknet(cfgfile) m.print_network() m.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) use_cuda = gpu 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/obj.names' class_names = load_class_names(namesfile) img_list = os.listdir(imgfolder) for imgfile in img_list: if imgfile[-3:] == 'jpg' or imgfile[-3:] == 'png': img = cv2.imread(imgfolder + imgfile) sized = cv2.resize(img, (m.width, m.height)) sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB) start = time.time() boxes = do_detect(m, sized, 0.25, 0.45, use_cuda, specialnms) finish = time.time() print('%s: Predicted in %f seconds.' % (imgfile, (finish - start))) # print("bboxes: ", boxes[0]) """write predicted bboxes into txt file with absolute coordinate form: [cls, conf, x, y, w, h]""" cp_boxes = copy.deepcopy( boxes[0]) # [[x1, y1, x2, y2, conf, cls], [], ...] img = np.copy(img) width = img.shape[1] height = img.shape[0] # normalised [x1, y1, x2, y2] --> original [x, y, w, h] for i in range(len(cp_boxes)): cp_boxes[i][0] = (boxes[0][i][0] + boxes[0][i][2]) / 2 * width cp_boxes[i][1] = (boxes[0][i][1] + boxes[0][i][3]) / 2 * height cp_boxes[i][2] = (boxes[0][i][2] - boxes[0][i][0]) * width cp_boxes[i][3] = (boxes[0][i][3] - boxes[0][i][1]) * height with open((imgfolder + imgfile[:-4] + '.txt'), 'a+') as a: if len(cp_boxes) == 0: a.write("0 0 0 0 0 0") else: for j in range(len(cp_boxes)): a.write(str(cp_boxes[j][5]) + ' ' + str(cp_boxes[j][4]) + ' ' \ + str(cp_boxes[j][0]) + ' ' + str(cp_boxes[j][1]) + ' ' \ + str(cp_boxes[j][2]) + ' ' + str(cp_boxes[j][3]) + '\n') plot_boxes_cv2(img, boxes[0], 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)) # 默认使用 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 conf_thresh:0.5 nms_thresh:0.4 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 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, 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 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_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 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 = os.path.dirname(__file__) + '/data/voc.names' elif num_classes == 80: namesfile = os.path.dirname(__file__) + '/data/coco.names' else: namesfile = os.path.dirname(__file__) + '/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)
def transform_to_onnx(cfgfile, weightfile, batch_size=1, dynamic=False): model = Darknet(cfgfile) model.print_network() model.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) # model.cuda() x = torch.randn((batch_size, 3, model.height, model.width), requires_grad=True) # .cuda() if dynamic: onnx_file_name = "yolov4_{}_3_{}_{}_dyna.onnx".format( batch_size, model.height, model.width) input_names = ["input"] output_names = ['boxes', 'confs'] dynamic_axes = { "input": { 0: "batch_size" }, "boxes": { 0: "batch_size" }, "confs": { 0: "batch_size" } } # Export the model print('Export the onnx model ...') torch.onnx.export(model, x, onnx_file_name, export_params=True, opset_version=11, do_constant_folding=True, input_names=input_names, output_names=output_names, dynamic_axes=dynamic_axes) print('Onnx model exporting done') return onnx_file_name else: onnx_file_name = "yolov4_{}_3_{}_{}_static.onnx".format( batch_size, model.height, model.width) torch.onnx.export(model, x, onnx_file_name, export_params=True, opset_version=11, do_constant_folding=True, input_names=['input'], output_names=['boxes', 'confs'], dynamic_axes=None) print('Onnx model exporting done') return onnx_file_name
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)) 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) 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, False) finish = time.time() if i == 1: print('%s: Predicted in %f seconds.' % (imgfile, (finish - start))) boxes_bird = [] for box in boxes[0]: if box[6] == 14: boxes_bird.append(box) print(len(boxes_bird)) plot_boxes_cv2(img, boxes_bird, savename='predictions', class_names=class_names)
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 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) 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.4, 0.4, use_cuda) finish = time.time() if i == 1: print('%s: Predicted in %f seconds.' % (imgfile, (finish - start))) plot_boxes_cv2(img, boxes, savename='predictions.jpg', class_names=class_names)
def transform_to_onnx(cfgfile, weightfile, onnx_file_name): model = Darknet(cfgfile) model.print_network() model.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) batch_size = 1 input_names = ["input"] output_names = ['boxes', 'confs'] if 'grey' in onnx_file_name: channels = 1 else: channels = 3 print('channels={}'.format(channels)) x = torch.randn((batch_size, channels, model.height, model.width), requires_grad=True) torch.onnx.export(model, x, onnx_file_name, export_params=True, opset_version=11, do_constant_folding=True, input_names=input_names, output_names=output_names, dynamic_axes=None) print('Onnx model exporting done')
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) 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.0003, 0.005, 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 = '/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 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_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 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() 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, 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(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)))
def transform_to_onnx(cfgfile, weightfile, batch_size=1): model = Darknet(cfgfile) model.print_network() model.load_weights(weightfile) print('Loading weights from %s... Done!' % (weightfile)) dynamic = False if batch_size <= 0: dynamic = True input_names = ["input"] output_names = ['boxes', 'confs'] cfg_filename = os.path.split(cfgfile)[-1] prefix = cfg_filename.split(".")[0] if dynamic: x = torch.randn((1, 3, model.height, model.width), requires_grad=True) onnx_file_name = prefix + "_-1_3_{}_{}_dynamic.onnx".format( model.height, model.width) dynamic_axes = { "input": { 0: "batch_size" }, "boxes": { 0: "batch_size" }, "confs": { 0: "batch_size" } } # Export the model print('Export the onnx model ...') torch.onnx.export(model, x, onnx_file_name, export_params=True, opset_version=12, do_constant_folding=True, input_names=input_names, output_names=output_names, dynamic_axes=dynamic_axes) print('Onnx model exporting done') return onnx_file_name else: x = torch.randn((batch_size, 3, model.height, model.width), requires_grad=True) onnx_file_name = prefix + "_{}_3_{}_{}_static.onnx".format( batch_size, model.height, model.width) torch.onnx.export(model, x, onnx_file_name, export_params=True, opset_version=12, do_constant_folding=True, input_names=input_names, output_names=output_names, dynamic_axes=None) print('Onnx model exporting done') return onnx_file_name
dest='cfgfile') parser.add_argument('-weightfile', type=str, default='./checkpoints/Yolov4_epoch1.pth', help='path of trained model.', dest='weightfile') parser.add_argument('-imgfolder', type=str, help='path of your image folder.', dest='imgfolder') args = parser.parse_args() return args if __name__ == '__main__': args = get_args() if args.imgfolder: m = Darknet(args.cfgfile) m.print_network() m.load_weights(args.weightfile) print('Loading weights from %s... Done!' % (args.weightfile)) if use_cuda: m.cuda() imgfiles = load_images_path(args.imgfolder) for imgfile in imgfiles: detect_cv2(m, imgfile)
class Yolov4Node(object): def __init__(self, cfgfile, weightfile): rospy.on_shutdown(self.shutdown_cb) self.model = Darknet(cfgfile) self.model.print_network() self.model.load_weights(weightfile) self.model.eval() print('Loading weights from %s... Done!' % (weightfile)) self.num_classes = 80 if self.num_classes == 20: namesfile = os.path.dirname(__file__) + '/data/voc.names' elif self.num_classes == 80: namesfile = os.path.dirname(__file__) + '/data/coco.names' else: namesfile = os.path.dirname(__file__) + '/data/names' self.class_names = load_class_names(namesfile) self.use_cuda = 1 if self.use_cuda: self.model.cuda() self.cvbridge = CvBridge() self.pub_bbox = rospy.Publisher('~det2d_result', Detection2D, queue_size=1) self.sub_image = rospy.Subscriber("/camera/color/image_raw", ROSImage, self.image_cb, queue_size=1) self.detection_srv = rospy.Service("~yolo_detect", Detection2DTrigger, self.srv_cb) print(rospy.get_name() + ' is ready.') def srv_cb(self, req): try: cv_image = self.cvbridge.imgmsg_to_cv2(req.image, "rgb8") # print("Get image...") except CvBridgeError as e: print(e) return img_sized = cv2.resize(cv_image, (self.model.width, self.model.height)) boxes_batch = do_detect(self.model, img_sized, 0.5, 0.2, self.use_cuda) detection_msg = Detection2D() detection_msg.header.stamp = rospy.Time.now() detection_msg.header.frame_id = req.image.header.frame_id # Batch size != 1 if len(boxes_batch) != 1: print("Batch size != 1, cannot handle it") exit(-1) boxes = boxes_batch[0] # print('num_detections:', len(boxes)) for index, box in enumerate(boxes): # print('box:', box) bbox_msg = BBox2D() bbox_msg.center.x = math.floor(box[0] * req.image.width) bbox_msg.center.y = math.floor(box[1] * req.image.height) bbox_msg.size_x = math.floor(box[2] * req.image.width) bbox_msg.size_y = math.floor(box[3] * req.image.height) bbox_msg.id = box[6] bbox_msg.score = box[5] bbox_msg.class_name = self.class_names[bbox_msg.id] detection_msg.boxes.append(bbox_msg) # cv_image = cv2.cvtColor(cv_image, cv2.COLOR_RGB2BGR) result_img = plot_boxes_cv2(cv_image, boxes, savename=None, class_names=self.class_names, interest_classes=INTEREST_CLASSES) detection_msg.result_image = self.cvbridge.cv2_to_imgmsg( result_img, "bgr8") print('return {} detection results'.format(len(boxes))) return Detection2DTriggerResponse(result=detection_msg) def image_cb(self, msg): try: cv_image = self.cvbridge.imgmsg_to_cv2(msg, "rgb8") rospy.loginfo("Get image") except CvBridgeError as e: print(e) return img_sized = cv2.resize(cv_image, (self.model.width, self.model.height)) boxes_batch = do_detect(self.model, img_sized, 0.4, 0.3, self.use_cuda) detection_msg = Detection2D() detection_msg.header.stamp = rospy.Time.now() detection_msg.header.frame_id = msg.header.frame_id detection_msg.result_image = msg # Batch size != 1 if len(boxes_batch) != 1: print("Batch size != 1, cannot handle it") exit(-1) boxes = boxes_batch[0] # print('num_detections:', len(boxes)) for index, box in enumerate(boxes): # print('box:', box) bbox_msg = BBox2D() bbox_msg.center.x = math.floor(box[0] * msg.width) bbox_msg.center.y = math.floor(box[1] * msg.height) bbox_msg.size_x = math.floor(box[2] * msg.width) bbox_msg.size_y = math.floor(box[3] * msg.height) bbox_msg.id = box[6] bbox_msg.score = box[5] bbox_msg.class_name = self.class_names[bbox_msg.id] detection_msg.boxes.append(bbox_msg) result_img = plot_boxes_cv2(cv_image, boxes, savename=None, class_names=self.class_names, interest_classes=INTEREST_CLASSES) detection_msg.result_image = self.cvbridge.cv2_to_imgmsg( result_img, "bgr8") self.pub_bbox.publish(detection_msg) result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR) cv2.imshow('Yolo demo', result_img) cv2.waitKey(1) def shutdown_cb(self): rospy.loginfo("Shutdown " + rospy.get_name()) if hasattr(self, 'model'): del self.model if hasattr(self, 'cv_bridge'): del self.cv_bridge