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
def model(img, adjust=False): """ @img: 图片 @adjust: 是否调整文字识别结果 """ cfg_from_file('./ctpn/ctpn/text.yml') sttime = time.time() text_recs, img_framed, img = text_detect(img) print("text_detect时间", time.time() - sttime) # print("test_rec", text_recs) # print("img_framed", img_framed) text_recs = sort_box(text_recs) result = charRec(img, text_recs, adjust) return result, img_framed
def model(img, adjust=False): """ @img: 图片 @adjust: 是否调整文字识别结果 """ cfg_from_file('../ctpn/ctpn/text.yml') text_recs, img_framed, img = text_detect(img) #检测文本框 # text_recs:文本框位置(列表形式), # img_framed:在原图上标注了文本框的图片, # img:原图 text_recs = sort_box(text_recs) #排序框 result = charRec(img, text_recs, adjust) #OCR模型,进行字符识别 return result, img_framed
def __init__(self): cfg_from_file(os.getcwd() + '/ctpn/ctpn/text.yml') # init session self.config = tf.ConfigProto(allow_soft_placement=True) self.sess = tf.Session(config=self.config) # load network self.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("ctpn/checkpoints/") # ckpt=tf.train.get_checkpoint_state("output/ctpn_end2end/voc_2007_trainval/") print('Restoring from {}...'.format(ckpt.model_checkpoint_path), end=' ') saver.restore(self.sess, ckpt.model_checkpoint_path) print('done', end=' ') except: raise 'Check your pretrained {:s}'.format(ckpt.model_checkpoint_path) print(' done.')
def model(img, adjust=False, detectAngle=False): """ @img: 图片 @adjust: 是否调整文字识别结果 """ angle = 0 if detectAngle: angle = angle_detect(img=np.copy(img)) ##文字朝向检测 im = Image.fromarray(img) if angle == 90: im = im.transpose(Image.ROTATE_90) elif angle == 180: im = im.transpose(Image.ROTATE_180) elif angle == 270: im = im.transpose(Image.ROTATE_270) img = np.array(im) 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
def __init__(self, sess): super(Detect, self).__init__() cfg_from_file('ctpn/ctpn/text.yml') # init session # config = tf.ConfigProto(allow_soft_placement=True) # sess = tf.Session(config=config) # sess = tf.Session() # load network net = get_network("VGGnet_test") # load model print(('Loading network {:s}... '.format("VGGnet_test"))) saver = tf.train.Saver() ctpn_model_path = 'ctpn/checkpoints/VGGnet_fast_rcnn_iter_50000.ckpt' print('Restoring from {}...'.format(ctpn_model_path)) saver.restore(sess, ctpn_model_path) print('Done\n') self.sess = sess self.net = net
def __init__(self,first = True): if not first: tf.get_variable_scope().reuse_variables() # cfg_from_file(os.getcwd() + '/ctpn/ctpn/text.yml') cfg_from_file(os.path.join(os.path.dirname(os.path.abspath(__file__)),'text.yml')) # init session self.config = tf.ConfigProto(allow_soft_placement=True) self.sess = tf.Session(config=self.config) # load network self.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("ctpn/checkpoints/") ckpt = tf.train.get_checkpoint_state("G:/DeepLearningProjects/Web_SceneRecognition/ScenceRecognition_master/ctpn/checkpoints/") # ckpt=tf.train.get_checkpoint_state("output/ctpn_end2end/voc_2007_trainval/") print('Restoring from {}...'.format(ckpt.model_checkpoint_path), end=' ') saver.restore(self.sess, ckpt.model_checkpoint_path) print('done', end=' ') except: raise 'Check your pretrained {:s}'.format(ckpt.model_checkpoint_path) print(' done.')
def predict(): clearData() cfg_from_file(r'./ctpn/ctpn/text.yml') # init session config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.per_process_gpu_memory_fraction = 0.4 sess = tf.Session(config=config) with gfile.FastGFile(r'.\ctpn\ctpn\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') im_names = os.listdir('./demo/test_images') index = 0 for im_name in im_names: print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') print(('finding card number for {:s}'.format(im_name))) img = cv2.imread('./demo/test_images' + '//' + im_name) img, scale = resize_im(img, scale=TextLineCfg.SCALE, max_scale=TextLineCfg.MAX_SCALE) blobs, im_scales = _get_blobs(img, None) if cfg.TEST.HAS_RPN: im_blob = blobs['data'] blobs['im_info'] = np.array( [[im_blob.shape[1], im_blob.shape[2], im_scales[0]]], dtype=np.float32) cls_prob, box_pred = sess.run([output_cls_prob, output_box_pred], feed_dict={input_img: blobs['data']}) rois, _ = proposal_layer(cls_prob, box_pred, blobs['im_info'], 'TEST', anchor_scales=cfg.ANCHOR_SCALES) scores = rois[:, 0] boxes = rois[:, 1:5] / im_scales[0] textdetector = TextDetector() boxes = textdetector.detect(boxes, scores[:, np.newaxis], img.shape[:2]) draw_boxes(img, im_name, boxes, scale, index) index += 1 print('recognizing card number:') crnn = CRNN(image_shape=crnn_config.image_shape, min_len=crnn_config.min_len, max_len=crnn_config.max_len, lstm_hidden=crnn_config.lstm_hidden, pool_size=crnn_config.pool_size, learning_decay_rate=crnn_config.learning_decay_rate, learning_rate=crnn_config.learning_rate, learning_decay_steps=crnn_config.learning_decay_steps, mode=crnn_config.mode, dict=crnn_config.dict, is_training=True, train_label_path=crnn_config.predict_label_path, train_images_path=crnn_config.predict_images_path, charset_path=crnn_config.charset_path) result = crnn.predict(epoch=crnn_config.epoch, batch_size=crnn_config.batch_size, train_images_path=crnn_config.cardNum_path, train_label_path=crnn_config.predict_label_path, restore=True, fonts=crnn_config.fonts, logs_path=crnn_config.logs_path, models_path=crnn_config.models_path) return result
import pprint import sys import os.path sys.path.append(os.getcwd()) this_dir = os.path.dirname(__file__) from ctpn.lib.fast_rcnn.train import get_training_roidb, train_net from ctpn.lib.fast_rcnn.config import cfg_from_file, get_output_dir, get_log_dir from ctpn.lib.datasets.factory import get_imdb from ctpn.lib.networks.factory import get_network from ctpn.lib.fast_rcnn.config import cfg if __name__ == '__main__': cfg_from_file('text.yml') print('Using config:') pprint.pprint(cfg) 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) 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) network = get_network('VGGnet_train')
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.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)
def lineModel(img): cfg_from_file('./ctpn/ctpn/text.yml') result = lineRec(img) return result
import sys sys.path.append('/'.join(os.getcwd().split('/')[:-2])) print sys.path os.system('echo $PYTHONPATH') from ctpn.lib.fast_rcnn.train import get_training_roidb, train_net from ctpn.lib.fast_rcnn.config import cfg_from_file, get_output_dir, get_log_dir from ctpn.lib.datasets.factory import get_imdb from ctpn.lib.networks.factory import get_network from ctpn.lib.fast_rcnn.config import cfg reload(sys) sys.setdefaultencoding('utf8') if __name__ == '__main__': cfg_from_file('./text.yml') print('Using config:') pprint.pprint(cfg) imdb = get_imdb('voc_2007_train') 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) 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) network = get_network('VGGnet_train')