Ejemplo n.º 1
0
def model(img, adjust=False):
    """
    @img: 图片
    @adjust: 是否调整文字识别结果
    """
    cfg_from_file('./ctpn/ctpn/text.yml')
    text_recs, img_framed, img = text_detect(img)
    text_recs = sort_box(text_recs)
    result = charRec(img, text_recs, adjust)
    return result, img_framed
Ejemplo n.º 2
0
def init_session():
    global sess, input_img, output_cls_prob, output_box_pred, text_detector

    cfg_from_file('ctpn/text.yml')
    # init session
    config = tf.ConfigProto(allow_soft_placement=True)
    sess = tf.Session(config=config)
    with gfile.FastGFile('data/ctpn.pb', 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        sess.graph.as_default()
        tf.import_graph_def(graph_def, name='')
    sess.run(tf.global_variables_initializer())

    input_img = sess.graph.get_tensor_by_name('Placeholder:0')
    output_cls_prob = sess.graph.get_tensor_by_name('Reshape_2:0')
    output_box_pred = sess.graph.get_tensor_by_name('rpn_bbox_pred/Reshape_1:0')
    text_detector = TextDetector()
Ejemplo n.º 3
0
    textdetector = TextDetector()
    boxes = textdetector.detect(boxes, scores[:, np.newaxis], img.shape[:2])
    draw_boxes(img, image_name, boxes, scale)
    timer.toc()
    print(('Detection took {:.3f}s for '
           '{:d} object proposals').format(timer.total_time, boxes.shape[0]))



if __name__ == '__main__':
    if os.path.exists("data/results/"):
        shutil.rmtree("data/results/")
    os.makedirs("data/results/")

    cfg_from_file('ctpn/text.yml')

    # init session
    config = tf.ConfigProto(allow_soft_placement=True)
    sess = tf.Session(config=config)
    # load network
    net = get_network("VGGnet_test")
    # load model
    print(('Loading network {:s}... '.format("VGGnet_test")), end=' ')
    saver = tf.train.Saver()

    try:
        ckpt = tf.train.get_checkpoint_state(cfg.TEST.checkpoints_path)
        print('Restoring from {}...'.format(ckpt.model_checkpoint_path), end=' ')
        saver.restore(sess, ckpt.model_checkpoint_path)
        print('done')
Ejemplo n.º 4
0
    output1 =''.join(output1)
    output1 =' '.join([output1[i:i+4] for i in range(0, len(output1), 4)])

    output2 =''.join(output2)
    output2 =' '.join([output2[i:i+4] for i in range(0, len(output2), 4)])

    print ("METHOD 1")
    print ("############")
    print("card number 1:",output1)
    print("card number 2:",output2)



    # Method 2 : Other info

    cfg_from_file('ctpn/text.yml')

    print ("METHOD 2")
    print ("############")

    # init session
    config = tf.ConfigProto(allow_soft_placement=True)
    sess = tf.Session(config=config)
    # load network
    net = get_network("VGGnet_test")
    # load model
    print(('Loading network {:s}... '.format("VGGnet_test")), end=' ')
    saver = tf.train.Saver()

    detect_graph = tf.get_default_graph()
Ejemplo n.º 5
0
    if len(sys.argv) == 1:
        parser.print_help()
        # sys.exit(1)

    args = parser.parse_args()
    return args


if __name__ == '__main__':
    args = parse_args()

    print('Called with args:')
    print(args)

    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    print('Using config:')
    pprint.pprint(cfg)

    if not args.randomize:
        # fix the random seeds (numpy and caffe) for reproducibility
        np.random.seed(cfg.RNG_SEED)
    imdb = get_imdb(
        args.imdb_name)  #-----------------------------------------------------
    print 'Loaded dataset `{:s}` for training'.format(imdb.name)
    roidb = get_training_roidb(imdb)

    output_dir = get_output_dir(imdb, None)
Ejemplo n.º 6
0
import os

# sys.path.append(os.getcwd())
# this_dir = os.path.dirname(__file__)
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(parentdir)

from lib.fast_rcnn.train import get_training_roidb, train_net
from lib.fast_rcnn.config import cfg_from_file, get_output_dir, get_log_dir
from lib.datasets.factory import get_imdb
from lib.networks.factory import get_network
from lib.fast_rcnn.config import cfg

if __name__ == '__main__':
    # 将text.yml的配置与默认config中的默认配置进行合并
    cfg_from_file('text.yml')
    print('Using config:~~~~~~~~~~~~~~~~')
    # 根据给定的名字,得到要加载的数据集
    imdb = get_imdb('voc_2007_trainval')
    print('Loaded dataset `{:s}` for training'.format(imdb.name))
    # 准备训练数据
    roidb = get_training_roidb(imdb)
    # 模型输出的路径
    output_dir = get_output_dir(imdb, None)
    # summary的输出路径
    log_dir = get_log_dir(imdb)
    print('Output will be saved to `{:s}`'.format(output_dir))
    print('Logs will be saved to `{:s}`'.format(log_dir))

    device_name = '/gpu:0'
    print(device_name)
Ejemplo n.º 7
0
def train(rpn_data,
          roi_data,
          rpn_cls_score_reshape,
          rpn_bbox_pred,
          feature_map,
          cls_score,
          bbox_pred,
          input_image_tensor,
          input_gt_box_tensor,
          input_im_info_tensor,
          pretrain_model=None):
    output_dir = '/home/give/PycharmProjects/MyFasterRCNN/parameters'
    saver = tf.train.Saver(max_to_keep=5)
    # RPN
    # classification loss(fg bg)
    rpn_cls_score = tf.reshape(rpn_cls_score_reshape, [-1, 2])
    rpn_label = tf.reshape(rpn_data[0], [-1])
    rpn_cls_score = tf.reshape(
        tf.gather(rpn_cls_score, tf.where(tf.not_equal(rpn_label, -1))),
        [-1, 2])
    rpn_label = tf.reshape(
        tf.gather(rpn_label, tf.where(tf.not_equal(rpn_label, -1))), [-1])
    rpn_cross_entropy = tf.reduce_mean(
        tf.nn.sparse_softmax_cross_entropy_with_logits(logits=rpn_cls_score,
                                                       labels=rpn_label))

    # bounding box regression L1 loss
    rpn_bbox_pred = rpn_bbox_pred
    rpn_bbox_targets = tf.transpose(rpn_data[1], [0, 2, 3, 1])
    rpn_bbox_inside_weights = tf.transpose(rpn_data[2], [0, 2, 3, 1])
    rpn_bbox_outside_weights = tf.transpose(rpn_data[3], [0, 2, 3, 1])

    rpn_smooth_l1 = _modified_smooth_l1(3.0, rpn_bbox_pred, rpn_bbox_targets,
                                        rpn_bbox_inside_weights,
                                        rpn_bbox_outside_weights)
    rpn_loss_box = tf.reduce_mean(
        tf.reduce_sum(rpn_smooth_l1, reduction_indices=[1, 2, 3]))

    # classification loss
    label = tf.reshape(roi_data[1], [-1])
    cross_entropy = tf.reduce_mean(
        tf.nn.sparse_softmax_cross_entropy_with_logits(logits=cls_score,
                                                       labels=label))

    smooth_l1 = _modified_smooth_l1(1.0, bbox_pred, roi_data[2], roi_data[3],
                                    roi_data[4])
    loss_box = tf.reduce_mean(tf.reduce_sum(smooth_l1, reduction_indices=[1]))

    loss = cross_entropy + loss_box + rpn_cross_entropy + rpn_loss_box

    global_step = tf.Variable(0, trainable=False)
    lr = tf.train.exponential_decay(cfg.TRAIN.LEARNING_RATE,
                                    global_step,
                                    cfg.TRAIN.STEPSIZE,
                                    0.001,
                                    staircase=True)
    momentum = cfg.TRAIN.MOMENTUM
    train_op = tf.train.MomentumOptimizer(lr, momentum).minimize(
        loss, global_step=global_step)
    with tf.Session() as sess:
        args = parse_args()
        from lib.datasets.factory import get_imdb
        from lib.fast_rcnn.train import get_training_roidb, filter_roidb
        from lib.fast_rcnn.train import get_data_layer
        import lib.roi_data_layer.roidb as rdl_roidb
        import numpy as np
        from lib.fast_rcnn.config import cfg_from_file, cfg_from_list
        if args.cfg_file is not None:
            cfg_from_file(args.cfg_file)
        if args.set_cfgs is not None:
            cfg_from_list(args.set_cfgs)
        imdb = get_imdb('voc_2007_trainval')
        roidb = get_training_roidb(imdb)
        roidb = filter_roidb(roidb)
        bbox_means, bbox_stds = rdl_roidb.add_bbox_regression_targets(roidb)
        data_layer = get_data_layer(roidb, imdb.num_classes)
        init_op = tf.global_variables_initializer()
        sess.run(init_op)
        if pretrain_model is not None:
            print('Loading pretrained model '
                  'weights from {:s}').format(pretrain_model)
            load(pretrain_model, sess, saver, True)
        for iter_index in range(args.max_iters):
            blobs = data_layer.forward()
            # blobs keys include: data, gt_boxesm im_info
            _, rpn_bbox_targets_value, rpn_bbox_inside_weights_value, rpn_bbox_outside_weights_value = sess.run(
                [
                    train_op, rpn_bbox_targets, rpn_bbox_inside_weights,
                    rpn_bbox_outside_weights
                ],
                feed_dict={
                    input_image_tensor: blobs['data'],
                    input_gt_box_tensor: blobs['gt_boxes'],
                    input_im_info_tensor: blobs['im_info']
                })
            rpn_label_value, loss_value, loss_box_value, cross_entropy_value, rpn_cross_entropy_value, rpn_loss_box_value = sess.run(
                [
                    rpn_label, loss, loss_box, cross_entropy,
                    rpn_cross_entropy, rpn_loss_box
                ],
                feed_dict={
                    input_image_tensor: blobs['data'],
                    input_gt_box_tensor: blobs['gt_boxes'],
                    input_im_info_tensor: blobs['im_info']
                })
            if iter_index % 100 == 0:
                print 'iter: %d / %d' % (iter_index, args.max_iters)
                print 'total loss: %.4f, rpn cross entropy: %.4f,  rpn_loss_box: %.4f, cross entroopy: %.4f, loss box: %.4f' % (
                    loss_value, rpn_cross_entropy_value, rpn_loss_box_value,
                    cross_entropy_value, loss_box_value)
                # print np.shape(rpn_label_value), rpn_label_value
                if iter_index == 0:
                    continue
                infix = ('_' + cfg.TRAIN.SNAPSHOT_INFIX
                         if cfg.TRAIN.SNAPSHOT_INFIX != '' else '')
                filename = (cfg.TRAIN.SNAPSHOT_PREFIX + infix +
                            '_iter_{:d}'.format(iter_index + 1) + '.ckpt')
                filename = os.path.join(output_dir, filename)

                saver.save(sess, filename)
Ejemplo n.º 8
0
plt.switch_backend('agg')
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

from CTPN.demo import ctpn
from lib.fast_rcnn.config import cfg,cfg_from_file
from lib.networks.factory import get_network
import glob
import shutil

from flask import Flask, redirect, request, url_for, send_file
from redis import Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)
app = Flask(__name__)

cfg_from_file('CTPN/ctpn/text.yml')

# init session
sess_CTPN = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
# load network
net = get_network("VGGnet_test")
# load model
print(('Loading network {:s}... '.format("VGGnet_test")), end=' ')
saver = tf.train.Saver()

try:
    ckpt = tf.train.get_checkpoint_state(cfg.TEST.checkpoints_path)
    print('Restoring from {}...'.format(ckpt.model_checkpoint_path), end=' ')
    saver.restore(sess_CTPN, ckpt.model_checkpoint_path)
    print('done')
except:
Ejemplo n.º 9
0
                           max_scale=TextLineCfg.MAX_SCALE)
    scores, boxes = test_ctpn(sess, net, img)
    textdetector = TextDetector()
    boxes = textdetector.detect(boxes, scores[:, np.newaxis], img.shape[:2])
    sort_index = np.argsort(boxes[:, -1])[::-1]
    boxes = boxes[sort_index]
    # print(boxes)
    texts = draw_boxes(img, image_name, boxes, scale)
    timer.toc()
    print(('Detection took {:.3f}s for '
           '{:d} object proposals').format(timer.total_time, boxes.shape[0]))
    return texts


if __name__ == '__main__':
    cfg_from_file('./text.yml')
    config = tf.ConfigProto(allow_soft_placement=True)
    sess = tf.Session(config=config)
    net = get_network("VGGnet_test")
    # load model
    print(('Loading network {:s}... '.format("VGGnet_test")), end=' ')
    saver = tf.train.Saver()

    try:
        ckpt = tf.train.get_checkpoint_state(cfg.TEST.checkpoints_path)
        print('Restoring from {}...'.format(ckpt.model_checkpoint_path),
              end=' ')
        saver.restore(sess, ckpt.model_checkpoint_path)
        print('done')
    except:
        raise 'Check your pretrained {:s}'.format(ckpt.model_checkpoint_path)
Ejemplo n.º 10
0
    if len(sys.argv) == 1:
        parser.print_help()
        # sys.exit(1)

    args = parser.parse_args()
    return args

if __name__ == '__main__':
    args = parse_args()

    print('Called with args:')
    print(args)

    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    print('Using config:')
    pprint.pprint(cfg)

    if not args.randomize:
        # fix the random seeds (numpy and caffe) for reproducibility
        np.random.seed(cfg.RNG_SEED)
    imdb = get_imdb(args.imdb_name)
    print 'Loaded dataset `{:s}` for training'.format(imdb.name)
    roidb = get_training_roidb(imdb)

    output_dir = get_output_dir(imdb, None)
    log_dir = get_log_dir(imdb)
Ejemplo n.º 11
0
 def __init__(self):
     # fast-rcnn 配置加载
     cfg_from_file('./ctpn/ctpn/text.yml')
Ejemplo n.º 12
0
def export():
    cfg_from_file(os.path.join(dir_path, 'text.yml'))
    config = tf.ConfigProto(allow_soft_placement=True)
    sess = tf.Session(config=config)
    # with gfile.FastGFile('../data/ctpn.pb', 'rb') as f:
    #   graph_def = tf.GraphDef()
    #   graph_def.ParseFromString(f.read())
    #   sess.graph.as_default()
    #   tf.import_graph_def(graph_def, name='')
    # sess.run(tf.global_variables_initializer())

    net = get_network("VGGnet_test")
    print(('Loading network {:s}... '.format("VGGnet_test")), end=' ')
    saver = tf.train.Saver()
    print(saver)
    try:
        ckpt_path = os.path.abspath(
            os.path.join(dir_path, cfg.TEST.checkpoints_path))
        print('check_path, ', ckpt_path)
        ckpt = tf.train.get_checkpoint_state(ckpt_path)
        print('Restoring from {}...'.format(ckpt.model_checkpoint_path),
              end=' ')
        saver.restore(sess, ckpt.model_checkpoint_path)
        print('done')
    except:
        raise 'Check your pretrained {:s}'.format(ckpt.model_checkpoint_path)
    print(' done.')
    '''
  input_img = sess.graph.get_tensor_by_name('Placeholder:0')
  output_cls_prob = sess.graph.get_tensor_by_name('Reshape_2:0')
  output_box_pred = sess.graph.get_tensor_by_name('rpn_bbox_pred/Reshape_1:0')
  '''

    raw_image = tf.placeholder(tf.float32,
                               shape=[None, None, None, 3
                                      ])  #\u8f93\u5165\u539f\u59cb\u56fe\u50cf
    print('raw_image, ')
    jpeg = preprocess_image(raw_image)  #\u9884\u5904\u7406,\u7f29\u653e
    #jpeg,im_info = preprocess_image(raw_image)  #\u9884\u5904\u7406,\u7f29\u653e
    # Preprocessing our input image

    #cls_prob_info,box_pred_info = tf.import_graph_def\
    output_tensor_cls_prob,output_tensor_box_pred = tf.import_graph_def\
                            (tf.get_default_graph().as_graph_def(),
                             input_map={'Placeholder:0': jpeg},
                             return_elements=['Reshape_2:0','rpn_bbox_pred/Reshape_1:0'])

    builder = tf.saved_model.builder.SavedModelBuilder('./exportn/1')

    imageplaceholder_info = tf.saved_model.utils.build_tensor_info(jpeg)
    #imageplaceholder_info = tf.saved_model.utils.build_tensor_info(input_img)
    #cls_prob_info = tf.saved_model.utils.build_tensor_info(output_cls_prob)
    #box_pred_info = tf.saved_model.utils.build_tensor_info(output_box_pred)
    print('predict_method_name,',
          tf.saved_model.signature_constants.PREDICT_METHOD_NAME)
    prediction_signature = (
        tf.saved_model.signature_def_utils.build_signature_def(
            inputs={'image': imageplaceholder_info},
            outputs={
                'output_cls_prob': output_tensor_cls_prob,
                'output_box_pred': output_tensor_box_pred
                #'output_cls_prob': cls_prob_info,
                #'output_box_pred': box_pred_info
            },
            method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)
    )
    init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
    builder.add_meta_graph_and_variables(
        sess, [tf.saved_model.tag_constants.SERVING],
        signature_def_map={'ctpn_recs_predict': prediction_signature},
        legacy_init_op=init_op)
    builder.save()
Ejemplo n.º 13
0
    timer.toc()
    print(('Detection took {:.3f}s for '
           '{:d} object proposals').format(timer.total_time, boxes.shape[0]))


if __name__ == '__main__':
    if os.path.exists("/content/drive/My Drive/GR2/ctpn/results/"):
        shutil.rmtree("/content/drive/My Drive/GR2/ctpn/results/")
        print("====== remove result folder =======")
    os.makedirs("/content/drive/My Drive/GR2/ctpn/results/")
    if os.path.exists("/content/drive/My Drive/GR2/ctpn/results_visualize/"):
        shutil.rmtree("/content/drive/My Drive/GR2/ctpn/results_visualize/")
        print("====== remove result folder results_visualize=======")
    os.makedirs("/content/drive/My Drive/GR2/ctpn/results_visualize/")

    cfg_from_file('/content/text-detection-ctpn/ctpn/text.yml')

    # init session
    config = tf.ConfigProto(allow_soft_placement=True)
    sess = tf.Session(config=config)
    # load network
    net = get_network("VGGnet_test")
    # load model
    print(('Loading network {:s}... '.format("VGGnet_test")), end=' ')
    saver = tf.train.Saver()

    try:
        print('==============')
        print(cfg.TEST.checkpoints_path)
        ckpt = tf.train.get_checkpoint_state(cfg.TEST.checkpoints_path)
        print(ckpt)
Ejemplo n.º 14
0
    scores, boxes = test_ctpn(sess, net, img)

    textdetector = TextDetector()
    boxes = textdetector.detect(boxes, scores[:, np.newaxis], img.shape[:2])
    draw_boxes(img, image_name, boxes, scale)
    timer.toc()
    print(('Detection took {:.3f}s for '
           '{:d} object proposals').format(timer.total_time, boxes.shape[0]))


if __name__ == '__main__':
    if os.path.exists("data/results/"):
        shutil.rmtree("data/results/")
    os.makedirs("data/results/")

    cfg_from_file('ctpn/text_demo.yml')

    # init session
    config = tf.ConfigProto(allow_soft_placement=True)
    sess = tf.Session(config=config)
    # load network
    net = get_network("VGGnet_test")
    # load model
    print(('Loading network {:s}... '.format("VGGnet_test")), end=' ')
    saver = tf.train.Saver()

    try:
        ckpt = tf.train.get_checkpoint_state(cfg.TEST.checkpoints_path)
        print('Restoring from {}...'.format(ckpt.model_checkpoint_path),
              end=' ')
        saver.restore(sess, ckpt.model_checkpoint_path)
Ejemplo n.º 15
0
    scores, boxes = test_ctpn(sess, net, img)

    textdetector = TextDetector()
    boxes = textdetector.detect(boxes, scores[:, np.newaxis], img.shape[:2])
    draw_boxes(img, image_name, boxes, scale)
    timer.toc()
    print(('Detection took {:.3f}s for '
           '{:d} object proposals').format(timer.total_time, boxes.shape[0]))


if __name__ == '__main__':
    if os.path.exists("data/results/"):
        shutil.rmtree("data/results/")
    os.makedirs("data/results/")

    cfg_from_file(os.path.join(cfg.ROOT_DIR, 'ctpn', 'text.yml'))

    # init session
    config = tf.ConfigProto(allow_soft_placement=True)
    sess = tf.Session(config=config)
    # load network
    net = get_network("VGGnet_test")
    # load model
    print(('Loading network {:s}... '.format("VGGnet_test")), end=' ')
    saver = tf.train.Saver()

    c_path = os.path.join(cfg.ROOT_DIR, cfg.TEST.checkpoints_path)
    try:
        ckpt = tf.train.get_checkpoint_state(cfg.TEST.checkpoints_path)
        print('Restoring from {}...'.format(ckpt.model_checkpoint_path),
              end=' ')
Ejemplo n.º 16
0
import pprint
import os
import tensorflow as tf
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from lib.fast_rcnn.train import SolverWrapper
from lib.fast_rcnn.config import cfg_from_file
from lib.networks.factory import get_network
from lib.fast_rcnn.config import cfg
import seetaas_helper as helper

if __name__ == '__main__':
    yaml_cfg = helper.get_parameter(yaml_file="adl.yml")
    cfg_from_file(yaml_cfg)
    print('Using config:')
    pprint.pprint(cfg)
    device_name = '/gpu:0'
    print(device_name)
    train_graph = tf.Graph()
    with train_graph.as_default():
        network = get_network('VGGnet_train')
        solver = SolverWrapper(train_graph,
                               network,
                               output_dir='/tmp/model',
                               dataset_path='/tmp/dataset',
                               dataset_name='root')
        config = tf.ConfigProto(allow_soft_placement=True,
                                log_device_placement=False)
        with tf.Session(config=config, graph=train_graph) as sess:
            solver.train_model(sess)