Beispiel #1
0
    def __init__(self, args):
        self.colorImage = None
        self.img = None
        self.pilImg = None
        self.yolo = YOLO(**vars(args))
        self.publishers = []
        self.callback_list = []



        self.detection_info_msg = detection_info()
        self.pub_detection = rospy.Publisher("/detection_info_publisher", detection_info)
        self.pub_color_image = rospy.Publisher("/detection_color", Image)
        self.pub_color_info = rospy.Publisher("/detection_color_info", CameraInfo)
        self.pub_depth_image = rospy.Publisher("/detection_depth", Image)
        self.pub_depth_info = rospy.Publisher("/detection_depth_info", CameraInfo)
        #self.publishers[0] = rospy.Publisher("/detection_info_publisher", detection_info)
        #self.publishers[1] = rospy.Publisher("/detection_color", Image)
        #self.publishers[2] = rospy.Publisher("/detection_color_info", CameraInfo)
        #self.publishers[3] = rospy.Publisher("/detection_depth", Image)
        #self.publishers[4] = rospy.Publisher("/detection_depth_info", CameraInfo)


        self.bridge = CvBridge()
        #self.image_sub = rospy.Subscriber("/kinect2/qhd/image_color_rect", Image, self.callback)
        self.fourcc = cv2.VideoWriter_fourcc(*'XVID')
        self.video = cv2.VideoWriter("/home/erlendb/Programmering/PCL/kinect_ws/test.avi", self.fourcc, 30, (960, 540))

        self.running = None

        #FPS variables
        self.accum_time = 0
        self.curr_fps = 0
        self.prev_time = timer()
        self.fps = "FPS: ??"
Beispiel #2
0
def process(path_to_image, out_file):
    """ Apply detection and classification ML model to the input image IN and stores the result to 
    output file OUT.
    """
    from keras_yolo3.yolo import YOLO
    from PIL import Image

    # Create an instance of YOLO. In fact, its name is YOLO but this implementation already contains
    # the custom network for classification, hence it returns a image already with the classification!
    yolo_model = YOLO()
    # Notice that there will be some warnings displayed regarding instantiating YOLO, but it's not
    # important by now, it should work fine

    # Load image
    image = Image.open(path_to_image)

    # Apply network to image
    image_result, result_list = yolo_model.detect_image(image)

    # Save the generated image on same directory, with name: "result_image.jpg"
    print("***")
    print("Saving image to: '{}'".format(out_file))
    try:
        image_result.save(out_file)
        print("Success! Image saved at: {}".format(out_file))
    except Exception as e:
        print(
            "An error occurred while trying to save image to '{}': {}".format(
                out_file, str(e)))
Beispiel #3
0
    def __init__(self):
        min_confidence = 0.25
        is_tiny = False

        if is_tiny and anchors_path:
            anchors_path = os.path.join(
                os.path.dirname(anchors_path), "yolo-tiny_anchors.txt"
            )

        anchors_path = os.path.join(src_path, "keras_yolo3", "model_data", "yolo_anchors.txt")
        anchors = get_anchors(anchors_path)
        # define YOLO detector
        self.yolo = YOLO(
            **{
                "model_path": model_weights,
                "anchors_path": anchors_path,
                "classes_path": model_classes,
                "score": min_confidence,
                "gpu_num": 0,
                "model_image_size": (416, 416),
            }
        )

        # labels to draw on images
        class_file = open(model_classes, "r")
        self.input_labels = [line.rstrip("\n") for line in class_file.readlines()]
Beispiel #4
0
def detect_object(imageFolder='imagesToDetect/', postfix=""):
    gpu_options = tf.compat.v1.GPUOptions(allow_growth=True)
    session = tf.compat.v1.InteractiveSession(config=tf.compat.v1.ConfigProto(
        gpu_options=gpu_options))
    """
    Calls the YOLO face detector on a folder and saves results to detectedImages folder

    Args:
      imageFolder: string
          folder where the images to detect are stored

      postfix: string 
          appends string to filenames
    Returns:
      detections: a list of bounding boxes in format [filename,[(xmin,ymin,xmax,ymax,class_id,confidence]]

    """
    save_img = True  #always save the images for now

    #keeping the output folder static for time being
    save_img_path = 'boxedImages/'

    #get detection output folder from params
    detect_path = imageFolder

    #make the yolo model
    yolo = YOLO()

    # list for min
    detections = []

    #iterate over all images in detect folder, try to open image and detect
    for img_path in os.listdir('imagesToDetect'):
        try:
            image = Image.open(detect_path + img_path)
            if image.mode != "RGB":
                image = image.convert("RGB")
            image_array = np.array(image)
        # thrown if file can't be opened, return None if this is the case
        except:
            print("File Open Error! Try again!")
            return None

        # make Prediction using yolo network
        prediction, new_image = yolo.detect_image(image)

        # add faces detected to be returned
        detections.append([img_path, prediction])

        # save image in output folder
        img_out = postfix.join(os.path.splitext(os.path.basename(img_path)))
        if save_img:
            new_image.save(os.path.join(save_img_path, img_out))

    session.close()
    return detections
Beispiel #5
0
 def __init__(self):
     self.depth_estimator = Depth(path['FCRN'])
     self.detector = YOLO(path['YOLO'], path['YOLO_anchor'], path['YOLO_classes'])
     #self.segmentator = KittiSeg(default_run=path['KITTI_MODEL'], runs_dir=path['KITTI_RUNS'], hype_path=path['KITTI_HYPES'], data_dir=path['KITTI_DATA_DIR'])        
     self.zebra_detector = Zebra()
     self.traffic_light_pool = LightPool()
     self.obstacle_pool = Obstacles()
     self.state = 'LIGHT_WAIT'
     self.alert = None
     self.zebra_contours = []
     self.is_stable = False
     """
Beispiel #6
0
    def __init__(self):
        # Yolo setup
        self.yolo = YOLO(
            **{
                "model_path": CONFIG["model_path"],
                "anchors_path": CONFIG["anchors_path"],
                "classes_path": CONFIG["classes_path"],
                "score": CONFIG["score"],
                "gpu_num": CONFIG["gpu_num"],
                "model_image_size": (416, 416),
            })

        # Make a dataframe for the prediction outputs
        self.out_df = pd.DataFrame(columns=OUT_COLS)
Beispiel #7
0
def initialize(filename):

    print('Initialization in progress...!\n')
    start = time.time()
    yolo = YOLO(
        **{
            "model_path":
            './model/keras_yolo3/model_data/yolo_weights_logos.h5',
            "anchors_path": './model/keras_yolo3/model_data/yolo_anchors.txt',
            "classes_path": './data/preprocessed/classes.txt',
            "score": 0.05,
            "gpu_num": 1,
            "model_image_size": (416, 416),
        })
    # get Inception/VGG16 model and flavor from filename
    model_name, flavor = model_flavor_from_name(filename)
    ## load pre-processed features database
    features, brand_map, input_shape = load_features(filename)

    ## load inception model
    model, preprocess_input, input_shape = load_extractor_model(
        model_name, flavor)
    my_preprocess = lambda x: preprocess_input(utils.pad_image(x, input_shape))

    with open('./data/preprocessed/trained_brands.pkl', 'rb') as f:
        img_input, input_labels = pickle.load(f)

    (img_input, feat_input, sim_cutoff,
     (bins, cdf_list)) = load_brands_compute_cutoffs(img_input,
                                                     (model, my_preprocess),
                                                     features, sim_threshold)
    print('Done! It tooks {:.2f} mins.\n'.format((time.time() - start) / 60))

    return (yolo, model, my_preprocess), (feat_input, sim_cutoff, bins,
                                          cdf_list, input_labels)
Beispiel #8
0
def arg_params_yolo():
    # class YOLO defines the default value, so suppress any default HERE
    parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS)
    # Command line options
    parser.add_argument('-w',
                        '--path_weights',
                        type=str,
                        help='path to model weight file')
    parser.add_argument('-a',
                        '--path_anchors',
                        type=str,
                        help='path to anchor definitions')
    parser.add_argument('-c',
                        '--path_classes',
                        type=str,
                        help='path to class definitions')
    parser.add_argument('--nb_gpu',
                        type=int,
                        help='Number of GPU to use',
                        default=str(YOLO.get_defaults("nb_gpu")))
    parser.add_argument('-o',
                        '--path_output',
                        required=False,
                        type=str,
                        default='.',
                        help='path to the output directory')
    return parser
Beispiel #9
0
    def initialize(self):

        print('Initialization in progress...!\n')        
        
        start = time.time()
        yolo = YOLO(**{"model_path": self.model_path, 
            "anchors_path": self.anchors,
            "classes_path": self.yolo_classes_path,
            "score" : self.confidence,
            "gpu_num" : self.gpu_num,
            "model_image_size" : (416, 416),
            })
        
        # load pre-processed features database
        features, _, _ = load_features(self.recog_model)
        with open(self.classes_path, 'rb') as f:
            #img_input, input_labels = pickle.load(f)
            input_feats, input_labels = pickle.load(f)

        # load pre-trained recognition model
        model, preprocessed, input_shape = load_extractor_model(self.recog_model)
        my_preprocess = lambda x: preprocessed(pad_image(x, input_shape))

        #input_feat = extract_features(img_input, model, my_preprocess)
        sim_cutoff, (bins, cdf_list) = similarity_cutoff(input_feats, features, 0.95)

        print("Done...! It tooks {:.3f} mins\n".format((time.time() - start)/60))
        
        self.model_preproc = (yolo, model, my_preprocess)
        self.params = (input_feats, sim_cutoff, bins, cdf_list, input_labels)        
        return True
Beispiel #10
0
def main(args):

    yolo = YOLO(**{'model_path': args.model_path,
                   'anchors_path': args.anchors,
                   'classes_path': args.yolo_classes_path,
                   'score': args.score,
                   'gpu_num': args.gpu_num,
                   'model_image_size': (416, 416),
                   })
    if not os.path.exists(args.result_path):
        os.mkdir(args.result_path)

    if args.mode.lower() == 'detect':
        img_list = list(Path(args.input_path).iterdir())

        elapsed_t = 0
        img_cnt = 0
        for img_path in img_list:
            pred, _, t = detect_logo_only(str(img_path), yolo,
                                 save_img=args.save_img, 
                                 save_img_path=args.result_path,
                                 crop=args.crop)
            '''
            for p in pred:
                x1 = p[0]
                y1 = p[1]
                x2 = p[2]
                y2 = p[3]
                roi = img[y1:y2, x1:x2]
            '''
            if t == 0:
                continue
            elapsed_t += t
            img_cnt += 1
        print('\nDetecting Logos from images completed! It tooks {:.3f} FPS'.format(img_cnt/elapsed_t))

    elif args.mode.lower() == 'recog':
        model_preproc, params = initialize(yolo, args.recog_model,
                                           args.DB_path)
        img_list = list(Path(args.input_path).iterdir())

        elapsed_t = 0
        img_cnt = 0
        for img_path in img_list:
            _, _, t = detect_and_match(str(img_path), model_preproc, params,
                             save_img=args.save_img, save_img_path=args.result_path)
            if t == 0:
                continue
            elapsed_t += t
            img_cnt += 1
        print('\nDetecting and Recognizing Logos from images completed! It tooks {:.3f} FPS'.format(img_cnt/elapsed_t))

    else: # args.model == 'DB'
        construct_DB(args.DB_list, args.recog_model, args.DB_path)

    print("Done...!")
Beispiel #11
0
def parser():
   parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS)
   '''
   Command line options
   '''
   parser.add_argument(
       '--model', type=str,
       help='path to model weight file, default ' + YOLO.get_defaults("model_path")
   )

   parser.add_argument(
       '--anchors', type=str,
       help='path to anchor definitions, default ' + YOLO.get_defaults("anchors_path")
   )

   parser.add_argument(
       '--classes', type=str,
       help='path to class definitions, default ' + YOLO.get_defaults("classes_path")
   )

   parser.add_argument(
       '--gpu_num', type=int,
       help='Number of GPU to use, default ' + str(YOLO.get_defaults("gpu_num"))
   )

   parser.add_argument(
       '--image', default=True, action="store_true",
       help='Image detection mode, will ignore all positional arguments'
   )
   '''
   Command line positional arguments -- for video detection mode
   '''
   parser.add_argument(
       "--input", nargs='?', type=str,required=False,default='./path2your_video',
       help = "Video input path"
   )

   parser.add_argument(
       "--output", nargs='?', type=str, default="",
       help = "[Optional] Video output path"
   )

   return parser.parse_args()
Beispiel #12
0
def init_yolo(model_weights, anchors_path, score, gpu_num, model_image_size):
    yolo = YOLO(
        **{
            "model_path": model_weights,
            "anchors_path": anchors_path,
            "classes_path": classes_path,
            "score": score,
            "gpu_num": gpu_num,
            "model_image_size": model_image_size,
        })
    print("MODELSERVER: Model initialized")
    return yolo
Beispiel #13
0
def classify_baby_yolo(baby_url):

    img_data = requests.get(baby_url).content
    image = Image.open(io.BytesIO(img_data))
    print('image loaded!', image.size)

    from keras_yolo3.yolo import YOLO
    yolo_obj = YOLO()
    print('YOLO loaded!')
    image_post_yolo, result = yolo_obj.detect_image(image)
    print('YOLO applied!', result)
    print(result[0])
    #result[0].show()
    '''imgs = []
    for obj in result[1]:
        if obj['predicted_class'] == 'person' and obj['score']>0.33:
            border = tuple(obj['box']) # left, up, right, bottom
            img_cropped = image.crop(border)
            imgs.append(img_cropped)
    print('Babies found: ',len(imgs))'''
    import keras
    keras.backend.clear_session()
    return result
    def __init__(self):
        #TODO load classifier
        model_data_path = os.path.dirname(os.path.abspath(__file__))
        self.detector = YOLO(anchors_path=model_data_path +
                             '/keras_yolo3/model_data/tiny_yolo_anchors.txt',
                             model_path=model_data_path +
                             '/model_data/tiny_yolo.h5',
                             class_name='traffic light',
                             height=240,
                             width=120)
        model_name = model_data_path + '/model_data/lenet_traffic_light.h5'
        f = h5py.File(model_name, mode='r')
        model_version = f.attrs.get('keras_version')
        keras_version = str(keras.__version__).encode('utf8')

        if model_version != keras_version:
            print('You are using Keras version ', keras_version,
                  ', but the model was built using ', model_version)

        self.classifier = load_model(
            model_name, custom_objects={'Normalization': Normalization()})
        global graph
        graph = tf.get_default_graph()
Beispiel #15
0
def main(args):
   ic = Receiver(args)



   rospy.init_node("Receiver", anonymous=True)

   try:
       ic.start()
   except KeyboardInterrupt:
       print("Shutting down")


   print(YOLO.get_defaults("model_path"))
def _main():
    with open(DATASET_DIR + "annotations_train.json") as file:
        annotations = json.load(file)

    model_path = "/floyd/input/yolo_weights/trained_weights_final.h5"
    anchors_path = "keras_yolo3/model_data/tiny_yolo_anchors.txt"
    classes_path = "keras_yolo3/model_data/vrd_classes.txt"

    detector = YOLO(model_path=model_path,
                    anchors_path=anchors_path,
                    classes_path=classes_path)

    classifier = PredicateClassifier()

    train_classifier(annotations, detector, classifier)
Beispiel #17
0
def _main(image):
    model_path = DATASET_DIR + "trained_weights_final.h5"
    anchors_path = "keras_yolo3/model_data/tiny_yolo_anchors.txt"
    classes_path = "keras_yolo3/model_data/vrd_classes.txt"

    detector = YOLO(model_path=model_path,
                    anchors_path=anchors_path,
                    classes_path=classes_path)

    classifier = PredicateClassifier(save_path=DATASET_DIR + "pred_cls.ckpt")

    annotations = evaluate_relations(image, detector, classifier)
    print(annotations)

    return annotations
Beispiel #18
0
def listener():
    global image
    yolo = YOLO(**vars(FLAGS))
    rospy.init_node('yolo', anonymous=True)
    pub = rospy.Publisher('yolo_res', String, queue_size=10)
    rospy.Subscriber("/camera/rgb/image_raw", msg.Image, callback)
    print("Waiting ...")
    while True:
        if image != None:
            res, rimage = yolo.detect_image(image)
            rimage.show()
            image = None
            message = "{ "
            for obj in res:
                top, left, bottom, right, label, score = obj
                message = message + "{top : " + str(top) + ", left : " + str(
                    left) + ", bottom : " + str(bottom)
                message = message + ", right : " + str(
                    right) + ", label : " + str(label) + ", score : " + str(
                        score) + "},"
            message = message[:-1]
            message = message + "}"
            rospy.loginfo(message)
            pub.publish(message)
Beispiel #19
0
def _main(path_weights, path_anchors, path_classes, path_output, nb_gpu=0, **kwargs):

    yolo = YOLO(weights_path=path_weights, anchors_path=path_anchors,
                classes_path=path_classes, nb_gpu=nb_gpu)

    logging.info('Start image/video processing..')
    if 'path_image' in kwargs:
        paths_img = expand_file_paths(kwargs['path_image'])
        for path_img in tqdm.tqdm(paths_img, desc='images'):
            logging.debug('processing: "%s"', path_img)
            predict_image(yolo, path_img, path_output)
    if 'path_video' in kwargs:
        paths_vid = expand_file_paths(kwargs['path_image'])
        for path_vid in tqdm.tqdm(paths_vid, desc='videos'):
            logging.debug('processing: "%s"', path_vid)
            predict_video(yolo, path_vid, path_output)
Beispiel #20
0
    def __init__(self,
                 n_predicates,
                 n_object_categories,
                 yolo_model=YOLO_PATH,
                 yolo_anchors=ANCHORS_PATH,
                 yolo_classes=CLASSES_PATH,
                 target_update_frequency=10000,
                 discount_factor=0.9,
                 learning_rate=0.001,
                 max_memory=2048,
                 batch_size=64,
                 save_path="./checkpoints/VRL.ckpt"):
        """Build online and target networks, initialize replay memory."""
        # RL hyperparameters
        self.target_update_frequency = target_update_frequency
        self.discount_factor = discount_factor

        # initialize object detector
        self.detector = YOLO(model_path=yolo_model,
                             anchors_path=yolo_anchors,
                             classes_path=yolo_classes)

        # build network
        network_args = [n_predicates, n_object_categories, learning_rate]

        tf.reset_default_graph()
        self.DQN = DQN(*network_args, save_path=self.save_path)

        self.target_DQN = DQN(*network_args, var_scope="target_DQN")

        # initialize experience replay buffer
        self.memory = Memory(max_memory)
        self.batch_size = batch_size

        # initialize tensorflow session
        self.sess = tf.Session()
        if os.path.isfile(self.save_path + ".index"):
            self.DQN.load(self.sess)
        else:
            self.sess.run(tf.global_variables_initializer())

        self._update_target_network()
Beispiel #21
0
class Yolo():
    def __init__(self):
        # Yolo setup
        self.yolo = YOLO(
            **{
                "model_path": CONFIG["model_path"],
                "anchors_path": CONFIG["anchors_path"],
                "classes_path": CONFIG["classes_path"],
                "score": CONFIG["score"],
                "gpu_num": CONFIG["gpu_num"],
                "model_image_size": (416, 416),
            })

        # Make a dataframe for the prediction outputs
        self.out_df = pd.DataFrame(columns=OUT_COLS)

    def predict(self, img_path, out_dir):
        class_file = open(CONFIG["classes_path"], "r")
        input_labels = [line.rstrip("\n") for line in class_file.readlines()]

        self.out_df = pd.DataFrame(columns=OUT_COLS)

        total = len(os.listdir(img_path))
        for i, filename in enumerate(os.listdir(img_path)):
            print("\rDetecting image ", i, " of ", total, end="", flush=True)
            image = open_image(os.path.join(img_path, filename))
            prediction, predict_image = self.yolo.detect_image(image)
            y_size, x_size, _ = np.array(predict_image).shape

            for single_prediction in prediction:
                self.out_df = self.out_df.append(
                    pd.DataFrame(
                        [[
                            filename,
                            img_path.rstrip("\n"),
                        ] + single_prediction + [x_size, y_size]],
                        columns=OUT_COLS,
                    ))

        self.out_df.to_csv(out_dir, index=False)
Beispiel #22
0
def _main(path_weights,
          path_anchors,
          path_classes,
          nb_gpu,
          path_output=None,
          images=False,
          videos=False,
          stream=False):
    assert any([images, videos, stream]), 'nothing to do...'

    yolo = YOLO(path_weights, path_anchors, path_classes, nb_gpu=nb_gpu)

    if images:
        # Image detection mode, disregard any remaining command line arguments
        logging.info('Image detection mode')
        loop_detect_image(yolo, path_output)
    elif videos:
        logging.info('Video detection mode')
        loop_detect_video(yolo, path_output)
    elif stream:
        logging.info('Video detection mode')
        loop_detect_stream(yolo, path_output)
    def __init__(self, package_name, **kwargs):
        """
        Init package attributes here

        :param kwargs: config params
        :return:
        """
        if not os.path.isdir('model_data'):
            os.makedirs('model_data')
            # download artifacts
        package = dl.packages.get(package_name=package_name)

        if not os.path.isfile('model_data/yolo.h5'):
            artifact = package.project.artifacts.get(package_name=package_name,
                                                     artifact_name='yolo.h5')
            artifact.download(local_path='model_data')
        if not os.path.isfile('model_data/yolo_anchors.txt'):
            artifact = package.project.artifacts.get(
                package_name=package_name, artifact_name='yolo_anchors.txt')
            artifact.download(local_path='model_data')
        if not os.path.isfile('model_data/coco_classes.txt'):
            artifact = package.project.artifacts.get(
                package_name=package_name, artifact_name='coco_classes.txt')
            artifact.download(local_path='model_data')
        if not os.path.isfile('model_data/mars-small128.pb'):
            artifact = package.project.artifacts.get(
                package_name=package_name, artifact_name='mars-small128.pb')
            artifact.download(local_path='model_data')

        ###############
        # load models #
        ###############
        self.yolo = YOLO()
        self.encoder = gdet.create_box_encoder('model_data/mars-small128.pb',
                                               batch_size=1)
        self.graph = tf.get_default_graph()
Beispiel #24
0
    save_img = not FLAGS.no_save_img

    input_image_paths = GetFileList(FLAGS.input_images)

    print('Found {} input images: {}...'.format(len(input_image_paths), [ os.path.basename(f) for f in input_image_paths[:5]]))

    output_path = FLAGS.output
    if not os.path.exists(output_path):
        os.makedirs(output_path)

    # define YOLO detector
    yolo = YOLO(**{"model_path": FLAGS.model_path,
                "anchors_path": FLAGS.anchors_path,
                "classes_path": FLAGS.classes_path,
                "score" : FLAGS.score,
                "gpu_num" : FLAGS.gpu_num,
                "model_image_size" : (416, 416),
                }
               )

    # Make a dataframe for the prediction outputs
    out_df = pd.DataFrame(columns=['image', 'image_path','xmin', 'ymin', 'xmax', 'ymax', 'label','confidence','x_size','y_size'])

    # labels to draw on images
    class_file = open(FLAGS.classes_path, 'r')
    input_labels = [line.rstrip('\n') for line in class_file.readlines()]
    print('Found {} input labels: {}...'.format(len(input_labels), input_labels))

    start = timer()
    text_out = ''
Beispiel #25
0
from keras_yolo3.yolo import YOLO
from PIL import Image
from libs.utils import *
import cv2
import numpy as np
import yaml
import serial
import time


# Read configuration
with open("config.yml", 'r') as ymlfile:
    config = yaml.load(ymlfile)

yolo = YOLO()
bt = serial.Serial('/dev/rfcomm0', 19200)
while True:
    in_data = bt.read_until().decode()
    if in_data[:-2] == "a":
        bt.write("b".encode())
        break
print("Connected!")

try:
    # Input video stream
    vid = cv2.VideoCapture(0)
    if not vid.isOpened():
        raise IOError("Couldn't open webcam or video") 
    video_size = (int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)),
                  int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))) 
Beispiel #26
0
class YoloModel:
    def __init__(self):
        min_confidence = 0.25
        is_tiny = False

        if is_tiny and anchors_path:
            anchors_path = os.path.join(
                os.path.dirname(anchors_path), "yolo-tiny_anchors.txt"
            )

        anchors_path = os.path.join(src_path, "keras_yolo3", "model_data", "yolo_anchors.txt")
        anchors = get_anchors(anchors_path)
        # define YOLO detector
        self.yolo = YOLO(
            **{
                "model_path": model_weights,
                "anchors_path": anchors_path,
                "classes_path": model_classes,
                "score": min_confidence,
                "gpu_num": 0,
                "model_image_size": (416, 416),
            }
        )

        # labels to draw on images
        class_file = open(model_classes, "r")
        self.input_labels = [line.rstrip("\n") for line in class_file.readlines()]
    
    def __del__(self):
        # Close the current yolo session
        self.yolo.close_session()

    
    def detect(self, img, show_stats=True):
        start = timer()
        prediction, detected_img = self.yolo.detect_image(img, show_stats=show_stats)
        detected_img = np.asarray(detected_img)
        y_size, x_size, _ = detected_img.shape

        # Make a dataframe for the prediction outputs
        out_df = pd.DataFrame(
            columns=[
                "xmin",
                "ymin",
                "xmax",
                "ymax",
                "label",
                "confidence",
                "x_size",
                "y_size",
            ]
        )

        for single_prediction in prediction:
            out_df = out_df.append(
                pd.DataFrame(
                    [
                        single_prediction
                        + [x_size, y_size]
                    ],
                    columns=[
                        "xmin",
                        "ymin",
                        "xmax",
                        "ymax",
                        "label",
                        "confidence",
                        "x_size",
                        "y_size",
                    ],
                )
            )
        end = timer()
        if show_stats:
            print(f"Yolo v3 detection took {end-start:.2f} s")
        return out_df, detected_img
Beispiel #27
0
def model(data):
    return YOLO(**data)
Beispiel #28
0
import cv2
import numpy as np

from keras_yolo3.yolo import YOLO
from tf_crnn.tf_predict import CRNN


crnn = CRNN()
yolo3 = YOLO()


def ocr_predict(image):
    text1 = []
    text3 = []
    detect_img = image.copy()
    for label, score, img, box in yolo3.get_text_box_area(image):
        cv2.rectangle(detect_img, box[0], box[1], (0, 0, 255), thickness=2)
        if label == 'text1':
            text1.append((score, img))
        elif label == 'text3':
            text3.append((score, img))
    text1 = sorted(text1, key=lambda x: -x[0])[:1]
    text3 = sorted(text3, key=lambda x: -x[0])[:2]
    predict = crnn.predict(np.asarray(text1 + text3)[:, 1])
    return predict, detect_img


if __name__ == '__main__':
    while True:
        command = input('Please input image path: ')
        if command == 'quit':
Beispiel #29
0
def test(filename):
    """
    Test function: runs pipeline for a small set of input images and input
    brands.
    """
    yolo = YOLO(**{"model_path": 'keras_yolo3/yolo_weights_logos.h5',
                "anchors_path": 'keras_yolo3/model_data/yolo_anchors.txt',
                "classes_path": 'data_classes.txt',
                "score" : 0.05,
                "gpu_num" : 1,
                "model_image_size" : (416, 416),
                }
               )
    save_img_logo, save_img_match = True, True

    test_dir = os.path.join(os.path.dirname(__file__), os.path.pardir, 'data/test')

    # get Inception/VGG16 model and flavor from filename
    model_name, flavor = model_flavor_from_name(filename)
    ## load pre-processed features database
    features, brand_map, input_shape = load_features(filename)

    ## load inception model
    model, preprocess_input, input_shape = load_extractor_model(model_name, flavor)
    my_preprocess = lambda x: preprocess_input(utils.pad_image(x, input_shape).astype(np.float32))

    ## load sample images of logos to test against
    input_paths = ['test_batman.jpg', 'test_robin.png', 'test_lexus.png', 'test_champions.jpg',
                   'test_duff.jpg', 'test_underarmour.jpg', 'test_golden_state.jpg']
    input_labels = [ s.split('test_')[-1].split('.')[0] for s in input_paths]
    input_paths = [os.path.join(test_dir, 'test_brands/', p) for p in input_paths]

    # compute cosine similarity between input brand images and all LogosInTheWild logos
    ( img_input, feat_input, sim_cutoff, (bins, cdf_list)
    ) = load_brands_compute_cutoffs(input_paths, (model, my_preprocess), features, sim_threshold, timing=True)

    images = [ p for p in os.listdir(os.path.join(test_dir, 'sample_in/')) if p.endswith('.jpg')]
    images_path = [ os.path.join(test_dir, 'sample_in/',p) for p in images]

    start = timer()
    times_list = []
    img_size_list = []
    candidate_len_list = []
    for i, img_path in enumerate(images_path):
        outtxt = img_path

        ## find candidate logos in image
        prediction, image = detect_logo(yolo, img_path, save_img = True,
                                          save_img_path = test_dir, postfix='_logo')

        ## match candidate logos to input
        outtxt, times = match_logo(image, prediction, (model, my_preprocess),
                outtxt, (feat_input, sim_cutoff, bins, cdf_list, input_labels),
                save_img = save_img_match, save_img_path=test_dir, timing=True)

        img_size_list.append(np.sqrt(np.prod(image.size)))
        candidate_len_list.append(len(prediction))
        times_list.append(times)

    end = timer()
    print('Processed {} images in {:.1f}sec - {:.1f}FPS'.format(
            len(images_path), end-start, len(images_path)/(end-start)
           ))

    fig, axes = plt.subplots(1,2, figsize=(9,4))
    for iax in range(2):
        for i in range(len(times_list[0])):
            axes[iax].scatter([candidate_len_list, img_size_list][iax], np.array(times_list)[:,i])

        axes[iax].legend(['read img','get box','get features','match','draw','save'])
        axes[iax].set(xlabel=['number of candidates', 'image size'][iax], ylabel='Time [sec]')
    plt.savefig(os.path.join(test_dir, 'timing_test.png'))
Beispiel #30
0
from skimage.io import imread

from evaluate import evaluate_relations
from keras_yolo3.yolo import YOLO
from relation_model.predicate_classifier import PredicateClassifier

ALLOWED_EXTENSIONS = set(['jpg', 'jpeg'])
app = Flask(__name__)

model_path = "/floyd/input/weights/trained_weights_final.h5"
anchors_path = "keras_yolo3/model_data/tiny_yolo_anchors.txt"
classes_path = "keras_yolo3/model_data/vrd_classes.txt"

detector = YOLO(model_path=model_path,
                anchors_path=anchors_path,
                classes_path=classes_path)

classifier = PredicateClassifier()


@app.route('/', methods=["GET", "POST"])
def evaluate():
    """Take the input image and style transfer it."""
    # check if the post request has the file part
    input_file = request.files.get('file')
    if not input_file:
        return BadRequest("File not present in request")

    filename = secure_filename(input_file.filename)
    if filename == '':