示例#1
0
    def __init__(self):

        self.counter = 0  # number of people
        self.violation = 0  # number of violations

        # The path of the anchor txt file.
        anchor_path = "./data/yolo_anchors.txt"
        # Resize the input image with `new_size`, size format: [width, height]
        self.new_size = [416, 416]
        # Whether to use the letterbox resize.
        self.letterbox_resizes = True
        # The path of the class names
        class_name_path = "./data/coco.names"
        # The path of the weights to restore
        restore_path = "./checkpoint/best_model_Epoch_75_step_29487_mAP_0.8609_loss_5.4903_lr_1e-05"
        # Whether to save the video detection results.
        self.save_video = True

        self.anchors = parse_anchors(anchor_path)

        self.classes = read_class_names(class_name_path)

        self.num_class = len(self.classes)

        self.color_table = get_color_table(
            self.num_class)  # color for each label

        self.tracker = Sort()
        self.memory = {}

        self.COLORS = np.random.randint(0, 255, size=(200, 3),
                                        dtype="uint8")  # tracker color

        self.sess = tf.Session()
        self.input_data = tf.compat.v1.placeholder(
            tf.float32, [1, self.new_size[1], self.new_size[0], 3],
            name='input_data')
        yolo_model = yolov3(self.num_class, self.anchors)
        with tf.compat.v1.variable_scope('yolov3'):
            pred_feature_maps = yolo_model.forward(self.input_data, False)

        pred_boxes, pred_confs, pred_probs = yolo_model.predict(
            pred_feature_maps)

        pred_scores = pred_confs * pred_probs

        self.boxes, self.scores, self.labels = gpu_nms(pred_boxes,
                                                       pred_scores,
                                                       self.num_class,
                                                       max_boxes=200,
                                                       score_thresh=0.6,
                                                       nms_thresh=0.01)

        saver = tf.compat.v1.train.Saver()
        saver.restore(self.sess, restore_path)
示例#2
0
 def __init__(self):
     self.model_path_detection = os.path.join(
         globalvar.globalvar.config.data_set["model_save_path"],
         globalvar.globalvar.config.model_set["model_save_name"])
     assert os.path.exists(
         self.model_path_detection +
         ".meta"), "detection model not exit:%s" % self.model_path_detection
     self.model_path_classify = os.path.join(
         globalvar.globalvar.config.data_set["classify_model_save_path"],
         globalvar.globalvar.config.model_set["classify_model_save_name"])
     assert os.path.exists(
         self.model_path_classify +
         ".meta"), "classify model not exit:%s" % self.model_path_classify
     self.score_thresh_detection = globalvar.globalvar.config.test_other_info_set[
         "score_thresh_detection"]
     self.score_thresh_classify = globalvar.globalvar.config.test_other_info_set[
         "score_thresh_classify"]
     self.labels_nms_threshold = globalvar.globalvar.config.test_other_info_set[
         "labels_nms_threshold"]
     self.backbone_name = globalvar.globalvar.config.model_set[
         "backbone_name"]
     self.train_with_gray = globalvar.globalvar.config.model_set[
         "train_with_gray"]
     self.train_with_two_feature_map = globalvar.globalvar.config.model_set[
         "train_with_two_feature_map"]
     self.class_name_detection, self.name2id_classify = self.get_class_name(
         globalvar.globalvar.config.data_set["detection_class_name_path"])
     self.num_class_detection = len(self.class_name_detection)
     self.class_name_classify, self.name2id_classify = self.get_class_name(
         globalvar.globalvar.config.data_set["class_name_path"])
     self.num_class_classify = len(self.class_name_classify)
     self.new_size_detection = globalvar.globalvar.config.test_other_info_set[
         "new_size_detection"]  # w,h
     self.new_size_classify = globalvar.globalvar.config.model_set[
         "classify_size"]
     self.anchors_detection = parse_anchors(
         globalvar.globalvar.config.data_set["anchor_path"]) * 2.5
     self.size_min = globalvar.globalvar.config.test_other_info_set[
         "size_min"]
     self.sess_detection = None
     self.sess_classify = None
示例#3
0
def freeze_graph_test():
    image_path = './1.jpg'
    anchor_path = "./data/yolo_anchors.txt"
    input_size = [416, 416]
    class_name_path = "./data/coco.names"
    pb_path= "./data/darknet_weights/yolov3.pb"
    score_threshold = 0.5
    iou_threshold = 0.5

    anchors = parse_anchors(anchor_path)
    classes = read_class_names(class_name_path)
    num_class = len(classes)

    # 读取图像数据
    img_ori = cv2.imread(image_path)
    img_resized = cv2.resize(img_ori, tuple(input_size))
    img_resized = cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB)
    img_resized = np.asarray(img_resized, np.float32)
    img_resized = img_resized[np.newaxis, :] / 255.

    feature_map = get_feature_map(img_resized, pb_path)
    pb_test_tf(img_ori, feature_map, classes, num_class, anchors, score_threshold, iou_threshold)
示例#4
0
    def __init(self):
        self.anchor_path = "./data/yolo_anchors.txt"
        self.new_size = [416, 416]
        self.class_name_path = "./data/my_data/data.names"
        self.restore_path = "./checkpoint/model-step_17500_loss_0.003654_lr_0.0004995866"
        self.anchors = parse_anchors(self.anchor_path)
        self.classes = read_class_names(self.class_name_path)
        self.num_class = len(self.classes)
        self.color_tabel = get_color_table(self.num_class)
        self.img_ori = cv2.imread(self.input_image)
        self.weight_ori, self.width_ori = self.img_ori.shape[:2]
        self.img = cv2.resize(img_ori, tuple(self.new_size))
        self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)
        self.img = np.asarray(self.img, np.float32)
        self.img = self.img[np.newaxis, :] / 255.
        #config = tf.ConfigProto()
        #config.gpu_options.allow_growth = True
        self.__sess = tf.Session()
        self.__sess.run(tf.global_variables_initializer())

        yolo_model = yolov3(self.num_class, self.anchors)
        with tf.variable_scope('yolov3'):
            pred_feature_maps = yolo_model.forward(input_data, False)
        pred_boxes, pred_confs, pred_probs = yolo_model.predict(
            pred_feature_maps)

        pred_scores = pred_confs * pred_probs
        self.boxes, self.scores, self.labels = gpu_nms(pred_boxes,
                                                       pred_scores,
                                                       args.num_class,
                                                       max_boxes=100,
                                                       score_thresh=0.4,
                                                       iou_thresh=0.5)
        self.__saver = tf.train.Saver()
        self.__saver.restore(self.__sess, self.restore_path)
        self.input_data = tf.placeholder(
            tf.float32, [1, self.new_size[1], self.new_size[0], 3],
            name='input_data')
示例#5
0
def yolodet(anchor_path, image_path, new_size, letterbox, class_name_path, restore_path):

    
    
    anchors = parse_anchors(anchor_path)
    classes = read_class_names(class_name_path)
    num_class = len(classes)
    color_table = get_color_table(num_class)
    
    img_ori = cv2.imread(image_path)
    
    if letterbox:
        img, resize_ratio, dw, dh = letterbox_resize(img_ori, new_size[0], new_size[1])
    else:
        height_ori, width_ori = img_ori.shape[:2]
        img = cv2.resize(img_ori, tuple(new_size))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = np.asarray(img, np.float32)
    img = img[np.newaxis, :] / 255.
    
    with tf.Session() as sess:
        input_data = tf.placeholder(tf.float32, [1, new_size[1], new_size[0], 3], name='input_data')
        yolo_model = yolov3(num_class, anchors)
        with tf.variable_scope('yolov3'):
            pred_feature_maps = yolo_model.forward(input_data, False)
        pred_boxes, pred_confs, pred_probs = yolo_model.predict(pred_feature_maps)
    
        pred_scores = pred_confs * pred_probs
    
        boxes, scores, labels = gpu_nms(pred_boxes, pred_scores, num_class, max_boxes=200, score_thresh=0.3, nms_thresh=0.45)
    
        saver = tf.train.Saver()
        saver.restore(sess, restore_path)
    
        boxes_, scores_, labels_ = sess.run([boxes, scores, labels], feed_dict={input_data: img})
    
        # rescale the coordinates to the original image
        if letterbox:
            boxes_[:, [0, 2]] = (boxes_[:, [0, 2]] - dw) / resize_ratio
            boxes_[:, [1, 3]] = (boxes_[:, [1, 3]] - dh) / resize_ratio
        else:
            boxes_[:, [0, 2]] *= (width_ori/float(new_size[0]))
            boxes_[:, [1, 3]] *= (height_ori/float(new_size[1]))
    
#        print("box coords:")
#        print(boxes_)
#        print('*' * 30)
#        print("scores:")
#        print(scores_)
#        print('*' * 30)
#        print("labels:")
#        print(labels_)
#    
#        for i in range(len(boxes_)):
#            x0, y0, x1, y1 = boxes_[i]
#            plot_one_box(img_ori, [x0, y0, x1, y1], label=classes[labels_[i]] + ', {:.2f}%'.format(scores_[i] * 100), color=color_table[labels_[i]])
#        cv2.imshow('Detection result', img_ori)
#        cv2.imwrite('detection_result.jpg', img_ori)
#        cv2.waitKey(0)
    tf.reset_default_graph()
    return boxes_, scores_, labels_
import tensorflow as tf
import numpy as np
import argparse
import cv2
import glob
import matplotlib.pyplot as plt
from utils.misc_utils import parse_anchors, read_class_names
from utils.nms_utils import batch_nms
from utils.plot_utils import get_color_table, plot_one_box
from utils.data_aug import letterbox_resize
from model import yolov3

anchors = parse_anchors("./data/yolo_anchors.txt")
classes = read_class_names("./data/coco.names")
num_class = len(classes)

color_table = get_color_table(num_class)

with tf.Session() as sess:
    # build graph
    input_data = tf.placeholder(tf.float32, [None, None, None, 3],
                                name='input')
    yolo_model = yolov3(num_class, anchors)
    with tf.variable_scope('yolov3'):
        pred_feature_maps = yolo_model.forward(input_data, False)
    pred_boxes, pred_confs, pred_probs = yolo_model.predict(pred_feature_maps)
    pred_scores = pred_confs * pred_probs
    boxes, scores, labels, num_dects = batch_nms(pred_boxes,
                                                 pred_scores,
                                                 max_boxes=20,
                                                 score_thresh=0.5,
示例#7
0
from __future__ import division, print_function

import os
import sys
import tensorflow as tf
import numpy as np

from model import yolov3
from utils.misc_utils import parse_anchors, load_weights

num_class = 80
img_size = 416
weight_path = '../yolov3.weights'
save_path = './data/tensorflow_weights/yolov3.ckpt'
anchors = parse_anchors('./data/yolo_anchors.txt')  # 将anchors编程(-1,2)的数据

model = yolov3(80, anchors)
with tf.Session() as sess:
    inputs = tf.placeholder(tf.float32, [1, img_size, img_size, 3])
    # 这里描述一下,为什么要加这一步,见B1_P108.  这一步很迷,不加的时候是否可以?
    # 生成一个上下文管理器,在这个管理器中,forward中的值将直接获取已经生成的变量
    with tf.variable_scope('yolov3'):
        # feature_map是一个len=3的数组,每一个元素都对应着一个scale上的prediction
        feature_map = model.forward(inputs)

    saver = tf.train.Saver(var_list=tf.global_variables(scope='yolov3'))

    load_ops = load_weights(tf.global_variables(scope='yolov3'), weight_path)
    sess.run(load_ops)
    saver.save(sess, save_path=save_path)
示例#8
0
def single_image_test(imgname):
    parser = argparse.ArgumentParser(
        description="YOLO-V3 test single image test procedure.")
    parser.add_argument("--input_image",
                        type=str,
                        default="./static/uploads/beforeimg/" + imgname,
                        help="The path of the input image.")
    parser.add_argument("--anchor_path",
                        type=str,
                        default="./data/yolo_anchors.txt",
                        help="The path of the anchor txt file.")
    parser.add_argument(
        "--new_size",
        nargs='*',
        type=int,
        default=[416, 416],
        help=
        "Resize the input image with `new_size`, size format: [width, height]")
    parser.add_argument("--letterbox_resize",
                        type=lambda x: (str(x).lower() == 'true'),
                        default=True,
                        help="Whether to use the letterbox resize.")
    parser.add_argument("--class_name_path",
                        type=str,
                        default="./data/coco.names",
                        help="The path of the class names.")
    parser.add_argument("--restore_path",
                        type=str,
                        default="./data/darknet_weights/yolov3.ckpt",
                        help="The path of the weights to restore.")
    args = parser.parse_args()

    args.anchors = parse_anchors(args.anchor_path)
    args.classes = read_class_names(args.class_name_path)
    args.num_class = len(args.classes)

    color_table = get_color_table(args.num_class)

    img_ori = cv2.imread(args.input_image)
    if args.letterbox_resize:
        img, resize_ratio, dw, dh = letterbox_resize(img_ori, args.new_size[0],
                                                     args.new_size[1])
    else:
        height_ori, width_ori = img_ori.shape[:2]
        img = cv2.resize(img_ori, tuple(args.new_size))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = np.asarray(img, np.float32)
    img = img[np.newaxis, :] / 255.

    with tf.Session() as sess:
        input_data = tf.placeholder(tf.float32,
                                    [1, args.new_size[1], args.new_size[0], 3],
                                    name='input_data')
        yolo_model = yolov3(args.num_class, args.anchors)
        with tf.variable_scope('yolov3'):
            pred_feature_maps = yolo_model.forward(input_data, False)
        pred_boxes, pred_confs, pred_probs = yolo_model.predict(
            pred_feature_maps)

        pred_scores = pred_confs * pred_probs

        boxes, scores, labels = gpu_nms(pred_boxes,
                                        pred_scores,
                                        args.num_class,
                                        max_boxes=200,
                                        score_thresh=0.3,
                                        nms_thresh=0.45)

        saver = tf.train.Saver()
        saver.restore(sess, args.restore_path)

        boxes_, scores_, labels_ = sess.run([boxes, scores, labels],
                                            feed_dict={input_data: img})

        # rescale the coordinates to the original image
        if args.letterbox_resize:
            boxes_[:, [0, 2]] = (boxes_[:, [0, 2]] - dw) / resize_ratio
            boxes_[:, [1, 3]] = (boxes_[:, [1, 3]] - dh) / resize_ratio
        else:
            boxes_[:, [0, 2]] *= (width_ori / float(args.new_size[0]))
            boxes_[:, [1, 3]] *= (height_ori / float(args.new_size[1]))

        print("box coords:")
        print(boxes_)
        print('*' * 30)
        print("scores:")
        print(scores_)
        print('*' * 30)
        print("labels:")
        print(labels_)

        for i in range(len(boxes_)):
            x0, y0, x1, y1 = boxes_[i]
            plot_one_box(img_ori, [x0, y0, x1, y1],
                         label=args.classes[labels_[i]] +
                         ', {:.2f}%'.format(scores_[i] * 100),
                         color=color_table[labels_[i]])
        #cv2.imshow('Detection result', img_ori)
        cv2.imwrite('static/uploads/afterimg/' + imgname, img_ori)
        #cv2.waitKey(0)

        doc = []
        doc.append("发现:")
        item = ["安全帽", "未带安全帽的人"]
        if (len(labels_) == 0):
            doc.append("什么都没有发现。")
        else:
            for i in range(len(labels_)):
                doc.append(item[labels_[i]] + ",范围:" + str(boxes_[i]) +
                           ",可能性为:" + str(scores_[i]))
        return doc
示例#9
0
                    type=lambda x: (str(x).lower() == 'true'),
                    default=True,
                    help="Whether to use the letterbox resize.")
parser.add_argument("--class_name_path",
                    type=str,
                    default="./data/my_data/voc.names",
                    help="The path of the class names.")
parser.add_argument(
    "--restore_path",
    type=str,
    default=
    "/media/ubutnu/fc1a3be7-9b03-427e-9cc9-c4b242cefbff/YOLOv3_TensorFlow/checkpoint/model-epoch_90_step_175083_loss_0.4213_lr_1e-05",
    help="The path of the weights to restore.")
args = parser.parse_args()

args.anchors = parse_anchors(args.anchor_path)
args.classes = read_class_names(args.class_name_path)
args.num_class = len(args.classes)

color_table = get_color_table(args.num_class)

img_ori = cv2.imread(args.input_image)
if args.letterbox_resize:
    img, resize_ratio, dw, dh = letterbox_resize(img_ori, args.new_size[0],
                                                 args.new_size[1])
else:
    height_ori, width_ori = img_ori.shape[:2]
    img = cv2.resize(img_ori, tuple(args.new_size))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = np.asarray(img, np.float32)
img = img[np.newaxis, :] / 255.
示例#10
0
from __future__ import division, print_function

import os
import sys
import tensorflow as tf
import numpy as np

from model import yolov3
from utils.misc_utils import parse_anchors, load_weights

num_class = 80
img_size = 416
weight_path = './data/darknet_weights/yolov3.weights'
save_path = './data/darknet_weights/yolov3.ckpt'
anchors = parse_anchors('./data/yolo_anchors.txt')

model = yolov3(80, anchors)
with tf.Session() as sess:
    inputs = tf.placeholder(tf.float32, [1, img_size, img_size, 3])

    with tf.variable_scope('yolov3'):
        feature_map = model.forward(inputs)

    saver = tf.train.Saver(var_list=tf.global_variables(scope='yolov3'))

    load_ops = load_weights(tf.global_variables(scope='yolov3'), weight_path)
    sess.run(load_ops)
    saver.save(sess, save_path=save_path)
    print('TensorFlow model checkpoint has been saved to {}'.format(save_path))
from __future__ import division, print_function

import os
import sys
import tensorflow as tf
import numpy as np

from model import yolov3
from utils.misc_utils import parse_anchors, load_weights

num_class = 3  #80
img_size = 28  #416
weight_path = './data-test/yolov3_final.weights'
save_path = './data-test/yolov3_final.ckpt'
anchors = parse_anchors('./data-test/yolov3_anchors.txt')

model = yolov3(num_class, anchors)  #80
with tf.Session() as sess:
    inputs = tf.placeholder(tf.float32, [1, img_size, img_size, 3])

    with tf.variable_scope('yolov3'):
        feature_map = model.forward(inputs)

    saver = tf.train.Saver(var_list=tf.global_variables(scope='yolov3'))

    load_ops = load_weights(tf.global_variables(scope='yolov3'), weight_path)

    sess.run(load_ops)
    saver.save(sess, save_path=save_path)
    print('TensorFlow model checkpoint has been saved to {}'.format(save_path))
import tensorflow as tf
import numpy as np
import argparse
import cv2
import glob
import matplotlib.pyplot as plt
from utils.misc_utils import parse_anchors, read_class_names
#from utils.nms_utils import batch_nms
from utils.nms_utils import gpu_nms
from utils.plot_utils import get_color_table, plot_one_box
#from utils.data_aug import letterbox_resize
from model import yolov3

anchors = parse_anchors("./data/smoke_anchor.txt")
classes = read_class_names("./data/my_data/data.names")
num_class = len(classes)

color_table = get_color_table(num_class)

with tf.Session() as sess:
    # build graph
    input_data = tf.placeholder(tf.float32, [1, None, None, 3],
                                name='Placeholder')
    yolo_model = yolov3(num_class, anchors)
    with tf.variable_scope('yolov3'):
        pred_feature_maps = yolo_model.forward(input_data, False)
    pred_boxes, pred_confs, pred_probs = yolo_model.predict(pred_feature_maps)
    pred_scores = pred_confs * pred_probs
    boxes, scores, labels = gpu_nms(pred_boxes, pred_scores, num_class)
    #boxes, scores, labels, num_dects = batch_nms(pred_boxes, pred_scores, max_boxes=20, score_thresh=0.5, nms_thresh=0.5)
示例#13
0
import tensorflow as tf
from model import yolov3
from utils.misc_utils import parse_anchors, read_class_names
from utils.nms_utils import gpu_nms
#from utils.data_aug import letterbox_resize

MODEL_NAME = './model/best_model_Epoch_28_step_1565_mAP_0.9963_loss_0.4472_lr_0.0001'
new_size = [416, 416]
anchors = parse_anchors('model/yolo_anchors.txt')
classes = read_class_names('model/coco.names')
num_class = len(classes)
height_ori, width_ori = 0, 0
#letterbox_resize = True

sess = tf.Session()

#with tf.Session() as sess:
input_data = tf.placeholder(tf.float32, [1, new_size[1], new_size[0], 3],
                            name='input_data')
yolo_model = yolov3(num_class, anchors)

with tf.variable_scope('yolov3'):
    pred_feature_maps = yolo_model.forward(input_data, False)
pred_boxes, pred_confs, pred_probs = yolo_model.predict(pred_feature_maps)

pred_scores = pred_confs * pred_probs

boxes, scores, labels = gpu_nms(pred_boxes,
                                pred_scores,
                                num_class,
                                max_boxes=200,
示例#14
0
def recognize(jpg_path, pb_file_path):
    anchors = parse_anchors("./data/yolo_anchors.txt")
    classes = read_class_names("./data/coco.names")
    num_class = len(classes)

    color_table = get_color_table(num_class)

    img_ori = cv2.imread(jpg_path)
    height_ori, width_ori = img_ori.shape[:2]
    img = cv2.resize(img_ori, tuple([IMAGE_SIZE, IMAGE_SIZE]))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = np.asarray(img, np.float32)
    img = img[np.newaxis, :] / 255.
    with tf.Graph().as_default():
        output_graph_def = tf.GraphDef()
        print("Load Frozen_Graph File ...")
        with open(pb_file_path, "rb") as f:
            output_graph_def.ParseFromString(f.read())
        tf.import_graph_def(output_graph_def, name="")
        print("Finished")

        # GPU_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
        config = tf.ConfigProto()  # gpu_options=GPU_options)
        config.gpu_options.allow_growth = True

        with tf.Session(config=config) as sess:

            # Define Input and Outputs
            input_x = sess.graph.get_tensor_by_name("Placeholder:0")
            feature_map_1 = sess.graph.get_tensor_by_name(
                "yolov3/yolov3_head/feature_map_1:0")
            feature_map_2 = sess.graph.get_tensor_by_name(
                "yolov3/yolov3_head/feature_map_2:0")
            feature_map_3 = sess.graph.get_tensor_by_name(
                "yolov3/yolov3_head/feature_map_3:0")
            features = feature_map_1, feature_map_2, feature_map_3
            # yolo config
            yolo_model = yolov3(num_class, anchors)
            yolo_model.pb_forward(input_x)
            # # use frozen_graph to inference
            # print "RUN Graph ..."
            # features = sess.run(features, feed_dict={input_x:np.reshape(img, [-1, IMAGE_SIZE, IMAGE_SIZE, 3])})
            # print "Finished"
            # feature1, feature2, feature3 = features

            # feature1 = tf.convert_to_tensor(feature1)
            # feature2 = tf.convert_to_tensor(feature2)
            # feature3 = tf.convert_to_tensor(feature3)
            # features = feature1, feature2, feature3
            print "Predicting ..."

            pred_boxes, pred_confs, pred_probs = yolo_model.predict(features)
            pred_scores = pred_confs * pred_probs

            boxes, scores, labels = gpu_nms(pred_boxes,
                                            pred_scores,
                                            num_class,
                                            max_boxes=30,
                                            score_thresh=0.4,
                                            iou_thresh=0.5)
            t0 = time.time()
            boxes_, scores_, labels_ = sess.run(
                [boxes, scores, labels],
                feed_dict={
                    input_x: np.reshape(img, [-1, IMAGE_SIZE, IMAGE_SIZE, 3])
                })
            t1 = time.time()
            print "Finished"

            # rescale the coordinates to the original image
            boxes_[:, 0] *= (width_ori / float(IMAGE_SIZE))
            boxes_[:, 2] *= (width_ori / float(IMAGE_SIZE))
            boxes_[:, 1] *= (height_ori / float(IMAGE_SIZE))
            boxes_[:, 3] *= (height_ori / float(IMAGE_SIZE))

            print("box coords:")
            print(boxes_)
            print('*' * 30)
            print("scores:")
            print(scores_)
            print('*' * 30)
            print("labels:")
            print(labels_)
            print("runtime:")
            print(t1 - t0)

            for i in range(len(boxes_)):
                x0, y0, x1, y1 = boxes_[i]
                plot_one_box(img_ori, [x0, y0, x1, y1],
                             label=classes[labels_[i]],
                             color=color_table[labels_[i]])
            #cv2.imshow('Detection result', img_ori)
            cv2.imwrite('pb_result.jpg', img_ori)
            #cv2.waitKey(0)
            num_samples = 50

            t0 = time.time()
            for i in range(num_samples):
                boxes_, scores_, labels_ = sess.run(
                    [boxes, scores, labels],
                    feed_dict={
                        input_x: np.reshape(img,
                                            [-1, IMAGE_SIZE, IMAGE_SIZE, 3])
                    })
            t1 = time.time()
            print('Average runtime: %f seconds' %
                  (float(t1 - t0) / num_samples))
示例#15
0
parser.add_argument("--warm_up_lr",
                    type=float,
                    default=5e-5,
                    help="Warm up learning rate.")

parser.add_argument("--warm_up_epoch",
                    type=int,
                    default=5,
                    help="Warm up training epoches.")
flag = parser.parse_args()

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)

# args params
flag.anchors = parse_anchors(flag.anchor_path)
flag.classes = read_class_names(flag.class_name_path)
flag.class_num = len(flag.classes)
flag.train_img_cnt = len(open(flag.train_file, 'r').readlines())
flag.val_img_cnt = len(open(flag.val_file, 'r').readlines())
flag.train_batch_num = int(np.ceil(
    float(flag.train_img_cnt) / flag.batch_size))
flag.val_batch_num = int(np.ceil(float(flag.val_img_cnt) / flag.batch_size))

# setting loggers
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename=flag.progress_log_path,
                    filemode='w')
示例#16
0
def recognize(jpg_path, pb_file_path):
    anchors = parse_anchors("./data/yolo_anchors.txt")
    classes = read_class_names("./data/coco.names")
    num_class = len(classes)

    color_table = get_color_table(num_class)

    img_ori = cv2.imread(jpg_path)
    height_ori, width_ori = img_ori.shape[:2]
    img = cv2.resize(img_ori, tuple([IMAGE_SIZE, IMAGE_SIZE]))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = np.asarray(img, np.float32)
    # img = img[np.newaxis, :] / 255.
    # img_resized = np.reshape(img, [-1, IMAGE_SIZE, IMAGE_SIZE, 3])

    with tf.Graph().as_default():
        tf_config = tf.ConfigProto()
        tf_config.gpu_options.allow_growth = True

        with tf.Session(config=tf_config) as sess:
            print("Load TRT_Graph File ...")
            with open(pb_file_path, "rb") as f:
                output_graph_def = tf.GraphDef()
                output_graph_def.ParseFromString(f.read())
            print("Finished")
            input_name = "import/Placeholder"
            output_name1 = "import/yolov3/yolov3_head/feature_map_1"
            output_name2 = "import/yolov3/yolov3_head/feature_map_2"
            output_name3 = "import/yolov3/yolov3_head/feature_map_3"
            output_names = [output_name1, output_name2, output_name3]

            yolo_model = yolov3(num_class, anchors)

            print("Import TRT Graph ...")
            output_node = tf.import_graph_def(
                output_graph_def,
                return_elements=[
                    "yolov3/yolov3_head/feature_map_1",
                    "yolov3/yolov3_head/feature_map_2",
                    "yolov3/yolov3_head/feature_map_3"
                ])
            print("Finished")
            # for op in tf.get_default_graph().as_graph_def().node:
            #    print(op.name)

            tf_input = sess.graph.get_tensor_by_name(input_name + ':0')
            feature_map_1 = sess.graph.get_tensor_by_name(output_name1 + ":0")
            feature_map_2 = sess.graph.get_tensor_by_name(output_name2 + ":0")
            feature_map_3 = sess.graph.get_tensor_by_name(output_name3 + ":0")
            features = feature_map_1, feature_map_2, feature_map_3
            sess.run(output_node, feed_dict={tf_input: img[None, ...]})
            print("1111111")

            yolo_model.pb_forward(tf_input)

            pred_boxes, pred_confs, pred_probs = yolo_model.predict(features)

            pred_scores = pred_confs * pred_probs
            print("Detection ......")
            boxes, scores, labels = gpu_nms(pred_boxes,
                                            pred_scores,
                                            num_class,
                                            max_boxes=30,
                                            score_thresh=0.4,
                                            iou_thresh=0.5)
            boxes_, scores_, labels_ = sess.run(
                [boxes, scores, labels], feed_dict={tf_input: img[None, ...]})

            # rescale the coordinates to the original image
            boxes_[:, 0] *= (width_ori / float(IMAGE_SIZE))
            boxes_[:, 2] *= (width_ori / float(IMAGE_SIZE))
            boxes_[:, 1] *= (height_ori / float(IMAGE_SIZE))
            boxes_[:, 3] *= (height_ori / float(IMAGE_SIZE))

            print("box coords:")
            print(boxes_)
            print('*' * 30)
            print("scores:")
            print(scores_)
            print('*' * 30)
            print("labels:")
            print(labels_)

            for i in range(len(boxes_)):
                x0, y0, x1, y1 = boxes_[i]
                plot_one_box(img_ori, [x0, y0, x1, y1],
                             label=classes[labels_[i]],
                             color=color_table[labels_[i]])
            # cv2.imshow('Detection result', img_ori)
            cv2.imwrite('detection_result.jpg', img_ori)
from utils.plot_utils import get_color_table, plot_one_box
from agegender.agegender_demo import detect_face_age_gender, match_face, match_people
from ui import *
from get_height import cal

from model import yolov3

lines_gender = ['female', 'male']

input_video = "./data/demo_data/test.mp4"
anchor_path = "./data/yolo_anchors.txt"
new_size = [416, 416]
class_name_path = "./data/coco.names"
restore_path = "./data/darknet_weights/yolov3.ckpt"

anchors = parse_anchors(anchor_path)
classes = read_class_names(class_name_path)
num_class = len(classes)

color_table = get_color_table(num_class)

vid = cv2.VideoCapture(input_video)
video_frame_cnt = int(vid.get(7))
video_width = int(vid.get(3))
video_height = int(vid.get(4))
video_fps = int(vid.get(5))

fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
videoWriter = cv2.VideoWriter('video_result.mp4', fourcc, video_fps,
                              (video_width, video_height))
示例#18
0
def estimatePose():
    parser = argparse.ArgumentParser(
        description="YOLO-V3 video test procedure.")
    # parser.add_argument("input_video", type=str,
    #                     help="The path of the input video.")
    parser.add_argument("--anchor_path",
                        type=str,
                        default="./data/yolo_anchors.txt",
                        help="The path of the anchor txt file.")
    parser.add_argument(
        "--new_size",
        nargs='*',
        type=int,
        default=[416, 416],
        help=
        "Resize the input image with `new_size`, size format: [width, height]")
    parser.add_argument("--letterbox_resize",
                        type=lambda x: (str(x).lower() == 'true'),
                        default=True,
                        help="Whether to use the letterbox resize.")
    parser.add_argument("--class_name_path",
                        type=str,
                        default="./data/my_data/YOLOPose.names",
                        help="The path of the class names.")
    parser.add_argument("--restore_path",
                        type=str,
                        default="./data/pose_weights/lunge_best",
                        help="The path of the weights to restore.")
    parser.add_argument("--save_video",
                        type=lambda x: (str(x).lower() == 'true'),
                        default=True,
                        help="Whether to save the video detection results.")
    args = parser.parse_args()

    args.anchors = parse_anchors(args.anchor_path)
    args.classes = read_class_names(args.class_name_path)
    args.num_class = len(args.classes)

    color_table = get_color_table(args.num_class)

    # vid = cv2.VideoCapture(args.input_video)
    vid = cv2.VideoCapture('./data/demo/lunge_03.mp4')
    # vid = cv2.VideoCapture(r'C:\Users\soma\SMART_Referee\SMART_Referee_DL\data\lunge\video\lunge_03.mp4')
    video_frame_cnt = int(vid.get(7))
    video_width = int(vid.get(3))
    video_height = int(vid.get(4))
    video_fps = int(vid.get(5))

    trainer_pose = pd.read_csv('./data/ground_truth/output_right.csv',
                               header=None)
    trainer_pose = trainer_pose.loc[:, [
        0, 1, 2, 3, 4, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28
    ]]
    pca_df = trainer_pose.loc[:, [
        1, 2, 3, 4, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28
    ]]
    pca_df.loc[:,
               [c for c in pca_df.columns if c % 2 ==
                1]] = pca_df.loc[:, [c for c in pca_df.columns
                                     if c % 2 == 1]] * video_width / 416
    pca_df.loc[:,
               [c for c in pca_df.columns if c % 2 ==
                0]] = pca_df.loc[:, [c for c in pca_df.columns
                                     if c % 2 == 0]] * video_height / 416
    pca_df = pca_df.astype(int)
    pca_df = pca_df.replace(0, np.nan)
    pca_df = pca_df.dropna()
    pca_df.describe()
    pca = PCA(n_components=1)
    pca.fit(pca_df)

    size = [video_width, video_height]
    list_p = []
    waist_err = 0
    critical_point = 0
    past_idx = 0
    startTrig = 0
    cntdown = 90
    t = 0
    TRLEN = len(trainer_pose)
    modify_ankle = pca_df.iloc[0, :].values
    base_rect = [(int(video_width / 4), int(video_height / 10)),
                 (int(video_width * 3 / 4), int(video_height * 19 / 20))]
    c_knee = c_waist = c_speed = 0

    if args.save_video:
        fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
        videoWriter = cv2.VideoWriter('video_result.mp4', fourcc, video_fps,
                                      (video_width, video_height))

    with tf.Session() as sess:
        input_data = tf.placeholder(tf.float32,
                                    [1, args.new_size[1], args.new_size[0], 3],
                                    name='input_data')
        yolo_model = yolov3(args.num_class, args.anchors)
        with tf.variable_scope('yolov3'):
            pred_feature_maps = yolo_model.forward(input_data, False)
        pred_boxes, pred_confs, pred_probs = yolo_model.predict(
            pred_feature_maps)

        pred_scores = pred_confs * pred_probs

        boxes, scores, labels = gpu_nms(pred_boxes,
                                        pred_scores,
                                        args.num_class,
                                        max_boxes=200,
                                        score_thresh=0.3,
                                        nms_thresh=0.45)

        saver = tf.train.Saver()
        saver.restore(sess, args.restore_path)

        for i in range(video_frame_cnt):
            ret, img_ori = vid.read()
            if args.letterbox_resize:
                img, resize_ratio, dw, dh = letterbox_resize(
                    img_ori, args.new_size[0], args.new_size[1])
            else:
                height_ori, width_ori = img_ori.shape[:2]
                img = cv2.resize(img_ori, tuple(args.new_size))
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            img = np.asarray(img, np.float32)
            img = img[np.newaxis, :] / 255.

            start_time = time.time()
            boxes_, scores_, labels_ = sess.run([boxes, scores, labels],
                                                feed_dict={input_data: img})

            # rescale the coordinates to the original image
            if args.letterbox_resize:
                boxes_[:, [0, 2]] = (boxes_[:, [0, 2]] - dw) / resize_ratio
                boxes_[:, [1, 3]] = (boxes_[:, [1, 3]] - dh) / resize_ratio
            else:
                boxes_[:, [0, 2]] *= (width_ori / float(args.new_size[0]))
                boxes_[:, [1, 3]] *= (height_ori / float(args.new_size[1]))

            people_pose = get_people_pose(boxes_, labels_,
                                          base_rect)  # list-dict
            people_pose = np.array([p[1] for p in people_pose[0]
                                    ]).flatten()  # dict-tuple -> list
            people_pose = people_pose[[
                0, 1, 2, 3, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27
            ]]

            # Start Trigger
            if startTrig == 2:
                pass
            elif startTrig == 0:  # start
                # 기준 박스
                cv2.rectangle(img_ori, base_rect[0], base_rect[1], (0, 0, 255),
                              2)
                if isInBox(people_pose, base_rect[0], base_rect[1]):
                    # t_resize_pose = resize_pose(people_pose, trainer_pose.iloc[0, 1:].values)
                    t_resize_pose = resize_pose(people_pose,
                                                pca_df.iloc[0, :].values)
                    img_ori = draw_ground_truth(img_ori, t_resize_pose)
                    # img_ori = draw_ground_truth(img_ori, pca_df.iloc[0, :].values)
                    startTrig = isStart(people_pose,
                                        trainer_pose.iloc[0, 1:].values, size)

                    cv2.imshow('image', img_ori)
                    if cv2.waitKey(1) & 0xFF == ord('q'):
                        break
                    continue
                else:
                    print("박스안에 들어와주세요!!")
                    continue

            elif startTrig == 1:
                img_ori = draw_ground_truth(img_ori, pca_df.iloc[0, :].values)
                cv2.putText(img_ori, str(int(cntdown / 30)), (100, 300),
                            cv2.FONT_HERSHEY_SIMPLEX, 10, (255, 0, 0), 10)
                cv2.imshow('image', img_ori)
                cntdown -= 1
                if cntdown == 0:
                    startTrig = 2
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
                continue
            '''check ankle : 편차 40이상 발생시 전에 값 으로 업데이트'''
            people_pose = check_ankle(list_p, people_pose, modify_ankle, size)

            # f = open('user.csv', 'a', encoding='utf-8', newline='')
            # wr = csv.writer(f)
            # wr.writerow(people_pose)
            # ground truth 그리기

            list_p.append(people_pose)

            img_ori = draw_ground_truth(img_ori, pca_df.iloc[t, :].values)

            if check_waist(people_pose):
                waist_err += 1

            if waist_err is 60:  # waist_err는 60번 틀리면 피드백함
                feedback_waist()
                c_waist += 1
                waist_err = 0

            if trainer_pose.iloc[t, 0] == 1:  # t는 특정 시점 + i frame
                critical_point += 1
                if critical_point % 2 == 0:
                    my_pose = makeMypose_df(list_p)
                    c_speed = check_speed(
                        my_pose, trainer_pose.iloc[past_idx:t + 1, 1:], pca,
                        c_speed)
                    c_knee = check_knee(people_pose, c_knee)
                    modify_ankle = list_p[-1]
                    list_p = []
                    past_idx = t
            t += 1
            if t == TRLEN:
                break

            # img_ori = draw_body(img_ori, boxes_, labels_)
            # for i in range(len(boxes_)):
            #     x0, y0, x1, y1 = boxes_[i]
            #     plot_one_box(img_ori, [x0, y0, x1, y1], label=args.classes[labels_[i]] + ', {:.2f}%'.format(scores_[i] * 100), color=color_table[labels_[i]])

            # 사용자 자세 그리기
            # img_ori = draw_truth(img_ori, people_pose)

            end_time = time.time()
            cv2.putText(img_ori,
                        '{:.2f}ms'.format((end_time - start_time) * 1000),
                        (40, 40),
                        0,
                        fontScale=1,
                        color=(0, 255, 0),
                        thickness=2)

            cv2.imshow('image', img_ori)
            if args.save_video:
                videoWriter.write(img_ori)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                cv2.destroyAllWindows()
                break

        vid.release()
        cv2.destroyAllWindows()
        if args.save_video:
            videoWriter.release()

    f = open('./data/score/result.csv', 'a', encoding='utf-8', newline='')
    wr = csv.writer(f)
    d = datetime.today().strftime("%Y/%m/%d")
    t = datetime.today().strftime("%H:%M:%S")
    wr.writerow([d, t, c_knee, c_waist, c_speed])
示例#19
0
def yolodet(image_path,
            anchor_path=rootpath + "/yolo/data/yolo_anchors.txt",
            new_size=[416, 416],
            letterbox=True,
            class_name_path=rootpath + "/yolo/data/coco.names",
            restore_path=rootpath + "/yolo/data/best_model"):

    anchors = parse_anchors(anchor_path)
    classes = read_class_names(class_name_path)
    num_class = len(classes)
    color_table = get_color_table(num_class)

    img_ori = cv2.imread(image_path)
    if letterbox:
        img, resize_ratio, dw, dh = letterbox_resize(img_ori, new_size[0],
                                                     new_size[1])
    else:
        height_ori, width_ori = img_ori.shape[:2]
        img = cv2.resize(img_ori, tuple(new_size))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = np.asarray(img, np.float32)
    img = img[np.newaxis, :] / 255.

    with tf.Session() as sess:
        input_data = tf.placeholder(tf.float32,
                                    [1, new_size[1], new_size[0], 3],
                                    name='input_data')
        yolo_model = yolov3(num_class, anchors)
        with tf.variable_scope('yolov3'):
            pred_feature_maps = yolo_model.forward(input_data, False)
        pred_boxes, pred_confs, pred_probs = yolo_model.predict(
            pred_feature_maps)

        pred_scores = pred_confs * pred_probs

        boxes, scores, labels = gpu_nms(pred_boxes,
                                        pred_scores,
                                        num_class,
                                        max_boxes=200,
                                        score_thresh=0.3,
                                        nms_thresh=0.45)

        saver = tf.train.Saver()
        saver.restore(sess, restore_path)

        boxes_, scores_, labels_ = sess.run([boxes, scores, labels],
                                            feed_dict={input_data: img})

        # rescale the coordinates to the original image
        if letterbox:
            boxes_[:, [0, 2]] = (boxes_[:, [0, 2]] - dw) / resize_ratio
            boxes_[:, [1, 3]] = (boxes_[:, [1, 3]] - dh) / resize_ratio
        else:
            boxes_[:, [0, 2]] *= (width_ori / float(new_size[0]))
            boxes_[:, [1, 3]] *= (height_ori / float(new_size[1]))

    tf.reset_default_graph()

    #transform detections into 1 line (#1class,#1conf,#1xmin,#1ymin,#1max,#1ymax,#2class,#2conf,...)
    boxes = []
    for i in range(np.shape(boxes_)[0]):
        boxes.append(labels_[i])
        boxes.append(scores_[i])
        boxes.extend(boxes_[i, :])

    return boxes
示例#20
0
def recognize(jpg_path, pb_file_path):
    anchors = parse_anchors("./data/yolo_anchors.txt")
    classes = read_class_names("./data/coco.names")
    num_class = len(classes)

    color_table = get_color_table(num_class)

    img_ori = cv2.imread(jpg_path)
    height_ori, width_ori = img_ori.shape[:2]
    img = cv2.resize(img_ori, tuple([IMAGE_SIZE, IMAGE_SIZE]))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = np.asarray(img, np.float32)
    img = img[np.newaxis, :] / 255.

    with tf.Graph().as_default():
        output_graph_def = tf.GraphDef()

        with open(pb_file_path, "rb") as f:
            output_graph_def.ParseFromString(f.read())
            _ = tf.import_graph_def(output_graph_def, name="")
        tf_config = tf.ConfigProto()
        tf_config.gpu_options.allow_growth = True

        with tf.Session(config=tf_config) as sess:
            init = tf.global_variables_initializer()
            sess.run(init)

            input_name = "Placeholder"
            output_name1 = "yolov3/yolov3_head/feature_map_1"
            output_name2 = "yolov3/yolov3_head/feature_map_2"
            output_name3 = "yolov3/yolov3_head/feature_map_3"
            output_names = [output_name1, output_name2, output_name3]

            yolo_model = yolov3(num_class, anchors)
            input_data = tf.placeholder(tf.float32,
                                        [1, IMAGE_SIZE, IMAGE_SIZE, 3],
                                        name='input_data')

            trt_graph = trt.create_inference_graph(
                input_graph_def=output_graph_def,
                outputs=output_names,
                max_batch_size=1,
                max_workspace_size_bytes=1 << 25,
                precision_mode='FP16',
                minimum_segment_size=5)

            with open('./data/yolov3_trt.pb', 'wb') as f:
                f.write(trt_graph.SerializeToString())

            tf.import_graph_def(trt_graph, name='')

            tf_input = sess.graph.get_tensor_by_name(input_name + ':0')
            feature_map_1 = sess.graph.get_tensor_by_name(output_name1 + ":0")
            feature_map_2 = sess.graph.get_tensor_by_name(output_name2 + ":0")
            feature_map_3 = sess.graph.get_tensor_by_name(output_name3 + ":0")
            features = feature_map_1, feature_map_2, feature_map_3
            # tf_scores = tf_sess.graph.get_tensor_by_name('detection_scores:0')
            # tf_boxes = tf_sess.graph.get_tensor_by_name('detection_boxes:0')
            # tf_classes = tf_sess.graph.get_tensor_by_name('detection_classes:0')
            # tf_num_detections = tf_sess.graph.get_tensor_by_name('num_detections:0')

            # features = sess.run(features, feed_dict={tf_input:np.reshape(img, [-1, IMAGE_SIZE, IMAGE_SIZE, 3])})
            # feature1, feature2, feature3 = features
            # feature1 = tf.convert_to_tensor(feature1)
            # feature2 = tf.convert_to_tensor(feature2)
            # feature3 = tf.convert_to_tensor(feature3)
            # features = feature1, feature2, feature3

            yolo_model.pb_forward(input_data)

            pred_boxes, pred_confs, pred_probs = yolo_model.predict(features)

            pred_scores = pred_confs * pred_probs

            boxes, scores, labels = gpu_nms(pred_boxes,
                                            pred_scores,
                                            num_class,
                                            max_boxes=30,
                                            score_thresh=0.4,
                                            iou_thresh=0.5)

            boxes_, scores_, labels_ = sess.run([boxes, scores, labels],
                                                feed_dict={input_data: img})

            # rescale the coordinates to the original image
            boxes_[:, 0] *= (width_ori / float(IMAGE_SIZE))
            boxes_[:, 2] *= (width_ori / float(IMAGE_SIZE))
            boxes_[:, 1] *= (height_ori / float(IMAGE_SIZE))
            boxes_[:, 3] *= (height_ori / float(IMAGE_SIZE))

            print("box coords:")
            print(boxes_)
            print('*' * 30)
            print("scores:")
            print(scores_)
            print('*' * 30)
            print("labels:")
            print(labels_)

            for i in range(len(boxes_)):
                x0, y0, x1, y1 = boxes_[i]
                plot_one_box(img_ori, [x0, y0, x1, y1],
                             label=classes[labels_[i]],
                             color=color_table[labels_[i]])
            # cv2.imshow('Detection result', img_ori)
            cv2.imwrite('detection_result.jpg', img_ori)
                            "fill_zero_label_names"] and label != "gj":
                        if label not in class_num_dic:
                            class_num_dic[label] = 1
                        else:
                            class_num_dic[label] += 1

        logger.info_ai(
            meg="prepare data over, get all class name from data file",
            get_ins={"class_num_dic": class_num_dic})

        lr_decay_freq = int(
            config_common.model_set["lr_decay_freq_epoch"] * train_num /
            (len(gpu_device) * config_common.model_set["batch_size"]))

        # args params
        anchors = parse_anchors(config_common.data_set["anchor_path"]) * 2.5
        classes = read_class_names(config_common.data_set["class_name_path"])
        classes_list = list(classes.values())
        logger.info_ai(meg="train class name get from class name file",
                       get_ins={"classes_list": classes_list})
        #把不在训练类别里面的类别从类别个数字典中删除
        for key in list(class_num_dic.keys()):
            if key not in classes_list:
                class_num_dic.pop(key)
                logger.info_ai(
                    meg="************data calss name:%s not in classes list" %
                    key)
        class_num_dic = {
            key: 1 / (value + 1)
            for key, value in class_num_dic.items()
        }
    def Run(self, img_name, half=True):
        if self.sess == None:
            import tensorflow as tf
            from model import yolov3
            from utils.misc_utils import parse_anchors, read_class_names
            from utils.nms_utils import gpu_nms

            self.anchors = parse_anchors('./model/yolo_anchors.txt')
            self.classes = read_class_names('./model/coco.names')
            self.num_class = len(self.classes)

            self.sess = tf.Session()

            self.input_data = tf.placeholder(
                tf.float32, [1, self.new_size[1], self.new_size[0], 3],
                name='input_data')
            self.yolo_model = yolov3(self.num_class, self.anchors)

            with tf.variable_scope('yolov3'):
                pred_feature_maps = self.yolo_model.forward(
                    self.input_data, False)
            self.pred_boxes, self.pred_confs, self.pred_probs, self.map4 = self.yolo_model.predict(
                pred_feature_maps)

            self.pred_scores = self.pred_confs * self.pred_probs

            self.boxes, self.scores, self.labels = gpu_nms(self.pred_boxes,
                                                           self.pred_scores,
                                                           self.num_class,
                                                           max_boxes=200,
                                                           score_thresh=0.3,
                                                           nms_thresh=0.45)

            self.saver = tf.train.Saver()
            self.saver.restore(self.sess, self.MODEL_NAME)

            # self.sess.run(tf.global_variables_initializer())
            print('Tensorflow intialized')

        # print('getting boxes')
        boxes, scores, feature_map = self.GetBoundingBox(img_name, half)
        # print('got boxes:',boxes)
        if len(boxes) != 0:
            dist, angle = self.getDistanceAndAngle(boxes[np.argmax(scores)],
                                                   self.width_ori,
                                                   self.height_ori)
            self.lastNDistances.append(dist)
            self.lastNAngles.append(angle)
            alpha = self.alpha if len(self.lastNDistances) > 1 else 1
            self.exponentialMovingAverageDist = alpha * dist + (
                1 - alpha) * self.exponentialMovingAverageDist
            self.exponentialMovingAverageAngle = alpha * angle + (
                1 - alpha) * self.exponentialMovingAverageAngle
            angle, _ = self.segmentation.FindPossibleAngle(
                boxes[np.argmax(scores)], angle, feature_map, self.width_ori,
                self.height_ori)
        else:
            print('No box found')
            dist, angle = self.Extrapolate()
            angle, _ = self.segmentation.FindPossibleAngle(
                boxes, angle, feature_map, self.width_ori, self.height_ori)

        self.KeepLastN()
        angle = self.LimitAngles(angle)
        dist = self.LimitDistance(dist)
        return dist, angle