123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- import base64
- import os
- import re
- from crnn.crnn import crnnOcr
- from ocrapi import ocr
- import numpy as np
- import cv2
- def get_image():
- image_list = []
- for i in os.listdir('./result/image'):
- if re.match('(\d+)-(\d+)-(\d+)-(\d+)',i):
- with open(os.path.join('./result/image',i), 'rb') as f:
- base64_data = base64.b64encode(f.read())
- s = base64_data.decode()
- y_min, y_max, x_min, x_max = re.match('(\d+)-(\d+)-(\d+)-(\d+)', i).groups()
- image_list.append({'left':x_min,'top':y_min,'b64':s})
- return image_list
- def get_text(online=False):
- image_list = []
- for i in os.listdir('./result/text_img'):
- if re.match('(\d+)-(\d+)-(\d+)-(\d+)',i):
- with open(os.path.join('./result/text_img',i), 'rb') as f:
- if online:
- s = ocr(os.path.join('./result/text_img',i))
- else:
- s = crnnOcr(os.path.join('./result/text_img', i))
- y_min, y_max, x_min, x_max = re.match('(\d+)-(\d+)-(\d+)-(\d+)', i).groups()
- image_list.append({'left':x_min,'top':y_min,
- 'w':int(x_max)-int(x_min),
- 'h':int(y_max)-int(y_min),
- 'size':int((int(y_max)-int(y_min))*0.6),
- 's':s})
- # print(i)
- # print({'left':x_min,'top':y_min,
- # 'w':int(x_max)-int(x_min),
- # 'h':int(y_max)-int(y_min),
- # 'size':int((int(y_max)-int(y_min))*0.6),
- # 's':s})
- return image_list
- def get_image_html():
- ...
- def get_text_html():
- image_list = []
- for i in os.listdir('./result/text_img'):
- if re.match('(\d+)-(\d+)-(\d+)-(\d+)',i):
- with open(os.path.join('./result/text_img',i), 'rb') as f:
- s = ocr(os.path.join('./result/text_img',i))
- y_min, y_max, x_min, x_max = re.match('(\d+)-(\d+)-(\d+)-(\d+)', i).groups()
- image_list.append({'left':x_min,'top':y_min,
- 'w':int(x_max)-int(x_min),
- 'h':int(y_max)-int(y_min),
- 'size':int((int(y_max)-int(y_min))*0.6),
- 's':s})
- import json
- def base64ocr(base64_str):
- imgString = base64.b64decode(base64_str)
- array = np.fromstring(imgString, np.uint8)
- image = cv2.imdecode(array, cv2.IMREAD_COLOR)
- cv2.imshow('1',image)
- cv2.waitKey()
- string = crnnOcr(image)
- return string
- if __name__ == '__main__':
- import pandas as pd
- data = pd.read_csv(r'F:\exam_segment_django_0330\x11.csv',encoding='utf8')
- for i in data['base64']:
- try:
- print(base64ocr(i))
- except Exception as e:
- print(e)
- # with open(r'F:\exam_segment_django_0330\2.txt') as f:
- # for i in f:
- # print(base64ocr(i))
|