Exemple #1
0
def main(argv=None):
    if FLAGS.tiny:
        model = yolo_v3_tiny.yolo_v3_tiny
    elif FLAGS.spp:
        model = yolo_v3.yolo_v3_spp
    else:
        model = yolo_v3.yolo_v3

    classes = load_coco_names(FLAGS.class_names)

    # placeholder for detector inputs
    inputs = tf.placeholder(
        tf.float32, [None, FLAGS.size, FLAGS.size, 3], "inputs")

    with tf.variable_scope('detector'):
        detections = model(inputs, len(classes), data_format=FLAGS.data_format)
        load_ops = load_weights(tf.global_variables(
            scope='detector'), FLAGS.weights_file)

    # Sets the output nodes in the current session
    boxes = detections_boxes(detections)

    with tf.Session() as sess:
        sess.run(load_ops)
        savepb(sess, FLAGS.output_graph)
Exemple #2
0
def main(argv=None):
    if FLAGS.tiny:
        model = yolo_v3_tiny.yolo_v3_tiny
    else:
        model = yolo_v3.yolo_v3

    classes = load_coco_names(FLAGS.class_names)

    # placeholder for detector inputs
    # any size > 320 will work here
    inputs = tf.placeholder(tf.float32, [None, 416, 416, 3])

    with tf.variable_scope('detector'):
        detections = model(inputs, len(classes),
                           data_format=FLAGS.data_format)
        load_ops = load_weights(tf.global_variables(
            scope='detector'), FLAGS.weights_file)

    saver = tf.train.Saver(tf.global_variables(scope='detector'))

    with tf.Session() as sess:
        sess.run(load_ops)

        save_path = saver.save(sess, save_path=FLAGS.ckpt_file)
        print('Model saved in path: {}'.format(save_path))
Exemple #3
0
def build_detection_graph():
    input_data = tf.placeholder(dtype=tf.uint8, shape=[FLAGS.size, FLAGS.size, 3], name='input_data')
    input_data = tf.expand_dims(input_data, 0)
    input_data = tf.cast(input_data, tf.float32)
    input_data = input_data / 255.
    if FLAGS.tiny:
        model = yolo_v3_tiny.yolo_v3_tiny
    elif FLAGS.spp:
        model = yolo_v3.yolo_v3_spp
    else:
        model = yolo_v3.yolo_v3
    classes = load_coco_names(FLAGS.class_names)

   # yolo_model = model(cfgs.class_num, cfgs.anchors)
    with tf.variable_scope('detector'):
        detections = model(input_data, len(classes), data_format=FLAGS.data_format)
        print(detections.get_shape().as_list())
    boxes, pred_confs, pred_probs = tf.split(detections, [4, 1, len(classes)], axis=-1)
    center_x, center_y, width, height = tf.split(boxes, [1, 1, 1, 1], axis=-1)
    x_min = center_x - width / 2
    y_min = center_y - height / 2
    x_max = center_x + width / 2
    y_max = center_y + height / 2
    pred_boxes = tf.concat([x_min, y_min, x_max, y_max], axis=-1)

    pred_scores = pred_confs * pred_probs
    boxes, scores, labels = gpu_nms(pred_boxes, pred_scores, len(classes), max_boxes=20, score_thresh=0.3,
                                    nms_thresh=0.4)

    boxes = tf.identity(boxes, name='boxes')
    scores = tf.identity(scores, name='scores')
    labels = tf.identity(labels, name='labels')

    return boxes, scores, labels
Exemple #4
0
def main(argv=None):
    if FLAGS.tiny:
        model = yolo_v3_tiny.yolo_v3_tiny
    elif FLAGS.spp:
        model = yolo_v3.yolo_v3_spp
    else:
        model = yolo_v3.yolo_v3

    classes = load_coco_names(FLAGS.class_names)

    # placeholder for detector inputs
    inputs = tf.placeholder(tf.float32, [None, FLAGS.size, FLAGS.size, 3],
                            "inputs")

    with tf.variable_scope('detector'):
        detections = model(
            inputs, len(classes), data_format=FLAGS.data_format
        )  # 得到yolov3整体模型(包含模型输出(?, 10647, (num_classes + 5)))
        load_ops = load_weights(tf.global_variables(scope='detector'),
                                FLAGS.weights_file)

    # Sets the output nodes in the current session
    boxes = detections_boxes(
        detections)  # 1,将整体输出分解为box结果与概率数值结果;2、将结果名称定义为output_boxes再放入graph中

    with tf.Session() as sess:
        sess.run(load_ops)
        reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path)
        var_to_shape_map = reader.get_variable_to_shape_map()
        for key in var_to_shape_map:
            print("tensor_name: ", key)
        freeze_graph(sess, FLAGS.output_graph)
Exemple #5
0
def main(input_path, DEBUG):
    gpu_options = tf.GPUOptions(
        per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction)
    config = tf.ConfigProto(
        gpu_options=gpu_options,
        log_device_placement=False,
    )
    classes = load_coco_names(FLAGS.class_names)
    frozenGraph = load_graph(FLAGS.frozen_model)
    boxes, inputs = get_boxes_and_inputs_pb(frozenGraph)
    boxes_list = []
    with tf.Session(graph=frozenGraph, config=config) as sess:
        for item in input_path:
            start = clock()
            FLAGS.input_img = item
            img = Image.open(FLAGS.input_img)
            img_resized = letter_box_image(img, FLAGS.size, FLAGS.size, 128)
            img_resized = img_resized.astype(np.float32)
            detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]})
            filtered_boxes = non_max_suppression(
                detected_boxes,
                confidence_threshold=FLAGS.conf_threshold,
                iou_threshold=FLAGS.iou_threshold)
            boxes_list.append(filtered_boxes)
            if DEBUG:
                draw_boxes(filtered_boxes, img, classes,
                           (FLAGS.size, FLAGS.size), True)
            print(filtered_boxes)
            print("Execution Time : {} / #Symbols : {}  / Path : {}".format(
                clock() - start, len(filtered_boxes), item))
        sess.close()
    tf.reset_default_graph()
    return boxes_list, classes, FLAGS.size
Exemple #6
0
def main(argv=None):

    gpu_options = tf.GPUOptions(
        per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction)

    config = tf.ConfigProto(
        gpu_options=gpu_options,
        log_device_placement=False,
    )

    img = Image.open(FLAGS.input_img)
    img_resized = letter_box_image(img, FLAGS.size, FLAGS.size, 128)
    img_resized = img_resized.astype(np.float32)
    classes = load_coco_names(FLAGS.class_names)

    if FLAGS.frozen_model:

        t0 = time.time()
        frozenGraph = load_graph(FLAGS.frozen_model)
        print("Loaded graph in {:.2f}s".format(time.time() - t0))

        #print(frozenGraph.inputs)
        #print(frozenGraph.outputs)

        boxes, inputs = get_boxes_and_inputs_pb(frozenGraph)

        with tf.Session(graph=frozenGraph, config=config) as sess:
            t0 = time.time()
            detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]})

    else:
        if FLAGS.tiny:
            model = yolo_v3_tiny.yolo_v3_tiny
        elif FLAGS.spp:
            model = yolo_v3.yolo_v3_spp
        else:
            model = yolo_v3.yolo_v3

        boxes, inputs = get_boxes_and_inputs(model, len(classes), FLAGS.size,
                                             FLAGS.data_format)

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

        with tf.Session(config=config) as sess:
            t0 = time.time()
            saver.restore(sess, FLAGS.ckpt_file)
            print('Model restored in {:.2f}s'.format(time.time() - t0))

            t0 = time.time()
            detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]})

    filtered_boxes = non_max_suppression(
        detected_boxes,
        confidence_threshold=FLAGS.conf_threshold,
        iou_threshold=FLAGS.iou_threshold)
    print("Predictions found in {:.2f}s".format(time.time() - t0))

    draw_boxes(filtered_boxes, img, classes, (FLAGS.size, FLAGS.size), True)

    img.save(FLAGS.output_img)
Exemple #7
0
def main(argv=None):
    if FLAGS.tiny:
        model = yolo_v3_tiny.yolo_v3_tiny
        print ('doing tiny')
    else:
        model = yolo_v3.yolo_v3

    classes = load_coco_names(FLAGS.class_names)
    print ('num classes',len(classes))

    # placeholder for detector inputs
    inputs = tf.placeholder(tf.float32, [None, FLAGS.size, FLAGS.size, 3], "inputs")

    with tf.variable_scope('detector'):
        detections = model(inputs, len(classes), data_format=FLAGS.data_format)
        load_ops = load_weights(tf.global_variables(scope='detector'), FLAGS.weights_file)

    #detect_1.shape = (?, 507, 85)
    #detect_2.shape = (?, 2028, 85)
    #detect_3.shape = (?, 8112, 85)
    #detections.shape = (?, 10647, 85)
    #detections = Tensor("detector/yolo-v3/detections:0", shape=(?, 10647, 85), dtype=float32)
    print("detections.shape =", detections.shape)
    print(detections)
    print(detections.name)
    # Sets the output nodes in the current session
    boxes = detections_boxes(detections)

    with tf.Session() as sess:
        sess.run(load_ops)
        freeze_graph(sess, FLAGS.output_graph, FLAGS.tiny)
def main(argv=None):
    if FLAGS.tiny:
        model = yolo_v3_tiny.yolo_v3_tiny
    elif FLAGS.spp:
        model = yolo_v3.yolo_v3_spp
    else:
        model = yolo_v3.yolo_v3

    classes = load_coco_names(FLAGS.class_names)

    # 定义网络对外的接口, 服务器端是自动base64解码,所以拿到的数据已经解码过了
    # 但还需将byte格式转换为图像矩阵格式
    jpeg_vec_bytes = tf.placeholder(tf.string, shape=None, name=None)
    jpeg_sca_bytes = tf.reshape(jpeg_vec_bytes, [])  #
    jpeg_ndarr = tf.image.decode_jpeg(jpeg_sca_bytes, fancy_upscaling=False) # 从字符串变为数组,且将标量形式字节流解码成图片,!!!这里参数必须设置成False否则不能与客服端的结果匹配
    jpeg_ndarr = tf.image.resize_images(jpeg_ndarr,  [FLAGS.size, FLAGS.size], method=0)    # 将图片拉伸成希望的尺寸
    inputs = tf.reshape(jpeg_ndarr, [1, FLAGS.size, FLAGS.size, 3], "inputs")
    # placeholder for detector inputs 原址输入参量处
    # inputs = tf.placeholder(tf.float32, [None, FLAGS.size, FLAGS.size, 3], "inputs")
    # 加载yolov3模型
    with tf.variable_scope('detector'):
        detections = model(inputs, len(classes), data_format=FLAGS.data_format) # 得到yolov3整体模型(包含模型输出(?, 10647, (num_classes + 5)))
        load_ops = load_weights(tf.global_variables(scope='detector'), FLAGS.weights_file)
    # Sets the output nodes in the current session
    boxes = detections_boxes(detections) # 1,将整体输出分解为box结果与概率数值结果;2、将结果名称定义为output_boxes再放入graph中
    # checkpoint读取
    with tf.Session() as sess:
        sess.run(load_ops)
        reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path)
        var_to_shape_map = reader.get_variable_to_shape_map()
        for key in var_to_shape_map:
            print("tensor_name: ", key)
        #############################################
        # output_node_names = ["output_boxes","inputs",]
        # output_node_names = ",".join(output_node_names)
        #
        # output_graph_def = tf.graph_util.convert_variables_to_constants(
        #     sess,tf.get_default_graph().as_graph_def(),output_node_names.split(","))
        #
        # with tf.gfile.GFile(FLAGS.output_graph, "wb") as f:
        #     f.write(output_graph_def.SerializeToString())
        # print("{} ops written to {}.".format(len(output_graph_def.node), FLAGS.output_graph))
        #############################################
        # pb_savemodel模式存储
        export_path = 'models/pb/20191226'
        builder = tf.saved_model.builder.SavedModelBuilder(export_path)

        images = tf.saved_model.utils.build_tensor_info(jpeg_vec_bytes)
        boxes = tf.saved_model.utils.build_tensor_info(boxes)
        prediction_signature = (
            tf.saved_model.signature_def_utils.build_signature_def(
                inputs={'images': images},
                outputs={'scores': boxes},
                method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))
        builder.add_meta_graph_and_variables(
            sess, [tf.saved_model.tag_constants.SERVING],
            signature_def_map={'predict_images': prediction_signature},
            main_op=tf.tables_initializer(),
            strip_default_attrs=True)
        builder.save()
def main(_argv):

    img = Image.open(FLAGS.input_img)
    img_resized = np.asarray(img.resize(size=(IMG_H, IMG_W)), dtype=np.float32)
    img_resized = img_resized / 255.0

    classes = load_coco_names(FLAGS.class_names)
    model = create_model(IMG_H, yolo_anchors, yolo_anchor_masks, len(classes))
    print("=> loading weights ...")
    model.load_weights(FLAGS.weights)
    print("=> sucessfully loaded weights ")

    start = time.time()
    boxes, scores, labels, nums = model(img_resized[np.newaxis, ...],
                                        training=False)
    boxes, scores, labels, nums = boxes.numpy(), scores.numpy(), labels.numpy(
    ), nums.numpy()
    #boxes, scores, labels = model(img_resized[np.newaxis, ...])
    print("=> nms on the number of boxes= %d  time=%.2f ms" %
          (nums, 1000 * (time.time() - start)))

    image = draw_boxes2(img,
                        boxes[0],
                        scores[0],
                        labels[0],
                        nums[0],
                        classes, [IMG_H, IMG_W],
                        show=True)
    #image = draw_boxes3(img, boxes, scores, labels, classes, [IMG_H, IMG_W], show=True)
    image.save(FLAGS.output_img)
Exemple #10
0
def show_camera(sess, boxes, inputs):
    # To flip the image, modify the flip_method parameter (0 and 2 are the most common)
    classes = load_coco_names(FLAGS.class_names)
    print(gstreamer_pipeline(flip_method=0))
    cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0),
                           cv2.CAP_GSTREAMER)
    if cap.isOpened():
        window_handle = cv2.namedWindow('CSI Camera', cv2.WINDOW_AUTOSIZE)
        while cv2.getWindowProperty('CSI Camera', 0) >= 0:
            ret_val, img = cap.read()
            cv2_im = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            pil_im = Image.fromarray(cv2_im)
            img_resized = letter_box_image(pil_im, FLAGS.size, FLAGS.size, 128)
            img_resized = img_resized.astype(np.float32)
            detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]})
            filtered_boxes = non_max_suppression(
                detected_boxes,
                confidence_threshold=FLAGS.conf_threshold,
                iou_threshold=FLAGS.iou_threshold)
            draw_boxes(filtered_boxes, pil_im, classes,
                       (FLAGS.size, FLAGS.size), True)
            img = np.array(pil_im)
            img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
            cv2.imshow('CSI Camera', img)
            keyCode = cv2.waitKey(30) & 0xff
            if keyCode == 27:
                break
        cap.release()
        cv2.destroyAllWindows()
    else:
        print('Unable to open camera')
Exemple #11
0
def main(argv=None):
    if FLAGS.tiny:
        model = yolo_v3_tiny.yolo_v3_tiny
    else:
        model = yolo_v3.yolo_v3

    config = configparser.ConfigParser(strict=False)
    config.read(FLAGS.model_config)

    classes = load_coco_names(FLAGS.class_names)
    # placeholder for detector inputs
    inputs = tf.placeholder(tf.float32, [
        None,
        config.getint("net", "height"),
        config.getint("net", "width"), 3
    ], "inputs")

    with tf.variable_scope('detector'):
        detections = model(inputs, len(classes), data_format=FLAGS.data_format)
        load_ops = load_weights(tf.global_variables(scope='detector'),
                                FLAGS.weights_file)
    # Sets the output nodes in the current session
    boxes = detections_boxes(detections)

    with tf.Session() as sess:
        sess.run(load_ops)
        freeze_graph(sess, FLAGS.output_graph)
Exemple #12
0
def main(argv=None):
    physical_devices = tf.config.experimental.list_physical_devices('GPU')
    if len(physical_devices) > 0:
        tf.config.experimental.set_memory_growth(physical_devices[0], True)

    if FLAGS.tiny:
        model = yolo_v3_tiny.yolo_v3_tiny
    elif FLAGS.spp:
        model = yolo_v3.yolo_v3_spp
    else:
        model = yolo_v3.yolo_v3

    classes = load_coco_names(FLAGS.class_names)

    # placeholder for detector inputs
    # any size > 320 will work here
    inputs = tf.placeholder(tf.float32, [None, 416, 416, 3])

    with tf.variable_scope('detector'):
        detections = model(inputs, len(classes), data_format=FLAGS.data_format)
        load_ops = load_weights(tf.global_variables(scope='detector'),
                                FLAGS.weights_file)

    saver = tf.train.Saver(tf.global_variables(scope='detector'))

    with tf.Session() as sess:
        sess.run(load_ops)

        save_path = saver.save(sess, save_path=FLAGS.ckpt_file)
        print('Model saved in path: {}'.format(save_path))
def main(argv=None):
    if FLAGS.tiny:
        model = yolo_v3_tiny.yolo_v3_tiny
    else:
        model = yolo_v3.yolo_v3

    # Load coco classes
    classes = load_coco_names(FLAGS.class_names)

    # Placeholder for detector inputs any size > 320 will work here
    inputs = tf.placeholder(tf.float32, [None, 416, 416, 3])

    with tf.variable_scope('detector'):
        # Initialize model with required input size.
        detections = model(inputs, len(classes), data_format=FLAGS.data_format)

        # Load weights file into the model
        load_ops = load_weights(tf.global_variables(scope='detector'),
                                FLAGS.weights_file)

    # Initialize model saver module
    saver = tf.train.Saver(tf.global_variables(scope='detector'))

    with tf.Session() as sess:
        # Run load_weight function
        sess.run(load_ops)

        # Save the loaded model into a proper TF file.
        save_path = saver.save(sess, save_path=FLAGS.ckpt_file)
        print('Model saved in path: {}'.format(save_path))
def main(argv=None):
    # GPU配置
    # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
    # config = tf.ConfigProto(gpu_options=gpu_options,log_device_placement=False,)
    # 类别、视频或图像输入
    classes = load_coco_names(args.class_names)

    vid = cv2.VideoCapture(args.input_video)
    video_frame_cnt = int(vid.get(7))  # AVI:10148  RSTP: 中无总帧数属性 视频文件中的帧数
    timeF = 10  # 分帧率 130ms配合2
    fpsnum = int(vid.get(1))  # 基于以0开始的被捕获或解码的帧索引
    if (fpsnum % timeF == 0):
        for i in range(video_frame_cnt):
            ret, img_ori = vid.read()
            # 图像填充
            img_ori = cv2.cvtColor(img_ori, cv2.COLOR_BGR2RGB)
            img_ori = Image.fromarray(img_ori) # CV2图片转PIL
            img_resized = letter_box_image(img_ori,img_ori.size[1], img_ori.size[0], args.size, args.size, 128)
            img_resized = img_resized.astype(np.float32)
            # 图像插值
            # img = cv2.resize(img_ori, (args.size, args.size))
            # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # cv2默认为bgr顺序
            # img_resized = np.asarray(img, np.float32)
            # 编码方式1
            # scipy.misc.imsave(args.temp_img, img_resized)
            # _, jpeg_bytes = base64_encode_img(args.temp_img)
            # 编码方式2
            img_encode = cv2.imencode('.jpg', img_resized)[1]
            data_encode = np.array(img_encode)
            jpeg_bytes = data_encode.tostring()
            start_time = time.time()
            # 服务器通讯配置
            channel = grpc.insecure_channel(args.server)
            stub = prediction_service_pb2.PredictionServiceStub(channel)
            request = predict_pb2.PredictRequest()
            request.model_spec.name = 'yolov3_2'
            request.model_spec.signature_name = 'predict_images'
            # 等待服务器答复
            request.inputs['images'].CopyFrom(tf.contrib.util.make_tensor_proto(jpeg_bytes, shape=[1]))
            response = stub.Predict(request, 10.0)
            # 对返回值进行操作
            results = {}
            for key in response.outputs:
                tensor_proto = response.outputs[key]
                nd_array = tf.contrib.util.make_ndarray(tensor_proto)
                results[key] = nd_array
            detected_boxes = results['scores']
            # nms计算
            filtered_boxes = non_max_suppression(detected_boxes,confidence_threshold=args.conf_threshold,iou_threshold=args.iou_threshold)
            end_time = time.time()
            difference_time = end_time - start_time  # 网络运行时间
            # 画图
            draw_boxes(filtered_boxes, img_ori, classes, (args.size, args.size), True)
            # 输出图像
            cv2charimg = cv2.cvtColor(np.array(img_ori), cv2.COLOR_RGB2BGR) # PIL图片转cv2 图片
            cv2.putText(cv2charimg, '{:.2f}ms'.format((difference_time) * 1000), (40, 40), 0,
                        fontScale=1, color=(0, 255, 0), thickness=2)
            cv2.imshow('image', cv2charimg)
            if cv2.waitKey(1) & 0xFF == ord('q'): # 视频退出
                break
def main(argv=None):

    gpu_options = tf.GPUOptions(
        per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction)

    config = tf.ConfigProto(
        gpu_options=gpu_options,
        log_device_placement=False,
    )

    classes = load_coco_names(FLAGS.class_names)

    t0 = time.time()
    frozenGraph = load_graph(FLAGS.frozen_model)
    print("Loaded graph in {:.2f}s".format(time.time() - t0))

    boxes, inputs = get_boxes_and_inputs_pb(frozenGraph)

    with tf.Session(graph=frozenGraph, config=config) as sess:
        t0 = time.time()
        print(FLAGS.input_img)
        cap = cv2.VideoCapture(FLAGS.input_img)
        # cap = cv2.VideoCapture(0)
        fps = cap.get(cv2.CAP_PROP_FPS)
        width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
        height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
        videoWriter = cv2.VideoWriter(
            "output.mp4", cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), fps,
            (int(width), int(height)))
        while (cap.isOpened()):
            ret, frame = cap.read()
            if ret == True:
                frame = cv2.flip(frame, 0)
                img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
                img_resized = letter_box_image(img, FLAGS.size, FLAGS.size,
                                               128)
                img_resized = img_resized.astype(np.float32)
                detected_boxes = sess.run(boxes,
                                          feed_dict={inputs: [img_resized]})
                filtered_boxes = non_max_suppression(
                    detected_boxes,
                    confidence_threshold=FLAGS.conf_threshold,
                    iou_threshold=FLAGS.iou_threshold)
                print("Predictions found in {:.2f}s".format(time.time() - t0))

                draw_boxes(filtered_boxes, img, classes,
                           (FLAGS.size, FLAGS.size), True)

                fimg = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
                cv2.imshow("show", fimg)
                videoWriter.write(fimg)
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
            else:
                break
        cap.release()
        videoWriter.release()
Exemple #16
0
    def __init__(self,
                 tiny=False,
                 cls_path='coco.names',
                 img_size=(416, 416),
                 data_format='NHWC',
                 frozen_model='',
                 ckpt_path='saved_model/model.ckpt',
                 conf_threshold=0.5,
                 iou_threshold=0.4,
                 gpu_memory_fraction=0.2,
                 is_training=False):
        """ Wrapper class for the YOLO v3 detector.

        :param tiny: if you want to use tiny yolo
        :param cls_path: file storing detection classes
        :param img_size: tuple storing image size
        :param data_format: Data format: NCHW (gpu only) / NHWC
        :param ckpt_path: path to model checkpoint file
        """
        self.gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=gpu_memory_fraction)
        self.config = tf.ConfigProto(gpu_options=self.gpu_options,
                                     log_device_placement=True)

        self.is_training = is_training
        self.frozen_model = frozen_model
        self.gpu_memory_fraction = gpu_memory_fraction
        self.tiny = tiny
        self.size = img_size
        self.data_format = data_format
        self.ckpt_file = ckpt_path

        self.conf_threshold = conf_threshold
        self.iou_threshold = iou_threshold

        if self.tiny:
            self.model = yolo_v3_tiny.yolo_v3_tiny
        else:
            self.model = yolo_v3.yolo_v3

        self.classes = load_coco_names(cls_path)
        self.boxes, self.inputs = get_boxes_and_inputs(self.model,
                                                       len(self.classes),
                                                       self.size,
                                                       self.data_format)
        self.saver = tf.train.Saver(var_list=tf.global_variables(
            scope='detector'))
        self.sess = tf.Session(config=self.config)

        t0 = time.time()
        self.saver.restore(self.sess, self.ckpt_file)
        print('Model restored in {:.2f}s'.format(time.time() - t0))
    def __init__(self,
                 tiny=False,
                 cls_file='coco.names',
                 img_size=(416, 416),
                 data_format='NHWC',
                 ckpt_file='./saved_model/model.ckpt',
                 conf_threshold=0.5,
                 iou_threshold=0.4):
        """ Wrapper class for the YOLO v3 detector.

        :param tiny: if you want to use tiny yolo
        :param cls_file: file storing detection classes
        :param img_size: tuple storing image size
        :param data_format: Data format: NCHW (gpu only) / NHWC
        :param ckpt_file: path to model checkpoint file
        """
        self.tiny = tiny
        self.size = img_size
        self.data_format = data_format

        # Temporary solution
        self.ckpt_file = ckpt_file if not tiny else '.' + ckpt_file.split(
            '.')[1] + '-tiny.ckpt'

        self.conf_threshold = conf_threshold
        self.iou_threshold = iou_threshold

        if self.tiny:
            self.model = yolo_v3_tiny.yolo_v3_tiny
        else:
            self.model = yolo_v3.yolo_v3

        self.classes = load_coco_names(cls_file)

        self.inputs = tf.placeholder(tf.float32,
                                     [1, self.size[0], self.size[1], 3])

        with tf.variable_scope('detector'):
            self.detections = self.model(self.inputs,
                                         len(self.classes),
                                         data_format=self.data_format)

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

        self.boxes = detections_boxes(self.detections)

        self.sess = tf.Session()
        self.saver.restore(self.sess, self.ckpt_file)
        print('Model restored.')
Exemple #18
0
	def __init__(self):

	    config = tf.ConfigProto()
	    config.gpu_options.allow_growth = True
	    config.gpu_options.per_process_gpu_memory_fraction = 0.5

	    self.classes = load_coco_names('coco.names')

	    self.model = yolo_v3.yolo_v3
	    self.boxes, self.inputs = get_boxes_and_inputs(self.model, len(self.classes), 416, 'NHWC')
	    self.saver = tf.train.Saver(var_list=tf.global_variables(scope='detector'))

	    self.sess = tf.Session(config=config)
	    t0 = time.time()
	    self.saver.restore(self.sess, './saved_model/model.ckpt')
	    print('Model restored in {:.3f}s'.format(time.time()-t0))
Exemple #19
0
def main(argv=None):

    classes = load_coco_names(args.class_names)

    model = create_model(IMG_H, yolo_anchors, yolo_anchor_masks, len(classes))
    load_ops = load_weights(model, args.weights_file)
    """
    Saving Subclassed Models

    https://www.tensorflow.org/alpha/guide/keras/saving_and_serializing
    Sequential models and Functional models are datastructures that represent a DAG of layers. 
    As such, they can be safely serialized and deserialized.

    https://medium.com/tensorflow/what-are-symbolic-and-imperative-apis-in-tensorflow-2-0-dfccecb01021
    """
    model.save_weights(args.tf2_weights)
Exemple #20
0
def main(argv=None):

    img = Image.open('out/images/19.png')
    # img = Image.open('city.png')
    img_resized = letter_box_image(img, size, size, 128)
    img_resized = img_resized.astype(np.float32)
    classes = load_coco_names('coco.names')

    if frozen_model:

        t0 = time.time()
        frozenGraph = load_graph(frozen_model)
        print("Loaded graph in {:.2f}s".format(time.time() - t0))

        boxes, inputs = get_boxes_and_inputs_pb(frozenGraph)

        with tf.Session(graph=frozenGraph) as sess:
            t0 = time.time()
            detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]})

    else:
        if tiny:
            model = yolo_v3_tiny.yolo_v3_tiny
        else:
            model = yolo_v3.yolo_v3

        boxes, inputs = get_boxes_and_inputs(model, len(classes), size,
                                             data_format)

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

        with tf.Session() as sess:
            t0 = time.time()
            saver.restore(sess, ckpt_file)
            print('Model restored in {:.2f}s'.format(time.time() - t0))

            t0 = time.time()
            detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]})

    filtered_boxes = non_max_suppression(detected_boxes,
                                         confidence_threshold=conf_threshold,
                                         iou_threshold=iou_threshold)
    print("Predictions found in {:.2f}s".format(time.time() - t0))

    draw_boxes(filtered_boxes, img, classes, (size, size), True)
    img.save('out_check.png')
Exemple #21
0
def main(argv=None):

    #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction)
    gpu_options = tf.GPUOptions(allow_growth=True)
    config = tf.ConfigProto(
        gpu_options=gpu_options,
        log_device_placement=False,
    )

    img = Image.open(FLAGS.input_img)

    classes = load_coco_names(FLAGS.class_names)

    if FLAGS.frozen_model:

        t0 = time.time()
        frozenGraph = load_graph(FLAGS.frozen_model)
        print("Loaded graph in {:.2f}s".format(time.time() - t0))

        boxes, inputs = get_boxes_and_inputs_pb(frozenGraph)

        with tf.Session(graph=frozenGraph, config=config) as sess:
            t0 = time.time()
            show_camera(sess, boxes, inputs)

    else:
        if FLAGS.tiny:
            model = yolo_v3_tiny.yolo_v3_tiny
        else:
            model = yolo_v3.yolo_v3

        boxes, inputs = get_boxes_and_inputs(model, len(classes), FLAGS.size,
                                             FLAGS.data_format)

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

        with tf.Session(config=config) as sess:
            t0 = time.time()
            saver.restore(sess, FLAGS.ckpt_file)
            print('Model restored in {:.2f}s'.format(time.time() - t0))

            t0 = time.time()
def main(argv=None):

    img = Image.open(args.input_img)
    img_resized = np.asarray(img.resize(size=(IMG_H, IMG_W)), dtype=np.float32)
    img_resized = img_resized/255.0

    classes = load_coco_names(args.class_names)
    model = yolo_v3.YoloV3(len(classes), data_format=args.data_format)
    inputs = tf.keras.Input(shape=(IMG_H, IMG_W, 3))
    output = model(inputs, training=False)
    print("=> loading weights ...")
    model.load_weights(args.weights)
    print("=> sucessfully loaded weights ")

    start = time.time()
    boxes, scores, labels = model.detector(img_resized[np.newaxis, ...], score_thresh=args.score_threshold, iou_thresh=args.iou_threshold)
    print("=> nms on the number of boxes= %d  time=%.2f ms" %(len(boxes), 1000*(time.time()-start)))

    image = draw_boxes(img, boxes, scores, labels, classes, [IMG_H, IMG_W], show=True)
    image.save(args.output_img)
    def __init__(self,
                 filename,
                 camera_id,
                 model,
                 label,
                 params,
                 prob_threshold=0.5,
                 iou_threshold=0.5):
        with open(params, "r") as readFile:
            self.params = json.load(readFile)

        if filename:
            self.video = cv2.VideoCapture(filename)
        else:
            #self.video = cv2.VideoCapture(camera_id + cv2.CAP_DSHOW)
            self.video = cv2.VideoCapture(camera_id)
        self.video.set(cv2.CAP_PROP_FRAME_WIDTH, self.params["input_w"])
        self.video.set(cv2.CAP_PROP_FRAME_HEIGHT, self.params["input_h"])

        self.fps = self.video.get(cv2.CAP_PROP_FPS)

        # Initialize model
        self.interpreter = tflite.Interpreter(
            model_path=model,
            experimental_delegates=[load_delegate("libedgetpu.so.1.0")])
        self.interpreter.allocate_tensors()
        # Get input and output tensors.
        self.input_details = self.interpreter.get_input_details()
        self.output_details = self.interpreter.get_output_details()
        # Dequantization
        self.scale, self.zero_point = self.input_details[0]['quantization']
        self.scale1, self.zero_point1 = self.output_details[0]['quantization']
        self.scale2, self.zero_point2 = self.output_details[1]['quantization']

        self.classes = load_coco_names(label)
        self.colors = [(random.randint(0, 255), random.randint(0, 255),
                        random.randint(0, 255))
                       for _ in range(len(self.classes))]
        self.prob_threshold = prob_threshold
        self.iou_threshold = iou_threshold
Exemple #24
0
def main(argv=None):

    gpu_options = tf.GPUOptions(
        per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction)

    config = tf.ConfigProto(
        gpu_options=gpu_options,
        log_device_placement=False,
    )

    cap = cv2.VideoCapture()
    cap.open(0)
    classes = load_coco_names(FLAGS.class_names)

    if FLAGS.frozen_model:

        t0 = time.time()
        frozenGraph = load_graph(FLAGS.frozen_model)
        print("Loaded graph in {:.2f}s".format(time.time() - t0))

        boxes, inputs = get_boxes_and_inputs_pb(frozenGraph)

        with tf.Session(graph=frozenGraph, config=config) as sess:
            while True:
                ret, img = cap.read()
                img_resized = cv2.resize(img, (int(416), int(416)))

                t0 = time.time()
                detected_boxes = sess.run(boxes,
                                          feed_dict={inputs: [img_resized]})

                filtered_boxes = non_max_suppression(
                    detected_boxes,
                    confidence_threshold=FLAGS.conf_threshold,
                    iou_threshold=FLAGS.iou_threshold)
                print("Predictions found in {:.2f}s".format(time.time() - t0))

                # img = draw_boxes(filtered_boxes, img, classes, (416, 416), True)
                print(filtered_boxes)
                cv2.imshow("Output", img)
Exemple #25
0
def get_score_from_image(img_fp, gpu_options, config, model):
    img = Image.open(img_fp)
    img_resized = letter_box_image(img, FLAGS.size, FLAGS.size, 128)
    img_resized = img_resized.astype(np.float32)
    classes = load_coco_names(FLAGS.class_names)

    inference_start_time = time.time()
    if FLAGS.frozen_model:
        boxes, inputs = get_boxes_and_inputs_pb(model)

        with tf.Session(graph=model, config=config) as sess:
            detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]})

    else:
        if FLAGS.tiny:
            model = yolo_v3_tiny.yolo_v3_tiny
        elif FLAGS.spp:
            model = yolo_v3.yolo_v3_spp
        else:
            model = yolo_v3.yolo_v3

        boxes, inputs = get_boxes_and_inputs(model, len(classes), FLAGS.size,
                                             FLAGS.data_format)

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

        with tf.Session(config=config) as sess:
            saver.restore(sess, FLAGS.ckpt_file)
            detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]})

    total_inference_time = time.time() - inference_start_time

    filtered_boxes = non_max_suppression(
        detected_boxes,
        confidence_threshold=FLAGS.conf_threshold,
        iou_threshold=FLAGS.iou_threshold)

    return get_person_scores(filtered_boxes,
                             classes), round(total_inference_time * 1000, 3)
def main():
    if tiny:
        model = yolo_v3_tiny.yolo_v3_tiny
    else:
        model = yolo_v3.yolo_v3

    classes = load_coco_names(class_names)

    # placeholder for detector inputs
    inputs = tf.placeholder(tf.float32, [None, size, size, 3], "inputs")

    with tf.variable_scope('detector'):
        detections = model(inputs, len(classes), data_format=data_format)
        load_ops = load_weights(tf.global_variables(scope='detector'),
                                weights_file)

    # Sets the output nodes in the current session
    boxes = detections_boxes(detections)

    with tf.Session() as sess:
        sess.run(load_ops)
        freeze_graph(sess, output_graph)
Exemple #27
0
    def __init__(self):
        #TODO load classifier
        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=options['gpu'])

        print('GPU options defined')

        self.config = tf.ConfigProto(
            gpu_options=gpu_options,
            log_device_placement=False,
        )

        dir_path = os.path.dirname(os.path.abspath(__file__))
        self.classes = load_coco_names(
            os.path.join(dir_path, options['labels']))

        t0 = time.time()
        self.frozenGraph = load_graph(
            os.path.join(dir_path, options['frozen_model']))

        self.sess = tf.Session(graph=self.frozenGraph, config=self.config)
        print("Loaded graph in {:.2f}s".format(time.time() - t0))
        pass
Exemple #28
0
def main(argv=None):
    if FLAGS.tiny:
        model = yolo_v3_tiny.yolo_v3_tiny
    else:
        model = yolo_v3.yolo_v3

    img = Image.open(FLAGS.input_img)
    img_resized = img.resize(size=(FLAGS.size, FLAGS.size))

    classes = load_coco_names(FLAGS.class_names)

    # placeholder for detector inputs
    inputs = tf.placeholder(tf.float32, [1, FLAGS.size, FLAGS.size, 3])

    with tf.variable_scope('detector'):
        detections = model(inputs, len(classes), data_format=FLAGS.data_format)

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

    boxes = detections_boxes(detections)

    with tf.Session() as sess:
        saver.restore(sess, FLAGS.ckpt_file)
        print('Model restored.')

        detected_boxes = sess.run(
            boxes,
            feed_dict={inputs: [np.array(img_resized, dtype=np.float32)]})

    filtered_boxes = non_max_suppression(
        detected_boxes,
        confidence_threshold=FLAGS.conf_threshold,
        iou_threshold=FLAGS.iou_threshold)

    draw_boxes(filtered_boxes, img, classes, (FLAGS.size, FLAGS.size))

    img.save(FLAGS.output_img)
import numpy as np
import tensorflow as tf
from PIL import Image
import time
import utils
import cv2
import os
# 本项目实现用户整体检测,获取在图像中的高度像素值,用于根据三角形近似定力计算用户身高,参照物选择尽量选取与用户身高等同或较大物品,所得结果会更为准确。
# input_image = './1/101115_4d9192a403db450da0b400e6116e686a.png'
input_image = './calibresult.png'
class_names = './person.names'
input_size = 416
frozen_model = './yolov3_voc_person.pb'
conf_threshold = 0.5
iou_threshold = 0.4
classes = utils.load_coco_names(class_names)
out_image = './person.jpg'

t0 = time.time()
frozenGraph = utils.load_graph(frozen_model)
print("Loaded graph in {:.2f}s".format(time.time() - t0))
sess = tf.Session(graph=frozenGraph)

# image = cv2.imread(input_image)
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# image = Image.fromarray(image.astype('uint8')).convert('RGB')
# 上面三步等同于下面的Image.open()操作
image = Image.open(input_image)
img_resized = utils.letter_box_image(image, input_size, input_size, 128)
img_resized = img_resized.astype(np.float32)
boxes, inputs = utils.get_boxes_and_inputs_pb(frozenGraph)
    # Function to change from cv2 to pil and resizing
    def prepare_image(self,img):
        cv2_im = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        pil_im = Image.fromarray(cv2_im)
        img_resized = letter_box_image(pil_im, FLAGS.size, FLAGS.size, 128)
        img_resized = img_resized.astype(np.float32)
        return img_resized,pil_im

def colors(classes):
    farbe =dict()
    for i,classe in enumerate(classes):
        farbe[i] = tuple(np.random.randint(0, 256, 3))
    return farbe

# Load the classes file and the graph
classes = load_coco_names(FLAGS.class_names)
frozenGraph = load_graph(FLAGS.frozen_model)

colors_array = colors(classes)

# Initialize the pipeline for the camera
# To flip the image, modify the flip_method parameter (0 and 2 are the most common)
print(gstreamer_pipeline(flip_method=2))
cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=2), cv2.CAP_GSTREAMER)

# Prepare the cv2 window
if cap.isOpened():
    window_handle = cv2.namedWindow('CSI Camera', cv2.WINDOW_AUTOSIZE)

# Create session and load graph
# Configure the tensorflow session, especially with allow_growth, so it doesnt fails to get memory