Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在windows 10下,使用python3.8.0,在pycharm 2022.1.3社区版运行正常。 #420

Open
wants to merge 1 commit into
base: onnx
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions backend/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

# coding=utf-8
import os
import sys

Expand All @@ -14,7 +14,8 @@
from backend.webInterface import tr_index
from backend.tools import log
import logging
logger = logging.getLogger(log.LOGGER_ROOT_NAME+'.'+__name__)

logger = logging.getLogger(log.LOGGER_ROOT_NAME + '.' + __name__)

current_path = os.path.dirname(__file__)
settings = dict(
Expand All @@ -34,7 +35,6 @@ def make_app():


if __name__ == "__main__":

port = 8089
app = make_app()
server = tornado.httpserver.HTTPServer(app)
Expand Down
2 changes: 1 addition & 1 deletion backend/tools/get_host_ip.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

# coding=utf-8

import socket

Expand Down
2 changes: 1 addition & 1 deletion backend/webInterface/tr_index.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

# coding=utf-8

import tornado.web
import tornado.gen
Expand Down
2 changes: 1 addition & 1 deletion backend/webInterface/tr_run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

# coding=utf-8

import time
from model import OcrHandle
Expand Down
17 changes: 6 additions & 11 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
import os

# coding=utf-8

import os

filt_path = os.path.abspath(__file__)
father_path = os.path.abspath(os.path.dirname(filt_path) + os.path.sep + ".")

# dbnet 参数
dbnet_max_size = 6000 #长边最大长度
pad_size = 0 #检测是pad尺寸,有些文档文字充满整个屏幕检测有误,需要pad

dbnet_max_size = 6000 # 长边最大长度
pad_size = 0 # 检测是pad尺寸,有些文档文字充满整个屏幕检测有误,需要pad

# crnn参数
crnn_lite = True
model_path = os.path.join(father_path, "models/dbnet.onnx")
is_rgb = True
crnn_model_path = os.path.join(father_path, "models/crnn_lite_lstm.onnx")



# angle
angle_detect = True
angle_detect_num = 30
angle_net_path = os.path.join(father_path, "models/angle_net.onnx")


max_post_time = 100 # ip 访问最大次数
max_post_time = 100 # ip 访问最大次数

from crnn.keys import alphabetChinese as alphabet


white_ips = [] #白名单
white_ips = [] # 白名单

version = 'api/v1'
31 changes: 13 additions & 18 deletions crnn/CRNN.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from PIL import Image
# coding=utf-8

from PIL import Image
import numpy as np
import cv2
from .keys import alphabetChinese as alphabet
Expand All @@ -10,19 +12,19 @@

converter = strLabelConverter(''.join(alphabet))


def softmax(x):
x_row_max = x.max(axis=-1)
x_row_max = x_row_max.reshape(list(x.shape)[:-1]+[1])
x_row_max = x_row_max.reshape(list(x.shape)[:-1] + [1])
x = x - x_row_max
x_exp = np.exp(x)
x_exp_row_sum = x_exp.sum(axis=-1).reshape(list(x.shape)[:-1]+[1])
x_exp_row_sum = x_exp.sum(axis=-1).reshape(list(x.shape)[:-1] + [1])
softmax = x_exp / x_exp_row_sum
return softmax


class CRNNHandle:
def __init__(self, model_path):

self.sess = rt.InferenceSession(model_path)

def predict(self, image):
Expand All @@ -43,21 +45,17 @@ def predict(self, image):

preds = preds[0]

length = preds.shape[0]
preds = preds.reshape(length, -1)

length = preds.shape[0]
preds = preds.reshape(length,-1)

preds = np.argmax(preds,axis=1)
preds = np.argmax(preds, axis=1)

preds = preds.reshape(-1)


sim_pred = converter.decode(preds, length, raw=False)

return sim_pred



def predict_rbg(self, im):
"""
预测
Expand All @@ -77,14 +75,12 @@ def predict_rbg(self, im):

preds = preds[0]


length = preds.shape[0]
preds = preds.reshape(length,-1)
length = preds.shape[0]
preds = preds.reshape(length, -1)

# preds = softmax(preds)


preds = np.argmax(preds,axis=1)
preds = np.argmax(preds, axis=1)

preds = preds.reshape(-1)

Expand All @@ -93,8 +89,7 @@ def predict_rbg(self, im):
return sim_pred



if __name__ == "__main__":
im = Image.open("471594277244_.pic.jpg")
crnn_handle = CRNNHandle(model_path="../models/crnn_lite_lstm_bk.onnx")
print(crnn_handle.predict(im))
print(crnn_handle.predict(im))
3 changes: 2 additions & 1 deletion crnn/keys.py

Large diffs are not rendered by default.

22 changes: 10 additions & 12 deletions model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding=utf-8
from config import *
from crnn import CRNNHandle
from angnet import AngleNetHandle
from angnet import AngleNetHandle
from utils import draw_bbox, crop_rect, sorted_boxes, get_rotate_crop_image
from PIL import Image
import numpy as np
Expand All @@ -10,15 +11,15 @@
import time
import traceback

class OcrHandle(object):

class OcrHandle(object):
def __init__(self):
self.text_handle = DBNET(model_path)
self.crnn_handle = CRNNHandle(crnn_model_path)
if angle_detect:
self.angle_handle = AngleNetHandle(angle_net_path)


def crnnRecWithBox(self,im, boxes_list,score_list):
def crnnRecWithBox(self, im, boxes_list, score_list):
"""
crnn模型,ocr识别
@@model,
Expand All @@ -43,18 +44,16 @@ def crnnRecWithBox(self,im, boxes_list,score_list):
angle_res = self.angle_handle.predict_rbgs(line_imgs)

count = 1
for index, (box ,score) in enumerate(zip(boxes_list,score_list)):
for index, (box, score) in enumerate(zip(boxes_list, score_list)):

tmp_box = copy.deepcopy(box)
partImg_array = get_rotate_crop_image(im, tmp_box.astype(np.float32))


partImg = Image.fromarray(partImg_array).convert("RGB")

if angle_detect and angle_res:
partImg = partImg.rotate(180)


if not is_rgb:
partImg = partImg.convert('L')

Expand All @@ -68,15 +67,14 @@ def crnnRecWithBox(self,im, boxes_list,score_list):
continue

if simPred.strip() != '':
results.append([tmp_box,"{}、 ".format(count)+ simPred,score])
results.append([tmp_box, "{}、 ".format(count) + simPred, score])
count += 1

return results


def text_predict(self,img,short_size):
boxes_list, score_list = self.text_handle.process(np.asarray(img).astype(np.uint8),short_size=short_size)
result = self.crnnRecWithBox(np.array(img), boxes_list,score_list)
def text_predict(self, img, short_size):
boxes_list, score_list = self.text_handle.process(np.asarray(img).astype(np.uint8), short_size=short_size)
result = self.crnnRecWithBox(np.array(img), boxes_list, score_list)

return result

Expand Down
22 changes: 15 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
tornado==5.1.1
numpy==1.19.1
opencv_python==4.3.0.36
onnxruntime==1.4.0
Shapely==1.7.0
pyclipper==1.2.0
Pillow==7.2.0
tornado==6.2
numpy==1.23.1
opencv_python==4.6.0.66
onnxruntime==1.12.0
Shapely==1.8.2
pyclipper==1.3.0.post3
Pillow==9.2.0
coloredlogs==15.0.1
flatbuffers==2.0
humanfriendly==10.0
mpmath==1.2.1
packaging==21.3
pyparsing==3.0.9
pyreadline3==3.4.1
sympy==1.10.1
23 changes: 12 additions & 11 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# coding=utf-8
import numpy as np
import cv2
from PIL import Image
Expand All @@ -12,16 +13,16 @@ def rotate_cut_img(im, degree, x_center, y_center, w, h, leftAdjust=False, right
right = 1
if leftAdjust:
left = 1

box = (max(1, x_center - w / 2 - left * alph * (w / 2))
, y_center - h / 2, # ymin
min(x_center + w / 2 + right * alph * (w / 2), im.size[0] - 1)
, y_center + h / 2) # ymax

newW = box[2] - box[0]
newH = box[3] - box[1]
tmpImg = im.rotate(degree, center=(x_center, y_center)).crop(box)

return tmpImg, newW, newH


Expand All @@ -32,7 +33,7 @@ def crop_rect(img, rect, alph=0.15):
# print(rect)
center, size, angle = rect[0], rect[1], rect[2]
min_size = min(size)

if angle > -45:
center, size = tuple(map(int, center)), tuple(map(int, size))
# angle-=270
Expand Down Expand Up @@ -90,10 +91,10 @@ def sort_box(boxs):
x2, y2 = sorted(newBox[:2], key=lambda x: x[1])[0]
index = newBox.index([x2, y2])
newBox.pop(index)

newBox = sorted(newBox, key=lambda x: -x[1])
x3, y3 = sorted(newBox[:2], key=lambda x: x[0])[0]

res.append([x1, y1, x2, y2, x3, y3, x4, y4])
return res

Expand All @@ -116,7 +117,7 @@ def solve(box):
cy = (y1 + y3 + y4 + y2) / 4.0
w = (np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) + np.sqrt((x3 - x4) ** 2 + (y3 - y4) ** 2)) / 2
h = (np.sqrt((x2 - x3) ** 2 + (y2 - y3) ** 2) + np.sqrt((x1 - x4) ** 2 + (y1 - y4) ** 2)) / 2

sinA = (h * (x1 - cx) - w * (y1 - cy)) * 1.0 / (h * h + w * w) * 2
angle = np.arcsin(sinA)
return angle, w, h, cx, cy
Expand All @@ -135,8 +136,8 @@ def sorted_boxes(dt_boxes):
_boxes = list(sorted_boxes)

for i in range(num_boxes - 1):
if abs(_boxes[i+1][0][1] - _boxes[i][0][1]) < 10 and \
(_boxes[i + 1][0][0] < _boxes[i][0][0]):
if abs(_boxes[i + 1][0][1] - _boxes[i][0][1]) < 10 and \
(_boxes[i + 1][0][0] < _boxes[i][0][0]):
tmp = _boxes[i]
_boxes[i] = _boxes[i + 1]
_boxes[i + 1] = tmp
Expand All @@ -154,8 +155,8 @@ def get_rotate_crop_image(img, points):
points[:, 1] = points[:, 1] - top
img_crop_width = int(np.linalg.norm(points[0] - points[1]))
img_crop_height = int(np.linalg.norm(points[0] - points[3]))
pts_std = np.float32([[0, 0], [img_crop_width, 0],\
[img_crop_width, img_crop_height], [0, img_crop_height]])
pts_std = np.float32([[0, 0], [img_crop_width, 0], \
[img_crop_width, img_crop_height], [0, img_crop_height]])

M = cv2.getPerspectiveTransform(points, pts_std)
dst_img = cv2.warpPerspective(
Expand Down