Skip to content

Commit

Permalink
chore: optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
SWHL committed Jan 17, 2025
1 parent 45bb4ed commit 94576e5
Show file tree
Hide file tree
Showing 17 changed files with 50 additions and 141 deletions.
4 changes: 2 additions & 2 deletions python/rapidocr_onnxruntime/ch_ppocr_det/text_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ def __call__(self, img: np.ndarray) -> Tuple[Optional[np.ndarray], float]:
return dt_boxes, elapse

def get_preprocess(self, max_wh):
if self.limit_type == 'min':
if self.limit_type == "min":
limit_side_len = self.limit_side_len
elif max_wh < 960:
limit_side_len = 960
elif max_wh < 1500:
limit_side_len = 1500
else:
limit_side_len = 2000
return DetPreProcess(limit_side_len, self.limit_type,self.mean, self.std)
return DetPreProcess(limit_side_len, self.limit_type, self.mean, self.std)

def filter_tag_det_res(
self, dt_boxes: np.ndarray, image_shape: Tuple[int, int]
Expand Down
7 changes: 5 additions & 2 deletions python/rapidocr_onnxruntime/ch_ppocr_det/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@


class DetPreProcess:
def __init__(self, limit_side_len: int = 736, limit_type: str = "min", mean=None,
std=None):
def __init__(
self, limit_side_len: int = 736, limit_type: str = "min", mean=None, std=None
):
if mean is None:
mean = [0.5, 0.5, 0.5]

if std is None:
std = [0.5, 0.5, 0.5]

self.mean = np.array(mean)
self.std = np.array(std)
self.scale = 1 / 255.0
Expand Down
5 changes: 1 addition & 4 deletions python/rapidocr_onnxruntime/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ Det:
limit_type: min
std: [ 0.5, 0.5, 0.5 ]
mean: [ 0.5, 0.5, 0.5 ]
# limit_side_len: 960
# limit_type: max
# std: [ 0.229, 0.224, 0.225 ]
# mean: [ 0.485, 0.456, 0.406 ]

thresh: 0.3
box_thresh: 0.5
max_candidates: 1000
Expand Down
14 changes: 0 additions & 14 deletions python/rapidocr_openvino/ch_ppocr_cls/config.yaml

This file was deleted.

21 changes: 0 additions & 21 deletions python/rapidocr_openvino/ch_ppocr_det/config.yaml

This file was deleted.

11 changes: 7 additions & 4 deletions python/rapidocr_openvino/ch_ppocr_det/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@


class DetPreProcess:
def __init__(self, limit_side_len: int = 736, limit_type: str = "min",mean=None,
std=None):
def __init__(
self, limit_side_len: int = 736, limit_type: str = "min", mean=None, std=None
):
if mean is None:
mean = [0.485, 0.456, 0.406]
mean = [0.5, 0.5, 0.5]

if std is None:
std = [0.229, 0.224, 0.225]
std = [0.5, 0.5, 0.5]

self.mean = np.array(mean)
self.std = np.array(std)
self.scale = 1 / 255.0
Expand Down
4 changes: 0 additions & 4 deletions python/rapidocr_openvino/ch_ppocr_rec/config.yaml

This file was deleted.

2 changes: 2 additions & 0 deletions python/rapidocr_openvino/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Det:

limit_side_len: 736
limit_type: min
std: [ 0.5, 0.5, 0.5 ]
mean: [ 0.5, 0.5, 0.5 ]

thresh: 0.3
box_thresh: 0.5
Expand Down
10 changes: 0 additions & 10 deletions python/rapidocr_paddle/ch_ppocr_cls/config.yaml

This file was deleted.

19 changes: 1 addition & 18 deletions python/rapidocr_paddle/ch_ppocr_cls/text_cls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import copy
import math
import time
Expand All @@ -20,7 +19,7 @@
import cv2
import numpy as np

from rapidocr_paddle.utils import PaddleInferSession, read_yaml
from rapidocr_paddle.utils import PaddleInferSession

from .utils import ClsPostProcess

Expand Down Expand Up @@ -96,19 +95,3 @@ def resize_norm_img(self, img):
padding_im = np.zeros((img_c, img_h, img_w), dtype=np.float32)
padding_im[:, :, :resized_w] = resized_image
return padding_im


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--image_path", type=str, help="image_dir|image_path")
parser.add_argument("--config_path", type=str, default="config.yaml")
args = parser.parse_args()

config = read_yaml(args.config_path)

text_classifier = TextClassifier(config)

img = cv2.imread(args.image_path)
img_list, cls_res, predict_time = text_classifier(img)
for ino in range(len(img_list)):
print(f"cls result:{cls_res[ino]}")
26 changes: 0 additions & 26 deletions python/rapidocr_paddle/ch_ppocr_det/config.yaml

This file was deleted.

11 changes: 7 additions & 4 deletions python/rapidocr_paddle/ch_ppocr_det/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@


class DetPreProcess:
def __init__(self, limit_side_len: int = 736, limit_type: str = "min", mean=None,
std=None):
def __init__(
self, limit_side_len: int = 736, limit_type: str = "min", mean=None, std=None
):
if mean is None:
mean = [0.485, 0.456, 0.406]
mean = [0.5, 0.5, 0.5]

if std is None:
std = [0.229, 0.224, 0.225]
std = [0.5, 0.5, 0.5]

self.mean = np.array(mean)
self.std = np.array(std)
self.scale = 1 / 255.0
Expand Down
8 changes: 0 additions & 8 deletions python/rapidocr_paddle/ch_ppocr_rec/config.yaml

This file was deleted.

17 changes: 1 addition & 16 deletions python/rapidocr_paddle/ch_ppocr_rec/text_recognize.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import math
import time
from pathlib import Path
Expand All @@ -20,7 +19,7 @@
import cv2
import numpy as np

from rapidocr_paddle.utils import PaddleInferSession, read_yaml
from rapidocr_paddle.utils import PaddleInferSession

from .utils import CTCLabelDecode

Expand Down Expand Up @@ -112,17 +111,3 @@ def resize_norm_img(self, img: np.ndarray, max_wh_ratio: float) -> np.ndarray:
padding_im = np.zeros((img_channel, img_height, img_width), dtype=np.float32)
padding_im[:, :, 0:resized_w] = resized_image
return padding_im


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--image_path", type=str, help="image_dir|image_path")
parser.add_argument("--config_path", type=str, default="config.yaml")
args = parser.parse_args()

config = read_yaml(args.config_path)
text_recognizer = TextRecognizer(config)

img = cv2.imread(args.image_path)
rec_res, predict_time = text_recognizer(img)
print(f"rec result: {rec_res}\t cost: {predict_time}s")
2 changes: 2 additions & 0 deletions python/rapidocr_paddle/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Det:

limit_side_len: 736
limit_type: min
std: [ 0.5, 0.5, 0.5 ]
mean: [ 0.5, 0.5, 0.5 ]

thresh: 0.3
box_thresh: 0.5
Expand Down
15 changes: 11 additions & 4 deletions python/tests/test_paddle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: liekkaskono@163.com
"""
以下测试用例在以下环境中可以全部通过
OS: macOS 15.1.1 (24B91)
Python: 3.10.13
paddlepaddle: 3.0.0b2 CPU
"""

import sys
from pathlib import Path
from typing import List
Expand All @@ -27,7 +34,7 @@ def test_long_img():
download_file(img_url, save_path=img_path)
result, _ = engine(img_path)
assert result is not None
assert len(result) == 55
assert len(result) == 53
img_path.unlink()


Expand Down Expand Up @@ -62,7 +69,7 @@ def test_transparent_img(img_name: str, gt: str):
(
"test_letterbox_like.jpg",
2,
"A::取决于所使用的执行提供者,它可能没有完全支持模型中的所有操作。回落到CPU操作可能会导致性能速度的下降。此外,即使一个操作是由CUDAeXecution",
"A::取决于所使用的执行提供者,它可能没有完全支持模型中的所有操作。回落到CPU操作可能会导致性能速度的下降。此外,即使一个操作是由CUDAexecution",
),
("test_without_det.jpg", 1, "在中国作家协会第三届儿童文学"),
],
Expand All @@ -79,7 +86,7 @@ def test_only_det():
result, _ = engine(img_path, use_det=True, use_cls=False, use_rec=False)

assert len(result) == 18
assert result[0][0] == [5.0, 2.0]
assert result[0][0] == [6.0, 2.0]


def test_only_cls():
Expand Down Expand Up @@ -212,7 +219,7 @@ def test_input_three_ndim_one_channel():
),
(
"text_vertical_words.png",
["已", "取", "之", "時", "不", "參", "一", "人", "", "而"],
["已", "取", "之", "時", "不", "參", "一", "人", "", "而"],
),
(
"issue_170.png",
Expand Down
15 changes: 11 additions & 4 deletions python/tests/test_vino.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: liekkaskono@163.com
"""
以下测试用例在以下环境中可以全部通过
OS: macOS 15.1.1 (24B91)
Python: 3.10.13
openvino: 2024.0.0
"""

import sys
from pathlib import Path
from typing import List
Expand All @@ -27,7 +34,7 @@ def test_long_img():
download_file(img_url, save_path=img_path)
result, _ = engine(img_path)
assert result is not None
assert len(result) == 55
assert len(result) == 53
img_path.unlink()


Expand Down Expand Up @@ -62,7 +69,7 @@ def test_transparent_img(img_name: str, gt: str):
(
"test_letterbox_like.jpg",
2,
"A::取决于所使用的执行提供者,它可能没有完全支持模型中的所有操作。回落到CPU操作可能会导致性能速度的下降。此外,即使一个操作是由CUDAeXecution",
"A::取决于所使用的执行提供者,它可能没有完全支持模型中的所有操作。回落到CPU操作可能会导致性能速度的下降。此外,即使一个操作是由CUDAexecution",
),
("test_without_det.jpg", 1, "在中国作家协会第三届儿童文学"),
],
Expand All @@ -79,7 +86,7 @@ def test_only_det():
result, _ = engine(img_path, use_det=True, use_cls=False, use_rec=False)

assert len(result) == 18
assert result[0][0] == [5.0, 2.0]
assert result[0][0] == [6.0, 2.0]


def test_only_cls():
Expand Down Expand Up @@ -212,7 +219,7 @@ def test_input_three_ndim_one_channel():
),
(
"text_vertical_words.png",
["已", "取", "之", "時", "不", "參", "一", "人", "", "而"],
["已", "取", "之", "時", "不", "參", "一", "人", "", "而"],
),
(
"issue_170.png",
Expand Down

0 comments on commit 94576e5

Please sign in to comment.