Пример #1
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)
Пример #2
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)
Пример #3
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(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(len(boxes), 'boxes are found')

        cv2.imwrite(image_path[:-4] + '_detected' + image_path[-4:], image)
Пример #4
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)
Пример #5
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")
Пример #6
0
class YOLO():
    def __init__(self, yaml_fp):
        yf_ = open(yaml_fp, 'r')
        yaml_ = yaml.load(yf_)
        config_path = yaml_['config_path']
        weights_path = yaml_['weights_path']
        self._score = yaml_['score']
        self._nms = yaml_['nms']
        yf_.close()

        os.chdir(config_path[:config_path.rfind('/') + 1])

        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(weights_path)
        self._labels = self._config['model']['labels']

        # for some reason, need to run once here
        from numpy import empty
        esz_ = [
            self._config['model']['input_size'],
            self._config['model']['input_size'], 3
        ]
        self.detect_img(empty(esz_))

        print('  YOLO object initialized!')

    def detect_img(self, img):
        boxes_ = self._yolo.predict(img, self._score, self._nms)
        msgs_ = []
        for box in boxes_:
            msg_ = YoloObj()
            msg_.box = bb_arr(box, img.shape)
            msg_.score = box.get_score()
            msg_.id = box.get_label()
            msg_.label = self._labels[msg_.id]
            msgs_.append(msg_)
        return YoloResponse(objects=msgs_)

    def annotate_img(self, img, draw_classes):
        boxes_ = self._yolo.predict(img, self._score, self._nms)

        if len(draw_classes):
            all_boxes_ = boxes_
            boxes_ = []
            for box in all_boxes_:
                if self._labels[box.get_label()] in draw_classes:
                    boxes_.append(box)

        return draw_boxes(img, boxes_, self._config['model']['labels'])
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)
Пример #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
Пример #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()      
Пример #10
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)
Пример #11
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()
Пример #12
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)
Пример #13
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)
Пример #14
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)
Пример #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
    ###############################
    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)))
Пример #16
0
def _main_(args):
    config_path = args.conf
    weights_path = args.weights
    input = args.input
    camera = args.camera

    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'],
                threshold=config['predict']['threshold'],
                max_sur=config["predict"]["max_sur"])

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

    ###############################
    #   Predict bounding boxes
    ###############################
    if camera:
        ph.predict_with_camera(yolo)

    elif input[-4:] == '.mp4':
        ph.predict_with_video(yolo, input, config['predict']['saved'])

    else:
        image = cv2.imread(input)
        boxes = yolo.predict(image)
        image = draw_boxes(image, boxes, config['model']['labels'])
        #print(len(boxes), 'boxes are found')
        cv2.imwrite(input[:-4] + '_detected' + input[-4:], image)
Пример #17
0
class DetectorAPI:
    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 processFrame(self, image):
        import numpy as np
        import time
        # Expand dimensions since the trained_model expects images to have shape: [1, None, None, 3]
        image_np_expanded = np.expand_dims(image, axis=0)
        # Actual detection.
        start_time = time.time()
        boxes = self.yolo.predict(image)
        end_time = time.time()

        print("Elapsed Time:", end_time - start_time)
        return boxes
Пример #18
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)
Пример #19
0
class Predictor():
    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)

    def predict(self, image):
        boxes = self.yolo.predict(image)
        image = draw_boxes(image, boxes, self.config['model']['labels'])
        return boxes, image
Пример #20
0
def _main_(args):

    config_path = args.conf
    weights_path = args.weights
    image_path = args.image

    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
    ###############################

    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)
Пример #21
0
def _main_():

    config_path = 'config.json'
    # weights_path = 'full_yolo_backend.h5'
    weights_path = 'model.h5'

    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
    ###############################

    for i in range(80):
        num = i + 21
        file_text = '/home/yuho/Face_Detection/data/photo/frame'
        image_path = file_text + str(num) + '.png'

        save_text = '/home/yuho/Face_Detection/data/save_data/frame'
        save_path = save_text + str(num) + '.png'

        # image_path   = '/home/yuho/Face_Detection/data/photo/frame56.png'
        # image_path = 'frame19.png'

        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)

            if boxes == 0:
                pass
            else:
                image = draw_boxes(image, boxes, config['model']['labels'])

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

            cv2.imwrite(save_path, image)
Пример #22
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(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
    ###############################

    print(image_path)
    imlist = os.listdir(image_path)
    print(imlist)
    for file in imlist:
        if file.endswith('jpg'):
            print(file)
            image = cv2.imread(
                os.path.join(
                    '/flashblade/lars_data/2018_Cyclomedia_panoramas/project_folder/YOLO',
                    image_path, file))
            boxes = yolo.predict(image)
            image = draw_boxes(image, boxes, config['model']['labels'])
            print(len(boxes), 'boxes are found')
            filename = file[:-4] + '_detected' + file[-4:]
            output = os.path.join(
                '/flashblade/lars_data/2018_Cyclomedia_panoramas/project_folder/YOLO',
                image_path, 'output', filename)
            print(output)
            if len(boxes) > 0:
                cv2.imwrite(output, image)
            temp_labels = config['model']['labels']
            for box in boxes:
                xmin = int(box.xmin * 1024)
                ymin = int(box.ymin * 512)
                xmax = int(box.xmax * 1024)
                ymax = int(box.ymax * 512)
                sign_class = temp_labels[box.get_label()]
                sign_score = box.get_score()
                print(
                    str(filename.split('_')[1] + '_' + filename.split('_')[2]),
                    xmin, xmax, ymin, ymax, sign_class, sign_score)
                print(
                    os.path.join(
                        '/flashblade/lars_data/2018_Cyclomedia_panoramas/project_folder/YOLO',
                        image_path, 'output', 'output.txt'))
                with open(
                        os.path.join(
                            '/flashblade/lars_data/2018_Cyclomedia_panoramas/project_folder/YOLO',
                            image_path, 'output', 'output.txt'), 'a') as f:
                    f.write(
                        "%s,%s,%s,%s,%s,%s,%s\n" % (str(
                            filename.split('_')[1] + '_' +
                            filename.split('_')[2]), xmin, xmax, ymin, ymax,
                                                    sign_class, sign_score))
Пример #23
0
def _main_(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(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)

    ###############################
    #   Parse the annotations
    ###############################
    # parse annotations of the validation set, if any, otherwise split the training set
    if os.path.exists(config['valid']['valid_annot_folder']):
        valid_imgs, valid_labels = parse_annotation(
            config['valid']['valid_annot_folder'],
            config['valid']['valid_image_folder'], config['model']['labels'])
    else:
        raise ValueError(
            'Validation folder does not exist or is not specified')

    ############################################
    # Make validation generators
    ############################################
    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': config['train']['batch_size'],
        'TRUE_BOX_BUFFER': yolo.max_box_per_image,
    }

    generator = BatchGenerator(valid_imgs,
                               generator_config,
                               norm=yolo.feature_extractor.normalize,
                               jitter=False)

    y_true = []
    y_predicted = []
    for i in range(generator.size()):
        raw_image = generator.load_image(i)
        raw_height, raw_width, raw_channels = raw_image.shape

        # make the boxes and the labels
        pred_boxes = yolo.predict(raw_image)

        score = np.array([box.score for box in pred_boxes])
        pred_labels = np.array([box.label for box in pred_boxes])

        if len(pred_boxes) > 0:
            pred_boxes = np.array([[
                box.xmin * raw_width, box.ymin * raw_height,
                box.xmax * raw_width, box.ymax * raw_height, box.score
            ] for box in pred_boxes])
        else:
            pred_boxes = np.array([[]])

        # sort the boxes and the labels according to scores
        score_sort = np.argsort(-score)
        pred_labels = pred_labels[score_sort]
        pred_boxes = pred_boxes[score_sort]

        # store predicted label for the image i.
        # since multiple boxes may be predicted, choose the one with the highest score
        # TODO: find out why there are no predictions at all for certain images
        if pred_labels.any():
            y_predicted.append(pred_labels[0])
        else:
            y_predicted.append(4)

        # load true image annotations
        annotations = generator.load_annotation(i)

        if annotations.shape[0] > 1:
            raise ValueError('Multiple objects exist per image not supported')

        ### store the true label for the image i
        y_true.append(annotations[0, 4])

    print('Processed ' + str(len(y_true)) + 'imgaes')

    print('Confusion Matrix')
    print(confusion_matrix(y_true, y_predicted))
    print('Classification Report')

    # added NoPrediction label to number of classes as yolo model returned null prediction for some images
    target_names = config['model']['labels'] + ['NoPrediction']
    print(classification_report(y_true, y_predicted,
                                target_names=target_names))
Пример #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)

    ###############################
    #   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
    #######d########################

    if image_path[-4:] == '.mp4' or image_path[-4:] == '.mov':
        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()
    elif image_path == 'webcam':
        video_reader = cv2.VideoCapture(0)
        frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT))
        frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH))
        while True:
            _, image = video_reader.read()
            boxes = yolo.predict(image)
            image = draw_boxes(image, boxes, config['model']['labels'])
            cv2.imshow('cube_detector', 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)
Пример #25
0
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']['pretrained_weights"']

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

    input_size = (config['model']['input_size_h'],
                  config['model']['input_size_w'])

    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'])

    if config['model']['gray_mode']:
        depth = 1
    else:
        depth = 3

    yolo.load_weights(weights_path)

    if image_path[-4:] == '.mp4':
        video_out = image_path[:-4] + '_detected' + image_path[-4:]
        #cap = FileVideoStream(image_path).start()
        cap = cv2.VideoCapture(image_path)
        time.sleep(1.0)
        #        fps = FPS().start()
        fps_img = 0.0
        counter = 0
        while True:
            start = time.time()
            ret, image = cap.read()

            if depth == 1:
                # convert video to gray
                image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                image = cv2.resize(image,
                                   input_size,
                                   interpolation=cv2.INTER_CUBIC)
                image = np.expand_dims(image, 2)
                #image = np.array(image, dtype='f')
            else:
                if counter == 1:
                    print("Color image")
                image = cv2.resize(image,
                                   input_size,
                                   interpolation=cv2.INTER_CUBIC)
                #image = np.array(image, dtype='f')

            #image = np.divide(image, 255.)
            tm_inf = time.time()
            boxes = yolo.predict(image)
            fps_img = (fps_img + (1 / (time.time() - start))) / 2

            print("Inference time: {:.4f}".format(time.time() - tm_inf))
            image = draw_boxes(image, boxes, config['model']['labels'])
            image = cv2.putText(image, "fps: {:.2f}".format(fps_img), (0, 20),
                                cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1, 4)
            cv2.imshow("Press q to quit", image)
            #            fps.update()

            #if counter == 10:
            #print(image.sum(), boxes)
            #    time.sleep(1)
            counter += 1

            if cv2.getWindowProperty("Press q to quit",
                                     cv2.WND_PROP_ASPECT_RATIO) < 0.0:
                print("Window closed")
                break
            elif cv2.waitKey(1) & 0xFF == ord('q'):
                print("Q pressed")
                break


#        fps.stop()
#        print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
#        print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
        cap.release()

    else:
        images = list(list_images(image_path))
        for fname in images[100:]:
            image = cv2.imread(fname)
            tm_inf = time.time()
            boxes = yolo.predict(image)
            print("Inference time: {:.4f}".format(time.time() - tm_inf))
            image = draw_boxes(image, boxes, config['model']['labels'])
            cv2.imshow("Press q to quit", image)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            time.sleep(2)
    cv2.destroyAllWindows()
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(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_h, image_w, _ = image.shape
            box_arr = []
            for box in boxes:
                xmin = int(box.xmin * image_w)
                ymin = int(box.ymin * image_h)
                xmax = int(box.xmax * image_w)
                ymax = int(box.ymax * image_h)
                box_arr.append([xmin, xmax, ymin, ymax])
            with open('box_arr.pkl', 'wb') as f:
                pickle.dump(box_arr, f)

            # 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_h, image_w, _ = image.shape
        box_arr = []
        for box in boxes:
            xmin = int(box.xmin * image_w)
            ymin = int(box.ymin * image_h)
            xmax = int(box.xmax * image_w)
            ymax = int(box.ymax * image_h)
            box_arr.append([xmin, xmax, ymin, ymax])
        with open('box_arr.pkl', 'wb') as f:
            pickle.dump(box_arr, f)

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

        print(len(boxes), 'boxes are found')
Пример #27
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
    ###############################

    # if it's an image, do detection, save image with bounding boxes to the same folder

    # if it's a folder, do detection, save images with boundins boxes to another folder

    # if result folder is present, save annotations to the result folder

    if image_path[-4:] == '.mp4':
        video_out = image_path[:-4] + '_detected' + image_path[-4:]

        video_reader = imageio.get_reader(image_path, 'ffmpeg')

        nb_frames = video_reader._meta['nframes']
        frame_h = 480
        frame_w = 640

        video_writer = imageio.get_writer(video_out, fps=20)

        for i in tqdm(range(0, nb_frames, 3)):
            image = video_reader.get_data(i)

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

            video_writer.append_data(np.uint8(image))
        video_writer.close()
    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)
Пример #28
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'])

    yolo2 = 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)  #feed in the main one, charm on can load on its own
    #yolo2.load_weights('charm_detector_3_12.h5')
    ###############################
    #   Predict bounding boxes
    ###############################
    print('reading input')
    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'), 30.0,
                                       (frame_w, frame_h))

        charm_count = 0
        charm_wait = 0

        orb_count = 0
        orb_wait = 0

        for i in tqdm(range(nb_frames)):
            #can just make function that checks labels.
            # and respits out the boxes? frames are at 60fps xD
            # i+240 can be how long it delays those things for
            _, image = video_reader.read()

            boxes = yolo.predict(image)
            #boxes2 = yolo2.predict(image)
            #dont really have to draw the orb boxes... but can tally at the side or something

            image = draw_boxes(image, boxes, config['model']['labels'])
            #def skillshot_count(skillshot_name,skillshot_count,boxes,labels,frame_i,wait_i):

            charm_count, charm_wait = skillshot_count(
                'charm', charm_count, boxes, config['model']['labels'], i,
                charm_wait)
            orb_count, orb_wait = skillshot_count('orb', orb_count, boxes,
                                                  config['model']['labels'], i,
                                                  orb_wait)

            label = 'Count of Orb of Deceptions: ' + str(orb_count)
            cv2.putText(image, label, (0, 475), 3, cv2.FONT_HERSHEY_PLAIN,
                        (0, 255, 0), 2)
            label = 'Count of Charms: ' + str(charm_count)
            cv2.putText(image, label, (0, 525), 3, cv2.FONT_HERSHEY_PLAIN,
                        (0, 255, 0), 2)

            video_writer.write(np.uint8(image))

        video_reader.release()
        video_writer.release()
    else:
        image = cv2.imread(image_path)
        boxes = yolo.predict(image)
        boxes2 = yolo2.predict(image)

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

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

        cv2.imwrite(image_path[:-4] + '_detected' + image_path[-4:], image)
Пример #29
0
def online_tracking(data_dir, STORE=False):
    if not os.path.exists(data_dir):
        raise IOError("Invalid data path:", data_dir)

    yolo_config_path = "../config_aerial.json"
    with open(yolo_config_path) as config_buffer:
        yolo_config = json.load(config_buffer)

    yolo = YOLO(architecture=yolo_config['model']['architecture'],
                input_size=yolo_config['model']['input_size'],
                labels=yolo_config['model']['labels'],
                max_box_per_image=yolo_config['model']['max_box_per_image'],
                anchors=yolo_config['model']['anchors'])
    yolo_weights_path = "../yolo_coco_aerial_person.h5"
    print("YOLO weights path:", yolo_weights_path)
    yolo.load_weights(yolo_weights_path)

    colours = np.round(np.random.rand(32, 3) * 255)

    frame_width = 1280
    frame_height = 720

    if STORE:
        video_name = data_dir.split('/')[-2]
        FPS = 30
        # remember to modify frame width and height before testing video
        video_writer = cv2.VideoWriter(
            'output_video/' + video_name + '.avi',
            cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), FPS,
            (frame_width, frame_height))

    image_paths = sorted(glob.glob(os.path.join(data_dir, '*jpg')))
    sort_nicely(image_paths)

    mot_tracker = Sort()  # create instance of the SORT tracker

    total_time = 0.0

    for i, image_path in enumerate(tqdm(image_paths)):
        image = cv2.imread(image_path)

        track_start_time = time.time()

        boxes = yolo.predict(image)

        detect_time = time.time() - track_start_time

        sort_start_time = time.time()

        dets = boxes2dets(boxes, image.shape)
        trackers = mot_tracker.update(dets)

        end_time = time.time()
        cycle_time = end_time - track_start_time
        total_time += cycle_time
        sort_time = end_time - sort_start_time

        for d in trackers:
            color = colours[int(d[4]) % 32]
            cv2.rectangle(image, (int(d[0]), int(d[1])),
                          (int(d[2]), int(d[3])), color, 3)
            cv2.putText(image, 'id = ' + str(int(d[4])),
                        (int(d[0]), int(d[1]) - 13), cv2.FONT_HERSHEY_SIMPLEX,
                        1e-3 * image.shape[0], color, 2)

        cv2.putText(image, 'Tracking FPS = {:.2f}'.format(1 / cycle_time),
                    (frame_width - 300, 25), cv2.FONT_HERSHEY_SIMPLEX,
                    1e-3 * image.shape[0], (0, 250, 0), 2)

        cv2.putText(image, '   YOLO FPS = {:.2f}'.format(1 / detect_time),
                    (frame_width - 300, 55), cv2.FONT_HERSHEY_SIMPLEX,
                    1e-3 * image.shape[0], (0, 250, 0), 2)

        cv2.putText(image, '   SORT FPS = {:.2f}'.format(1 / sort_time),
                    (frame_width - 300, 75), cv2.FONT_HERSHEY_SIMPLEX,
                    1e-3 * image.shape[0], (0, 250, 0), 2)

        if STORE:
            video_writer.write(image)
        else:
            cv2.imshow("output", image)
            cv2.waitKey(0)

        if i > 450:
            break

    total_frames = i + 1
    print("Total Tracking took: %.3f for %d frames or %.1f FPS" %
          (total_time, total_frames, total_frames / total_time))
Пример #30
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(backend          = config['model']['backend'],
        input_shape         = config['model']['input_shape'],
        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 
    ###############################

    fig,ax=plt.subplots(1)
# bbox x                    # globe eta
# bbox y                    # globe phi
# bbox width             # Gaussian sigma (required to be 3*sigma<pi)
# bbox height            # Gaussian sigma (required to be 3*sigma<pi)

    file_content = np.load(image_path)
    images = file_content['raw']
    truth_boxes = file_content['truth']
    for image_index in range(10):
        image = images[image_index]
        all_objs = truth_boxes[image_index]

        print(image.shape)
        boxes = yolo.predict(image)
        print(len(boxes), 'boxes are found')
        for i in range(len(boxes)):
            b = boxes[i]
            print('box:',i,b)
        draw_boxes(image, ax, boxes, config['model']['labels'],color='y',scale=True)

        obj_boxes=[]
        i=0
        for obj in all_objs:
            # x,y,w,h = obj[:4]
            y,x,h,w = obj[1:5]
            b = BoundBox(x-w/2,y-h/2,x+w/2,y+h/2)
            # print('box:',i,b,obj[5],obj[6],obj[7],obj[8],obj[9])
            print('box:',i,obj)
            obj_boxes.append( b )
            i+=1
        draw_boxes(image, ax, obj_boxes, config['model']['labels'],color='g',scale=False)

        #image = draw_boxes(image, boxes, config['model']['labels'])
        i=np.swapaxes(np.swapaxes(image,0,1),1,2)
        x=np.sum(i,axis=2)
        
        #plt.imshow(x,cmap='hot')
        plt.imshow(x,aspect='auto',extent=(0,256,0,9600),interpolation='nearest',cmap=cm.jet)        
        plt.savefig('out%d.png' % image_index ,dpi=200)
Пример #31
0
def predict(config_path, weights_path, filenames, path_in, path_out):

    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 
    ###############################

    for filename in filenames:

        fp_in = os.path.join(path_in, filename)
        fp_out = os.path.join(path_out, filename)

        if fp_in[-4:] == '.mp4':

            video_reader = cv2.VideoCapture(fp_in)

            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))
            fps = int(video_reader.get(cv2.CAP_PROP_FPS))

            video_writer = cv2.VideoWriter(fp_out,
                                   cv2.VideoWriter_fourcc(*'mp4v'), 
                                   fps, 
                                   (frame_w, frame_h))
            boxes_count = 0
            for i in tqdm(range(nb_frames)):
                _, image = video_reader.read()
                
                boxes = yolo.predict(image)
                boxes_count += len(boxes)
                # image = draw_boxes(image, boxes, config['model']['labels'])
                image = draw_boxes(image, boxes, config['model']['labels'], 'pixelate', .025, .5)
                video_writer.write(np.uint8(image))

            acc = boxes_count/nb_frames
            print('%s average accuracy (boxes found per frame)' % acc )

            if (acc < .3):
                os.remove(fp_out)

            video_reader.release()
            video_writer.release() 

        else:

            image = cv2.imread(fp_in)
            boxes = yolo.predict(image)
            if len(boxes) > 0 or '_thumb' in filename:
                print('%s: %d boxes found ' % (filename, len(boxes)) )
                # good settings for cv2.INTER_CUBIC
                # img = draw_boxes(img, boxes, config['model']['labels'], 'pixelate', .04, .5)
                # good settings for cv2.INTER_LINEAR
                image = draw_boxes(image, boxes, config['model']['labels'], 'pixelate', .025, .5)
                # Save 
                cv2.imwrite(fp_out, image)