Esempio n. 1
0
def detect(image_path, model_path, yolo_weights=None):
    """
    Introduction
    ------------
        加载模型,进行预测
    Parameters
    ----------
        model_path: 模型路径
        image_path: 图片路径
    """
    image = Image.open(image_path)
    resize_image = letterbox_image(image, (416, 416))
    image_data = np.array(resize_image, dtype=np.float32)
    image_data /= 255.
    image_data = np.expand_dims(image_data, axis=0)

    pb_graph = tf.Graph()
    with pb_graph.as_default():
        input_image_shape = tf.placeholder(dtype=tf.int32,
                                           shape=(2, ),
                                           name="pred_im_shape")
        input_image = tf.placeholder(shape=[None, 416, 416, 3],
                                     dtype=tf.float32,
                                     name='pred_input_img')
        predictor = yolo_predictor(config.obj_threshold,
                                   config.nms_iou_threshold,
                                   config.classes_path, config.anchors_path)
        boxes, scores, classes = predictor.predict(input_image,
                                                   input_image_shape)
        print(input_image_shape)
        print(input_image)
        print(boxes)
        print(scores)
        print(classes)
    with tf.Session(graph=pb_graph) as sess:
        saver = tf.train.Saver()
        saver.restore(sess, model_path)
        out_boxes, out_scores, out_classes = sess.run(
            [boxes, scores, classes],
            feed_dict={
                input_image: image_data,
                input_image_shape: [image.size[1], image.size[0]]
            })
        graph_def = tf.get_default_graph().as_graph_def()
        out_put_name_list = [
            'predict/pred_boxes', 'predict/pred_scores', 'predict/pred_classes'
        ]
        out_put_grah_def = tf.graph_util.convert_variables_to_constants(
            sess, graph_def, out_put_name_list)
        pb_file_path = 'F:\\github_working\\version_2_190114\\alsochen-tensorflow-yolo3-threeoutput\\tensorflow-yolo3\\pb_file\\model.pb'
        with tf.gfile.GFile(pb_file_path, 'wb') as f:
            f.write(out_put_grah_def.SerializeToString())
            print("pb save done")
        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))
Esempio n. 2
0
def eval_neg(model_path, neg_path, yolo_weights=None):
    """
        Introduction
        ------------
            计算模型在negtive datasets验证集上的MAP, 用于评价模型
    """
    input_image_shape = tf.placeholder(dtype=tf.int32, shape=(2, ))
    input_image = tf.placeholder(shape=[None, 416, 416, 3], dtype=tf.float32)
    predictor = yolo_predictor(config.obj_threshold, config.nms_threshold,
                               config.classes_path, config.anchors_path)
    boxes, scores, classes = predictor.predict(input_image, input_image_shape)

    image_files = os.listdir(neg_path)
    tp_nums = 0
    all_nums = len(image_files)
    with tf.Session() as sess:
        if yolo_weights is not None:
            with tf.variable_scope('predict'):
                boxes, scores, classes = predictor.predict(
                    input_image, input_image_shape)
            load_op = load_weights(tf.global_variables(scope='predict'),
                                   weights_file=yolo_weights)
            sess.run(load_op)
        else:
            saver = tf.train.Saver()
            ckpt = tf.train.get_checkpoint_state(model_path)
            saver.restore(sess, ckpt.model_checkpoint_path)
            # saver.restore(sess, model_path)

        for image_file in image_files:
            image = Image.open(neg_path + image_file)
            resize_image = letterbox_image(image, (416, 416))
            image_data = np.array(resize_image, dtype=np.float32)
            image_data /= 255.
            image_data = np.expand_dims(image_data, axis=0)

            out_boxes, out_scores, out_classes = sess.run(
                [boxes, scores, classes],
                feed_dict={
                    input_image: image_data,
                    input_image_shape: [image.size[1], image.size[0]]
                })
            print(image_file)
            print(out_classes)
            print(out_scores)
            if len(out_classes) == 0:
                tp_nums += 1
        print(tp_nums / all_nums)
Esempio n. 3
0
def detect(image_path, model_path, yolo_weights=None):
    """
    Introduction
    ------------
        加载模型,进行预测
    Parameters
    ----------
        model_path: 模型路径
        image_path: 图片路径
    """
    image = Image.open(image_path)
    resize_image = letterbox_image(image, (192, 192))
    image_data = np.array(resize_image, dtype=np.float32)
    image_data /= 255.
    image_data = np.expand_dims(image_data, axis=0)
    input_image_shape = tf.placeholder(dtype=tf.int32, shape=(2, ))
    input_image = tf.placeholder(shape=[None, 192, 192, 3], dtype=tf.float32)

    with tf.variable_scope("model_gd"):
        predictor = yolo_predictor(config.obj_threshold, config.nms_threshold,
                                   config.classes_path, config.anchors_path2)
        boxes, scores, classes = predictor.predict(input_image,
                                                   input_image_shape)

    with tf.Session() as sess:
        if yolo_weights is not None:
            with tf.variable_scope('predict'):
                boxes, scores, classes = predictor.predict(
                    input_image, input_image_shape)
            load_op = load_weights(tf.global_variables(scope='predict'),
                                   weights_file=yolo_weights)
            sess.run(load_op)
        else:
            saver = tf.train.Saver()
            model_file = tf.train.latest_checkpoint(model_path)
            saver.restore(sess, model_file)
        out_boxes, out_scores, out_classes = sess.run(
            [boxes, scores, classes],
            feed_dict={
                input_image: image_data,
                input_image_shape: [image.size[1], image.size[0]]
            })
        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))
        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                  size=np.floor(3e-2 * image.size[1] +
                                                0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = predictor.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))

            data = str(label) + "," + str(left) + "." + str(top) + "," + str(
                right) + "," + str(bottom) + "\n"

            with open('./res/data.txt', "a") as f:
                f.write(data)

            print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=predictor.colors[c])
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=predictor.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw
        image.show()
        image.save('./res/1.jpg')
Esempio n. 4
0
def detect(image_path, model_path, yolo_weights=None):
    """
    Introduction
    ------------
        加载模型,进行预测
    Parameters
    ----------
        model_path: 模型路径
        image_path: 图片路径
    """
    image = Image.open(image_path)
    resize_image = letterbox_image(image,
                                   (416, 416))  #resize 和padding图像 与训练的时候保持一致
    image_data = np.array(resize_image, dtype=np.float32)
    image_data /= 255.
    image_data = np.expand_dims(image_data, axis=0)  #扩展为[1 416 416 3]用于输入网络

    input_image_shape = tf.placeholder(dtype=tf.int32, shape=(2, ))
    input_image = tf.placeholder(shape=[None, 416, 416, 3], dtype=tf.float32)
    predictor = yolo_predictor(config.obj_threshold, config.nms_threshold,
                               config.classes_path, config.anchors_path)
    boxes, scores, classes = predictor.predict(input_image, input_image_shape)

    with tf.Session() as sess:
        if yolo_weights is not None:
            with tf.variable_scope('predict'):
                boxes, scores, classes = predictor.predict(
                    input_image, input_image_shape)
            load_op = load_weights(tf.global_variables(scope='predict'),
                                   weights_file=yolo_weights)
            sess.run(load_op)
        else:
            saver = tf.train.Saver()
            saver.restore(sess, model_path)

        #运行检测
        out_boxes, out_scores, out_classes = sess.run(
            [boxes, scores, classes],
            feed_dict={
                input_image: image_data,
                input_image_shape: [image.size[1], image.size[0]]
            })
        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))
        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                  size=np.floor(3e-2 * image.size[1] +
                                                0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300

        for i, c in reversed(list(enumerate(
                out_classes))):  #使用了reversed但是 枚举的索引与元素的对应关系没变,reversed意义何在?
            predicted_class = predictor.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)  #使用PIL里面的函数
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box  #这里解析  box的形式是 y1 x1 y2 x2
            top = max(
                0,
                np.floor(top + 0.5).astype(
                    'int32'))  #因为是预测给出的框的大小,有可能越界,判断一下边界还是必要的,加0.5是为了四舍五入,向上取整
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([
                    left, top - label_size[1]
                ])  #标签的文本一般放在框的上面,如果框的上面的高度大于label_size[1],则可以在上面画框,否则在下面画框
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=predictor.colors[c])
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=predictor.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw
        image.show()
        image.save('./test/result1.jpg')
Esempio n. 5
0
def eval(model_path, min_Iou=0.5, yolo_weights=None):
    """
    Introduction
    ------------
        计算模型在coco验证集上的MAP, 用于评价模型
    """
    ground_truth = {}
    class_pred = defaultdict(list)
    gt_counter_per_class = defaultdict(int)
    input_image_shape = tf.placeholder(dtype=tf.int32, shape=(2, ))
    input_image = tf.placeholder(shape=[None, 416, 416, 3], dtype=tf.float32)
    predictor = yolo_predictor(config.obj_threshold, config.nms_threshold,
                               config.classes_path, config.anchors_path)
    boxes, scores, classes = predictor.predict(input_image, input_image_shape)
    val_Reader = Reader("val",
                        config.data_dir,
                        config.anchors_path,
                        config.num_classes,
                        input_shape=config.input_shape,
                        max_boxes=config.max_boxes)
    image_files, bboxes_data = val_Reader.read_annotations()
    allBBox = 0
    with tf.Session() as sess:
        if yolo_weights is not None:
            with tf.variable_scope('predict'):
                boxes, scores, classes = predictor.predict(
                    input_image, input_image_shape)
            load_op = load_weights(tf.global_variables(scope='predict'),
                                   weights_file=yolo_weights)
            sess.run(load_op)
        else:
            saver = tf.train.Saver()
            ckpt = tf.train.get_checkpoint_state(model_path)
            #saver.restore(sess, model_path)
            saver.restore(sess, ckpt.model_checkpoint_path)
        for index in range(len(image_files)):
            val_bboxes = []
            image_file = image_files[index]
            file_id = os.path.split(image_file)[-1].split('.')[0]
            for bbox in bboxes_data[index]:
                left, top, right, bottom, class_id = bbox[0], bbox[1], bbox[
                    2], bbox[3], bbox[4]
                class_name = val_Reader.class_names[int(class_id)]
                bbox = [float(left), float(top), float(right), float(bottom)]
                val_bboxes.append({
                    "class_name": class_name,
                    "bbox": bbox,
                    "used": False
                })
                gt_counter_per_class[class_name] += 1
            ground_truth[file_id] = val_bboxes
            image = Image.open(image_file)
            resize_image = letterbox_image(image, (416, 416))
            image_data = np.array(resize_image, dtype=np.float32)
            image_data /= 255.
            image_data = np.expand_dims(image_data, axis=0)

            out_boxes, out_scores, out_classes = sess.run(
                [boxes, scores, classes],
                feed_dict={
                    input_image: image_data,
                    input_image_shape: [image.size[1], image.size[0]]
                })
            allBBox += len(out_boxes)
            print("detect {}/{} found boxes: {},allBBox:{}".format(
                index, len(image_files), len(out_boxes), allBBox))
            for o, c in enumerate(out_classes):
                predicted_class = val_Reader.class_names[c]
                box = out_boxes[o]
                score = out_scores[o]

                top, left, bottom, right = box
                top = max(0, np.floor(top + 0.5).astype('int32'))
                left = max(0, np.floor(left + 0.5).astype('int32'))
                bottom = min(image.size[1],
                             np.floor(bottom + 0.5).astype('int32'))
                right = min(image.size[0],
                            np.floor(right + 0.5).astype('int32'))

                bbox = [left, top, right, bottom]
                class_pred[predicted_class].append({
                    "confidence": str(score),
                    "file_id": file_id,
                    "bbox": bbox
                })

    # 计算每个类别的AP
    sum_AP = 0.0
    sum_rec = 0.0
    sum_prec = 0.0
    count_true_positives = {}
    for class_index, class_name in enumerate(
            sorted(gt_counter_per_class.keys())):
        count_true_positives[class_name] = 0
        predictions_data = class_pred[class_name]
        # 该类别总共有多少个box
        nd = len(predictions_data)
        tp = [0] * nd  # true positive
        fp = [0] * nd  # false positive
        for idx, prediction in enumerate(predictions_data):
            file_id = prediction['file_id']
            ground_truth_data = ground_truth[file_id]
            bbox_pred = prediction['bbox']
            Iou_max = -1
            gt_match = None
            for obj in ground_truth_data:
                if obj['class_name'] == class_name:
                    bbox_gt = obj['bbox']
                    bbox_intersect = [
                        max(bbox_pred[0], bbox_gt[0]),
                        max(bbox_gt[1], bbox_pred[1]),
                        min(bbox_gt[2], bbox_pred[2]),
                        min(bbox_gt[3], bbox_pred[3])
                    ]
                    intersect_weight = bbox_intersect[2] - bbox_intersect[0] + 1
                    intersect_high = bbox_intersect[3] - bbox_intersect[1] + 1
                    if intersect_high > 0 and intersect_weight > 0:
                        union_area = (bbox_pred[2] - bbox_pred[0] + 1) * (
                            bbox_pred[3] - bbox_pred[1] +
                            1) + (bbox_gt[2] - bbox_gt[0] +
                                  1) * (bbox_gt[3] - bbox_gt[1] +
                                        1) - intersect_weight * intersect_high
                        Iou = intersect_high * intersect_weight / union_area
                        if Iou > Iou_max:
                            Iou_max = Iou
                            gt_match = obj
            if Iou_max > min_Iou:
                if not gt_match['used'] and gt_match is not None:
                    tp[idx] = 1
                    gt_match['used'] = True
                else:
                    fp[idx] = 1
            else:
                fp[idx] = 1
        # 计算精度和召回率
        sum_class = 0
        for idx, val in enumerate(fp):
            fp[idx] += sum_class
            sum_class += val
        sum_class = 0
        for idx, val in enumerate(tp):
            tp[idx] += sum_class
            sum_class += val
        rec = tp[:]
        for idx, val in enumerate(tp):
            rec[idx] = tp[idx] / gt_counter_per_class[class_name]
        prec = tp[:]
        for idx, val in enumerate(tp):
            prec[idx] = tp[idx] / (fp[idx] + tp[idx])

        ap, mrec, mprec = voc_ap(rec, prec)
        sum_AP += ap
        sum_rec += (mrec[-2])
        sum_prec += sum(mprec) / (allBBox + 2)
        f1 = 2 * sum_rec * sum_prec / (sum_rec + sum_prec)

    MAP = sum_AP / len(gt_counter_per_class) * 100
    #rec = sum_rec / len(gt_counter_per_class) * 100
    #prec = sum_prec / len(gt_counter_per_class) * 100
    print("The Model Eval MAP: {},prec:{},rec:{},f1:{}".format(
        MAP, sum_prec, sum_rec, f1))
Esempio n. 6
0
def detect(image, yolo_weights = config.yolo3_weights_path,image_size=(416,416)):
    """
    Introduction
    ------------
        加载模型,进行预测
    Parameters
    ----------
        model_path: 模型路径
        image_path: 图片路径
    """
    image = Image.open(image)
    if image_size != (None, None):

        assert image_size[0] % 32 == 0, 'Multiples of 32 required'
        assert image_size[1] % 32 == 0, 'Multiples of 32 required'
        resize_image = letterbox_image(image, tuple(reversed(image_size)))
    else:
        new_image_size = (image.width - (image.width % 32),
                          image.height - (image.height % 32))
        resize_image = letterbox_image(image, new_image_size)

    image_data = np.array(resize_image, dtype = 'float32')
    image_data /= 255.
    image_data = np.expand_dims(image_data, axis = 0)
    print(image_data.shape)
    input_image_shape = tf.placeholder(dtype = tf.int32, shape = (2,))
    input_image = tf.placeholder(shape = [None, 416, 416, 3], dtype = tf.float32)
    predictor = yolo_predictor(config.obj_threshold, config.nms_threshold, config.classes_path, config.anchors_path)
    boxes, scores, classes = predictor.predict(input_image, input_image_shape)
    with tf.Session() as sess:
            if yolo_weights is not None:
                print("yes")
                with tf.variable_scope('predict'):
                    boxes, scores, classes = predictor.predict(input_image, input_image_shape)
                load_op = load_weights(tf.global_variables(scope = 'predict'), weights_file = yolo_weights)
                sess.run(load_op)
            else:
                saver = tf.train.Saver()
                saver.restore(sess,  config.yolo3_weights_path)
            out_boxes, out_scores, out_classes = sess.run(
                [boxes, scores, classes],
                feed_dict={
                    input_image: image_data,
                    input_image_shape: [image.size[1], image.size[0]]
                })
            print('Found {} boxes for {}'.format(len(out_boxes), 'img'))
            font = ImageFont.truetype(font = 'font/FiraMono-Medium.otf', size = np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
            thickness = (image.size[0] + image.size[1]) // 300

            for i, c in reversed(list(enumerate(out_classes))):
                predicted_class = predictor.class_names[c]
                box = out_boxes[i]
                score = out_scores[i]

                label = '{} {:.2f}'.format(predicted_class, score)
                draw = ImageDraw.Draw(image)
                label_size = draw.textsize(label, font)

                top, left, bottom, right = box
                top = max(0, np.floor(top + 0.5).astype('int32'))
                left = max(0, np.floor(left + 0.5).astype('int32'))
                bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
                right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
                print(label, (left, top), (right, bottom))

                if top - label_size[1] >= 0:
                    text_origin = np.array([left, top - label_size[1]])
                else:
                    text_origin = np.array([left, top + 1])

                # My kingdom for a good redistributable image drawing library.
                for i in range(thickness):
                    draw.rectangle(
                        [left + i, top + i, right - i, bottom - i],
                        outline = predictor.colors[c])
                draw.rectangle(
                    [tuple(text_origin), tuple(text_origin + label_size)],
                    fill = predictor.colors[c])
                draw.text(text_origin, label, fill=(0, 0, 0), font=font)
                del draw
            result = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)
            result = np.asarray(result)
            cv2.imwrite("./output.png", result)
Esempio n. 7
0
def detect(model_path, yolo_weights=None, image_path=None):
    """
    Introduction
    ------------
        加载模型,进行预测
    Parameters
    ----------
        model_path: 模型路径
        image_path: 图片路径
    """
    cap = None
    if image_path == None:
        cap = cv2.VideoCapture(0)
    input_image_shape = tf.placeholder(dtype=tf.int32, shape=(2, ))
    input_image = tf.placeholder(shape=[None, 416, 416, 3], dtype=tf.float32)
    predictor = yolo_predictor(config.obj_threshold, config.nms_threshold,
                               config.classes_path, config.anchors_path)
    boxes, scores, classes = predictor.predict(input_image, input_image_shape)
    with tf.Session() as sess:
        if yolo_weights is not None:
            with tf.variable_scope('predict'):
                boxes, scores, classes = predictor.predict(
                    input_image, input_image_shape)
            load_op = load_weights(tf.global_variables(scope='predict'),
                                   weights_file=yolo_weights)
            sess.run(load_op)
        else:
            saver = tf.train.Saver()
            saver.restore(
                sess,
                "./test_model/model.ckpt-192192/model.ckpt-44865")  # emotion
            # saver.restore(sess, "./test_model/model.ckpt-192192/model.ckpt-19940") # detection
        while True:
            start_time = time.time()
            if image_path == None:
                ret, image = cap.read()
                if ret == 0:
                    break
                [h, w] = image.shape[:2]
                print(h, w)
                image = cv2.flip(image, 1)
                image_np = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                image = Image.fromarray(image_np)
            else:
                image = Image.open(image_path)
            resize_image = letterbox_image(image, (416, 416))
            image_data = np.array(resize_image, dtype=np.float32)
            image_data /= 255.
            image_data = np.expand_dims(image_data, axis=0)

            out_boxes, out_scores, out_classes = sess.run(
                [boxes, scores, classes],
                feed_dict={
                    input_image: image_data,
                    input_image_shape: [image.size[1], image.size[0]]
                })
            print('Found {} boxes for {}'.format(len(out_boxes), 'img'))
            font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                      size=np.floor(3e-2 * image.size[1] +
                                                    0.5).astype('int32'))
            thickness = (image.size[0] + image.size[1]) // 300

            for i, c in reversed(list(enumerate(out_classes))):
                c = int(c[0])
                print("i:{}, c:{}, type:{}".format(i, c, type(c)))
                if c > 2:
                    continue

                predicted_class = predictor.class_names[c]
                box = out_boxes[i]
                score = out_scores[i]

                label = '{} {:.2f}'.format(predicted_class, score)
                draw = ImageDraw.Draw(image)
                label_size = draw.textsize(label, font)

                top, left, bottom, right = box
                top = max(0, np.floor(top + 0.5).astype('int32'))
                left = max(0, np.floor(left + 0.5).astype('int32'))
                bottom = min(image.size[1],
                             np.floor(bottom + 0.5).astype('int32'))
                right = min(image.size[0],
                            np.floor(right + 0.5).astype('int32'))
                print(label, (left, top), (right, bottom))

                if top - label_size[1] >= 0:
                    text_origin = np.array([left, top - label_size[1]])
                else:
                    text_origin = np.array([left, top + 1])
                duration = time.time() - start_time
                # My kingdom for a good redistributable image drawing library.
                for i in range(thickness):
                    draw.rectangle([left + i, top + i, right - i, bottom - i],
                                   outline=predictor.colors[c])
                draw.rectangle(
                    [tuple(text_origin),
                     tuple(text_origin + label_size)],
                    fill=predictor.colors[c])
                frame_rate = '{:.2f}'.format(1.0 / duration)
                draw.text(text_origin, label, fill=(0, 0, 0), font=font)
                draw.text(np.array([0, 0]),
                          frame_rate,
                          fill=(0, 0, 0),
                          font=font)
                del draw
            # image.show()
            # image.save('./result1.jpg')
            # cv_img = cv2.CreateImageHeader(image.size, cv2.IPL_DEPTH_8U, 3)  # RGB image
            # cv2.SetData(cv_img, image.tostring(), image.size[0]*3)

            if image_path != None:
                print('just one image')
                image.show()
                image.save('./result1.jpg')
                break
            else:
                open_cv_image = np.array(image)[:, :, ::-1].copy()
                cv2.imshow('cimage', open_cv_image)
                k = cv2.waitKey(1) & 0xff
                if k == ord('q') or k == 27:
                    break
Esempio n. 8
0
def detect(image_path, model_path, yolo_weights = None):
   
    image = Image.open(image_path)
   
    resize_image = letterbox_image(image,(416,416))
    image_data = np.array(resize_image, dtype=np.float32)
    #皈一
    image_data/=255
  
    image_data = np.expand_dims(image_data,axis=0)

 
    input_image_shape = tf.placeholder(dtype=tf.int32, shape=(2, ))
    #圖像
    input_image = tf.placeholder(shape = [None,416,416,3], dtype=tf.float32)


    predictor = yolo_predictor(config.obj_threshold,config.nms_threshold, config.classes_path, config.anchors_path)
    with tf.Session() as sess:
     
        if yolo_weights is not None:
            with tf.variable_scope('predict'):
                boxes, scores, classes = predictor.predict(input_image, input_image_shape)

            #載入模型
            load_op = load_weights(tf.global_variables(scores="predict"), weights_file=yolo_weights)
            sess.run(load_op)

            #進行預測
            out_boxes, out_scores, out_classes = sess.run(
                [boxes, scores,classes],
                feed_dict={
                
                    input_image:image_data,
         
                    input_image_shape:[image.size[1],image.size[0]]
                }
            )

        else:
            boxes, out_scores, out_classes = predictor.predict(input_image, input_image_shape)
        saver = tf.train.Saver()
        saver.restore(sess,model_path)
        out_boxes, out_scores, out_classes = sess.run(
        [boxes, scores, classes],
        feed_dict={
            input_image:image_data,
            input_image_shape:[image.size[1], image.size[0]]
        }
        )


    print('Found {} boxes for {}'.format(len(out_boxes),'img'))
    font = ImageFont.truetype(font='font/FiraMono-Medium.otf',size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))

 
    thickness = (image.size[0] + image.size[1]) // 300

    for i, c in reversed(list(enumerate(out_classes))):
    
        predicted_class = predictor.class_names[c]
        box = out_boxes[i]
        score = out_scores[i]

        #打印
        label = '{} {:.2f}'.format(predicted_class, score)

       
        draw = ImageDraw.Draw(image)
       
        label_size = draw.textsize(label,font)

     
        top, left, bottom, right = box
        top = max(0,np.floor(top+0.5).astype('int32'))
        left = max(0,np.floor(left+0.5).astype('int32'))
        bottom = min(image.size[1]-1,np.floor(bottom+0.5).astype('int32'))
        right = min(image.size[0]-1,np.floor(right+0.5).astype('int32'))
        print(label, (left,top),(right,bottom))
        print(label_size)

        if top - label_size[1] >= 0:
            text_origin = np.array(left,top - label_size[1])
        else:
            text_origin = np.array([left, top+1])

      
        for i in range(thickness):
            draw.rectangle(
                [left+i,top+i,right-i,bottom-i],
                outline=predictor.colors[c]
            )

        draw.rectangle(
            [tuple(text_origin), tuple(text_origin + label_size)],
            fill=predictor.colors[c]
        )
        draw.text(text_origin, label,fill=(0,0,0), font=font)
        del draw

    image.show()
    image.save('./img/result1111.jpg')
Esempio n. 9
0
def detect(image_path, model_path, yolo_weights=None):
    """
    Introduction
    ------------
        加载模型,进行预测
    Parameters
    ----------
        model_path: 模型路径
        image_path: 图片路径
    """

    image = Image.open(image_path)
    resize_image = letterbox_image(image, (416, 416))
    image_data = np.array(resize_image, dtype=np.float32)
    image_data /= 255.
    image_data = np.expand_dims(image_data, axis=0)
    input_image_shape = tf.placeholder(dtype=tf.int32, shape=(2, ))
    input_image = tf.placeholder(shape=[None, 416, 416, 3], dtype=tf.float32)
    predictor = yolo_predictor(config.obj_threshold, config.nms_iou_threshold,
                               config.classes_path, config.anchors_path)
    boxes, scores, classes = predictor.predict(input_image, input_image_shape)
    with tf.Session() as sess:
        if yolo_weights is not None:
            with tf.variable_scope('predict'):
                boxes, scores, classes = predictor.predict(
                    input_image, input_image_shape)
            load_op = load_weights(tf.global_variables(scope='predict'),
                                   weights_file=yolo_weights)
            sess.run(load_op)
        else:
            saver = tf.train.Saver()
            saver.restore(sess, model_path)
        out_boxes, out_scores, out_classes = sess.run(
            [boxes, scores, classes],
            feed_dict={
                input_image: image_data,
                input_image_shape: [image.size[1], image.size[0]]
            })
        print([out_boxes, out_scores, out_classes])
        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))
        font = ImageFont.truetype(
            font=
            'F:\\github_working\\version_2_190114\\aloyschen-tensorflow-yolo3\\tensorflow-yolo3\\font\\FiraMono-Medium.otf',
            size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = predictor.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=predictor.colors[c])
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=predictor.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw
        image.show()
        print("done")
        img_save_path = "F:\\github_working\\version_2_190114\\aloyschen-tensorflow-yolo3\\single_img_out"
        img_name = "valid_" + image_path.split("\\")[-1]
        final_save_path = os.path.join(img_save_path, img_name)
        image.save(final_save_path)
        print("image save done")
Esempio n. 10
0
def detect(image_path, model_path, yolo_weights=None):
    start_time = time.time()
    for image_id in name_space[0:5]:
        reset_time = time.time()
        tf.reset_default_graph()

        reset_end = time.time()
        st_image_prep = time.time()

        image = Image.open(image_path + image_id)  #variable was: image

        resize_image = letterbox_image(image,
                                       (416, 416))  # should be (320, 240)
        image_data = np.array(resize_image, dtype=np.float32)
        image_data /= 255.
        image_data = np.expand_dims(image_data, axis=0)
        input_image_shape = tf.placeholder(dtype=tf.int32, shape=(2, ))
        input_image = tf.placeholder(shape=[None, 416, 416, 3],
                                     dtype=tf.float32)
        predictor = yolo_predictor(config.obj_threshold, config.nms_threshold,
                                   config.classes_path, config.anchors_path)
        # boxes, scores, classes = predictor.predict(input_image, input_image_shape)

        en_image_prep = time.time()

        with tf.Session() as sess:
            st1_sess = time.time()
            if yolo_weights is not None:
                with tf.variable_scope('predict'):
                    st1_end = time.time()
                    st2_sess = time.time()
                    boxes, scores, classes = predictor.predict(
                        input_image, input_image_shape)
                    st2_end = time.time()
                    st3_sess = time.time()
                    load_op = load_weights(
                        tf.global_variables(scope='predict'),
                        weights_file=yolo_weights)
                    st3_end = time.time()
                    st4_sess = time.time()
                    sess.run(load_op)
                    st4_end = time.time()
                    end = time.time()

                    print(
                        'Image preprocessing takes: {}'.format(en_image_prep -
                                                               st_image_prep))
                    print(
                        'tf.reset_default_graph takes: {}'.format(reset_end -
                                                                  reset_time))
                    print(
                        'if not None statement an with... time is: {}'.format(
                            st1_end - st1_sess))
                    print(
                        'boxes, scores, classes time is: {}'.format(st2_end -
                                                                    st2_sess))
                    print(
                        'load_op = load_weights(tf.global time is: {}'.format(
                            st3_end - st3_sess))
                    print('sess.run(load_op) time is: {}'.format(st4_end -
                                                                 st4_sess))

            else:
                print('session not running')
                saver = tf.train.Saver()
                saver.restore(sess, model_path)

            out_boxes, out_scores, out_classes = sess.run(
                [boxes, scores, classes],
                feed_dict={
                    input_image: image_data,
                    input_image_shape: [image.size[1], image.size[0]]
                })

            print('Found {} boxes for {}'.format(len(out_boxes), 'image: ',
                                                 image_id))
            font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                      size=np.floor(3e-2 * image.size[1] +
                                                    0.5).astype('int32'))
            thickness = (image.size[0] + image.size[1]) // 300

            for i, c in reversed(list(enumerate(out_classes))):
                predicted_class = predictor.class_names[c]
                box = out_boxes[i]
                score = out_scores[i]

                label = '{} {:.2f}'.format(predicted_class, score)
                print(label)

                print("--- %s seconds ---" % (time.time() - start_time))

                draw = ImageDraw.Draw(image)
                label_size = draw.textsize(label, font)

                top, left, bottom, right = box
                top = max(0, np.floor(top + 0.5).astype('int32'))
                left = max(0, np.floor(left + 0.5).astype('int32'))
                bottom = min(image.size[1],
                             np.floor(bottom + 0.5).astype('int32'))
                right = min(image.size[0],
                            np.floor(right + 0.5).astype('int32'))
                print(label, (left, top), (right, bottom))

                if top - label_size[1] >= 0:
                    text_origin = np.array([left, top - label_size[1]])
                else:
                    text_origin = np.array([left, top + 1])

                for i in range(thickness):
                    draw.rectangle([left + i, top + i, right - i, bottom - i],
                                   outline=predictor.colors[c])
                draw.rectangle(
                    [tuple(text_origin),
                     tuple(text_origin + label_size)],
                    fill=predictor.colors[c])
                draw.text(text_origin, label, fill=(0, 0, 0), font=font)
                del draw

            image.show()
            image.save('./result1.jpg')