Exemple #1
0
def save_tf():
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)

    input_layer = tf.keras.layers.Input(
        [FLAGS.input_size, FLAGS.input_size, 3])
    feature_maps = YOLOv4(input_layer, NUM_CLASS)
    bbox_tensors = []
    prob_tensors = []

    for i, fm in enumerate(feature_maps):
        if i == 0:
            output_tensors = decode(fm, FLAGS.input_size // 8, NUM_CLASS,
                                    STRIDES, ANCHORS, i, XYSCALE)
        elif i == 1:
            output_tensors = decode(fm, FLAGS.input_size // 16, NUM_CLASS,
                                    STRIDES, ANCHORS, i, XYSCALE)
        else:
            output_tensors = decode(fm, FLAGS.input_size // 32, NUM_CLASS,
                                    STRIDES, ANCHORS, i, XYSCALE)
        bbox_tensors.append(output_tensors[0])
        prob_tensors.append(output_tensors[1])

    pred_bbox = tf.concat(bbox_tensors, axis=1)
    pred_prob = tf.concat(prob_tensors, axis=1)

    boxes, pred_conf = filter_boxes(pred_bbox,
                                    pred_prob,
                                    score_threshold=FLAGS.score_thres,
                                    input_shape=tf.constant(
                                        [FLAGS.input_size, FLAGS.input_size]))
    pred = tf.concat([boxes, pred_conf], axis=-1)
    model = tf.keras.Model(input_layer, pred)
    utils.load_weights(model, FLAGS.weights, FLAGS.model)
    model.summary()
    model.save(FLAGS.output)
Exemple #2
0
def save_tf():
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    print("load_config XYSCALE:{}".format(XYSCALE))
    input_layer = tf.keras.layers.Input(
        [FLAGS.input_size, FLAGS.input_size, 3])
    feature_maps = YOLO(input_layer, NUM_CLASS, FLAGS.model,
                        FLAGS.num_detection_layer)
    bbox_tensors = []
    prob_tensors = []
    if FLAGS.num_detection_layer == 1:  # yolo-custom
        output_tensors = decode(feature_maps[0], FLAGS.input_size // 32,
                                NUM_CLASS, STRIDES, ANCHORS, 0, XYSCALE,
                                FLAGS.framework)
        bbox_tensors.append(output_tensors[0])
        prob_tensors.append(output_tensors[1])
    elif FLAGS.num_detection_layer == 2:  # yolo-tiny
        for i, fm in enumerate(feature_maps):
            if i == 0:
                output_tensors = decode(fm, FLAGS.input_size // 16, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            else:
                output_tensors = decode(fm, FLAGS.input_size // 32, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            bbox_tensors.append(output_tensors[0])
            prob_tensors.append(output_tensors[1])
    elif FLAGS.num_detection_layer == 3:  # yolo
        for i, fm in enumerate(feature_maps):
            print("i:{}".format(i))
            if i == 0:
                output_tensors = decode(fm, FLAGS.input_size // 8, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            elif i == 1:
                output_tensors = decode(fm, FLAGS.input_size // 16, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            else:
                output_tensors = decode(fm, FLAGS.input_size // 32, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            bbox_tensors.append(output_tensors[0])
            prob_tensors.append(output_tensors[1])
    pred_bbox = tf.concat(bbox_tensors, axis=1)
    pred_prob = tf.concat(prob_tensors, axis=1)
    if FLAGS.framework == 'tflite':
        pred = (pred_bbox, pred_prob)
    else:
        boxes, pred_conf = filter_boxes(
            pred_bbox,
            pred_prob,
            score_threshold=FLAGS.score_thres,
            input_shape=tf.constant([FLAGS.input_size, FLAGS.input_size]))
        pred = tf.concat([boxes, pred_conf], axis=-1)
    model = tf.keras.Model(input_layer, pred)
    utils.load_weights(model, FLAGS.weights, FLAGS.model,
                       FLAGS.num_detection_layer)
    model.summary()
    model.save(FLAGS.output)
Exemple #3
0
def load_net():
    print("start")

    # load configuration for object detector
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    video_path = FLAGS.video

    saved_model_loaded = None
    # load tflite model if flag is set
    if FLAGS.framework == 'tflite':
        interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
        interpreter.allocate_tensors()
        input_details = interpreter.get_input_details()
        output_details = interpreter.get_output_details()
        print(input_details)
        print(output_details)
    # otherwise load standard tensorflow saved model
    else:
        saved_model_loaded = tf.saved_model.load(FLAGS.weights,
                                                 tags=[tag_constants.SERVING])

    print("weights loaded")

    # begin video capture
    try:
        vid = cv2.VideoCapture(int(video_path))
    except:
        vid = cv2.VideoCapture(video_path)

    return vid, saved_model_loaded
Exemple #4
0
    def __init__(self,
                 FLAGS,
                 is_training: bool,
                 dataset_type: str = "converted_coco"):
        #def __init__(self, FLAGS, is_training: bool, dataset_type: str = "yolo"):
        self.tiny = FLAGS.tiny
        self.strides, self.anchors, NUM_CLASS, XYSCALE = utils.load_config(
            FLAGS)
        self.dataset_type = dataset_type

        self.annot_path = (cfg.TRAIN.ANNOT_PATH
                           if is_training else cfg.TEST.ANNOT_PATH)
        self.input_sizes = (cfg.TRAIN.INPUT_SIZE
                            if is_training else cfg.TEST.INPUT_SIZE)
        self.batch_size = (cfg.TRAIN.BATCH_SIZE
                           if is_training else cfg.TEST.BATCH_SIZE)
        self.data_aug = cfg.TRAIN.DATA_AUG if is_training else cfg.TEST.DATA_AUG

        self.train_input_sizes = cfg.TRAIN.INPUT_SIZE
        self.classes = utils.read_class_names(cfg.YOLO.CLASSES)
        self.num_classes = len(self.classes)
        self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE
        self.max_bbox_per_scale = 150

        self.annotations = self.load_annotations()
        self.num_samples = len(self.annotations)
        self.num_batchs = int(np.ceil(self.num_samples / self.batch_size))
        self.batch_count = 0
    def __init__(self):
        """Initialize class object"""
        self._lock = threading.Lock()

        with open('./data/classes/coco.names', "r") as f:
            self._labelList = [l.rstrip() for l in f]

        config = ConfigProto()
        session = InteractiveSession(config=config)
        STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)

        self.input_size = FLAGS.size

        self.interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
        self.interpreter.allocate_tensors()
        self.input_details = self.interpreter.get_input_details()
        self.output_details = self.interpreter.get_output_details()

        # Connect to local, edge Blob Storage
        self._local_account = os.getenv("LOCAL_STORAGE_ACCOUNT_NAME",
                                        "UNKNOWN_NAME")
        self._local_blob_key = os.getenv("LOCAL_STORAGE_ACCOUNT_KEY",
                                         "UNKNOWN_KEY")
        self.local_container_name_annotated = os.getenv(
            "LOCAL_STORAGE_CONTAINER_NAME_ANNOTATED", "UNKNOWN_NAME")
        self.local_container_name_lowconf = os.getenv(
            "LOCAL_STORAGE_CONTAINER_NAME_LOWCONF", "UNKNOWN_NAME")
        super_str = 'DefaultEndpointsProtocol=http;AccountName=' + \
            self._local_account + \
            ';AccountKey=' + self._local_blob_key + \
            ';BlobEndpoint=http://azureblobstorageoniotedge:11002/' + \
            self._local_account + ';'
        self.blob_service_client = BlobServiceClient.from_connection_string(
            super_str, api_version='2019-07-07')
def main(_argv):
    """
    r = redis.Redis(host='localhost', port=6379, db=0)
    pipe = r.pubsub()
    pipe.subscribe({'birdEvents': my_handler})
    """
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)

    input_size = FLAGS.size
    saved_model_loaded = tf.saved_model.load(FLAGS.weights,
                                             tags=[tag_constants.SERVING])
    yolo_classifier = YoloClassifier(saved_model_loaded, input_size)
    species_classifier = SpeciesClassifier("../best_model_inception.hdf5")
    new_vid = True
    while True:
        # Poll for a new video and load it
        yolo_classifier.load_video("vid07-02-2020_08:29:08.avi")
        yolo_classifier.classify()

        species_classifier.classify(yolo_classifier.get_crops(),
                                    yolo_classifier.confidence_arr)
        # Save information about species in DB
        yolo_classifier.clear_data()
        species_classifier.reset_pred_dict()
def pred_dir(score=0.25):
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = FLAGS.size
    image_dir_path = FLAGS.image

    print("loading Model ...")
    saved_model_loaded = tf.saved_model.load(FLAGS.weights,
                                             tags=[tag_constants.SERVING])
    infer = saved_model_loaded.signatures['serving_default']

    imgs = os.listdir(image_dir_path)
    out_list = []
    for img in imgs:
        if img.split(".")[-1] == 'jpg' or img.split(".")[-1] == 'png':

            original_image = cv2.imread(image_dir_path + "/" + img)
            original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)

            # image_data = utils.image_preprocess(np.copy(original_image), [input_size, input_size])
            image_data = cv2.resize(original_image, (input_size, input_size))
            image_data = image_data / 255.
            # image_data = image_data[np.newaxis, ...].astype(np.float32)

            images_data = []
            for i in range(1):
                images_data.append(image_data)
            images_data = np.asarray(images_data).astype(np.float32)

            batch_data = tf.constant(images_data)
            pred_bbox = infer(batch_data)
            for key, value in pred_bbox.items():
                boxes = value[:, :, 0:4]
                pred_conf = value[:, :, 4:]

            boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
                boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
                scores=tf.reshape(
                    pred_conf,
                    (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
                max_output_size_per_class=50,
                max_total_size=50,
                iou_threshold=FLAGS.iou,
                score_threshold=score)
            pred_bbox = [
                boxes.numpy(),
                scores.numpy(),
                classes.numpy(),
                valid_detections.numpy()
            ]
            image, exist_classes = utils.draw_bbox(original_image, pred_bbox)
            # image = utils.draw_bbox(image_data*255, pred_bbox)
            image = Image.fromarray(image.astype(np.uint8))
            image.show()
            image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
            cv2.imwrite("./pred_result/" + img, image)
            print(img, exist_classes)
            out_list.append([img, exist_classes])
Exemple #8
0
def load_obj_detector_cfg():
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = FLAGS.size
    video_path = FLAGS.video
    return input_size, video_path
def save_tf():
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)

    input_layer = tf.keras.layers.Input(
        [FLAGS.input_size, FLAGS.input_size, 3])
    feature_maps = YOLO(input_layer, NUM_CLASS, FLAGS.model, FLAGS.tiny)
    bbox_tensors = []
    prob_tensors = []
    if FLAGS.tiny:
        for i, fm in enumerate(feature_maps):
            if i == 0:
                output_tensors = decode(fm, FLAGS.input_size // 16, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            else:
                output_tensors = decode(fm, FLAGS.input_size // 32, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            bbox_tensors.append(output_tensors[0])
            prob_tensors.append(output_tensors[1])
    else:
        for i, fm in enumerate(feature_maps):
            if i == 0:
                output_tensors = decode(fm, FLAGS.input_size // 8, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            elif i == 1:
                output_tensors = decode(fm, FLAGS.input_size // 16, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            else:
                output_tensors = decode(fm, FLAGS.input_size // 32, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            bbox_tensors.append(output_tensors[0])
            prob_tensors.append(output_tensors[1])
    pred_bbox = tf.concat(bbox_tensors, axis=1)
    pred_prob = tf.concat(prob_tensors, axis=1)
    if FLAGS.framework == 'tflite':
        pred = (pred_bbox, pred_prob)
    else:
        boxes, pred_conf = filter_boxes(
            pred_bbox,
            pred_prob,
            score_threshold=FLAGS.score_thres,
            input_shape=tf.constant([FLAGS.input_size, FLAGS.input_size]))
        pred = tf.concat([boxes, pred_conf], axis=-1)
    model = tf.keras.Model(input_layer, pred)
    model.load_weights(FLAGS.weights)
    #utils.load_weights(model, FLAGS.weights, FLAGS.model, FLAGS.tiny) #weight파일일 경

    #model.summary()
    #model.save('/checkpoints/yolov4-416')
    #model.save(FLAGS.output, save_format = 'tf')
    tf.saved_model.save(
        model, FLAGS.output
    )  #현재 이 저장 형태의 경우: assets/, variables/, saved_model.pb 형태로 저장됨.
def save_tf():
    if FLAGS.license:
        cfg.YOLO.CLASSES = "./data/classes/custom.names"
    else:
        cfg.YOLO.CLASSES = "./data/classes/char.names"
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    #print(read_class_names(cfg.YOLO.CLASSES))

    input_layer = tf.keras.layers.Input(
        [FLAGS.input_size, FLAGS.input_size, 3])
    feature_maps = YOLO(input_layer, NUM_CLASS, FLAGS.model, FLAGS.tiny)
    bbox_tensors = []
    prob_tensors = []
    if FLAGS.tiny:
        for i, fm in enumerate(feature_maps):
            if i == 0:
                output_tensors = decode(fm, FLAGS.input_size // 16, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            else:
                output_tensors = decode(fm, FLAGS.input_size // 32, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            bbox_tensors.append(output_tensors[0])
            prob_tensors.append(output_tensors[1])
    else:
        for i, fm in enumerate(feature_maps):
            if i == 0:
                output_tensors = decode(fm, FLAGS.input_size // 8, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            elif i == 1:
                output_tensors = decode(fm, FLAGS.input_size // 16, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            else:
                output_tensors = decode(fm, FLAGS.input_size // 32, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            bbox_tensors.append(output_tensors[0])
            prob_tensors.append(output_tensors[1])
    pred_bbox = tf.concat(bbox_tensors, axis=1)
    pred_prob = tf.concat(prob_tensors, axis=1)
    if FLAGS.framework == 'tflite':
        pred = (pred_bbox, pred_prob)
    else:
        boxes, pred_conf = filter_boxes(
            pred_bbox,
            pred_prob,
            score_threshold=FLAGS.score_thres,
            input_shape=tf.constant([FLAGS.input_size, FLAGS.input_size]))
        pred = tf.concat([boxes, pred_conf], axis=-1)
    model = tf.keras.Model(input_layer, pred)
    utils.load_weights(model, FLAGS.weights, FLAGS.model, FLAGS.tiny)
    model.summary()
    model.save(FLAGS.output)
def save_tf():
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_layer = tf.keras.layers.Input(
        [FLAGS.input_size, FLAGS.input_size, 3])
    feature_maps = YOLO(input_layer, NUM_CLASS, FLAGS.model, FLAGS.tiny)
    bbox_tensors = []
    prob_tensors = []
    if FLAGS.tiny:
        for i, fm in enumerate(feature_maps):
            if i == 0:
                output_tensors = decode(fm, FLAGS.input_size // 16, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            else:
                output_tensors = decode(fm, FLAGS.input_size // 32, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            bbox_tensors.append(output_tensors[0])
            prob_tensors.append(output_tensors[1])
    else:
        for i, fm in enumerate(feature_maps):
            if i == 0:
                output_tensors = decode(fm, FLAGS.input_size // 8, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            elif i == 1:
                output_tensors = decode(fm, FLAGS.input_size // 16, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            else:
                output_tensors = decode(fm, FLAGS.input_size // 32, NUM_CLASS,
                                        STRIDES, ANCHORS, i, XYSCALE,
                                        FLAGS.framework)
            bbox_tensors.append(output_tensors[0])
            prob_tensors.append(output_tensors[1])

    pred_bbox = tf.concat(bbox_tensors, axis=1)
    pred_prob = tf.concat(prob_tensors, axis=1)

    if FLAGS.framework == 'tflite':
        pred = (pred_bbox, pred_prob)
    else:
        boxes, pred_conf = filter_boxes(
            pred_bbox,
            pred_prob,
            score_threshold=FLAGS.score_thres,
            input_shape=tf.constant([FLAGS.input_size, FLAGS.input_size]))
        pred = tf.concat([boxes, pred_conf], axis=-1)

    model = tf.keras.Model(input_layer, pred)
    model.load_weights(FLAGS.input_model_path)
    model.summary()
    # model.save(FLAGS.output_model_path)
    return model
Exemple #12
0
def save_tf(parameters):
    """Transform a darknet model of YOLO to a TensorFlow model

    Args:
        parameters (dictionary): input parameters
        - weights: path to the darknet weights
        - input_size: input size of the model
        - model: model to transform
        - weights_tf: path to save the tf weights
    Returns:
        [void]:
    """
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(
        tiny=False, model=parameters['model'])

    input_layer = tf.keras.layers.Input(
        [parameters['input_size'], parameters['input_size'], 3])
    feature_maps = YOLO(input_layer, NUM_CLASS, parameters['model'], False)
    bbox_tensors = []
    prob_tensors = []
    for i, fm in enumerate(feature_maps):
        if i == 0:
            output_tensors = decode(fm, parameters['input_size'] // 8,
                                    NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE,
                                    'tf')
        elif i == 1:
            output_tensors = decode(fm, parameters['input_size'] // 16,
                                    NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE,
                                    'tf')
        else:
            output_tensors = decode(fm, parameters['input_size'] // 32,
                                    NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE,
                                    'tf')
        bbox_tensors.append(output_tensors[0])
        prob_tensors.append(output_tensors[1])
    pred_bbox = tf.concat(bbox_tensors, axis=1)
    pred_prob = tf.concat(prob_tensors, axis=1)

    boxes, pred_conf = filter_boxes(pred_bbox,
                                    pred_prob,
                                    score_threshold=parameters['score_thres'],
                                    input_shape=tf.constant([
                                        parameters['input_size'],
                                        parameters['input_size']
                                    ]))
    pred = tf.concat([boxes, pred_conf], axis=-1)
    model = tf.keras.Model(input_layer, pred)
    utils.load_weights(model, parameters['weights'], parameters['model'],
                       False)
    model.summary()
    model.save(parameters['weights_tf'])
    def get_bbox(self, image_bytes):

        STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
        input_size = CONFIG.image_size
        cameraId = image_bytes["CameraId"]
        nparr = np.frombuffer(image_bytes["ImageBytes"], np.uint8)
        original_image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
        # print("Image shape: ",original_image.shape)
        image_h, image_w, _ = original_image.shape
        image_data = cv2.resize(original_image, (input_size[0], input_size[1]))
        image_data = image_data / 255.

        images_data = []
        for i in range(1):
            images_data.append(image_data)
        images_data = np.asarray(images_data).astype(np.float32)

        self.interpreter.allocate_tensors()
        input_details = self.interpreter.get_input_details()
        output_details = self.interpreter.get_output_details()
        self.interpreter.set_tensor(input_details[0]['index'], images_data)
        self.interpreter.invoke()
        pred = [
            self.interpreter.get_tensor(output_details[i]['index'])
            for i in range(len(output_details))
        ]
        boxes, pred_conf = filter_boxes(pred[0],
                                        pred[1],
                                        score_threshold=0.25,
                                        input_shape=tf.constant(
                                            [input_size[0], input_size[1]]))

        boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
            boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
            scores=tf.reshape(
                pred_conf,
                (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
            max_output_size_per_class=50,
            max_total_size=50,
            iou_threshold=CONFIG.iou,
            score_threshold=CONFIG.score)
        pred_bbox = [
            boxes.numpy(),
            scores.numpy(),
            classes.numpy(),
            valid_detections.numpy()
        ]
        detections = utils.bbox_details(original_image, pred_bbox)

        return original_image, detections, classes.numpy()
Exemple #14
0
def main(_argv):
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = FLAGS.size
    image_path = FLAGS.image

    saved_model_loaded = tf.saved_model.load(FLAGS.weights, tags=[tag_constants.SERVING])
    infer = saved_model_loaded.signatures['serving_default']

    prev_time = time.time()
    original_image = cv2.imread(image_path)
    original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)

    image_data = cv2.resize(original_image, (input_size, input_size))
    image_data = image_data / 255.

    images_data = []
    for i in range(1):
        images_data.append(image_data)
    images_data = np.asarray(images_data).astype(np.float32)

    batch_data = tf.constant(images_data)
    pred_bbox = infer(batch_data)
    for key, value in pred_bbox.items():
        boxes = value[:, :, 0:4]
        pred_conf = value[:, :, 4:]

    boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
        boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
        scores=tf.reshape(
            pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
        max_output_size_per_class=50,
        max_total_size=50,
        iou_threshold=FLAGS.iou,
        score_threshold=FLAGS.score
    )

    pred_bbox = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()]
    image = utils.draw_bbox(original_image, pred_bbox)
    cur_time = time.time()
    exec_time = cur_time - prev_time
    time_info = "Inference time: %.2f ms" %(1000*exec_time)
    print(time_info)

    image = Image.fromarray(image.astype(np.uint8))
    image.show()
    image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
    cv2.imwrite(FLAGS.output, image)
Exemple #15
0
def load_settings(config):
    settings = load_config(config)
    db = motor.MotorClient(config.MONGODB_URI)['test_database']  # 异步方式的DB对象
    dbsync = MongoClient(config.MONGODB_URI)['test_database']  # 同步方式的DB对象
    redis = tornadoredis.Client(host=config.REDIS_SETTING['host'],
                                port=config.REDIS_SETTING['port'],
                                selected_db=config.REDIS_SETTING['db'])
    redis.connect()

    settings.update({'db': db})
    settings.update({'dbsync': dbsync})
    settings.update({"cookie_secret": make_cookie_securety()})
    settings.update({'redis': redis})
    return settings
def main(_argv):
    config = ConfigProto()
    # config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = FLAGS.size
    image_path = FLAGS.image
    image_dir = FLAGS.dir

    filesNames = listdir(image_dir)

    for file in filesNames:
        print('./car/' + file)

        original_image = cv2.imread('./car/' + file)
        original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)

        image_data = cv2.resize(original_image, (input_size, input_size))
        image_data = image_data / 255.
        images_data = []
        for i in range(1):
            images_data.append(image_data)
        images_data = np.asarray(images_data).astype(np.float32)

        saved_model_loaded = tf.saved_model.load(FLAGS.weights,
                                                 tags=[tag_constants.SERVING])
        infer = saved_model_loaded.signatures['serving_default']
        batch_data = tf.constant(images_data)
        pred_bbox = infer(batch_data)
        for key, value in pred_bbox.items():
            boxes = value[:, :, 0:4]
            pred_conf = value[:, :, 4:]

        boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
            boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
            scores=tf.reshape(
                pred_conf,
                (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
            max_output_size_per_class=50,
            max_total_size=50,
            iou_threshold=FLAGS.iou,
            score_threshold=FLAGS.score)
        pred_bbox = [
            boxes.numpy(),
            scores.numpy(),
            classes.numpy(),
            valid_detections.numpy()
        ]
        print(pred_bbox[2])
Exemple #17
0
    def __init__(self,
                 FLAGS,
                 input_channel,
                 is_training: bool,
                 dataset_type: str = "yolo"):
        self.input_channel = input_channel
        self.strides, self.anchors, NUM_CLASS, XYSCALE = utils.load_config(
            FLAGS)
        print(
            f"self.strides: {self.strides}, self.anchors: {self.anchors}, NUM_CLASS: {NUM_CLASS}, XYSCALE: {XYSCALE}"
        )
        self.dataset_type = dataset_type

        self.annot_path = (cfg.TRAIN.ANNOT_PATH
                           if is_training else cfg.TEST.ANNOT_PATH)
        self.input_sizes = (cfg.TRAIN.INPUT_SIZE
                            if is_training else cfg.TEST.INPUT_SIZE)
        self.batch_size = (cfg.TRAIN.BATCH_SIZE
                           if is_training else cfg.TEST.BATCH_SIZE)
        self.data_aug = cfg.TRAIN.DATA_AUG if is_training else cfg.TEST.DATA_AUG

        self._input = ImageShape((cfg.TRAIN.INPUT_SIZE, cfg.TRAIN.INPUT_SIZE, input_channel))  if is_training \
            else ImageShape((cfg.TEST.INPUT_SIZE, cfg.TEST.INPUT_SIZE, input_channel))  # (y, x, c)
        output_shape = None
        self._output = ImageShape(
            output_shape) if output_shape is not None else self._input
        self.num_detection_layers = cfg.YOLO.NUM_YOLOLAYERS

        self.classes = utils.read_class_names(cfg.YOLO.CLASSES)
        self.num_classes = len(self.classes)
        self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE
        self.num_detection_layers = cfg.YOLO.NUM_YOLOLAYERS
        self.max_bbox_per_scale = 150

        self.train_input_sizes = cfg.TRAIN.INPUT_SIZE if is_training else cfg.TEST.INPUT_SIZE
        self.train_input_size = cfg.TRAIN.INPUT_SIZE if is_training else cfg.TEST.INPUT_SIZE
        self.train_output_sizes = self.train_input_size // self.strides  # big -> small
        self._train_images = []
        self._train_labels = []

        self.load_train_label()
        self.steps_for_train = int(len(self._train_images) / self.batch_size)
        # self.annotations = self.load_annotations()
        self.num_samples = len(self._train_images)
        self.num_batchs = int(np.ceil(self.num_samples / self.batch_size))
        self.batch_count = 0
Exemple #18
0
    def setYoloTensor(self):
        #detect plate with tensorflow framework and GPU device

        config = ConfigProto()
        config.gpu_options.allow_growth = True
        session = InteractiveSession(config=config)
        STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(self.FLAG)

        start_time = time.time()
        print("Loading started")

        self.saved_model_loaded = tf.saved_model.load(
            self.FLAG.weights, tags=[tag_constants.SERVING])
        self.infer = self.saved_model_loaded.signatures['serving_default']

        end_time = time.time() - start_time
        print("Model loaded in: ", end_time)
    def __init__(self):
        """Initialize class object"""
        self._lock = threading.Lock()

        with open('./data/classes/coco.names', "r") as f:
            self._labelList = [l.rstrip() for l in f]

        config = ConfigProto()
        session = InteractiveSession(config=config)
        STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)

        self.input_size = FLAGS.size

        self.interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
        self.interpreter.allocate_tensors()
        self.input_details = self.interpreter.get_input_details()
        self.output_details = self.interpreter.get_output_details()
def main(_argv):
    print("initializing services")
    # Video Settings
    # set 0 for camera
    video_src = '../../data/videos/demo.mp4'
    resolution_width = 608
    resolution_height = 608
    dim = (resolution_width, resolution_height)

    video = cv2.VideoCapture(video_src)
    video.set(cv2.CAP_PROP_AUTOFOCUS, 1)

    #tensorflow loading
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = 608
    images = FLAGS.images

    #MidasEstimator = depth.construct_midas_large()

    while video.isOpened():
        frame_time = datetime.datetime.now()
        success, frame = video.read()
        if not success:
            break
        frame = cv2.resize(frame, dim)

        executor = ThreadPoolExecutor(max_workers=2)
        #depth_value = executor.submit(get_depth(MidasEstimator,frame))
        objects = executor.submit(
            get_object(_argv, frame, config, session, input_size, images))

        #Calculate the depth map
        #depth_value = get_depth(MidasEstimator,frame)
        #depth_map = 1.0/depth_value
        #depth_visualization(depth_map, depth_value, frame)

        #objects = get_object(_argv, frame, config, session, input_size, images)

        #print(depth_value)
        #print(objects)

        print(frame_time)
Exemple #21
0
def main(_argv):
    config = tf.compat.v1.ConfigProto()
    # config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = tf.compat.v1.InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)

    saved_model_loaded = tf.saved_model.load(FLAGS.weights, tags=[tag_constants.SERVING])
    model = saved_model_loaded.signatures['serving_default']
    input_size = FLAGS.size
    path = FLAGS.image_path
    img_list = os.listdir(path)
    x = time.time()
    for name in img_list:
        img_path = '/'.join([path, name])
        detect(model, img_path, name, input_size)
    x2 = time.time()
    print('Done in %d'%(x2-x))
    def __init__(
            self,
            tfmodel_path="tf-model",
            random_colors=False,
            framework='tf',  # '(tf, tflite, trt')
            size=416,
            probability_minimum=0.5,
            threshold=0.5):

        self.weights = tfmodel_path
        self.random_colors = random_colors
        self.probability_minimum = probability_minimum
        self.threshold = threshold
        self.size = size
        self.framework = framework

        self.FLAGS = Flags()

        config = ConfigProto()
        config.gpu_options.allow_growth = True
        session = InteractiveSession(config=config)
        STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(self.FLAGS)

        # load model
        if self.framework == 'tflite':
            self.interpreter = tf.lite.Interpreter(model_path=self.weights)
        else:
            self.saved_model_loaded = tf.saved_model.load(
                self.weights, tags=[tag_constants.SERVING])

        # Generate colors for representing every detected object
        # with function randint(low, high=None, size=None, dtype='l')

        class_names = utils.read_class_names(cfg.YOLO.CLASSES)
        print(class_names)

        if random_colors:
            self.colors = np.random.randint(0,
                                            255,
                                            size=(len(class_names), 3),
                                            dtype='uint8')
        else:
            self.colors = array = np.array([[0, 255, 0]
                                            for _ in range(len(class_names))])
Exemple #23
0
    def __init__(self,arg):
        self.im_data = []
        
        

        STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
        input_size = FLAGS.size
        image_path = FLAGS.image
    
        image = cv2.imread(image_path)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        image_data = cv2.resize(original_image, (input_size, input_size))
        image_data = image_data / 255.
    
        images_data = []
        for i in range(1):
            self.im_data.append(image_data)
            self.im_data = np.asarray(images_data).astype(np.float32)
Exemple #24
0
def evaluate_line(input_str):
    config = load_config(file_path + "/" + FLAGS.config_file)
    logger = get_logger(FLAGS.log_file)
    tf_config = tf.ConfigProto()
    tf_config.gpu_options.allow_growth = True
    with open(file_path + "/" + FLAGS.map_file, "rb") as f:
        char_to_id, id_to_char, tag_to_id, id_to_tag = pickle.load(f)
    with tf.Session(config=tf_config) as sess:
        model = create_model(sess, Model, file_path + "/" + FLAGS.ckpt_path,
                             load_word2vec, config, id_to_char, logger)
        # for report in input_str:
        #     result = model.evaluate_line(sess, input_from_line(report, char_to_id), id_to_tag)
        #     print(result)

        report = input_str
        result = model.evaluate_line(sess, input_from_line(report, char_to_id),
                                     id_to_tag)
        print(result)
        return result
def main(_argv):
    flags.DEFINE_boolean('tiny', False, 'yolo or yolo-tiny')
    flags.DEFINE_string('model', 'yolov4', 'yolov3 or yolov4')
    flags.DEFINE_list('images', './data/images/kite.jpg',
                      'path to input image')
    flags.DEFINE_string('output', './detections/', 'path to output folder')
    flags.DEFINE_float('iou', 0.45, 'iou threshold')
    flags.DEFINE_float('score', 0.25, 'score threshold')
    flags.DEFINE_boolean('dont_show', False, 'dont show image output')

    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = 608
    images = FLAGS.images

    #load model
    model_path = "./yolov4-608"
    saved_model_loaded = tf.saved_model.load(model_path,
                                             tags=[tag_constants.SERVING])
Exemple #26
0
def initial():
    global saved_model_loaded, infer
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config()

    # original_image = cv2.imread(image_path)
    # original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)

    # image_data = utils.image_preprocess(np.copy(original_image), [input_size, input_size])
    # image_data = cv2.resize(original_image, (input_size, input_size))
    # image_data = image_data / 255.
    # image_data = image_data[np.newaxis, ...].astype(np.float32)

    # images_data = []
    # for i in range(1):
    #     images_data.append(image_data)
    # images_data = np.asarray(images_data).astype(np.float32)

    saved_model_loaded = tf.saved_model.load(export_dir=weights,
                                             tags=[tag_constants.SERVING])
    infer = saved_model_loaded.signatures['serving_default']
Exemple #27
0
def main(_argv):
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)

    input_file = FLAGS.input
    output_folder = FLAGS.output

    print('Input video: ' + input_file)
    print('Output frames directory: ' + output_folder)

    if not (path.exists(output_folder)):
        os.mkdir(output_folder)

    vid_cap = cv2.VideoCapture(input_file)
    success, image = vid_cap.read()
    count = 0
    if success:
        print('Video successful loaded. Extracting frames...')
    while success:
        cv2.imwrite('{}/frame{}.jpg'.format(output_folder, count),
                    image)  # Save frame as JPEG file
        success, image = vid_cap.read()
        print('Read a new frame: {}'.format(count))
        count += 1
def main(args):
    cfg = load_config(args.cfg)
    pipeline = TimeSeriesTrainer(cfg)
    if args.ckpt is not None:
        pipeline.load_state_dict(
            torch.load(args.ckpt, map_location="cpu")["state_dict"]
        )

    # checkpoint_callback
    checkpoint_callback = pl.callbacks.ModelCheckpoint(
        filepath=args.output_dir,
        save_top_k=True,
        save_last=True,
        verbose=True,
        monitor='loss_val',
        mode='min'
    )

    # create trainer
    trainer = pl.Trainer(
        gradient_clip_val=0.01,
        gpus=args.gpus,
        logger=None,
        max_epochs=cfg.pipeline.epochs,
        accumulate_grad_batches=args.grad_batches,
        distributed_backend=args.distributed_backend,
        precision=32,
        checkpoint_callback=checkpoint_callback,
        val_check_interval=args.val_check_interval,
    )
    if args.test:
        trainer.test(pipeline)
    elif args.to_onnx is not None:
        pipeline.to_onnx(args.to_onnx, args.onnx_batch_size)
    else:
        trainer.fit(pipeline)
Exemple #29
0
def main(_argv):
    physical_devices = tf.config.experimental.list_physical_devices('GPU')
    # if len(physical_devices) > 0:
    if physical_devices:
        # tf.config.experimental.set_memory_growth(physical_devices[0], True)
        tf.config.experimental.set_visible_devices(physical_devices[0], "GPU")

    trainset = Dataset(FLAGS, is_training=True)
    testset = Dataset(FLAGS, is_training=False)
    logdir = "./data/log"
    isfreeze = False
    steps_per_epoch = len(trainset)
    first_stage_epochs = cfg.TRAIN.FISRT_STAGE_EPOCHS
    second_stage_epochs = cfg.TRAIN.SECOND_STAGE_EPOCHS
    global_steps = tf.Variable(1, trainable=False, dtype=tf.int64)
    warmup_steps = cfg.TRAIN.WARMUP_EPOCHS * steps_per_epoch
    total_steps = (first_stage_epochs + second_stage_epochs) * steps_per_epoch
    # train_steps = (first_stage_epochs + second_stage_epochs) * steps_per_period

    input_layer = tf.keras.layers.Input([cfg.TRAIN.INPUT_SIZE, cfg.TRAIN.INPUT_SIZE, 3])
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    IOU_LOSS_THRESH = cfg.YOLO.IOU_LOSS_THRESH

    freeze_layers = utils.load_freeze_layer(FLAGS.model, FLAGS.tiny)

    feature_maps = YOLO(input_layer, NUM_CLASS, FLAGS.model, FLAGS.tiny)
    if FLAGS.tiny:
        bbox_tensors = []
        for i, fm in enumerate(feature_maps):
            if i == 0:
                bbox_tensor = decode_train(fm, cfg.TRAIN.INPUT_SIZE // 16, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE)
            else:
                bbox_tensor = decode_train(fm, cfg.TRAIN.INPUT_SIZE // 32, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE)
            bbox_tensors.append(fm)
            bbox_tensors.append(bbox_tensor)
    else:
        bbox_tensors = []
        for i, fm in enumerate(feature_maps):
            if i == 0:
                bbox_tensor = decode_train(fm, cfg.TRAIN.INPUT_SIZE // 8, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE)
            elif i == 1:
                bbox_tensor = decode_train(fm, cfg.TRAIN.INPUT_SIZE // 16, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE)
            else:
                bbox_tensor = decode_train(fm, cfg.TRAIN.INPUT_SIZE // 32, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE)
            bbox_tensors.append(fm)
            bbox_tensors.append(bbox_tensor)

    model = tf.keras.Model(input_layer, bbox_tensors)
    model.summary()

    if FLAGS.weights == None:
        print("Training from scratch")
    else:
        if FLAGS.weights.split(".")[len(FLAGS.weights.split(".")) - 1] == "weights":
            utils.load_weights(model, FLAGS.weights, FLAGS.model, FLAGS.tiny)
        else:
            model.load_weights(FLAGS.weights)
        print('Restoring weights from: %s ... ' % FLAGS.weights)


    optimizer = tf.keras.optimizers.Adam()
    if os.path.exists(logdir): shutil.rmtree(logdir)
    writer = tf.summary.create_file_writer(logdir)

    # define training step function
    # @tf.function
    def train_step(image_data, target):
        with tf.GradientTape() as tape:
            pred_result = model(image_data, training=True)
            giou_loss = conf_loss = prob_loss = 0

            # optimizing process
            for i in range(len(freeze_layers)):
                conv, pred = pred_result[i * 2], pred_result[i * 2 + 1]
                loss_items = compute_loss(pred, conv, target[i][0], target[i][1], STRIDES=STRIDES, NUM_CLASS=NUM_CLASS, IOU_LOSS_THRESH=IOU_LOSS_THRESH, i=i)
                giou_loss += loss_items[0]
                conf_loss += loss_items[1]
                prob_loss += loss_items[2]

            total_loss = giou_loss + conf_loss + prob_loss

            gradients = tape.gradient(total_loss, model.trainable_variables)
            optimizer.apply_gradients(zip(gradients, model.trainable_variables))
            tf.print("=> STEP %4d/%4d   lr: %.6f   giou_loss: %4.2f   conf_loss: %4.2f   "
                     "prob_loss: %4.2f   total_loss: %4.2f" % (global_steps, total_steps, optimizer.lr.numpy(),
                                                               giou_loss, conf_loss,
                                                               prob_loss, total_loss))
            # update learning rate
            global_steps.assign_add(1)
            if global_steps < warmup_steps:
                lr = global_steps / warmup_steps * cfg.TRAIN.LR_INIT
            else:
                lr = cfg.TRAIN.LR_END + 0.5 * (cfg.TRAIN.LR_INIT - cfg.TRAIN.LR_END) * (
                    (1 + tf.cos((global_steps - warmup_steps) / (total_steps - warmup_steps) * np.pi))
                )
            optimizer.lr.assign(lr.numpy())

            # writing summary data
            with writer.as_default():
                tf.summary.scalar("lr", optimizer.lr, step=global_steps)
                tf.summary.scalar("loss/total_loss", total_loss, step=global_steps)
                tf.summary.scalar("loss/giou_loss", giou_loss, step=global_steps)
                tf.summary.scalar("loss/conf_loss", conf_loss, step=global_steps)
                tf.summary.scalar("loss/prob_loss", prob_loss, step=global_steps)
            writer.flush()
    def test_step(image_data, target):
        with tf.GradientTape() as tape:
            pred_result = model(image_data, training=True)
            giou_loss = conf_loss = prob_loss = 0

            # optimizing process
            for i in range(len(freeze_layers)):
                conv, pred = pred_result[i * 2], pred_result[i * 2 + 1]
                loss_items = compute_loss(pred, conv, target[i][0], target[i][1], STRIDES=STRIDES, NUM_CLASS=NUM_CLASS, IOU_LOSS_THRESH=IOU_LOSS_THRESH, i=i)
                giou_loss += loss_items[0]
                conf_loss += loss_items[1]
                prob_loss += loss_items[2]

            total_loss = giou_loss + conf_loss + prob_loss

            tf.print("=> TEST STEP %4d   giou_loss: %4.2f   conf_loss: %4.2f   "
                     "prob_loss: %4.2f   total_loss: %4.2f" % (global_steps, giou_loss, conf_loss,
                                                               prob_loss, total_loss))

    for epoch in range(first_stage_epochs + second_stage_epochs):
        if epoch < first_stage_epochs:
            if not isfreeze:
                isfreeze = True
                for name in freeze_layers:
                    freeze = model.get_layer(name)
                    freeze_all(freeze)
        elif epoch >= first_stage_epochs:
            if isfreeze:
                isfreeze = False
                for name in freeze_layers:
                    freeze = model.get_layer(name)
                    unfreeze_all(freeze)
        for image_data, target in trainset:
            train_step(image_data, target)
        for image_data, target in testset:
            test_step(image_data, target)
        model.save_weights("./checkpoints/yolov4")
def main(_argv):
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = FLAGS.size
    images = FLAGS.images

    # load model
    if FLAGS.framework == 'tflite':
            interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
    else:
            saved_model_loaded = tf.saved_model.load(FLAGS.weights, tags=[tag_constants.SERVING])

    # loop through images in list and run Yolov4 model on each
    for count, image_path in enumerate(images, 1):
        original_image = cv2.imread(image_path)
        original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)

        image_data = cv2.resize(original_image, (input_size, input_size))
        image_data = image_data / 255.

        images_data = []
        for i in range(1):
            images_data.append(image_data)
        images_data = np.asarray(images_data).astype(np.float32)

        if FLAGS.framework == 'tflite':
            interpreter.allocate_tensors()
            input_details = interpreter.get_input_details()
            output_details = interpreter.get_output_details()
            print(input_details)
            print(output_details)
            interpreter.set_tensor(input_details[0]['index'], images_data)
            interpreter.invoke()
            pred = [interpreter.get_tensor(output_details[i]['index']) for i in range(len(output_details))]
            if FLAGS.model == 'yolov3' and FLAGS.tiny == True:
                boxes, pred_conf = filter_boxes(pred[1], pred[0], score_threshold=0.25, input_shape=tf.constant([input_size, input_size]))
            else:
                boxes, pred_conf = filter_boxes(pred[0], pred[1], score_threshold=0.25, input_shape=tf.constant([input_size, input_size]))
        else:
            infer = saved_model_loaded.signatures['serving_default']
            batch_data = tf.constant(images_data)
            pred_bbox = infer(batch_data)
            for key, value in pred_bbox.items():
                boxes = value[:, :, 0:4]
                pred_conf = value[:, :, 4:]

        boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
            boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
            scores=tf.reshape(
                pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
            max_output_size_per_class=5,
            max_total_size=10,
            iou_threshold=FLAGS.iou,
            score_threshold=FLAGS.score,
        )
        pred_bbox = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()]

        # read in all class names from config
        class_names = utils.read_class_names(cfg.YOLO.CLASSES)

        # by default allow all classes in .names file
        allowed_classes = list(class_names.values())
        
        # custom allowed classes (uncomment line below to allow detections for only people)
        #allowed_classes = ['person']

        image = utils.draw_bbox(original_image, pred_bbox, allowed_classes = allowed_classes)

        image = Image.fromarray(image.astype(np.uint8))

        if not FLAGS.dont_show:
            image.show()
        image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
        cv2.imwrite(FLAGS.output + 'detection' + str(count) + '.png', image)

        # ========== voiceFeedback ==========

        valid_items = pred_bbox[3][0]
        valid_classes = pred_bbox[2][0]
        valid_boxes = pred_bbox[0][0]
        # section = (input_size/3)
        (H, W) = original_image.shape[:2]
        res = []
        for i in range(valid_items):
            (top, left, bottom, right) = valid_boxes[i]

            centerX = round((right + left)/2)
            centerY = round((top + bottom)/2)
            if centerX <= W/3:
                w_pos = 'left '
            elif centerX <= (W/3 * 2):
                w_pos = 'center '
            else:
                w_pos = 'right '

            if centerY <= H/3:
                h_pos = 'top '
            elif centerY <= (H/3 * 2):
                h_pos = 'mid '
            else:
                h_pos = 'bottom '
            res.append(h_pos + w_pos + allowed_classes[int(valid_classes[i])])

        description = ', '.join(res)

        tts = gTTS(text=description, lang="en", slow=False)
        filename = f'./detections/voice{count}.mp3'
        tts.save(filename)
        playsound.playsound(filename)
Exemple #31
0
def main(_argv):
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = FLAGS.size
    images = FLAGS.images

    # load model
    if FLAGS.framework == 'tflite':
            interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
    else:
            saved_model_loaded = tf.saved_model.load(FLAGS.weights, tags=[tag_constants.SERVING])

    # loop through images in list and run Yolov4 model on each
    for count, image_path in enumerate(images, 1):
        original_image = cv2.imread(image_path)
        original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)

        image_data = cv2.resize(original_image, (input_size, input_size))
        image_data = image_data / 255.

        images_data = []
        for i in range(1):
            images_data.append(image_data)
        images_data = np.asarray(images_data).astype(np.float32)

        if FLAGS.framework == 'tflite':
            interpreter.allocate_tensors()
            input_details = interpreter.get_input_details()
            output_details = interpreter.get_output_details()
            print(input_details)
            print(output_details)
            interpreter.set_tensor(input_details[0]['index'], images_data)
            interpreter.invoke()
            pred = [interpreter.get_tensor(output_details[i]['index']) for i in range(len(output_details))]
            if FLAGS.model == 'yolov3' and FLAGS.tiny == True:
                boxes, pred_conf = filter_boxes(pred[1], pred[0], score_threshold=0.25, input_shape=tf.constant([input_size, input_size]))
            else:
                boxes, pred_conf = filter_boxes(pred[0], pred[1], score_threshold=0.25, input_shape=tf.constant([input_size, input_size]))
        else:
            infer = saved_model_loaded.signatures['serving_default']
            batch_data = tf.constant(images_data)
            pred_bbox = infer(batch_data)
            for key, value in pred_bbox.items():
                boxes = value[:, :, 0:4]
                pred_conf = value[:, :, 4:]

        boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
            boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
            scores=tf.reshape(
                pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
            max_output_size_per_class=50,
            max_total_size=50,
            iou_threshold=FLAGS.iou,
            score_threshold=FLAGS.score
        )
        pred_bbox = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()]
        image = utils.draw_bbox(original_image, pred_bbox)
        # image = utils.draw_bbox(image_data*255, pred_bbox)
        image = Image.fromarray(image.astype(np.uint8))
        if not FLAGS.dont_show:
            image.show()
        image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
        cv2.imwrite(FLAGS.output + 'detection' + str(count) + '.png', image)