def test_detect(setup_tf_eager, setup_darknet_weights): darknet_weights = setup_darknet_weights image_path = os.path.join(PROJECT_ROOT, "tests", "samples", "sample.jpeg") # 1. create yolo model & load weights yolov3 = Yolonet() yolov3.load_darknet_params(darknet_weights) # 2. preprocess the image image = cv2.imread(image_path) image = image[:,:,::-1] d = YoloDetector(yolov3, COCO_ANCHORS, net_size=416) boxes, labels, probs = d.detect(image) assert len(boxes) == 31
def create_detector(self, model): d = YoloDetector(model, self._model_config["anchors"], net_size=self._model_config["net_size"]) return d
def create_detector(self, model): detector = YoloDetector(model, self._model_config['anchors'], net_size=self._model_config['net_size']) return detector
def detect(self, image, threshold=.5): detector = YoloDetector(self.yolonet(), self.anchors, net_size=self.net_size) return detector.detect(image, threshold) # boxes, labels, probs
print(train_generator.steps_per_epoch) # 2. create model model = Yolonet(n_classes=len(config["model"]["labels"])) model.load_darknet_params(config["pretrained"]["darknet_format"], skip_detect_layer=True) # 4. traini train_fn(model, train_generator, valid_generator, learning_rate=config["train"]["learning_rate"], save_dname=config["train"]["save_folder"], num_epoches=config["train"]["num_epoch"]) # 5. prepare sample images img_fnames = glob.glob(os.path.join(config["train"]["train_image_folder"], "*.*")) imgs = [cv2.imread(fname)[:,:,::-1] for fname in img_fnames] # 6. create new model & load trained weights model = Yolonet(n_classes=len(config["model"]["labels"])) model.load_weights(os.path.join(config["train"]["save_folder"], "weights.h5")) detector = YoloDetector(model) # 7. predict & plot boxes = detector.detect(imgs[0], config["model"]["anchors"]) image = draw_boxes(imgs[0], boxes, labels=config["model"]["labels"]) plt.imshow(image) plt.show()
if __name__ == '__main__': args = argparser.parse_args() image_path = args.image import json with open(args.config) as data_file: config = json.load(data_file) # Download if not exits weight file download_if_not_exists(config["pretrained"]["darknet_format"], "https://pjreddie.com/media/files/yolov3.weights") # 1. create yolo model & load weights yolov3 = Yolonet() yolov3.load_darknet_params(config["pretrained"]["darknet_format"]) # 2. preprocess the image image = cv2.imread(image_path) image = image[:, :, ::-1] d = YoloDetector(yolov3) boxes = d.detect(image, COCO_ANCHORS, net_size=416) # 4. draw detected boxes image = draw_boxes(image, boxes, config["model"]["labels"], obj_thresh=0.5) # 5. plot plt.imshow(image) plt.show()