-
-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
71 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,10 @@ | |
# @Contact: [email protected] | ||
import cv2 | ||
|
||
from rapidocr import RapidOCR, VisRes | ||
|
||
# from rapidocr_onnxruntime import RapidOCR, VisRes | ||
from rapidocr_torch import RapidOCR, VisRes | ||
|
||
# from rapidocr import RapidOCR, VisRes | ||
|
||
|
||
# from rapidocr_paddle import RapidOCR, VisRes | ||
|
@@ -14,7 +15,7 @@ | |
# yaml_path = "tests/test_files/config.yaml" | ||
# engine = RapidOCR(config_path=yaml_path) | ||
|
||
engine = RapidOCR(params={"Cls.model_path": "1.onnx"}) | ||
engine = RapidOCR(params={"Global.with_paddle": True}) | ||
vis = VisRes() | ||
|
||
image_path = "tests/test_files/ch_en_num.jpg" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# -*- encoding: utf-8 -*- | ||
# @Author: SWHL | ||
# @Contact: [email protected] | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
import requests | ||
from tqdm import tqdm | ||
|
||
|
||
def download_file(url: str, save_path: Union[str, Path]): | ||
response = requests.get(url, stream=True, timeout=60) | ||
status_code = response.status_code | ||
|
||
if status_code != 200: | ||
raise DownloadFileError("Something went wrong while downloading models") | ||
|
||
total_size_in_bytes = int(response.headers.get("content-length", 1)) | ||
block_size = 1024 # 1 Kibibyte | ||
with tqdm(total=total_size_in_bytes, unit="iB", unit_scale=True) as pb: | ||
with open(save_path, "wb") as file: | ||
for data in response.iter_content(block_size): | ||
pb.update(len(data)) | ||
file.write(data) | ||
|
||
|
||
class DownloadFileError(Exception): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
# -*- encoding: utf-8 -*- | ||
# @Author: SWHL | ||
# @Contact: [email protected] | ||
import os | ||
import platform | ||
from pathlib import Path | ||
from typing import Optional, Union, Dict | ||
from typing import Dict, Optional, Union | ||
|
||
import numpy as np | ||
import torch | ||
|
@@ -13,18 +11,20 @@ | |
root_dir = Path(__file__).resolve().parent.parent | ||
DEFAULT_CFG_PATH = root_dir / "arch_config.yaml" | ||
|
||
|
||
def read_yaml(yaml_path: Union[str, Path]) -> Dict[str, Dict]: | ||
with open(yaml_path, "rb") as f: | ||
data = yaml.load(f, Loader=yaml.Loader) | ||
return data | ||
|
||
from .logger import get_logger | ||
|
||
from rapidocr_torch.modeling.architectures.base_model import BaseModel | ||
|
||
from .logger import get_logger | ||
|
||
|
||
class TorchInferSession: | ||
def __init__(self, config, mode: Optional[str] = None) -> None: | ||
|
||
all_arch_config = read_yaml(DEFAULT_CFG_PATH) | ||
|
||
self.logger = get_logger("TorchInferSession") | ||
|
@@ -42,6 +42,7 @@ def __init__(self, config, mode: Optional[str] = None) -> None: | |
if config["use_cuda"]: | ||
self.predictor.cuda() | ||
self.use_gpu = True | ||
|
||
def __call__(self, img: np.ndarray): | ||
with torch.no_grad(): | ||
inp = torch.from_numpy(img) | ||
|
@@ -50,6 +51,7 @@ def __call__(self, img: np.ndarray): | |
# 适配跟onnx对齐取值逻辑 | ||
outputs = self.predictor(inp).unsqueeze(0) | ||
return outputs.cpu().numpy() | ||
|
||
@staticmethod | ||
def _verify_model(model_path): | ||
model_path = Path(model_path) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters