def _main_(args): config_path = args.conf weights_path = args.weights image_path = args.input with open(config_path) as config_buffer: config = json.load(config_buffer) yolo = YOLO(backend=config['model']['architecture'], input_size=config['model']['input_size'], labels=config['model']['labels'], max_box_per_image=config['model']['max_box_per_image'], anchors=config['model']['anchors']) yolo.load_weights(weights_path) if image_path[-4:] == '.mp4': video_out = image_path[:-4] + '_detected' + image_path[-4:] video_reader = cv2.VideoCapture(image_path) nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT)) frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH)) video_writer = cv2.VideoWriter(video_out, cv2.VideoWriter_fourcc(*'MPEG'), 50.0, (frame_w, frame_h)) for i in tqdm(range(nb_frames)): _, image = video_reader.read() boxes = yolo.predict(image) image = draw_boxes(image, boxes, config['model']['labels']) video_writer.write(np.uint8(image)) video_reader.release() video_writer.release() else: image = cv2.imread(image_path) boxes = yolo.predict(image) image = draw_boxes(image, boxes, config['model']['labels']) print(len(boxes), 'boxes are found') cv2.imwrite(image_path[:-4] + '_result' + image_path[-4:], image)
def load_model(modules): model = YOLO(modules) model.to(DEVICE) model.load_weights('./weights/yolov3.weights') return model