예제 #1
0
def to_txt(txt_path, image_name,
           image_data, pixel_pos_scores, link_pos_scores):
    # write detection result as txt files
    def write_result_as_txt(image_name, bboxes, path):
        filename = util.io.join_path(path, 'res_%s.txt'%(image_name))
        lines = []
        for b_idx, bbox in enumerate(bboxes):
              values = [int(v) for v in bbox]
              line = "%d, %d, %d, %d, %d, %d, %d, %d\n"%tuple(values)
              lines.append(line)
        util.io.write_lines(filename, lines)

    mask = pixel_link.decode_batch(pixel_pos_scores, link_pos_scores)[0, ...]
    bboxes = pixel_link.mask_to_bboxes(mask, image_data.shape)
    write_result_as_txt(image_name, bboxes, txt_path)
예제 #2
0
def to_txt(txt_path, image_name, 
           image_data, pixel_pos_scores, link_pos_scores):
    # write detection result as txt files
    def write_result_as_txt(image_name, bboxes, path):
        filename = util.io.join_path(path, 'res_%s.txt'%(image_name))
        lines = []
        for b_idx, bbox in enumerate(bboxes):
              values = [int(v) for v in bbox]
              line = "%d, %d, %d, %d, %d, %d, %d, %d\n"%tuple(values)
              lines.append(line)
        util.io.write_lines(filename, lines)
        print 'result has been written to:', filename
    
    mask = pixel_link.decode_batch(pixel_pos_scores, link_pos_scores)[0, ...]
    bboxes = pixel_link.mask_to_bboxes(mask, image_data.shape)
    write_result_as_txt(image_name, bboxes, txt_path)
예제 #3
0
def to_txt(txt_path, image_name, image_data, pixel_pos_scores,
           link_pos_scores):
    # write detection result as txt files
    def write_result_as_pred_txt(image_name, bboxes, bboxes_score):
        filename = util.io.join_path(FLAGS.pred_path, '%s.txt' % image_name)
        lines = []
        for b_idx, (bbox, bbox_score) in enumerate(zip(bboxes, bboxes_score)):
            min_x = np.min([bbox[0], bbox[2], bbox[4], bbox[6]])
            max_x = np.max([bbox[0], bbox[2], bbox[4], bbox[6]])
            min_y = np.min([bbox[1], bbox[3], bbox[5], bbox[7]])
            max_y = np.max([bbox[1], bbox[3], bbox[5], bbox[7]])
            lines.append('CYST %.4f %d %d %d %d\n' %
                         (bbox_score, min_x, min_y, max_x, max_y))
        util.io.write_lines(filename, lines)
        print('result has been written to: ', filename)

    def write_result_as_txt(image_name, bboxes, path):
        filename = util.io.join_path(path, 'res_%s.txt' % (image_name))
        lines = []
        pred_lines = []
        for b_idx, bbox in enumerate(bboxes):
            values = [int(v) for v in bbox]
            line = "%d, %d, %d, %d, %d, %d, %d, %d\n" % tuple(values)
            lines.append(line)

        util.io.write_lines(filename, lines)
        print('result has been written to:', filename)

    # 其实只有一个image, [1, W, H, C]
    print('the shape of pixel_pos_scores is ', np.shape(pixel_pos_scores),
          np.min(pixel_pos_scores), np.max(pixel_pos_scores))
    mask = pixel_link.decode_batch(pixel_pos_scores, link_pos_scores)[0, ...]
    bboxes, bboxes_score, pixel_pos_scores = pixel_link.mask_to_bboxes(
        mask, pixel_pos_scores, image_data.shape)
    print('the shape of pixel_pos_scores is ', np.shape(pixel_pos_scores),
          np.min(pixel_pos_scores), np.max(pixel_pos_scores))
    score_map_path = util.io.join_path(FLAGS.score_map_path,
                                       '%s.jpg' % image_name)
    cv2.imwrite(score_map_path, np.asarray(pixel_pos_scores * 255, np.uint8))
    print('score will be written in ', score_map_path)
    write_result_as_txt(image_name, bboxes, txt_path)
    write_result_as_pred_txt(image_name, bboxes, bboxes_score)
예제 #4
0
def main(_):
    pb_file = 'e:/ocr/pixel_link.pb'
    image_path = 'd:/tt/TextVOC/JPEGImages/000000.jpg'

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

        with tf.gfile.FastGFile(pb_file, mode='rb') as f:
            graph_def.ParseFromString(f.read())
            tf.import_graph_def(graph_def)

        config.init_config((512, 512))

        with tf.Session() as sess:
            # with tf.summary.FileWriter(logdir='e:/ocr/logdir', graph=sess.graph) as w:
            #     w.flush()
            input_image = sess.graph.get_tensor_by_name('import/input_image:0')
            pixel_pos_scores = sess.graph.get_tensor_by_name(
                'import/pixel_pos_scores:0')
            link_pos_scores = sess.graph.get_tensor_by_name(
                'import/link_pos_scores:0')

            img = cv2.imread(image_path)
            pps, lps = sess.run([pixel_pos_scores, link_pos_scores],
                                feed_dict={input_image: img})

            mask = pixel_link.decode_batch(pps, lps)[0, ...]
            bboxes = pixel_link.mask_to_bboxes(mask, img.shape)
            print(bboxes)

            for points in bboxes:
                points = np.reshape(points, (4, 2))
                cnts = util.img.points_to_contours(points)
                util.img.draw_contours(img,
                                       cnts,
                                       -1,
                                       color=util.img.COLOR_GREEN,
                                       border_width=3)

            cv2.imwrite('d:/tt/000000.jpg', img)
예제 #5
0
def to_txt(txt_path, image_name, image_data, pixel_pos_scores,
           link_pos_scores):
    # write detection result as txt files
    def write_result_as_txt(image_name, bboxes, path):
        filename = util.io.join_path(path, 'res_%s.txt' % (image_name))
        lines = []
        for b_idx, bbox in enumerate(bboxes):
            values = [int(v) for v in bbox]
            line = "%d, %d, %d, %d, %d, %d, %d, %d\n" % tuple(values)
            lines.append(line)
        util.io.write_lines(filename, lines)
        print 'result has been written to:', filename

    start_time = time()
    mask = pixel_link.decode_batch(pixel_pos_scores, link_pos_scores)[0, ...]
    bboxes = pixel_link.mask_to_bboxes(mask, image_data.shape)
    end_time = time()
    print(image_name, ' cost time: ', end_time - start_time)
    # ##
    # output_path = os.path.join(os.getcwd(), 'vis_result')
    # to_mask(image_name, mask, output_path)
    # ##
    write_result_as_txt(image_name, bboxes, txt_path)
예제 #6
0
def to_txt_mask(txt_path, image_name, image_data, pixel_pos_scores,
                link_pos_scores, pixel_seg_score):
    # write detection result as txt files
    def write_result_as_pred_txt(image_name, bboxes, bboxes_score,
                                 pixel_wise_category):
        from config import pixel2type

        def compute_the_category(pixel_wise_category,
                                 min_x,
                                 max_x,
                                 min_y,
                                 max_y,
                                 class_num=5):
            pixel_num = []
            cropped = pixel_wise_category[min_x:max_x, min_y:max_y]
            for i in range(1, class_num + 1):
                pixel_num.append(np.sum(cropped == i))
            return np.argmax(pixel_num) + 1

        filename = util.io.join_path(FLAGS.pred_path, '%s.txt' % image_name)
        lines = []
        for b_idx, (bbox, bbox_score) in enumerate(zip(bboxes, bboxes_score)):
            min_x = np.min([bbox[0], bbox[2], bbox[4], bbox[6]])
            max_x = np.max([bbox[0], bbox[2], bbox[4], bbox[6]])
            min_y = np.min([bbox[1], bbox[3], bbox[5], bbox[7]])
            max_y = np.max([bbox[1], bbox[3], bbox[5], bbox[7]])
            label_idx = compute_the_category(pixel_wise_category, min_x, max_x,
                                             min_y, max_y)
            label_name = pixel2type[label_idx * 50]
            lines.append('%s %.4f %d %d %d %d\n' %
                         (label_name, bbox_score, min_x, min_y, max_x, max_y))
        util.io.write_lines(filename, lines)
        print('result has been written to: ', filename)

    def write_result_as_txt(image_name, bboxes, path):
        filename = util.io.join_path(path, 'res_%s.txt' % (image_name))
        lines = []
        pred_lines = []
        for b_idx, bbox in enumerate(bboxes):
            values = [int(v) for v in bbox]
            line = "%d, %d, %d, %d, %d, %d, %d, %d\n" % tuple(values)
            lines.append(line)

        util.io.write_lines(filename, lines)
        print('result has been written to:', filename)

    pixel_seg_score = util.img.resize(img=pixel_seg_score[0],
                                      size=image_data.shape[:2])
    pixel_wise_category = np.argmax(pixel_seg_score, axis=-1)
    # 其实只有一个image, [1, W, H, C]

    mask = pixel_link.decode_batch(pixel_pos_scores, link_pos_scores)[0, ...]
    bboxes, bboxes_score, pixel_pos_scores = pixel_link.mask_to_bboxes(
        mask, pixel_pos_scores, image_data.shape)

    print('the shape of pixel_pos_scores is ', np.shape(pixel_pos_scores),
          np.min(pixel_pos_scores), np.max(pixel_pos_scores))
    print('the shape of pixel_wise_category is ',
          np.shape(pixel_wise_category), np.min(pixel_wise_category),
          np.max(pixel_wise_category))
    score_map_path = util.io.join_path(FLAGS.score_map_path,
                                       '%s.jpg' % image_name)
    seg_map_path = util.io.join_path(FLAGS.seg_map_path, '%s.png' % image_name)
    cv2.imwrite(score_map_path, np.asarray(pixel_pos_scores * 255, np.uint8))
    cv2.imwrite(seg_map_path, np.asarray(pixel_wise_category * 50, np.uint8))
    print('score will be written in ', score_map_path)
    write_result_as_txt(image_name, bboxes, txt_path)
    write_result_as_pred_txt(image_name, bboxes, bboxes_score,
                             pixel_wise_category)
예제 #7
0
def to_bboxes(image_data, pixel_pos_scores, link_pos_scores):
    link_pos_scores=np.transpose(link_pos_scores,(0,2,3,1))    
    mask = decode_batch(pixel_pos_scores, link_pos_scores,0.6,0.9)[0, ...]
    bboxes = mask_to_bboxes(mask, image_data.shape)
    return mask,bboxes
예제 #8
0
def test():
    if config.model_type == 'vgg16':
        from nets import pixel_link_symbol1 as pixel_link_symbol
    elif config.model_type == 'vgg16_dssd':
        from nets import pixel_link_symbol1 as pixel_link_symbol
    else:  # 'vgg16_dssd_ssd'
        from nets import pixel_link_symbol2 as pixel_link_symbol

    with tf.name_scope('test'):
        image = tf.placeholder(dtype=tf.int32, shape=[None, None, 3])
        image_shape = tf.placeholder(dtype=tf.int32, shape=[
            3,
        ])
        processed_image, _, _, _, _ = ssd_vgg_preprocessing.preprocess_image(
            image,
            None,
            None,
            None,
            None,
            out_shape=config.image_shape,
            data_format=config.data_format,
            is_training=False)
        b_image = tf.expand_dims(processed_image, axis=0)
        net = pixel_link_symbol.PixelLinkNet(b_image, is_training=True)
        global_step = slim.get_or_create_global_step()

    sess_config = tf.ConfigProto(log_device_placement=False,
                                 allow_soft_placement=True)
    if FLAGS.gpu_memory_fraction < 0:
        sess_config.gpu_options.allow_growth = True
    elif FLAGS.gpu_memory_fraction > 0:
        sess_config.gpu_options.per_process_gpu_memory_fraction = FLAGS.gpu_memory_fraction

    checkpoint_dir = util.io.get_dir(FLAGS.checkpoint_path)
    logdir = util.io.join_path(
        checkpoint_dir, 'test',
        FLAGS.dataset_name + '_' + FLAGS.dataset_split_name)

    # Variables to restore: moving avg. or normal weights.
    if FLAGS.using_moving_average:
        variable_averages = tf.train.ExponentialMovingAverage(
            FLAGS.moving_average_decay)
        variables_to_restore = variable_averages.variables_to_restore()
        variables_to_restore[global_step.op.name] = global_step
    else:
        variables_to_restore = slim.get_variables_to_restore()

    saver = tf.train.Saver(var_list=variables_to_restore)

    image_names = util.io.ls(FLAGS.dataset_dir)
    image_names.sort()
    # image_names = image_names[1100:]

    checkpoint = FLAGS.checkpoint_path
    checkpoint_name = util.io.get_filename(str(checkpoint))
    dump_path = util.io.join_path('/home/zhy/pixel_link/test/mtwi_2018/',
                                  checkpoint_name + '-dssd-eval')
    txt_path = util.io.join_path(dump_path, 'txt')
    zip_path = util.io.join_path(dump_path, checkpoint_name + '_det.zip')

    with tf.Session(config=sess_config) as sess:
        saver.restore(sess, checkpoint)

        for iter, image_name in enumerate(image_names):
            # image_name = 'TB210QccTAlyKJjSZFhXXc8XFXa_!!2227306651.jpg.jpg'
            image_data = util.img.imread(util.io.join_path(
                FLAGS.dataset_dir, image_name),
                                         rgb=True)
            # image_name = image_name.split('.jpg')[0]
            image_name = image_name[0:-4]
            pixel_pos_scores, link_pos_scores = sess.run(
                [net.pixel_pos_scores, net.link_pos_scores],
                feed_dict={image: image_data})

            mask = pixel_link.decode_batch(pixel_pos_scores,
                                           link_pos_scores)[0, ...]

            # bboxes = pixel_link.mask_to_bboxes(mask, image_data.shape)
            # pp.show_result(image_data.copy(), bboxes)
            bboxes = pp.modify_mask_to_bboxes(mask, image_data.shape)
            # pp.show_result(image_data.copy(), bboxes)

            print '%d/%d: %s' % (iter + 1, len(image_names), image_name)

            to_txt(txt_path, image_name, image_data, bboxes)

    final_precision, final_recall, final_score = \
        zhy_evaluator.zhy_evaluate(txt_path, '/home/zhy/pixel_link/dataset/mtwi_2018/data_train_eval_split/split_eval/txt_eval')
    print final_precision, final_recall, final_score
예제 #9
0
    def _get_bbox(self, image_data, pixel_pos_scores, link_pos_scores):

        mask = pixel_link.decode_batch(pixel_pos_scores, link_pos_scores)[0, ...]
        bboxes = pixel_link.mask_to_bboxes(mask, image_data.shape)

        return bboxes