整理代码

This commit is contained in:
SWHL committed 2021-05-22 11:13:33 +08:00
1 parent 908d12b2fd
commit d8c45c8a0c
10 files changed
+197 -267

No files matched your search

+4 -4
View File
@@ -121,12 +121,12 @@ class TextClassifier(object):
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--image_dir', type=str, help='image_dir|image_path')
parser.add_argument('--cls_model_dir', type=str, help='cls_model_dir')
parser.add_argument('--image_path', type=str, help='image_dir|image_path')
parser.add_argument('--model_path', type=str, help='model_path')
args = parser.parse_args()
text_classifier = TextClassifier(args.cls_model_dir)
text_classifier = TextClassifier(args.model_path)
img_list, cls_res, predict_time = text_classifier(args.image_dir)
img_list, cls_res, predict_time = text_classifier(args.image_path)
for ino in range(len(img_list)):
print(f"分类结果:{cls_res[ino]}")
+2 -4
View File
@@ -137,10 +137,8 @@ class TextDetector(object):
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--image_path', type=str, default=None,
help='image_path|image_dir')
parser.add_argument('--model_path', type=str, default=None,
help='model_path')
parser.add_argument('--image_path', type=str, help='image_path|image_dir')
parser.add_argument('--model_path', type=str, help='model_path')
args = parser.parse_args()
text_detector = TextDetector(args.model_path)
@@ -32,23 +32,19 @@ root_path = Path(__file__).parent
class TextRecognizer(object):
def __init__(self, rec_model_path):
self.rec_image_shape = [3, 32, 320]
self.character_type = 'ch'
self.rec_batch_num = 6
self.rec_algorithm = 'CRNN'
self.character_dict_path = str(root_path / 'ppocr_keys_v1.txt')
self.use_space_char = True
self.postprocess_op = CTCLabelDecode(self.character_dict_path,
self.character_type,
self.use_space_char)
self.postprocess_op = CTCLabelDecode(self.character_dict_path)
self.session = onnxruntime.InferenceSession(rec_model_path)
def resize_norm_img(self, img, max_wh_ratio):
img_channel, img_height, img_width = self.rec_image_shape
assert img_channel == img.shape[2]
if self.character_type == "ch":
img_width = int((32 * max_wh_ratio))
max_wh_ratio = 1
img_width = int((32 * max_wh_ratio))
max_wh_ratio = 1
h, w = img.shape[:2]
ratio = w / float(h)
if math.ceil(img_height * ratio) > img_width:
@@ -127,10 +123,10 @@ class TextRecognizer(object):
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('image_dir', type=str, help='image_dir|image_path')
parser.add_argument('model_path', type=str, help='rec_model_path')
parser.add_argument('--image_path', type=str, help='image_dir|image_path')
parser.add_argument('--model_path', type=str, help='rec_model_path')
args = parser.parse_args()
text_recognizer = TextRecognizer(args.model_path)
rec_res, predict_time = text_recognizer(args.image_dir)
rec_res, predict_time = text_recognizer(args.image_path)
print(f'识别结果: {rec_res}\t cost: {predict_time}s')
+55 -74
View File
@@ -1,7 +1,7 @@
# !/usr/bin/env python
# -*- encoding: utf-8 -*-
# @File: PostProcess.py
# @Time: 2021/01/19 14:12:35
# @File: utils.py
# @Time: 2021/05/22 10:53:17
# @Author: Max
import cv2
import os
@@ -9,35 +9,75 @@ import imghdr
import numpy as np
class BaseRecLabelDecode(object):
def get_image_file_list(img_file):
imgs_lists = []
if img_file is None or not os.path.exists(img_file):
raise FileNotFoundError("not found any img file in {}".format(img_file))
img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff', 'gif', 'GIF'}
if os.path.isfile(img_file) and imghdr.what(img_file) in img_end:
imgs_lists.append(img_file)
elif os.path.isdir(img_file):
for single_file in os.listdir(img_file):
file_path = os.path.join(img_file, single_file)
if imghdr.what(file_path) in img_end:
imgs_lists.append(file_path)
if len(imgs_lists) == 0:
raise FileNotFoundError("not found any img file in {}".format(img_file))
return imgs_lists
def check_and_read_gif(img_path):
if os.path.basename(img_path)[-3:] in ['gif', 'GIF']:
gif = cv2.VideoCapture(img_path)
ret, frame = gif.read()
if not ret:
print("Cannot read {}. This gif image maybe corrupted.")
return None, False
if len(frame.shape) == 2 or frame.shape[-1] == 1:
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
imgvalue = frame[:, :, ::-1]
return imgvalue, True
return None, False
class CTCLabelDecode(object):
""" Convert between text-label and text-index """
def __init__(self,
character_dict_path=None,
character_type='ch',
use_space_char=False):
self.character_str = ""
assert character_dict_path is not None, "character_dict_path should not be None when character_type is ch"
def __init__(self, character_dict_path):
super(CTCLabelDecode, self).__init__()
self.character_str = []
assert character_dict_path is not None, "character_dict_path should not be None"
with open(character_dict_path, "rb") as fin:
lines = fin.readlines()
for line in lines:
line = line.decode('utf-8').strip("\n").strip("\r\n")
self.character_str += line
if use_space_char:
self.character_str += " "
dict_character = list(self.character_str)
self.character_str.append(line)
dict_character = self.character_str
self.character_type = character_type
dict_character = self.add_special_char(dict_character)
self.dict = {}
for i, char in enumerate(dict_character):
self.dict[char] = i
self.character = dict_character
def __call__(self, preds, label=None, *args, **kwargs):
preds_idx = preds.argmax(axis=2)
preds_prob = preds.max(axis=2)
text = self.decode(preds_idx, preds_prob, is_remove_duplicate=True)
if label is None:
return text
label = self.decode(label)
return text, label
def add_special_char(self, dict_character):
dict_character = ['blank'] + dict_character
return dict_character
def decode(self, text_index, text_prob=None, is_remove_duplicate=True):
def get_ignored_tokens(self):
return [0] # for ctc blank
def decode(self, text_index, text_prob=None, is_remove_duplicate=False):
""" convert text-index into text-label. """
result_list = []
ignored_tokens = self.get_ignored_tokens()
@@ -63,62 +103,3 @@ class BaseRecLabelDecode(object):
result_list.append((text, np.mean(conf_list)))
return result_list
def get_ignored_tokens(self):
return [0] # for ctc blank
class CTCLabelDecode(BaseRecLabelDecode):
""" Convert between text-label and text-index """
def __init__(self,
character_dict_path=None,
character_type='ch',
use_space_char=False,
**kwargs):
super(CTCLabelDecode, self).__init__(character_dict_path,
character_type, use_space_char)
def __call__(self, preds, label=None, *args, **kwargs):
preds_idx = preds.argmax(axis=2)
preds_prob = preds.max(axis=2)
text = self.decode(preds_idx, preds_prob)
if label is None:
return text
label = self.decode(label, is_remove_duplicate=False)
return text, label
def add_special_char(self, dict_character):
dict_character = ['blank'] + dict_character
return dict_character
def get_image_file_list(img_file):
imgs_lists = []
if img_file is None or not os.path.exists(img_file):
raise Exception("not found any img file in {}".format(img_file))
img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff', 'gif', 'GIF'}
if os.path.isfile(img_file) and imghdr.what(img_file) in img_end:
imgs_lists.append(img_file)
elif os.path.isdir(img_file):
for single_file in os.listdir(img_file):
file_path = os.path.join(img_file, single_file)
if imghdr.what(file_path) in img_end:
imgs_lists.append(file_path)
if len(imgs_lists) == 0:
raise Exception("not found any img file in {}".format(img_file))
return imgs_lists
def check_and_read_gif(img_path):
if os.path.basename(img_path)[-3:] in ['gif', 'GIF']:
gif = cv2.VideoCapture(img_path)
ret, frame = gif.read()
if not ret:
print("Cannot read {}. This gif image maybe corrupted.")
return None, False
if len(frame.shape) == 2 or frame.shape[-1] == 1:
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
imgvalue = frame[:, :, ::-1]
return imgvalue, True
return None, False
+2 -4
View File
@@ -135,10 +135,8 @@ class TextDetector(object):
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--image_path', type=str, default=None,
help='image_path|image_dir')
parser.add_argument('--model_path', type=str, default=None,
help='model_path')
parser.add_argument('--image_path', type=str, help='image_path|image_dir')
parser.add_argument('--model_path', type=str, help='model_path')
args = parser.parse_args()
text_detector = TextDetector(args.model_path)
@@ -32,23 +32,19 @@ root_path = Path(__file__).parent
class TextRecognizer(object):
def __init__(self, rec_model_path):
self.rec_image_shape = [3, 32, 320]
self.character_type = 'ch'
self.rec_batch_num = 6
self.rec_algorithm = 'CRNN'
self.character_dict_path = str(root_path / 'ppocr_keys_v1.txt')
self.use_space_char = True
self.postprocess_op = CTCLabelDecode(self.character_dict_path,
self.character_type,
self.use_space_char)
self.postprocess_op = CTCLabelDecode(self.character_dict_path)
self.session = onnxruntime.InferenceSession(rec_model_path)
def resize_norm_img(self, img, max_wh_ratio):
img_channel, img_height, img_width = self.rec_image_shape
assert img_channel == img.shape[2]
if self.character_type == "ch":
img_width = int((32 * max_wh_ratio))
max_wh_ratio = 1
img_width = int((32 * max_wh_ratio))
max_wh_ratio = 1
h, w = img.shape[:2]
ratio = w / float(h)
if math.ceil(img_height * ratio) > img_width:
@@ -128,10 +124,10 @@ class TextRecognizer(object):
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--image_dir', type=str, help='image_dir|image_path')
parser.add_argument('--image_path', type=str, help='image_dir|image_path')
parser.add_argument('--model_path', type=str, help='rec_model_path')
args = parser.parse_args()
text_recognizer = TextRecognizer(args.model_path)
rec_res, predict_time = text_recognizer(args.image_dir)
rec_res, predict_time = text_recognizer(args.image_path)
print(f'识别结果: {rec_res}\t cost: {predict_time}s')
+53 -72
View File
@@ -9,35 +9,75 @@ import imghdr
import numpy as np
class BaseRecLabelDecode(object):
def get_image_file_list(img_file):
imgs_lists = []
if img_file is None or not os.path.exists(img_file):
raise FileNotFoundError("not found any img file in {}".format(img_file))
img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff', 'gif', 'GIF'}
if os.path.isfile(img_file) and imghdr.what(img_file) in img_end:
imgs_lists.append(img_file)
elif os.path.isdir(img_file):
for single_file in os.listdir(img_file):
file_path = os.path.join(img_file, single_file)
if imghdr.what(file_path) in img_end:
imgs_lists.append(file_path)
if len(imgs_lists) == 0:
raise FileNotFoundError("not found any img file in {}".format(img_file))
return imgs_lists
def check_and_read_gif(img_path):
if os.path.basename(img_path)[-3:] in ['gif', 'GIF']:
gif = cv2.VideoCapture(img_path)
ret, frame = gif.read()
if not ret:
print("Cannot read {}. This gif image maybe corrupted.")
return None, False
if len(frame.shape) == 2 or frame.shape[-1] == 1:
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
imgvalue = frame[:, :, ::-1]
return imgvalue, True
return None, False
class CTCLabelDecode(object):
""" Convert between text-label and text-index """
def __init__(self,
character_dict_path=None,
character_type='ch',
use_space_char=False):
self.character_str = ""
assert character_dict_path is not None, "character_dict_path should not be None when character_type is ch"
def __init__(self, character_dict_path):
super(CTCLabelDecode, self).__init__()
self.character_str = []
assert character_dict_path is not None, "character_dict_path should not be None"
with open(character_dict_path, "rb") as fin:
lines = fin.readlines()
for line in lines:
line = line.decode('utf-8').strip("\n").strip("\r\n")
self.character_str += line
if use_space_char:
self.character_str += " "
dict_character = list(self.character_str)
self.character_str.append(line)
dict_character = self.character_str
self.character_type = character_type
dict_character = self.add_special_char(dict_character)
self.dict = {}
for i, char in enumerate(dict_character):
self.dict[char] = i
self.character = dict_character
def __call__(self, preds, label=None, *args, **kwargs):
preds_idx = preds.argmax(axis=2)
preds_prob = preds.max(axis=2)
text = self.decode(preds_idx, preds_prob, is_remove_duplicate=True)
if label is None:
return text
label = self.decode(label)
return text, label
def add_special_char(self, dict_character):
dict_character = ['blank'] + dict_character
return dict_character
def decode(self, text_index, text_prob=None, is_remove_duplicate=True):
def get_ignored_tokens(self):
return [0] # for ctc blank
def decode(self, text_index, text_prob=None, is_remove_duplicate=False):
""" convert text-index into text-label. """
result_list = []
ignored_tokens = self.get_ignored_tokens()
@@ -63,62 +103,3 @@ class BaseRecLabelDecode(object):
result_list.append((text, np.mean(conf_list)))
return result_list
def get_ignored_tokens(self):
return [0] # for ctc blank
class CTCLabelDecode(BaseRecLabelDecode):
""" Convert between text-label and text-index """
def __init__(self,
character_dict_path=None,
character_type='ch',
use_space_char=False,
**kwargs):
super(CTCLabelDecode, self).__init__(character_dict_path,
character_type, use_space_char)
def __call__(self, preds, label=None, *args, **kwargs):
preds_idx = preds.argmax(axis=2)
preds_prob = preds.max(axis=2)
text = self.decode(preds_idx, preds_prob)
if label is None:
return text
label = self.decode(label, is_remove_duplicate=False)
return text, label
def add_special_char(self, dict_character):
dict_character = ['blank'] + dict_character
return dict_character
def get_image_file_list(img_file):
imgs_lists = []
if img_file is None or not os.path.exists(img_file):
raise Exception("not found any img file in {}".format(img_file))
img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff', 'gif', 'GIF'}
if os.path.isfile(img_file) and imghdr.what(img_file) in img_end:
imgs_lists.append(img_file)
elif os.path.isdir(img_file):
for single_file in os.listdir(img_file):
file_path = os.path.join(img_file, single_file)
if imghdr.what(file_path) in img_end:
imgs_lists.append(file_path)
if len(imgs_lists) == 0:
raise Exception("not found any img file in {}".format(img_file))
return imgs_lists
def check_and_read_gif(img_path):
if os.path.basename(img_path)[-3:] in ['gif', 'GIF']:
gif = cv2.VideoCapture(img_path)
ret, frame = gif.read()
if not ret:
print("Cannot read {}. This gif image maybe corrupted.")
return None, False
if len(frame.shape) == 2 or frame.shape[-1] == 1:
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
imgvalue = frame[:, :, ::-1]
return imgvalue, True
return None, False
@@ -32,23 +32,19 @@ root_path = Path(__file__).parent
class TextRecognizer(object):
def __init__(self, rec_model_path):
self.rec_image_shape = [3, 32, 320]
self.character_type = 'ch'
self.rec_batch_num = 6
self.rec_algorithm = 'CRNN'
self.character_dict_path = str(root_path / 'japan_dict.txt')
self.use_space_char = True
self.postprocess_op = CTCLabelDecode(self.character_dict_path,
self.character_type,
self.use_space_char)
self.postprocess_op = CTCLabelDecode(self.character_dict_path)
self.session = onnxruntime.InferenceSession(rec_model_path)
def resize_norm_img(self, img, max_wh_ratio):
img_channel, img_height, img_width = self.rec_image_shape
assert img_channel == img.shape[2]
if self.character_type == "ch":
img_width = int((32 * max_wh_ratio))
max_wh_ratio = 1
img_width = int((32 * max_wh_ratio))
max_wh_ratio = 1
h, w = img.shape[:2]
ratio = w / float(h)
if math.ceil(img_height * ratio) > img_width:
@@ -127,10 +123,10 @@ class TextRecognizer(object):
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--image_dir', type=str, help='image_dir|image_path')
parser.add_argument('--image_path', type=str, help='image_dir|image_path')
parser.add_argument('--model_path', type=str, help='rec_model_path')
args = parser.parse_args()
text_recognizer = TextRecognizer(args.model_path)
rec_res, predict_time = text_recognizer(args.image_dir)
rec_res, predict_time = text_recognizer(args.image_path)
print(f'识别结果: {rec_res}\t cost: {predict_time}s')
+55 -74
View File
@@ -1,7 +1,7 @@
# !/usr/bin/env python
# -*- encoding: utf-8 -*-
# @File: PostProcess.py
# @Time: 2021/01/19 14:12:35
# @File: utils.py
# @Time: 2021/05/22 10:53:17
# @Author: Max
import cv2
import os
@@ -9,35 +9,75 @@ import imghdr
import numpy as np
class BaseRecLabelDecode(object):
def get_image_file_list(img_file):
imgs_lists = []
if img_file is None or not os.path.exists(img_file):
raise FileNotFoundError("not found any img file in {}".format(img_file))
img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff', 'gif', 'GIF'}
if os.path.isfile(img_file) and imghdr.what(img_file) in img_end:
imgs_lists.append(img_file)
elif os.path.isdir(img_file):
for single_file in os.listdir(img_file):
file_path = os.path.join(img_file, single_file)
if imghdr.what(file_path) in img_end:
imgs_lists.append(file_path)
if len(imgs_lists) == 0:
raise FileNotFoundError("not found any img file in {}".format(img_file))
return imgs_lists
def check_and_read_gif(img_path):
if os.path.basename(img_path)[-3:] in ['gif', 'GIF']:
gif = cv2.VideoCapture(img_path)
ret, frame = gif.read()
if not ret:
print("Cannot read {}. This gif image maybe corrupted.")
return None, False
if len(frame.shape) == 2 or frame.shape[-1] == 1:
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
imgvalue = frame[:, :, ::-1]
return imgvalue, True
return None, False
class CTCLabelDecode(object):
""" Convert between text-label and text-index """
def __init__(self,
character_dict_path=None,
character_type='ch',
use_space_char=False):
self.character_str = ""
assert character_dict_path is not None, "character_dict_path should not be None when character_type is ch"
def __init__(self, character_dict_path):
super(CTCLabelDecode, self).__init__()
self.character_str = []
assert character_dict_path is not None, "character_dict_path should not be None"
with open(character_dict_path, "rb") as fin:
lines = fin.readlines()
for line in lines:
line = line.decode('utf-8').strip("\n").strip("\r\n")
self.character_str += line
if use_space_char:
self.character_str += " "
dict_character = list(self.character_str)
self.character_str.append(line)
dict_character = self.character_str
self.character_type = character_type
dict_character = self.add_special_char(dict_character)
self.dict = {}
for i, char in enumerate(dict_character):
self.dict[char] = i
self.character = dict_character
def __call__(self, preds, label=None, *args, **kwargs):
preds_idx = preds.argmax(axis=2)
preds_prob = preds.max(axis=2)
text = self.decode(preds_idx, preds_prob, is_remove_duplicate=True)
if label is None:
return text
label = self.decode(label)
return text, label
def add_special_char(self, dict_character):
dict_character = ['blank'] + dict_character
return dict_character
def decode(self, text_index, text_prob=None, is_remove_duplicate=True):
def get_ignored_tokens(self):
return [0] # for ctc blank
def decode(self, text_index, text_prob=None, is_remove_duplicate=False):
""" convert text-index into text-label. """
result_list = []
ignored_tokens = self.get_ignored_tokens()
@@ -63,62 +103,3 @@ class BaseRecLabelDecode(object):
result_list.append((text, np.mean(conf_list)))
return result_list
def get_ignored_tokens(self):
return [0] # for ctc blank
class CTCLabelDecode(BaseRecLabelDecode):
""" Convert between text-label and text-index """
def __init__(self,
character_dict_path=None,
character_type='ch',
use_space_char=False,
**kwargs):
super(CTCLabelDecode, self).__init__(character_dict_path,
character_type, use_space_char)
def __call__(self, preds, label=None, *args, **kwargs):
preds_idx = preds.argmax(axis=2)
preds_prob = preds.max(axis=2)
text = self.decode(preds_idx, preds_prob)
if label is None:
return text
label = self.decode(label, is_remove_duplicate=False)
return text, label
def add_special_char(self, dict_character):
dict_character = ['blank'] + dict_character
return dict_character
def get_image_file_list(img_file):
imgs_lists = []
if img_file is None or not os.path.exists(img_file):
raise Exception("not found any img file in {}".format(img_file))
img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff', 'gif', 'GIF'}
if os.path.isfile(img_file) and imghdr.what(img_file) in img_end:
imgs_lists.append(img_file)
elif os.path.isdir(img_file):
for single_file in os.listdir(img_file):
file_path = os.path.join(img_file, single_file)
if imghdr.what(file_path) in img_end:
imgs_lists.append(file_path)
if len(imgs_lists) == 0:
raise Exception("not found any img file in {}".format(img_file))
return imgs_lists
def check_and_read_gif(img_path):
if os.path.basename(img_path)[-3:] in ['gif', 'GIF']:
gif = cv2.VideoCapture(img_path)
ret, frame = gif.read()
if not ret:
print("Cannot read {}. This gif image maybe corrupted.")
return None, False
if len(frame.shape) == 2 or frame.shape[-1] == 1:
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
imgvalue = frame[:, :, ::-1]
return imgvalue, True
return None, False
+4 -1
View File
@@ -1,4 +1,7 @@
python RapidOCR.py --use_server False \
#! /bin/bash
set -e errexit
python rapidOCR.py --use_server False \
--det_model_path models/ch_ppocr_mobile_v2.0_det_infer.onnx \
--cls_model_path models/ch_ppocr_mobile_v2.0_cls_infer.onnx \
--rec_model_path models/ch_ppocr_mobile_v2.0_rec_infer.onnx \