Esempio n. 1
0
 def __init__(self):
     self.IMAGE_H = 800
     self.IMAGE_W = 1360
     # self.classes = utils.read_coco_names('./data/coco.names')
     self.label_map = self.load_labelmap("./gtsdb/gtsdb3_label_map.pbtxt")
     self.categories = self.convert_label_map_to_categories(self.label_map, max_num_classes=3,
                                                       use_display_name=True)
     self.category_index = self.create_category_index(self.categories)
     self.classes = self.get_classes(self.category_index)
     self.num_classes = len(self.category_index)
     self.gpu_nms_graph = tf.Graph()
     self.model_path = "./gtsdb/frozen_inference_graph.pb"
     # self.input_tensor, self.output_tensors = utils.read_pb_return_tensors(
     #     self.gpu_nms_graph,
     #     "./checkpoint/yolov3_gpu_nms.pb",
     #     ["Placeholder:0", "concat_10:0", "concat_11:0", "concat_12:0"])
     self.input_tensor, self.output_tensors = utils.read_pb_return_tensors(
         self.gpu_nms_graph,
         "./gtsdb/frozen_inference_graph.pb",
         ["image_tensor:0", "detection_boxes:0", "detection_scores:0", "detection_classes:0", "num_detections:0"])
     self.sess = tf.Session(graph=self.gpu_nms_graph)
     self.last_PIL_image = None  # 原图
     self.last_boxes = None
     self.last_scores = None
     self.last_labels = None
     self.last_nd = None
def predict(image_path):

    original_image = cv2.imread(image_path)  # 读取图片
    original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
    original_image_size = original_image.shape[:2]
    image_data = utils.image_preporcess(np.copy(original_image),
                                        [input_size, input_size])
    image_data = image_data[np.newaxis, ...]

    return_tensors = utils.read_pb_return_tensors(graph, pb_file,
                                                  return_elements)

    with tf.Session(graph=graph) as sess:
        pred_sbbox, pred_mbbox, pred_lbbox = sess.run(
            [return_tensors[1], return_tensors[2], return_tensors[3]],
            feed_dict={return_tensors[0]: image_data})

    pred_bbox = np.concatenate([
        np.reshape(pred_sbbox, (-1, 5 + num_classes)),
        np.reshape(pred_mbbox, (-1, 5 + num_classes)),
        np.reshape(pred_lbbox, (-1, 5 + num_classes))
    ],
                               axis=0)

    bboxes = utils.postprocess_boxes(pred_bbox, original_image_size,
                                     input_size, 0.35)
    bboxes = utils.nms(bboxes, 0.45, method='nms')
    image = utils.draw_bbox(original_image, bboxes)

    image = Image.fromarray(image)
    image.show()
    image.save(output_path)
Esempio n. 3
0
    def __init__(self, cfg):

        self.cfg = cfg
        self.graph = tf.Graph()
        self.model = utils.read_pb_return_tensors(self.graph, pb_file,
                                                  return_elements)
        self.sess = tf.Session(graph=self.graph)
def alone_bbox(image_path, save_path):
    class_list = ["bench", "roadblock", "babycar", "wheelchair"]
    return_elements = [
        "input/input_data:0", "pred_sbbox/concat_2:0", "pred_mbbox/concat_2:0",
        "pred_lbbox/concat_2:0"
    ]
    input_size = cfg.TEST.INPUT_SIZE
    graph = tf.Graph()
    save_file_name = osp.basename(image_path).split('.')[0] + '.txt'
    original_image = cv2.imread(image_path)
    original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
    original_image_size = original_image.shape[:2]
    image_data = utils.image_preporcess(np.copy(original_image),
                                        [input_size, input_size])
    image_data = image_data[np.newaxis, ...]

    with open(osp.join(save_path, save_file_name), 'a') as fp:
        name_dict_list = []
        bboxes_list = []
        for newlabel in class_list:
            pb_file = "./yolov3_{}.pb".format(newlabel)
            name_dict = utils.read_class_names(
                "./data/classes/{}.names".format(newlabel))
            name_dict_list.append(name_dict)
            num_classes = len(name_dict)

            return_tensors = utils.read_pb_return_tensors(
                graph, pb_file, return_elements)

            with tf.Session(graph=graph) as sess:
                pred_sbbox, pred_mbbox, pred_lbbox = sess.run(
                    [return_tensors[1], return_tensors[2], return_tensors[3]],
                    feed_dict={return_tensors[0]: image_data})

            pred_bbox = np.concatenate([
                np.reshape(pred_sbbox, (-1, 5 + num_classes)),
                np.reshape(pred_mbbox, (-1, 5 + num_classes)),
                np.reshape(pred_lbbox, (-1, 5 + num_classes))
            ],
                                       axis=0)

            bboxes = utils.postprocess_boxes(pred_bbox, original_image_size,
                                             input_size, 0.9)
            bboxes = utils.nms(bboxes, 0.90, method='nms')
            bboxes_list.append(bboxes)
            o = str(' ' + str(0))
            for bbox in bboxes:
                min_x, min_y, max_x, max_y = [
                    str(int(bbox[i])) for i in range(4)
                ]
                label_name = name_dict[int(bbox[5])]
                fp.writelines(label_name + o + o + o + ' ' + min_x + ' ' +
                              min_y + ' ' + max_x + ' ' + max_y + o + o + o +
                              o + o + o + o)
                fp.writelines('\n')
        # for i,bboxes_ in enumerate(bboxes_list):
        #     image = utils.draw_bbox(original_image, bboxes_, name_dict_list[i])
        # image = Image.fromarray(image)
        # image.show()
    return bboxes_list, name_dict_list
Esempio n. 5
0
def test_system():
    image_path = "./gui/imgs/shot.png"
    return_elements = ["input/input_data:0", "pred_sbbox/concat_2:0", "pred_mbbox/concat_2:0", "pred_lbbox/concat_2:0"]
    pb_file = "./yolov3_web.pb"
    num_classes = 2
    input_size = 544
    graph = tf.Graph()
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    return_tensors = utils.read_pb_return_tensors(graph, pb_file, return_elements)
    with tf.Session(graph=graph, config=config) as sess:
        try:
            original_image = cv2.imread(image_path)
            ocr_boxes, yolo_boxes = predict_logo_boxes(image_path, input_size, num_classes, original_image,
                                                            return_tensors, sess)

            # TODO retrieve images
            image_list = write_cropped_images(image_path, ocr_boxes, original_image, yolo_boxes)

            # TODO connect to Yuxuan


        except Exception as e:
            print(e)
Esempio n. 6
0
def predicate(original_image, pb_file):
    if pb_file == None:
        pb_file = DEFAULT_PB
    
    return_elements = ["input/input_data:0", "pred_sbbox/concat_2:0", "pred_mbbox/concat_2:0", "pred_lbbox/concat_2:0"]
    num_classes     = 66
    input_size      = 608
    graph           = tf.Graph()

    original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
    original_image_size = original_image.shape[:2]
    image_data = utils.image_preporcess(np.copy(original_image), [input_size, input_size])
    image_data = image_data[np.newaxis, ...]

    return_tensors = utils.read_pb_return_tensors(graph, pb_file, return_elements)


    with tf.compat.v1.Session(graph=graph) as sess:
        pred_sbbox, pred_mbbox, pred_lbbox = sess.run(
            [return_tensors[1], return_tensors[2], return_tensors[3]],
                    feed_dict={ return_tensors[0]: image_data})

    pred_bbox = np.concatenate([np.reshape(pred_sbbox, (-1, 5 + num_classes)),
                                np.reshape(pred_mbbox, (-1, 5 + num_classes)),
                                np.reshape(pred_lbbox, (-1, 5 + num_classes))], axis=0)

    bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.3)
    bboxes = utils.nms(bboxes, 0.45, method='nms')
    return bboxes, original_image
Esempio n. 7
0
    def __init__(self,
                 model_path='',
                 names_path='',
                 gpu_memory=0.2,
                 image_size=416):
        '''
        Constructor : Called when class object is created.
        
        '''
        self.names_file = names_path
        self.img_size = image_size
        self.max_batch_size = batchsize
        self.num_classes = len(utils.read_names(names_path))
        self.model_file = model_path

        self.input_tensor, self.output_tensors = utils.read_pb_return_tensors(
            tf.get_default_graph(), self.model_file,
            ["Placeholder:0", "concat_9:0", "mul_6:0"])
        self.config = tf.ConfigProto()
        self.config.gpu_options.per_process_gpu_memory_fraction = gpu_memory
        self.config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=self.config)
        _ = self.sess.run(
            self.output_tensors,
            feed_dict={self.input_tensor: self.createRandomSample()})
Esempio n. 8
0
def mul_image(watch_dir="./docs/images", output_path='./output'):
    imageDir = os.path.abspath(watch_dir)
    imageList = glob.glob(os.path.join(imageDir, '*.jpg'))
    # print(imageList)

    graph = tf.Graph()
    pb_file = "./yolov3_coco_v3.pb"
    return_elements = [
        "input/input_data:0", "pred_sbbox/concat_2:0", "pred_mbbox/concat_2:0",
        "pred_lbbox/concat_2:0"
    ]
    return_tensors = utils.read_pb_return_tensors(graph, pb_file,
                                                  return_elements)

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

        for item in imageList:
            image_path = item
            # print('item',item)
            end = "/"
            name = item[item.rfind(end):]
            # print(name)
            num_classes = 80
            input_size = 608
            out = output_path + name

            original_image = cv2.imread(image_path)
            # original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
            original_image_size = original_image.shape[:2]
            image_data = utils.image_preporcess(np.copy(original_image),
                                                [input_size, input_size])
            image_data = image_data[np.newaxis, ...]

            pred_sbbox, pred_mbbox, pred_lbbox = sess.run(
                [return_tensors[1], return_tensors[2], return_tensors[3]],
                feed_dict={return_tensors[0]: image_data})

            pred_bbox = np.concatenate([
                np.reshape(pred_sbbox, (-1, 5 + num_classes)),
                np.reshape(pred_mbbox, (-1, 5 + num_classes)),
                np.reshape(pred_lbbox, (-1, 5 + num_classes))
            ],
                                       axis=0)

            bboxes = utils.postprocess_boxes(pred_bbox, original_image_size,
                                             input_size, 0.45)

            # print('bboxes:',bboxes)
            # bboxes: [[301.13088989 118.44700623 346.95623779 172.39486694   0.97461057   0]...]

            bboxes = utils.nms(bboxes, 0.45, method='nms')
            # print('bboxes:',bboxes)
            # bboxes: [array([105.31238556,  54.51167679, 282.53552246, 147.27146912, 0.99279714,   0.        ])]

            image = utils.draw_bbox(original_image, bboxes)

            cv2.imwrite(out, image)
Esempio n. 9
0
def video_without_saving():

    classes = utils.read_class_names(cfg.YOLO.CLASSES)
    num_classes = len(classes)
    return_elements = [
        "input/input_data:0", "pred_sbbox/concat_2:0", "pred_mbbox/concat_2:0",
        "pred_lbbox/concat_2:0"
    ]
    pb_file = "./yolov3_coco.pb"
    video_path = "docs/images/racoon.mp4"
    video_path = 0

    input_size = 416
    graph = tf.Graph()
    return_tensors = utils.read_pb_return_tensors(graph, pb_file,
                                                  return_elements)

    with tf.Session(graph=graph) as sess:
        vid = cv2.VideoCapture(video_path)
        while True:
            return_value, frame = vid.read()
            if return_value:
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                image = Image.fromarray(frame)
            else:
                raise ValueError("No image!")
            frame_size = frame.shape[:2]
            image_data = utils.image_preporcess(np.copy(frame),
                                                [input_size, input_size])
            image_data = image_data[np.newaxis, ...]
            prev_time = time.time()

            pred_sbbox, pred_mbbox, pred_lbbox = sess.run(
                [return_tensors[1], return_tensors[2], return_tensors[3]],
                feed_dict={return_tensors[0]: image_data})

            pred_bbox = np.concatenate([
                np.reshape(pred_sbbox, (-1, 5 + num_classes)),
                np.reshape(pred_mbbox, (-1, 5 + num_classes)),
                np.reshape(pred_lbbox, (-1, 5 + num_classes))
            ],
                                       axis=0)

            bboxes = utils.postprocess_boxes(pred_bbox, frame_size, input_size,
                                             0.3)
            bboxes = utils.nms(bboxes, 0.45, method='nms')
            image = utils.draw_bbox(frame, bboxes)

            curr_time = time.time()
            exec_time = curr_time - prev_time
            result = np.asarray(image)
            info = "time: %.2f ms" % (1000 * exec_time)
            cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE)
            result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
            cv2.imshow("result", result)
            if cv2.waitKey(1) & 0xFF == ord('q'): break
Esempio n. 10
0
def get_tensors():
    return_elements = [
        "input/input_data:0", "pred_sbbox/concat_2:0", "pred_mbbox/concat_2:0",
        "pred_lbbox/concat_2:0"
    ]
    pb_file = "./yolov3_coco.pb"
    graph = tf.Graph()
    return_tensors = utils.read_pb_return_tensors(graph, pb_file,
                                                  return_elements)
    return return_tensors, graph
Esempio n. 11
0
 def __init__(self, model_path, num_classes, input_size, return_elements):
     self.model_path = model_path
     self.num_classes = num_classes
     self.input_size = input_size
     self.return_elements = return_elements
     os.environ['CUDA_VISIBLE_DEVICES'] = '0'
     self.conf = tf.ConfigProto()
     self.conf.gpu_options.per_process_gpu_memory_fraction = 0.8
     self.conf.gpu_options.allow_growth = True
     self.graph = tf.Graph()
     self.return_tensors = utils.read_pb_return_tensors(
         self.graph, self.model_path, self.return_elements)
     self.sess = tf.Session(graph=self.graph, config=self.conf)
Esempio n. 12
0
 def get_tensors(frozen_model_dir):
     if session is not None:
         sess = session
     else:
         if not use_gpu:
             cpu_nms_graph = tf.Graph()
             sess = tf.Session(graph=cpu_nms_graph)
         else:
             gpu_nms_graph = tf.Graph()
             sess = tf.Session(graph=gpu_nms_graph)
     if not use_gpu:
         input_tensors, output_tensors = utils.read_pb_return_tensors(
             cpu_nms_graph,
             os.path.join(frozen_model_dir, "yolov3_cpu_nms.pb"),
             ["Placeholder:0", "concat_9:0", "mul_6:0", "concat_8:0"])
     else:
         input_tensors, output_tensors = utils.read_pb_return_tensors(
             gpu_nms_graph,
             os.path.join(frozen_model_dir, "yolov3_gpu_nms.pb"), [
                 "Placeholder:0", "concat_10:0", "concat_11:0",
                 "concat_8:0", "concat_13:0"
             ])
     return sess, input_tensors, output_tensors
Esempio n. 13
0
    def __init__(self):
        ''' Called when class object is created. '''

        self.img_size = imgsize
        self.max_batch_size = batchsize
        self.num_classes = len(utils.read_coco_names('./data/coco.names'))

        self.input_tensor, self.output_tensors = utils.read_pb_return_tensors(
            tf.compat.v1.get_default_graph(), "./checkpoint/yolov3_cpu_nms.pb",
            ["Placeholder:0", "concat_9:0", "mul_6:0"])

        self.config = tf.compat.v1.ConfigProto()
        # self.config.gpu_options.per_process_gpu_memory_fraction = 0.4
        self.sess = tf.compat.v1.Session(config=self.config)
        _ = self.sess.run(
            self.output_tensors,
            feed_dict={self.input_tensor: self.createRandomSample()})
Esempio n. 14
0
    def __init__(self):

        self.SIZE = [416, 416]
        self.classes = utils.read_coco_names('./data/coco.names')
        self.num_classes = len(self.classes)
        self.input_tensor, self.output_tensors = utils.read_pb_return_tensors(
            tf.get_default_graph(), "./checkpoint/yolov3_gpu_nms.pb",
            ["Placeholder:0", "concat_10:0", "concat_11:0", "concat_12:0"])
        self.sess = tf.Session()
        self.LOWER_RED_RANGE = np.array([17, 15, 100])
        self.UPPER_RED_RANGE = np.array([50, 56, 200])
        self.pitch_rate = 0
        self.yaw_rate = 0
        self.vertical_rate = 0
        self.TARGET = 0
        self.drone_centroid = (int(856 / 2), int(480 * (0.4))
                               )  # drone_centroid
        self.result = None
Esempio n. 15
0
def yoloPredict(sess, file_name):
    t1 = time.time()
    original_image = cv2.imread(image_path_prefix + file_name + '.png')
    original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
    original_image_size = original_image.shape[:2]
    image_data = utils.image_preporcess(np.copy(original_image),
                                        [input_size, input_size])
    image_data = image_data[np.newaxis, ...]
    return_tensors = utils.read_pb_return_tensors(graph, pb_file,
                                                  return_elements)
    print("Image loaded in {:.3f}s".format(time.time() - t1))
    t1 = time.time()
    pred_sbbox, pred_mbbox, pred_lbbox = sess.run(
        [return_tensors[1], return_tensors[2], return_tensors[3]],
        feed_dict={return_tensors[0]: image_data})
    pred_bbox = np.concatenate([
        np.reshape(pred_sbbox, (-1, 5 + num_classes)),
        np.reshape(pred_mbbox, (-1, 5 + num_classes)),
        np.reshape(pred_lbbox, (-1, 5 + num_classes))
    ],
                               axis=0)
    print("YOLO Inferenced in {:.3f}s".format(time.time() - t1))
    t1 = time.time()
    bboxes = utils.postprocess_boxes(pred_bbox, original_image_size,
                                     input_size, 0.2)
    bboxes = utils.nms(bboxes, 0.45, method='nms')
    print("NMS in {:.3f}s".format(time.time() - t1))
    roi_box = []
    for i, bbox in enumerate(bboxes):
        if (bbox[4] >= 0.5):  # 0: person, 2: car , 1: bicycle (NOT cyclist!)
            roi_box.append(bboxes[i])
            x_min = int(bbox[0])
            x_max = int(bbox[2])
            y_min = int(bbox[1])
            y_max = int(bbox[3])


#            print (x_min, y_min, x_max, y_max)
    image = utils.draw_bbox(original_image, roi_box)
    image = Image.fromarray(image)
    image.show()
    input("ENTER")
    return roi_box
Esempio n. 16
0
 def __init__(self):
     self.IMAGE_H = 416
     self.IMAGE_W = 416
     self.classes = utils.read_coco_names('./data/coco.names')
     self.num_classes = len(self.classes)
     self.gpu_nms_graph = tf.Graph()
     self.input_tensor, self.output_tensors = utils.read_pb_return_tensors(
         self.gpu_nms_graph,
         "./checkpoint/yolov3_gpu_nms.pb",
         ["Placeholder:0", "concat_10:0", "concat_11:0", "concat_12:0"])
     self.sess = tf.Session(graph=self.gpu_nms_graph)
     self.last_PIL_image = None  # 原图
     self.last_boxes = None
     self.last_scores = None
     self.last_labels = None
     self.colors = [[254.0, 254.0, 254], [248.92, 228.6, 127], [243.84, 203.20000000000002, 0], [238.76, 177.79999999999998, -127], [233.68, 152.4, -254], [228.6, 127.0, 254], [223.52, 101.60000000000001, 127], [218.44, 76.20000000000002, 0], [213.35999999999999, 50.79999999999999, -127], [208.28000000000003, 25.399999999999995, -254], [203.20000000000002, 0.0, 254], [198.12, -25.400000000000023, 127], [193.04, -50.79999999999999, 0], [187.96, -76.20000000000002, -127], [182.88, -101.59999999999998, -254], [177.79999999999998, -127.0, 254], [172.71999999999997, -152.40000000000003, 127], [167.64, -177.79999999999998, 0], [162.56, -203.20000000000002, -127], [157.48, -228.59999999999997, -254], [152.4, -254.0, 254], [147.32000000000002, -279.40000000000003, 127], [142.24, -304.80000000000007, 0], [137.16, -330.19999999999993, -127], [132.08, -355.59999999999997, -254], [127.0, 254.0, 254], [121.92, 228.6, 127], [116.83999999999999, 203.20000000000002, 0], [111.75999999999999, 177.79999999999998, -127], [106.68, 152.4, -254], [101.60000000000001, 127.0, 254], [96.52, 101.60000000000001, 127], [91.44, 76.20000000000002, 0], [86.35999999999999, 50.79999999999999, -127], [81.27999999999999, 25.399999999999995, -254], [76.20000000000002, 0.0, 254], [71.12, -25.400000000000023, 127], [66.04, -50.79999999999999, 0], [60.96, -76.20000000000002, -127], [55.879999999999995, -101.59999999999998, -254], [50.79999999999999, -127.0, 254], [45.72000000000001, -152.40000000000003, 127], [40.64000000000001, -177.79999999999998, 0], [35.56, -203.20000000000002, -127], [30.48, -228.59999999999997, -254], [25.399999999999995, -254.0, 254], [20.31999999999999, -279.40000000000003, 127], [15.240000000000013, -304.80000000000007, 0], [10.160000000000009, -330.19999999999993, -127], [5.0800000000000045, -355.59999999999997, -254], [0.0, 254.0, 254], [-5.0800000000000045, 228.6, 127], [-10.160000000000009, 203.20000000000002, 0], [-15.240000000000013, 177.79999999999998, -127], [-20.320000000000018, 152.4, -254], [-25.400000000000023, 127.0, 254], [-30.480000000000025, 101.60000000000001, 127], [-35.559999999999974, 76.20000000000002, 0], [-40.63999999999998, 50.79999999999999, -127], [-45.719999999999985, 25.399999999999995, -254], [-50.79999999999999, 0.0, 254], [-55.879999999999995, -25.400000000000023, 127], [-60.96, -50.79999999999999, 0], [-66.04, -76.20000000000002, -127], [-71.12, -101.59999999999998, -254], [-76.20000000000002, -127.0, 254], [-81.28000000000002, -152.40000000000003, 127], [-86.36000000000001, -177.79999999999998, 0], [-91.44000000000003, -203.20000000000002, -127], [-96.51999999999997, -228.59999999999997, -254], [-101.59999999999998, -254.0, 254], [-106.67999999999998, -279.40000000000003, 127], [-111.75999999999999, -304.80000000000007, 0], [-116.83999999999999, -330.19999999999993, -127], [-121.92, -355.59999999999997, -254], [-127.0, 254.0, 254], [-132.08, 228.6, 127], [-137.16, 203.20000000000002, 0], [-142.24, 177.79999999999998, -127], [-147.32000000000002, 152.4, -254]]
     self.label_name = ['person', 'bicycle', 'car', 'motorbike', 'aeroplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter',
                        'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
                        'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
                        'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'sofa', 'pottedplant', 'bed', 'diningtable', 'toilet', 'tvmonitor', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven',
                        'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']
Esempio n. 17
0
    def __init__(self):
        # pb 模型路径
        self.pb_model_path = cfg.TEST.TEST_PB_MODEL_PATH
        # yolov3 网络 返回 3 个尺度节点
        self.return_elements = cfg.TEST.RETURN_ELEMENTS
        # class_name 字典
        self.class_name_dir = utils.read_class_names(
            cfg.COMMON.CLASS_FILE_PATH)
        # c 类 个数
        self.class_name_len = len(self.class_name_dir)
        # 输入尺度
        self.input_size = cfg.TEST.INPUT_SIZE
        # 视频文件路径
        self.video_path = cfg.TEST.VEDIO_PATH

        self.graph = tf.Graph()
        # 加载模型
        self.return_tensors = utils.read_pb_return_tensors(
            self.graph, self.pb_model_path, self.return_elements)
        self.sess = tf.Session(graph=self.graph)
        pass
Esempio n. 18
0
    def __init__(self):

        self.car_boxes = []

        os.chdir(cwd)

        self.return_elements = [
            "input/input_data:0", "pred_sbbox/concat_2:0",
            "pred_mbbox/concat_2:0", "pred_lbbox/concat_2:0"
        ]
        self.pb_file = "./yolov3/frozen_inference_graph.pb"
        self.num_classes = 80
        self.input_size = 416
        self.detection_graph = tf.Graph()

        # configuration for possible GPU use
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True

        self.sess = tf.Session(graph=self.detection_graph, config=config)
        self.return_tensors = utils.read_pb_return_tensors(
            self.detection_graph, self.pb_file, self.return_elements)
Esempio n. 19
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('file_path',
                        help='Provide the path to a file with data')
    args = parser.parse_args()

    inputFilename = args.file_path

    if not os.path.exists(inputFilename):
        print(f"The folder {inputFilename} does not exist. Quitting...")
        sys.exit()

    now = datetime.now().strftime('%Y-%m-%d_%H:%M:%S')

    sourcePathAbs = os.path.abspath(inputFilename)
    sourceFileHead, sourceFileTail = os.path.split(sourcePathAbs)
    outputPath = sourceFileTail + "_" + now
    targetFolder = sourceFileHead + "/" + outputPath
    print("Processed files will be saved to folder ", targetFolder)

    try:
        os.mkdir(targetFolder)
        print("Directory ", targetFolder, " created")
    except FileExistsError:
        print("Directory ", targetFolder, " already exists...")

    with open(inputFilename, 'r') as f:
        lines = f.readlines()

    with tf.Session(graph=graph) as sess:
        return_tensors = utils.read_pb_return_tensors(graph, pb_file,
                                                      return_elements)

        for line in tqdm(lines):
            process(line.split()[0], targetFolder, sess, return_tensors)

    print(f"Done! {len(lines)} files have been saved to folder ", targetFolder)
Esempio n. 20
0
    "pred_lbbox/concat_2:0"
]
pb_file = "./yolov3_96_coco.pb"
image_path = "./0C006B5C.jpg"
num_classes = 1
input_size = 544
graph = tf.Graph()

original_image = cv2.imread(image_path)
original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
original_image_size = original_image.shape[:2]
image_data = utils.image_preporcess(np.copy(original_image),
                                    [input_size, input_size])
image_data = image_data[np.newaxis, ...]

return_tensors = utils.read_pb_return_tensors(graph, pb_file, return_elements)

with tf.Session(graph=graph) as sess:
    pred_sbbox, pred_mbbox, pred_lbbox = sess.run(
        [return_tensors[1], return_tensors[2], return_tensors[3]],
        feed_dict={return_tensors[0]: image_data})

pred_bbox = np.concatenate([
    np.reshape(pred_sbbox, (-1, 5 + num_classes)),
    np.reshape(pred_mbbox, (-1, 5 + num_classes)),
    np.reshape(pred_lbbox, (-1, 5 + num_classes))
],
                           axis=0)
print(pred_bbox)
bboxes = utils.nms(pred_bbox, 0.45, method='soft-nms')
bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size,
Esempio n. 21
0
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
config = tf.ConfigProto()
config.gpu_options.allow_growth = True

IMAGE_H, IMAGE_W = 416, 416
classes = utils.read_coco_names('./data/raccoon.names')
num_classes = len(classes)
image_path = "data/hide_specific_target_dataset/000000336584.jpg"  # 181,
img = Image.open(image_path)
img_resized = np.array(img.resize(size=(IMAGE_W, IMAGE_H)), dtype=np.float32)
img_resized = img_resized / 255.
cpu_nms_graph = tf.Graph()

input_tensor, output_tensors = utils.read_pb_return_tensors(
    cpu_nms_graph, "./checkpoint/yolov3_cpu_nms.pb",
    ["Placeholder:0", "concat_9:0", "mul_6:0"])
with tf.Session(graph=cpu_nms_graph) as sess:
    boxes, scores = sess.run(
        output_tensors,
        feed_dict={input_tensor: np.expand_dims(img_resized, axis=0)})
    boxes, scores, labels = utils.cpu_nms(boxes,
                                          scores,
                                          num_classes,
                                          score_thresh=0.3,
                                          iou_thresh=0.5)
    image = utils.draw_bdeoxes(img,
                               boxes,
                               scores,
                               labels,
                               classes, [IMAGE_H, IMAGE_W],
Esempio n. 22
0
def main(video_path):
    global end,init
    return_elements = ["input/input_data:0", "pred_sbbox/concat_2:0", "pred_mbbox/concat_2:0", "pred_lbbox/concat_2:0"]
    pb_file = "./yolov3_coco.pb"
    num_classes = 80  # 使用原有训练权重,有80个
    input_size = 416
    graph = tf.Graph()  # 计算图,表示实例化一个用于tensorflow计算和表示用的数据流图,不负责运行计算
    return_tensors = utils.read_pb_return_tensors(graph, pb_file, return_elements)
    framenumber = 1
    junzhi=10
    with tf.Session(graph=graph) as sess:
        vid = cv2.VideoCapture(video_path)  # 获取视频
        init = 0
        fourcc = cv2.VideoWriter_fourcc('X','V','I','D')
        size = (int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)), int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)))
        out = cv2.VideoWriter(os.path.join(path_,'result2.avi'), fourcc, 30.0, size)
        while (1):
            return_value, frame = vid.read()
            #frame = cv2.flip(frame, 1)   #水平颠倒
            framenumber = framenumber + 1
            currentFrame = framenumber
            if currentFrame % 6 != 0:
                out.write(frame)
                continue
            if return_value:
                J = dehaze.DeHaze(frame)
                # frame = cv2.cvtColor(J, cv2.COLOR_BGR2RGB)
                #image = Image.fromarray(J)
                frame = J
            else:
                raise ValueError("No image!")
            frame1 = Pedestrian_Detection.detect(frame)    #识别行人
            frame_size = frame.shape[:2]
            image_data = utils.image_preporcess(np.copy(frame), [input_size, input_size])
            image_data = image_data[np.newaxis, ...]
            #prev_time = time.time()

            pred_sbbox, pred_mbbox, pred_lbbox = sess.run(
                [return_tensors[1], return_tensors[2], return_tensors[3]],
                feed_dict={return_tensors[0]: image_data})

            pred_bbox = np.concatenate([np.reshape(pred_sbbox, (-1, 5 + num_classes)),
                                        np.reshape(pred_mbbox, (-1, 5 + num_classes)),
                                        np.reshape(pred_lbbox, (-1, 5 + num_classes))], axis=0)
            # np.concatenate  numpy.concatenate((a1,a2,...), axis=0)函数。能够一次完成多个数组的拼接。其中a1,a2,...是拼接数组的名字
            # np.reshape(a,newshape,order='C')  a:数组——需要处理的数据  newshape:新的格式——整数或整数数组,如(2,3)表示2行3列。新的形状应该与原来的形状兼容,即行数和列数相乘后等于a中元素的数量。
            bboxes = utils.postprocess_boxes(pred_bbox, frame_size, input_size,
                                             0.3)  # pred_bbox 预测的框架  frame_size  框架尺寸

            bboxes = utils.nms(bboxes, 0.45, method='nms')

            image,junzhi = utils.draw_bbox(frame1, bboxes, junzhi)  # 绘制框
            for i, bbox in enumerate(bboxes):
                coor = np.array(bbox[:4], dtype=np.int32)
                if coor[2]-coor[0] > junzhi*1.5:
                    winsound.Beep(600, 100)
            #pygame.mixer.music.play(1)
            out.write(image)
            #result = np.asarray(image)
            #cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE)
            #result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
            pilImage = Image.fromarray(image)
            pilImage = pilImage.resize((700, 380), Image.ANTIALIAS)
            tkImage = ImageTk.PhotoImage(image=pilImage)
            bj.create_image(0, 0, anchor='nw', image=tkImage)
            window.update_idletasks()
            window.update()
            str1.set('进行中')
            if end == 1:
                str1.set('欢迎使用')
                end = 0
                init = 1
                window.update()
                sess.close()
            if cv2.waitKey(1) & 0xFF == ord('q'):
                str1.set('欢迎使用')
                window.update()
                init = 1

                break
    str1.set('欢迎使用')
    window.update()
    init = 1
    end = 0
    return
Esempio n. 23
0
    p2x = box[2] * xDist
    p2y = box[3] * yDist

    return int(p1x), int(p1y), int(p2x), int(p2y)


classesPath = "/home/benoit/Documents/Stage2A/tensorflow-yolov3/data/coco.names"
modelPath = "/home/benoit/Documents/Stage2A/tensorflow-yolov3/checkpoint/yolov3_cpu_nms.pb"
destPath = "/home/benoit/Documents/Stage2A/resources/results/detection"

IMAGE_H, IMAGE_W = 416, 416
classes = utils.read_coco_names(classesPath)
num_classes = len(classes)
input_tensor, output_tensors = utils.read_pb_return_tensors(
    tf.get_default_graph(), modelPath,
    ["Placeholder:0", "concat_9:0", "mul_6:0"])

videosPaths = {
    "CP":
    "/home/benoit/Documents/Stage2A/resources/CP_dataset/data/P2L_S5_C3.1/P2L_S5_C3.1.mp4",
    # "PCDS": "/home/benoit/Documents/Stage2A/resources/PCDS_dataset/25_20160407_back/normal/crowd/2016_04_07_18_24_54BackColor.avi",
    # "MIVIA": "/home/benoit/Documents/Stage2A/resources/MIVIA_dataset/Dataset People Counting MIVIA/DBc/VIDEOS/RGB/C_I_S_1.mkv",
    # "MOT": "/home/benoit/Documents/Stage2A/resources/MOT_dataset/2DMOT2015/train/PETS09-S2L1/img1/PETS09-S2L1.mp4"
}

with tf.Session() as sess:
    for name, videoPath in videosPaths.items():
        video = cv.VideoCapture(videoPath)
        name = destPath + "/" + name + ".avi"
Esempio n. 24
0
#================================================================

import cv2
import time
import numpy as np
import tensorflow as tf
from PIL import Image
from core import utils

IMAGE_H, IMAGE_W = 416, 416
video_path = "./data/demo_data/road.mp4"
video_path = 0  # use camera
classes = utils.read_class_names('./data/coco.names')
num_classes = len(classes)
input_tensor, output_tensors = utils.read_pb_return_tensors(
    tf.get_default_graph(), "./checkpoint/yolov3_cpu_nms.pb",
    ["Placeholder:0", "concat_9:0", "mul_6:0", "concat_8:0"])
with tf.Session() as sess:
    vid = cv2.VideoCapture(video_path)
    while True:
        return_value, frame = vid.read()
        if return_value:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            image = Image.fromarray(frame)
        else:
            raise ValueError("No image!")
        img_resized = np.array(image.resize(size=(IMAGE_H, IMAGE_W)),
                               dtype=np.float32)
        img_resized = img_resized / 255.
        prev_time = time.time()
Esempio n. 25
0
def video_without_saving(ip, threshold):

    # 对方socket
    des_socket = socket(AF_INET, SOCK_STREAM)
    # 链接服务器
    des_socket.connect(('127.0.0.1', 8000))

    classes = utils.read_class_names(cfg.YOLO.CLASSES)
    num_classes = len(classes)
    return_elements = [
        "input/input_data:0", "pred_sbbox/concat_2:0", "pred_mbbox/concat_2:0",
        "pred_lbbox/concat_2:0"
    ]
    pb_file = "./yolov3_coco.pb"
    video_path = ip

    input_size = 416
    graph = tf.Graph()
    return_tensors = utils.read_pb_return_tensors(graph, pb_file,
                                                  return_elements)

    with tf.Session(graph=graph) as sess:
        messageId = 0
        encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 50]
        # 之后修改
        # vid = cv2.VideoCapture(video_path)
        vid = cv2.VideoCapture(0)
        while True:
            # time.sleep(0.01)
            curr_time = datetime.datetime.now()
            timestamp = '%s-%s-%s %s:%s:%s' % (
                curr_time.year, curr_time.month, curr_time.day, curr_time.hour,
                curr_time.minute, curr_time.second)

            return_value, frame = vid.read()

            result_, imgencode = cv2.imencode('.jpg', frame, encode_param)
            data = np.array(imgencode)
            stringData = data.tostring()
            length = len(stringData)


            if return_value:
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                image = Image.fromarray(frame)
            else:
                raise ValueError("No image!")
            frame_size = frame.shape[:2]
            image_data = utils.image_preporcess(np.copy(frame),
                                                [input_size, input_size])
            image_data = image_data[np.newaxis, ...]

            pred_sbbox, pred_mbbox, pred_lbbox = sess.run(
                [return_tensors[1], return_tensors[2], return_tensors[3]],
                feed_dict={return_tensors[0]: image_data})

            pred_bbox = np.concatenate([
                np.reshape(pred_sbbox, (-1, 5 + num_classes)),
                np.reshape(pred_mbbox, (-1, 5 + num_classes)),
                np.reshape(pred_lbbox, (-1, 5 + num_classes))
            ],
                                       axis=0)

            bboxes = utils.postprocess_boxes(pred_bbox, frame_size, input_size,
                                             threshold)
            bboxes = utils.nms(bboxes, 0.45, method='nms')
            image = utils.draw_bbox(frame, bboxes)

            result = np.asarray(image)

            cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE)
            result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
            cv2.imshow("result", result)
            if cv2.waitKey(1) & 0xFF == ord('q'): break

            messageId += 1
            mess_send(des_socket, bboxes, timestamp, messageId, ip, length,stringData)
Esempio n. 26
0
    def test(self, source_dir=None, image_path=None, show=False):
        if source_dir:
            image_name = choice(os.listdir(source_dir))
            image_path = os.path.join(source_dir, image_name)

        img = cv2.imread(image_path, 0)
        img = cv2.resize(src=img, dsize=(0, 0), fx=1 / 2, fy=1 / 2)
        if img.shape[0] < self.img_h and img.shape[1] < self.img_w:
            img = cv2.copyMakeBorder(src=img,
                                     top=(self.img_h - img.shape[0]) // 2,
                                     bottom=(self.img_h - img.shape[0]) // 2,
                                     left=(self.img_w - img.shape[1]) // 2,
                                     right=(self.img_w - img.shape[1]) // 2,
                                     borderType=cv2.BORDER_CONSTANT,
                                     value=[255, 255, 255])

        img = cv2.resize(img, (self.img_w, self.img_h))
        img = expand_dims(img, axis=2)

        classes = os.listdir(self.orig_letters)

        cpu_nms_graph = tf.Graph()

        input_tensor, output_tensors = utils.read_pb_return_tensors(
            cpu_nms_graph,
            os.path.join(self.checkpoint_dir, "yolov3_cpu_nms.pb"),
            ["Placeholder:0", "concat_5:0", "mul_2:0"])

        with tf.Session(graph=cpu_nms_graph) as sess:
            boxes, scores = sess.run(
                output_tensors,
                feed_dict={input_tensor: np.expand_dims(img, axis=0)})
            boxes = boxes.reshape(-1, 4)
            scores = scores.reshape(-1, self.num_classes)

            min_wh, max_wh = -10000, 10000
            min_ratio = 1 / 4  # 0 -- 1

            mask = np.logical_and(boxes[:, 0] >= min_wh, boxes[:, 0] <= max_wh)
            mask = np.logical_and(mask, boxes[:, 1] >= min_wh)
            mask = np.logical_and(mask, boxes[:, 2] >= min_wh)
            mask = np.logical_and(mask, boxes[:, 3] >= min_wh)
            mask = np.logical_and(mask, boxes[:, 1] <= max_wh)
            mask = np.logical_and(mask, boxes[:, 2] <= max_wh)
            mask = np.logical_and(mask, boxes[:, 3] <= max_wh)
            mask = np.logical_and(mask, boxes[:, 0] < boxes[:, 2])
            mask = np.logical_and(mask, boxes[:, 1] < boxes[:, 3])

            boxes = boxes[mask]
            scores = scores[mask]

            h = abs(boxes[:, 2] - boxes[:, 0])
            w = abs(boxes[:, 3] - boxes[:, 1])

            mask = np.logical_and(w / h > min_ratio, h / w > min_ratio)

            boxes = boxes[mask]
            scores = scores[mask]

            if self.filters:
                # Harder filters
                print(f"Test: Boxes before filtering:\t{boxes.shape[0]}")

                mask = np.logical_and(boxes[:, 0] >= 0,
                                      boxes[:, 0] <= img.shape[1])
                mask = np.logical_and(mask, boxes[:, 1] >= 0)
                mask = np.logical_and(mask, boxes[:, 2] >= 0)
                mask = np.logical_and(mask, boxes[:, 3] >= 0)
                mask = np.logical_and(mask, boxes[:, 1] <= img.shape[0])
                mask = np.logical_and(mask, boxes[:, 2] <= img.shape[1])
                mask = np.logical_and(mask, boxes[:, 3] <= img.shape[0])
                mask = np.logical_and(mask, boxes[:, 0] < boxes[:, 2])
                mask = np.logical_and(mask, boxes[:, 1] < boxes[:, 3])
                mask = np.logical_and(
                    mask,
                    abs(boxes[:, 2] - boxes[:, 0]) >= self.size_threshold[0])
                mask = np.logical_and(
                    mask,
                    abs(boxes[:, 3] - boxes[:, 1]) >= self.size_threshold[1])

                boxes = boxes[mask]
                scores = scores[mask]

                print(f"Test: Boxes after filtering:\t{boxes.shape[0]}")

                if boxes.shape[0] == 0:
                    print(
                        f"Try changing the filters/thresholds in the parameters."
                    )

            boxes, scores, labels = utils.cpu_nms(boxes=boxes,
                                                  scores=scores,
                                                  num_classes=self.num_classes,
                                                  max_boxes=self.max_boxes)

            (image,
             results) = utils.draw_boxes(img,
                                         boxes,
                                         scores,
                                         labels,
                                         classes, [self.img_h, self.img_w],
                                         show=show,
                                         size_threshold=self.size_threshold)

        return results
def main(video_path):
    return_elements = [
        "input/input_data:0", "pred_sbbox/concat_2:0", "pred_mbbox/concat_2:0",
        "pred_lbbox/concat_2:0"
    ]
    pb_file = "./yolov3_coco.pb"
    num_classes = 80  # 使用原有训练权重,有80个
    input_size = 416
    graph = tf.Graph()  # 计算图,表示实例化一个用于tensorflow计算和表示用的数据流图,不负责运行计算
    return_tensors = utils.read_pb_return_tensors(graph, pb_file,
                                                  return_elements)
    framenumber = 1

    with tf.Session(graph=graph) as sess:
        vid = cv2.VideoCapture(video_path)  # 获取视频
        while True:
            return_value, frame = vid.read()
            framenumber = framenumber + 1
            currentFrame = framenumber
            if currentFrame % 6 != 0: continue
            if return_value:
                J = dehaze.DeHaze(frame)
                # frame = cv2.cvtColor(J, cv2.COLOR_BGR2RGB)
                #image = Image.fromarray(J)
                frame = J
            else:
                raise ValueError("No image!")
            frame1 = Pedestrian_Detection.detect(frame)  #识别行人
            frame_size = frame.shape[:2]
            image_data = utils.image_preporcess(np.copy(frame),
                                                [input_size, input_size])
            image_data = image_data[np.newaxis, ...]
            prev_time = time.time()

            pred_sbbox, pred_mbbox, pred_lbbox = sess.run(
                [return_tensors[1], return_tensors[2], return_tensors[3]],
                feed_dict={return_tensors[0]: image_data})

            pred_bbox = np.concatenate([
                np.reshape(pred_sbbox, (-1, 5 + num_classes)),
                np.reshape(pred_mbbox, (-1, 5 + num_classes)),
                np.reshape(pred_lbbox, (-1, 5 + num_classes))
            ],
                                       axis=0)
            # np.concatenate  numpy.concatenate((a1,a2,...), axis=0)函数。能够一次完成多个数组的拼接。其中a1,a2,...是拼接数组的名字
            # np.reshape(a,newshape,order='C')  a:数组——需要处理的数据  newshape:新的格式——整数或整数数组,如(2,3)表示2行3列。新的形状应该与原来的形状兼容,即行数和列数相乘后等于a中元素的数量。
            bboxes = utils.postprocess_boxes(
                pred_bbox, frame_size, input_size,
                0.3)  # pred_bbox 预测的框架  frame_size  框架尺寸
            bboxes = utils.nms(bboxes, 0.45, method='nms')
            image = utils.draw_bbox(frame1, bboxes)  # 绘制框
            #result = np.asarray(image)
            curr_time = time.time()
            exec_time = curr_time - prev_time
            #info = "time: %.2f ms" % (1000 * exec_time)
            cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE)
            result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
            cv2.imshow("result", result)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        vid.release()
        cv2.destroyAllWindows()
Esempio n. 28
0
IMAGE_H, IMAGE_W = 416, 416
classes = utils.read_coco_names('./data/coco.names')
num_classes = len(classes)

photo_path = "./data/demo_data/car.jpg"
image = cv2.imread(photo_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = Image.fromarray(image)
img_resized = np.array(image.resize(size=(IMAGE_H, IMAGE_W)), dtype=np.float32)
img_resized = img_resized / 255.

gpu_nms_graph = tf.Graph()
# nms on GPU
input_tensor, output_tensors = utils.read_pb_return_tensors(
    gpu_nms_graph, "./checkpoint/yolov3_gpu_nms.pb",
    ["Placeholder:0", "concat_10:0", "concat_11:0", "concat_12:0"])

with tf.Session(graph=gpu_nms_graph) as sess:
    for i in range(5):
        start = time.time()
        boxes, scores, labels = sess.run(
            output_tensors,
            feed_dict={input_tensor: np.expand_dims(img_resized, axis=0)})
        print("=> nms on gpu the number of boxes= %d  time=%.2f ms" %
              (len(boxes), 1000 * (time.time() - start)))

    image = utils.draw_boxes(image,
                             boxes,
                             scores,
                             labels,
Esempio n. 29
0
import cv2
import argparse

IMAGE_H, IMAGE_W = 416, 416
parser = argparse.ArgumentParser(description="gpu模式下不能设置score_thresh和iou_thresh")
parser.add_argument("--video_id", "-vi", default=0, help="传入相机的id,可以是图片,视频,网络摄像头(eg:http://admin:admin@ip:端口/")
parser.add_argument("--model", "-m", default="cpu", choices=["cpu", "gpu"], help="选择gpu中运行还是在cpu中运行")
parser.add_argument("--score_thresh", "-st", default=0.5, type=float, help="设置score_thresh值,越高所获得的box越少(仅在cpu模式下生效)")
parser.add_argument("--iou_thresh", "-it", default=0.5, type=float, help="设置score_thresh值,越高所获得的box越少(仅在cpu模式下生效)")
flags = parser.parse_args()

classes = utils.read_coco_names('./data/coco.names')
num_classes = len(classes)
graph = tf.Graph()
if flags.model == "cpu":
    input_tensor, output_tensors = utils.read_pb_return_tensors(graph, "data/checkpoint/yolov3_cpu_nms.pb",
                                                                ["Placeholder:0", "concat_9:0", "mul_6:0"])
else:
    input_tensor, output_tensors = utils.read_pb_return_tensors(graph, "data/checkpoint/yolov3_gpu_nms.pb",
                                                                ["Placeholder:0", "concat_10:0", "concat_11:0",
                                                                 "concat_12:0"])

with tf.Session(graph=graph) as sess:
    vid = cv2.VideoCapture(flags.video_id)
    while True:
        return_value, frame = vid.read()
        if return_value:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            image = Image.fromarray(frame)
        else:
            raise ValueError("No image!")
        img_resized = np.array(image.resize(size=(IMAGE_H, IMAGE_W)), dtype=np.float32)
Esempio n. 30
0
 def detect_inference(self):
     self.update_process_message.emit('Detection Process')
     graph = tf.Graph()
     detections = []
     return_tensors = utils.read_pb_return_tensors(
         graph, self.window.pb_file, self.window.return_elements)
     # 创建进度条
     pbar = tqdm(total=self.window.total_frame_counter)
     with tf.Session(graph=graph) as sess:
         if self.window.writeVideo_flag:
             isOutput = True if self.window.output_path != "" else False
             if isOutput:
                 video_FourCC = cv2.VideoWriter_fourcc(*'MPEG')
                 out = cv2.VideoWriter(self.window.output_path,
                                       video_FourCC, self.window.media_fps,
                                       self.window.media_size)
                 frame_index = -1
         while True:
             while not self.window.is_on:
                 pass
             return_value, frame = self.window.vid.read()
             image_index = self.window.vid.get(1)
             if return_value != True:
                 break
             if return_value:
                 image = Image.fromarray(frame)
                 self.window.frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
             else:
                 raise ValueError("No image!")
             frame_size = frame.shape[:2]
             image_data = utils.image_preporcess(
                 np.copy(frame),
                 [self.window.input_size, self.window.input_size])
             image_data = image_data[np.newaxis, ...]
             pred_sbbox, pred_mbbox, pred_lbbox = sess.run(
                 [return_tensors[1], return_tensors[2], return_tensors[3]],
                 feed_dict={return_tensors[0]: image_data})
             pred_bbox = np.concatenate([
                 np.reshape(pred_sbbox, (-1, 5 + self.window.num_classes)),
                 np.reshape(pred_mbbox, (-1, 5 + self.window.num_classes)),
                 np.reshape(pred_lbbox, (-1, 5 + self.window.num_classes))
             ],
                                        axis=0)
             bboxes = utils.postprocess_boxes(pred_bbox, frame_size,
                                              self.window.input_size, 0.45)
             bboxes = utils.nms(bboxes, 0.45, method='nms')
             image = utils.draw_bbox(frame, bboxes)
             detections = save_mod(bboxes, 0.6)
             result = np.asarray(image)
             if self.window.writeVideo_flag:
                 # save a frame
                 out.write(result)
             if self.window.showVideo_flag:
                 self.update_graphic_viewer.emit(result)
                 pbar.update(1)
                 self.window.ui.processrate.setText(str(pbar))
             else:
                 pbar.update(1)
                 self.window.ui.processrate.setText(str(pbar))
     # Release everything if job is finished
     if self.window.writeVideo_flag:
         out.release()
     pbar.close()
     # 多目标追踪
     trackers = track_viou_video(self.window.media_path, detections, 0.5,
                                 0.6, 0.1, 23, 16, 'MEDIANFLOW', 1.0,
                                 self.window)
     # 保存 trackers
     with open(self.window.pickle_file_path, 'wb') as pk_f:
         pickle.dump(trackers, pk_f)
         self.window.ui.processrate.setText('=> saved trackers to pk file.')