Esempio n. 1
0
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)

    #model 

    yolo = YOLO(backend             = config['model']['backend'],
                input_size          = config['model']['input_size'], 
                labels              = config['model']['labels'], 
                max_box_per_image   = config['model']['max_box_per_image'],
                anchors             = config['model']['anchors'])

    #Load weights   
	
    yolo.load_weights(weights_path)

    # Predict
    image = cv2.imread(image_path)
    boxes = yolo.predict(image)
    image = draw_boxes(image, boxes, config['model']['labels'])
	print(len(boxes), 'boxes :')
	cv2.imwrite(image_path[:-4] + '_detected' + image_path[-4:], image)
Esempio n. 2
0
    def __init__(self):
        #TODO load classifier
        self.config_path = 'config.json'  # Loading config file for tiny-yolo processing

        # Check if weight file is exist or not, if not
        # create merge weight file from archive files inside
        # folder "yolo_weight_archive"
        if os.path.isfile('tiny_yolo_finalweight.h5'):
            self.weights_path = 'tiny_yolo_finalweight.h5'
        else:
            os.system('cat yolo_weight_archive/tiny_yolo_archivea* > tiny_yolo_finalweight.h5')
            self.weights_path = 'tiny_yolo_finalweight.h5'

        print(os.getcwd())
        with open(self.config_path) as config_buffer:
            config = json.load(config_buffer)

        # ==============================================
        #   Initialize Yolo model                      =
        # ==============================================
        self.yolo = YOLO(backend=config['model']['backend'],
                    input_size = config['model']['input_size'],
                    labels=config['model']['labels'],
                    max_box_per_image=config['model']['max_box_per_image'],
                    anchors=config['model']['anchors'])
        # Load the weight
        self.yolo.load_weights(self.weights_path)

        self.graph = tf.get_default_graph()

        print('Final yolo weight is loaded')
def _main_(args):
    config_path = args.conf
    weights_path = args.weights
    output_path = args.output

    with open(config_path) as config_buffer:
        config = json.load(config_buffer)

    ###############################
    #   load the model
    ###############################

    # keras.backend.set_session(K.tf.Session(config=K.tf.ConfigProto(intra_op_parallelism_threads=8, inter_op_parallelism_threads=8)))

    yolo = YOLO(backend=config['model']['backend'],
                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)

    yolo_inf = yolo.get_inference_model()
    yolo_inf.load_weights(weights_path)

    frozen_graph = freeze_session(
        K.get_session(),
        output_names=[out.op.name for out in yolo_inf.outputs])

    tf.train.write_graph(frozen_graph,
                         output_path,
                         "convertedModel.pb",
                         as_text=False)
Esempio n. 4
0
def _main_(args):
    config_path = args.conf
    weights_path = args.weights
    image_path = args.input
    show_stats = args.stats

    with open(config_path) as config_buffer:
        config = json.load(config_buffer)

    ###############################
    #   Make the model
    ###############################

    yolo = YOLO(backend=config['model']['backend'],
                input_size=config['model']['input_size'],
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'])

    ###############################
    #   Load trained weights
    ###############################

    yolo.load_weights(weights_path)

    ###############################
    #   Predict bounding boxes
    ###############################

    if os.path.isdir(image_path):
        for img in os.listdir(image_path):
            predict_file(os.path.join(image_path, img), yolo, config, show_stats)
    else:
        predict_file(image_path, yolo, config, show_stats)
def _main_(args):
    config_path = args.conf
    weights_path = args.weights
    image_path = args.input

    keras.backend.tensorflow_backend.set_session(get_session())

    with open(config_path) as config_buffer:
        config = json.load(config_buffer)

    if weights_path == '':
        weights_path = config['train']['saved_weights_name']

    ###############################
    #   Make the model
    ###############################

    yolo = YOLO(backend=config['model']['backend'],
                input_size=(config['model']['input_size_h'],
                            config['model']['input_size_w']),
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'],
                gray_mode=config['model']['gray_mode'])

    ###############################
    #   Load trained weights
    ###############################

    yolo.load_weights(weights_path)

    inference_model = yolo.get_inference_model()
    inference_model.save("inference.h5")
Esempio n. 6
0
    def __init__(self, weights_path, config_path):
        import json
        import os
        from frontend import YOLO
        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        os.environ["CUDA_VISIBLE_DEVICES"] = "0"

        with open(config_path) as config_buffer:
            config = json.load(config_buffer)

        ###############################
        #   Make the model
        ###############################

        self.yolo = YOLO(
            backend=config['model']['backend'],
            input_size=config['model']['input_size'],
            labels=config['model']['labels'],
            max_box_per_image=config['model']['max_box_per_image'],
            anchors=config['model']['anchors'])

        ###############################
        #   Load trained weights
        ###############################

        self.yolo.load_weights(weights_path)
        self.labels = config['model']['labels']
def _main_(args):

    config_path = args.conf

    with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())

    test_imgs, test_labels = parse_annotation(
        config['test']['test_annot_folder'],
        config['test']['test_image_folder'], config['model']['labels'])

    ###############################
    #   Construct the model
    ###############################
    yolo = YOLO(feature_extractor=config['model']['backend'],
                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(config['train']['saved_weights_name'])
    print('weights loaded from {}'.format(
        config['train']['saved_weights_name']))

    yolo.evaluate(test_imgs[:500],
                  iou_threshold=0.3,
                  obj_threshold=0.2,
                  nms_threshold=0.2)
Esempio n. 8
0
def _detection_thread():
    global detected_image, loaded
    print('Creating model...', end = ' ')
    yolo = YOLO(architecture='Tiny Yolo',
                    input_size=416,
                    labels=['cube'],
                    max_box_per_image=3,
                    anchors=[0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828])
    yolo.load_weights('save_tiny.h5')
    print('Done!')
    loaded = True
    while True:
        webcam_lock.acquire()
        img = webcam_image
        webcam_lock.release()
        if img is not None:
            start = time.time()
            boxes = yolo.predict(img, nms_threshold=0.5, bgr=False)
            drawn_img = draw_boxes(img, boxes, ['cube'])
            end = time.time()
            fps = 1.0/(end-start)
            past_fps.append(fps)
            while len(past_fps) > 10:
                del past_fps[0]
            avg_fps = sum(past_fps)/len(past_fps)
            print('\rFPS: {:.2f}'.format(avg_fps), end='')
            detected_lock.acquire()
            detected_image = drawn_img
            detected_lock.release()
        if should_stop:
            break
Esempio n. 9
0
def test(argparser,test_path):
  args=argparser.parse_args()
  config_path=args.conf
  weights_path=args.weights
  with open(config_path) as config_buffer:
    config=json.loads(config_buffer.read())
  os.environ["CUDA_VISIBLE_DEVICES"]=config["env"]["gpu"]
  
  yolo=YOLO(architecture=config["model"]["architecture"],
            input_size=config["model"]["input_size"],
            labels=config["model"]["labels"],
            max_box_per_img=config["model"]["max_box_per_image"],
            anchors=config["model"]["anchors"])
  yolo.load_weights(weights_path)
  if test_path[-3:]=="mp4":
    pass
  else:
    video_writer=cv2.VideoWriter(video_out,cv2.VideoWriter_fourcc(*'MPEG'),50.0,(w,h))
    start=time.time()
    for f in os.listdir(test_path):
      print(f)
      f_path=os.path.join(test_path,f)
#      print(f_path)
      img=cv2.imread(f_path)
      boxes=yolo.predict(img)
      img=draw_boxes(boxes,img,config["model"]["labels"])
#      cv2.imwrite(out_dir+f_path[-9:],img)
      video_writer.write(np.uint8(img))
    print(time.time()-start)
    video_writer.release()      
Esempio n. 10
0
def _main_(args):
    config_path = args.conf
    image_path = args.input

    with open(config_path) as config_buffer:
        config = json.load(config_buffer)

    ###############################
    #   Make the model
    ###############################

    yolo = YOLO(backend=config['model']['backend'],
                input_width=config['model']['input_width'],
                input_height=config['model']['input_height'],
                input_channel=config['model']['input_channel'],
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'],
                saved_config_name=config['train']['saved_config_name'])

    ###############################
    #   Load trained weights
    ###############################
    print(config['train']['saved_weights_name'])
    yolo.load_weights(config['train']['saved_weights_name'])

    ###############################
    #   Predict bounding boxes
    ###############################

    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] + '_detected' + image_path[-4:], image)
Esempio n. 11
0
def load_model(args):
    config_path = args.conf
    weights_path = args.weights

    with open(config_path) as config_buffer:
        config = json.load(config_buffer)

    ###############################
    #   Make the model
    ###############################

    yolo = YOLO(architecture=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'])

    ###############################
    #   Load trained weights
    ###############################

    print(weights_path)
    yolo.load_weights(weights_path)

    return yolo, config
Esempio n. 12
0
def _main_(args):
    config_path = args.conf
    weights_path = args.weights

    keras.backend.tensorflow_backend.set_session(get_session())

    with open(config_path) as config_buffer:
        config = json.load(config_buffer)

    if weights_path == '':
        weights_path = config['train']['saved_weights_name']

    yolo = YOLO(backend=config['model']['backend'],
                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)

    inference_model = yolo.get_inference_model()

    inference_model.save("{}_inference.h5".format(
        os.path.split(weights_path)[0]))
    print("done")
Esempio n. 13
0
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)

    ###############################
    #   Make the model 
    ###############################
    start = time.time()
    yolo = YOLO(backend             = config['model']['backend'],
                input_size          = config['model']['input_size'], 
                labels              = config['model']['labels'], 
                max_box_per_image   = config['model']['max_box_per_image'],
                anchors             = config['model']['anchors'])
    print('Finish making model, using time: ',round(time.time()-start,3))
    ###############################
    #   Load trained weights
    ###############################    
    start = time.time()
    yolo.load_weights(weights_path)
    print('Finish loading weights, using time: ',round(time.time()-start,3))
    ###############################
    #   Predict bounding boxes 
    ###############################
    start = time.time()
    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('Finish predicting, using time: ',round(time.time()-start,3))
        print(len(boxes), 'boxes are found')

        cv2.imwrite(image_path[:-4] + '_detected' + image_path[-4:], image)
Esempio n. 14
0
def _main_():
    config_path  = 'config.json'
    weights_path = 'pre_train_model.h5'
    image_path   = 'sample_example/PartA_00640.jpg'

    with open(config_path) as config_buffer:    
        config = json.load(config_buffer)

    ###############################
    #   Make the model 
    ###############################

    yolo = YOLO(backend             = config['model']['backend'],
                input_size          = config['model']['input_size'], 
                labels              = config['model']['labels'], 
                max_box_per_image   = config['model']['max_box_per_image'],
                anchors             = config['model']['anchors'])

    ###############################
    #   Load trained weights
    ###############################    

    yolo.load_weights(weights_path)

    ###############################
    #   Predict bounding boxes 
    ###############################

    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(boxes)
        print(len(boxes), 'boxes are found')

        cv2.imwrite(image_path[:-4] + '_detected' + image_path[-4:], image)
Esempio n. 15
0
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)

    ###############################
    #   Make the model
    ###############################

    yolo = YOLO(architecture=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'])

    ###############################
    #   Load trained weights
    ###############################

    print(weights_path)
    yolo.load_weights(weights_path)

    ###############################
    #   Predict bounding boxes
    ###############################

    pattern = '*.jpg'
    log_file = 'log_' + datetime.datetime.now().strftime(
        "%Y_%m_%d_%H_%M_%S") + '.txt'
    log_str = ''

    for path, subdirs, files in os.walk(image_path):
        for name in files:
            if fnmatch(name, pattern):
                filename = os.path.join(path, name)
                print(filename)
                log_str += path + ',' + name

                image = cv2.imread(filename)
                boxes = yolo.predict(image)
                image = draw_boxes(image, boxes, config['model']['labels'])

                print(len(boxes), 'boxes are found')
                for box in boxes:
                    box_label = config['model']['labels'][box.get_label()]
                    box_score = box.get_score()
                    log_str += ',' + box_label + ',' + str(box_score)

                cv2.imwrite(os.path.join(path, "detected_" + name), image)
                log_str += '\n'

    text_file = open(log_file, "w")
    text_file.write(log_str)
    text_file.close()
Esempio n. 16
0
def main():
    config_path = "config.json"
    weights_path = "../large_weights/full_yolo_whale.h5"
    directory_in_str = "../large_dataset/whale_files/test"

    df = pd.read_csv("../large_dataset/whale_files/sample_submission.csv",
                     header=None,
                     names=["Image", "Id", "Xmin", "Ymin", "Xmax", "Ymax"])
    df = df.drop([0])
    manual_check = pd.DataFrame(
        columns=["Image", "Id", "Xmin", "Ymin", "Xmax", "Ymax"])

    with open(config_path) as config_buffer:
        config = json.load(config_buffer)

    ###############################
    #   Make the model
    ###############################

    yolo = YOLO(backend=config['model']['backend'],
                input_size=config['model']['input_size'],
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'])

    ###############################
    #   Load trained weights
    ###############################

    yolo.load_weights(weights_path)

    ###############################
    #   Predict bounding boxes
    ###############################
    directory = os.fsencode(directory_in_str)
    for file in tqdm(os.listdir(directory)):
        filename = os.fsdecode(file)
        if filename.endswith(".jpg"):
            image = cv2.imread(os.path.join(directory_in_str, filename))
            image_h, image_w, _ = image.shape
            boxes = yolo.predict(image)
            if len(boxes) == 1:
                df.at[df.Image == filename,
                      'Xmin'] = max(floor(boxes[0].xmin * image_w), 0)
                df.at[df.Image == filename,
                      'Ymin'] = max(floor(boxes[0].ymin * image_h), 0)
                df.at[df.Image == filename,
                      'Xmax'] = min(ceil(boxes[0].xmax * image_w), image_w)
                df.at[df.Image == filename,
                      'Ymax'] = min(ceil(boxes[0].ymax * image_h), image_h)
            else:
                s = df[df.Image == filename]
                manual_check = pd.concat([manual_check, s])
        else:
            continue
    df.to_csv('test_box.csv', encoding='utf-8', index=False)
    manual_check.to_csv('manual_check_test.csv', encoding='utf-8', index=False)
Esempio n. 17
0
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)
    # build model
    yolo = YOLO(architecture=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'])

    # load pretrained model
    print(weights_path)
    yolo.load_weights(weights_path)

    # predict bounding box
    if image_path[-4:] == '.mp4':
        input_file = os.path.basename(args.input)
        video_out = args.output + config['model']['architecture'].replace(
            " ", "") + "_" + input_file[:-4] + ".mp4"

        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'), 30.0,
                                       (frame_w, frame_h))

        for i in tqdm(range(nb_frames)):
            _, image = video_reader.read()

            # boxes is list of box. normalize to 0~1 with input shape
            # box.x: xmin, box.y: ymin, box.w: box width, box.h: box height
            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')

        input_file = os.path.basename(args.input)
        cv2.imwrite(args.output +
                    config['model']['architecture'].replace(" ", "") + "_" +
                    input_file[:-4] + ".png")
Esempio n. 18
0
def _main_(args):

    ###############################
    #   Prepare data to be detected
    ###############################

    # data_folder = "/home/peng/data/good_rolo_data/"
    data_folder = "/home/peng/data/sort_data/images/"
    # data_folder = "/home/peng/data/sort_data/images/"
    video_folders_list = sorted(glob.glob(data_folder + '*'))
    sort_nicely(video_folders_list)
 
    config_path  = args.conf
    weights_path = args.weights

    with open(config_path) as config_buffer:    
        config = json.load(config_buffer)

    ###############################
    #   Make the model 
    ###############################

    yolo = YOLO(architecture        = 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'])

    ###############################
    #   Load trained weights
    ###############################    

    print("Weights path:", weights_path)
    yolo.load_weights(weights_path)

    ###############################
    #   Predict bounding boxes 
    ###############################

    for video_folder in video_folders_list:
        video_name = basename(video_folder)
        print("Processing %s." % video_name)
        image_paths = sorted(glob.glob(os.path.join(video_folder, '*jpg')))
        sort_nicely(image_paths)

        with open('det_mot/' + video_name + '.txt', 'w') as out_file:
            for i in tqdm(range(len(image_paths))):
                image = cv2.imread(image_paths[i])
                boxes = yolo.predict(image)

                for box in boxes:
                    x1 = (box.x - box.w/2) * 1280
                    y1 = (box.y - box.h/2) * 720
                    w = box.w * 1280
                    h = box.h * 720
                    print('%d,-1,%.2f,%.2f,%.2f,%.2f,%.6f,-1,-1,-1' % (i+1, x1, y1, w, h, box.c), file=out_file)
Esempio n. 19
0
def create_graph():
	global yolo, config
	with open(config_path) as config_buffer:
		config = json.load(config_buffer)

	yolo = YOLO(architecture=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'])
	print weights_path
	yolo.load_weights(weights_path)
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)

    ###############################
    #   Make the model
    ###############################
    yolo = YOLO(feature_extractor=config['model']['backend'],
                input_size=config['model']['input_size'],
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'])

    ###############################
    #   Load trained weights
    ###############################
    yolo.load_weights(weights_path)

    ###############################
    #   Predict bounding boxes
    ###############################
    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_object(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_object(image, boxes, config['model']['labels'])
        image_filename = image_path.split('/')[-1]
        cv2.imwrite('./sample/image_pred_box/' + image_filename, image)
Esempio n. 21
0
def _main_(args):
    config_path = args.conf
    weights_path = args.weights
    image_path = args.input
    runID = args.runID

    with open(config_path) as config_buffer:
        config = json.load(config_buffer)

    ###############################
    #   Make the model
    ###############################

    yolo = YOLO(backend=config['model']['backend'],
                input_size=config['model']['input_size'],
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'])

    ###############################
    #   Load trained weights
    ###############################

    yolo.load_weights(weights_path)

    ###############################
    #   Predict bounding boxes
    ###############################
    outputdir = os.path.join(
        '/flashblade/lars_data/2018_Cyclomedia_panoramas/project_folder/YOLO/false_positives_dir',
        runID)
    if os.path.exists(outputdir):
        print('path exists')
    else:
        os.makedirs(outputdir)
    print(image_path)
    imlist = os.listdir(image_path)
    print(len(imlist))
    for file in imlist:
        if file.endswith('jpg'):
            print(file)
            image = cv2.imread(os.path.join(image_path, file))
            boxes = yolo.predict(image)
            image = draw_boxes(image, boxes, config['model']['labels'])
            print(len(boxes), 'boxes are found')
            if len(boxes) > 0:
                filename = file[:-4] + '_detected' + file[-4:]
                output = os.path.join(outputdir, filename)
                print(output)
                cv2.imwrite(output, image)
Esempio n. 22
0
    def __init__(self, config_path, weights_path):
        with open(config_path) as config_buffer:
            config = json.loads(config_buffer.read())

        self.labels = config['model']['labels']

        self.yolo = YOLO(
            architecture=config['model']['architecture'],
            input_size=config['model']['input_size'],
            labels=self.labels,
            max_box_per_image=config['model']['max_box_per_image'],
            anchors=config['model']['anchors'])

        self.yolo.load_weights(weights_path)
Esempio n. 23
0
    def __init__(self, config_path, weights_path):
        self.config_path = config_path
        self.weights_path = weights_path

        with open(config_path) as config_buffer:
            self.config = json.load(config_buffer)

        self.yolo = YOLO(
            backend=self.config['model']['backend'],
            input_size=self.config['model']['input_size'],
            labels=self.config['model']['labels'],
            max_box_per_image=self.config['model']['max_box_per_image'],
            anchors=self.config['model']['anchors'])

        self.yolo.load_weights(self.weights_path)
Esempio n. 24
0
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)

    K.set_learning_phase(0)

    ###############################
    #   Make the model
    ###############################

    yolo = YOLO(backend=config['model']['backend'],
                input_size=config['model']['input_size'],
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'])

    ###############################
    #   Load trained weights
    ###############################

    print(weights_path)
    yolo.load_weights(weights_path)

    export_base = 'tfexport'
    export_version = 1
    export_path = os.path.join(tf.compat.as_bytes(export_base),
                               tf.compat.as_bytes(str(export_version)))

    builder = saved_model_builder.SavedModelBuilder(export_path)
    print(yolo.model.inputs[0])
    signature = predict_signature_def(
        inputs={
            "input_image": yolo.model.inputs[0],
            "true_boxes": yolo.model.inputs[1]
        },
        outputs={"outputs": yolo.model.outputs[0]})

    with K.get_session() as sess:
        builder.add_meta_graph_and_variables(
            sess=sess,
            tags=[tag_constants.SERVING],
            signature_def_map={'predict': signature})
        builder.save()
Esempio n. 25
0
def _main_(args):
    with open(args.conf) as config_buffer:
        config = json.load(config_buffer)

    ###############################
    #   Load the model
    ###############################
    # keras.backend.set_session(K.tf.Session(config=K.tf.ConfigProto(intra_op_parallelism_threads=8, inter_op_parallelism_threads=8)))

    yolo = YOLO(backend=config['model']['backend'],
                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(args.weights)

    ###############################
    #   Decide what to predict on
    ###############################
    if args.input[-4:] in ['.mp4', '.avi', '.mov']:
        predictOnVideo(args.input,
                       args.output,
                       config,
                       maxFramesToRunOn=5000,
                       runFrameSelection=args.frame_select,
                       detectionModel=yolo)

    else:
        first_file_name = os.listdir(args.input)[0]

        # predict on folder of images and save images with overlayed bboxes
        if first_file_name[-4:] in [".jpg", ".png"]:
            predictOnImageDir(args.input,
                              args.output,
                              config,
                              savePredictionsAsXmlToo=args.save_soft,
                              detectionModel=yolo)

        elif first_file_name[-4:] in [".h5", ".hdf5"]:
            predictOnH5Dir(args.input,
                           args.output,
                           config,
                           obj_threshold=obj_thresh,
                           detectionModel=yolo)

        else:
            print('input -i argument extension did not match what we expected')
Esempio n. 26
0
 def classifier(self):
     if not self._classifier:
         from frontend import YOLO
         # Khởi tạo Yolo
         self._classifier = YOLO(
             backend='Full Yolo',
             input_size=416,
             anchors=[
                 0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434,
                 7.88282, 3.52778, 9.77052, 9.16828
             ],
             max_box_per_image=10,
             labels=['Pothole'],
         )
         # Load dữ liệu cho model nhận diện
         self._classifier.load_weights('%s/classifier.h5' %
                                       NanoPotholeDetection.DATA_DIR)
     return self._classifier
Esempio n. 27
0
    def get_keras_model(self):
        config_path = 'ncsmodel/config.json'
        weights_path = 'ncsmodel/openimages_best3.h5'

        def weights_name(layer_list, name):
            for l in layer_list:
                if l.name == name:
                    return l.get_weights()

        with open(config_path) as config_buffer:
            config = json.load(config_buffer)

        yolo = YOLO(backend=config['model']['backend'],
                    input_size=(config['model']['input_size_h'],
                                config['model']['input_size_w']),
                    labels=config['model']['labels'],
                    max_box_per_image=config['model']['max_box_per_image'],
                    anchors=config['model']['anchors'],
                    gray_mode=config['model']['gray_mode'])

        # Loading weights:
        yolo.load_weights(weights_path)
        ymodel = yolo.model

        ylayers = ymodel.layers[1].layers
        for l in ymodel.layers[2:]:
            ylayers.append(l)

        print("Layers:")
        for i, l in enumerate(ylayers):
            print(i, l.name)

        # Save weights to numpy - 9 conv layers
        # 1 based index
        q_conv_bn = 6
        layer_weights = []
        for i in range(q_conv_bn):
            weights = weights_name(ylayers, 'conv_{}'.format(i + 1))[0]
            biases = weights_name(ylayers, 'norm_{}'.format(i + 1))
            layer_weights.append((biases, weights))

        weights, biases = weights_name(ylayers, 'DetectionLayer')
        layer_weights.append((biases, weights))
        return layer_weights
def _main_(args):
    config_path = args.conf
    weights_path = args.weights

    with open(config_path) as config_buffer:
        config = json.load(config_buffer)

    yolo = YOLO(backend=config['model']['backend'],
                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)

    valid_imgs, valid_labels = parse_annotation(
        config['valid']['valid_annot_folder'],
        config['valid']['valid_image_folder'], config['model']['labels'])
    yolo.batch_size = config['train']['batch_size']
    yolo.sequence_length = 1
    generator_config = {
        'IMAGE_H': yolo.input_size,
        'IMAGE_W': yolo.input_size,
        'GRID_H': yolo.grid_h,
        'GRID_W': yolo.grid_w,
        'BOX': yolo.nb_box,
        'LABELS': yolo.labels,
        'CLASS': len(yolo.labels),
        'ANCHORS': yolo.anchors,
        'BATCH_SIZE': yolo.batch_size,
        'TRUE_BOX_BUFFER': yolo.max_box_per_image,
        'SEQUENCE_LENGTH': yolo.sequence_length
    }
    valid_generator = BatchGenerator(valid_imgs,
                                     generator_config,
                                     norm=yolo.feature_extractor.normalize,
                                     jitter=False)
    ave_precisions = yolo.evaluate(valid_generator,
                                   iou_threshold=0.3,
                                   score_threshold=0.2)
    print("ave precisions: ", ave_precisions)
    print('mAP: {:.4f}'.format(
        sum(ave_precisions.values()) / len(ave_precisions)))
Esempio n. 29
0
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)

    ###############################
    #   Make the model
    ###############################
    model_load_time = time.time()
    yolo = YOLO(backend=config['model']['backend'],
                input_size=config['model']['input_size'],
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'])

    ###############################
    #   Load trained weights
    ###############################

    yolo.load_weights(weights_path)

    print('Model load time is ' + str(time.time() - model_load_time))

    ###############################
    #   Predict bounding boxes
    ###############################

    inference_time = []
    for filename in os.listdir(image_path):
        img_path = os.path.join(image_path, filename)
        inference_start_time = time.time()
        image = cv2.imread(img_path)
        boxes = yolo.predict(image)
        inference_time.append(time.time() - inference_start_time)
        print(len(boxes), 'boxes are found')
        image = draw_boxes(image, boxes, config['model']['labels'])
        cv2.imwrite(img_path[:-4] + '_detected' + img_path[-4:], image)
    print('Avg inference time is ' + str(np.mean(inference_time)) + '+/-' +
          str(np.std(inference_time)))
Esempio n. 30
0
def _main_(args):
    config_path = 'config.json'
    weights_path = 'tiny_yolo_trafficred3.h5'
    image_path = args.input

    with open(config_path) as config_buffer:
        config = json.load(config_buffer)

    ###############################
    #   Make the model
    ###############################

    yolo = YOLO(backend=config['model']['backend'],
                input_size=config['model']['input_size'],
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'])

    ###############################
    #   Load trained weights
    ###############################

    yolo.load_weights(weights_path)

    ###############################
    #   Predict bounding boxes
    ###############################

    image = cv2.imread(image_path)

    for item in range(10):
        start_time = time.time()
        boxes = yolo.predict(image)
        duration = time.time() - start_time
        print('Prediction time %5.2f' % (duration))

    image = draw_boxes(image, boxes, config['model']['labels'])

    print(len(boxes), 'boxes are found')

    cv2.imwrite(image_path[:-4] + '_detected' + image_path[-4:], image)