Skip to content

Commit 05b4b32

Browse files
committed
chore: optimize code
1 parent 6c27a10 commit 05b4b32

13 files changed

+540
-291
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
1111
<a href="https://pypi.org/project/rapid-orientation/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapid-orientation"></a>
1212
<a href="https://pepy.tech/project/rapid-orientation"><img src="https://static.pepy.tech/personalized-badge/rapid-orientation?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=Downloads"></a>
13+
<a href="https://github.com/RapidAI/RapidOrientation/stargazers"><img src="https://img.shields.io/github/stars/RapidAI/RapidOrientation?color=ccf"></a>
14+
<a href="https://semver.org/"><img alt="SemVer2.0" src="https://img.shields.io/badge/SemVer-2.0-brightgreen"></a>
15+
<a href="https://github.com/psf/black"><img src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
1316

1417
</div>
1518

demo.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,36 @@
55

66
from rapid_orientation import RapidOrientation
77

8+
9+
def scale_resize(img, resize_value=(280, 32)):
10+
"""
11+
@params:
12+
img: ndarray
13+
resize_value: (width, height)
14+
"""
15+
# padding
16+
ratio = resize_value[0] / resize_value[1] # w / h
17+
h, w = img.shape[:2]
18+
if w / h < ratio:
19+
# 补宽
20+
t = int(h * ratio)
21+
w_padding = (t - w) // 2
22+
img = cv2.copyMakeBorder(
23+
img, 0, 0, w_padding, w_padding, cv2.BORDER_CONSTANT, value=(0, 0, 0)
24+
)
25+
else:
26+
# 补高 (top, bottom, left, right)
27+
t = int(w / ratio)
28+
h_padding = (t - h) // 2
29+
color = tuple([int(i) for i in img[0, 0, :]])
30+
img = cv2.copyMakeBorder(
31+
img, h_padding, h_padding, 0, 0, cv2.BORDER_CONSTANT, value=(0, 0, 0)
32+
)
33+
img = cv2.resize(img, resize_value, interpolation=cv2.INTER_LANCZOS4)
34+
return img
35+
36+
837
orientation_engine = RapidOrientation()
9-
img = cv2.imread("tests/test_files/img_rot180_demo.jpg")
38+
img = cv2.imread("tests/test_files/1.png")
1039
cls_result, _ = orientation_engine(img)
1140
print(cls_result)

rapid_orientation/config.yaml

-8
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,3 @@ CUDAExecutionProvider:
66
arena_extend_strategy: kNextPowerOfTwo
77
cudnn_conv_algo_search: EXHAUSTIVE
88
do_copy_in_default_stream: true
9-
10-
PreProcess:
11-
- ResizeImage:
12-
resize_short: 256
13-
- CropImage:
14-
size: 224
15-
- NormalizeImage:
16-
- ToCHWImage:

rapid_orientation/main.py

+22-23
Original file line numberDiff line numberDiff line change
@@ -17,53 +17,52 @@
1717
import argparse
1818
import time
1919
from pathlib import Path
20-
from typing import Optional, Union
20+
from typing import Union
2121

2222
import cv2
2323
import numpy as np
24-
import yaml
2524

26-
from .utils import LoadImage, OrtInferSession, create_operators
25+
from .utils.infer_engine import OrtInferSession
26+
from .utils.load_image import LoadImage
27+
from .utils.preprocess import Preprocess
28+
from .utils.utils import read_yaml
2729

2830
root_dir = Path(__file__).resolve().parent
31+
DEFAULT_PATH = root_dir / "models" / "rapid_orientation.onnx"
32+
DEFAULT_CFG = root_dir / "config.yaml"
2933

3034

3135
class RapidOrientation:
32-
def __init__(self, model_path: Optional[str] = None):
33-
config_path = str(root_dir / "config.yaml")
34-
config = self.read_yaml(config_path)
35-
if model_path is None:
36-
model_path = str(root_dir / "models" / "rapid_orientation.onnx")
36+
def __init__(
37+
self,
38+
model_path: Union[str, Path] = DEFAULT_PATH,
39+
cfg_path: Union[str, Path] = DEFAULT_CFG,
40+
):
41+
config = read_yaml(cfg_path)
3742
config["model_path"] = model_path
3843

3944
self.session = OrtInferSession(config)
40-
self.labels = self.session.get_metadata()["character"].splitlines()
41-
42-
self.preprocess_ops = create_operators(config["PreProcess"])
45+
self.labels = self.session.get_character_list()
4346

47+
self.preprocess = Preprocess()
4448
self.load_img = LoadImage()
4549

4650
def __call__(self, img_content: Union[str, np.ndarray, bytes, Path]):
47-
images = self.load_img(img_content)
51+
image = self.load_img(img_content)
52+
53+
s = time.perf_counter()
4854

49-
s = time.time()
50-
for ops in self.preprocess_ops:
51-
images = ops(images)
52-
image = np.array(images)[None, ...]
55+
image = self.preprocess(image)
56+
image = image[None, ...]
5357

5458
pred_output = self.session(image)[0]
5559

5660
pred_output = pred_output.squeeze()
5761
pred_idx = np.argmax(pred_output)
5862
pred_txt = self.labels[pred_idx]
59-
elapse = time.time() - s
60-
return pred_txt, elapse
6163

62-
@staticmethod
63-
def read_yaml(yaml_path):
64-
with open(yaml_path, "rb") as f:
65-
data = yaml.load(f, Loader=yaml.Loader)
66-
return data
64+
elapse = time.perf_counter() - s
65+
return pred_txt, elapse
6766

6867

6968
def main():

rapid_orientation/utils.py

-258
This file was deleted.

rapid_orientation/utils/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# -*- encoding: utf-8 -*-
2+
# @Author: SWHL
3+
# @Contact: liekkaskono@163.com

0 commit comments

Comments
 (0)