def OCR(filepath, OCRtarget): global frcnn_pro global OCR_pro global select_pro image_unit.convert_png2jpg(filepath, saveDir) image_unit.add_side(saveDir) frcnn_len = len(os.listdir(saveDir)) #frcnn_detect predict sess, net = frcnn.init(0, 0, OCRtarget) for count, x in enumerate(os.listdir(saveDir)): results = frcnn.detect(sess, net, os.path.join(saveDir, x)) #xmin,ymin,xmax,ymax = result[0]*width, result[1]*height, result[2]*width, result[3]*height #object_num = result[4] prob = result[5] object_name = result[6] if len(results) != 0: image_unit.frcnn_crop(os.path.join(saveDir, x), results, splitDir, charDir) frcnn_pro = float(count + 1) / float(frcnn_len) * 100.0 sess.close() ch_image_set, en_image_set = OCR_unit.load_charimg(charDir, img_size=64) image_set = ch_image_set + en_image_set text = {} val_dict, idx_dict = OCR_unit.detect(OCRmodel, 3700, image_set) text = OCR_unit.predict_text(val_dict, idx_dict, OCRlabel, text) return OCR_unit.select_target(text, OCRtarget, saveDir)
def segment_frcnn(): image_unit.convert_png2jpg(pngdir, savedir) image_unit.add_side(savedir) #frcnn_detect predict sess, net = frcnn.init(0, 0) for x in os.listdir(savedir): results = frcnn.detect(sess, net, savedir + x) #xmin,ymin,xmax,ymax = result[0]*width, result[1]*height, result[2]*width, result[3]*height #object_num = result[4] prob = result[5] object_name = result[6] if len(results) != 0: image_unit.frcnn_crop(savedir + x, results, splitdir, chardir) sess.close()
def OCR(filepath): image_unit.convert_png2jpg(filepath, saveDir) image_unit.add_side(saveDir) #frcnn_detect predict sess, net = frcnn.init(0,0) for x in os.listdir(saveDir): results = frcnn.detect(sess, net, os.path.join(saveDir, x)) #xmin,ymin,xmax,ymax = result[0]*width, result[1]*height, result[2]*width, result[3]*height #object_num = result[4] prob = result[5] object_name = result[6] if len(results) != 0: image_unit.frcnn_crop(os.path.join(saveDir, x), results, splitDir, charDir) sess.close() ch_image_set, en_image_set = OCR_unit.load_charimg(charDir, img_size = 64) image_set = ch_image_set + en_image_set text = {} val_dict, idx_dict = OCR_unit.detect(OCRmodel, 3700, image_set) text = OCR_unit.predict_text(val_dict, idx_dict, OCRlabel, text) return OCR_unit.select_target(text, [u'名称',u'注册号'])
def segment(): image_unit.convert_png2jpg(pngdir, savedir) image_unit.add_side(savedir) detection = ssd.CaffeDetection(gpu_id, model_def, model_weights, image_resize, labelmap_file) #ssd_detect predict for x in os.listdir(savedir): result = detection.detect(savedir + x) #xmin,ymin,xmax,ymax = result[0]*width, result[1]*height, result[2]*width, result[3]*height #object_num = result[4] prob = result[5] object_name = result[6] if len(result) != 0: image_unit.ssd_crop(savedir + x, result, splitdir) #segmentation char if os.path.exists(chardir): shutil.rmtree(chardir) os.mkdir(chardir) os.mkdir(os.path.join(chardir, 'firm_num')) os.mkdir(os.path.join(chardir, 'firm_name')) for x in os.listdir(os.path.join(splitdir, 'firm_num')): id = x img_path = os.path.join(splitdir, 'firm_num', id, 'num.jpg') char_path = os.path.join(chardir, 'firm_num', id) if os.path.exists(img_path): os.mkdir(char_path) os.system('/home/zhangzm/win3/ch_rec_ssd/test/text_split ' + img_path + ' ' + char_path) if os.path.exists(char_path): image_unit.num_handle(char_path + '/') for x in os.listdir(os.path.join(splitdir, 'firm_name')): id = x img_path = os.path.join(splitdir, 'firm_name', id, 'name.jpg') char_path = os.path.join(chardir, 'firm_name', id) if os.path.exists(img_path): os.mkdir(char_path) os.system('/home/zhangzm/win3/ch_rec_ssd/test/text_split ' + img_path + r' ' + char_path) if os.path.exists(char_path): image_unit.cn_handle(char_path + '/')