mirror of
https://github.com/RapidAI/RapidOCR.git
synced 2026-11-11 05:20:49 +08:00
Add verify model function.
This commit is contained in:
1 parent
b95cb49371
commit
c1fa81f176
12 files changed
+138
-41
No files matched your search
@@ -11,6 +11,7 @@
|
||||
# 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.
|
||||
from pathlib import Path
|
||||
import warnings
|
||||
|
||||
import yaml
|
||||
@@ -38,6 +39,7 @@ class OrtInferSession(object):
|
||||
EP_list = [(cuda_ep, config[cuda_ep])]
|
||||
EP_list.append((cpu_ep, cpu_provider_options))
|
||||
|
||||
self._verify_model(config['model_path'])
|
||||
self.session = InferenceSession(config['model_path'],
|
||||
sess_options=sess_opt,
|
||||
providers=EP_list)
|
||||
@@ -55,6 +57,14 @@ class OrtInferSession(object):
|
||||
def get_output_name(self, output_idx=0):
|
||||
return self.session.get_outputs()[output_idx].name
|
||||
|
||||
@staticmethod
|
||||
def _verify_model(model_path):
|
||||
model_path = Path(model_path)
|
||||
if not model_path.exists():
|
||||
raise FileNotFoundError(f'{model_path} does not exists.')
|
||||
if not model_path.is_file():
|
||||
raise FileExistsError(f'{model_path} is not a file.')
|
||||
|
||||
|
||||
def read_yaml(yaml_path):
|
||||
with open(yaml_path, 'rb') as f:
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# @Author: SWHL
|
||||
# @Contact: liekkaskono@163.com
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import warnings
|
||||
|
||||
@@ -49,6 +50,7 @@ class OrtInferSession(object):
|
||||
EP_list = [(cuda_ep, config[cuda_ep])]
|
||||
EP_list.append((cpu_ep, cpu_provider_options))
|
||||
|
||||
self._verify_model(config['model_path'])
|
||||
self.session = InferenceSession(config['model_path'],
|
||||
sess_options=sess_opt,
|
||||
providers=EP_list)
|
||||
@@ -66,6 +68,14 @@ class OrtInferSession(object):
|
||||
def get_output_name(self, output_idx=0):
|
||||
return self.session.get_outputs()[output_idx].name
|
||||
|
||||
@staticmethod
|
||||
def _verify_model(model_path):
|
||||
model_path = Path(model_path)
|
||||
if not model_path.exists():
|
||||
raise FileNotFoundError(f'{model_path} does not exists.')
|
||||
if not model_path.is_file():
|
||||
raise FileExistsError(f'{model_path} is not a file.')
|
||||
|
||||
|
||||
def read_yaml(yaml_path):
|
||||
with open(yaml_path, 'rb') as f:
|
||||
|
||||
@@ -30,10 +30,9 @@ class TextRecognizer(object):
|
||||
session_instance = OrtInferSession(config)
|
||||
self.session = session_instance.session
|
||||
self.input_name = session_instance.get_input_name()
|
||||
meta_dict = session_instance.get_metadata()
|
||||
|
||||
if 'character' in meta_dict.keys():
|
||||
self.character_dict_path = meta_dict['character'].splitlines()
|
||||
if session_instance.have_key():
|
||||
self.character_dict_path = session_instance.get_character_list()
|
||||
else:
|
||||
self.character_dict_path = config.get('keys_path', None)
|
||||
self.postprocess_op = CTCLabelDecode(self.character_dict_path)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# @Author: SWHL
|
||||
# @Contact: liekkaskono@163.com
|
||||
from pathlib import Path
|
||||
import warnings
|
||||
|
||||
import numpy as np
|
||||
@@ -29,6 +30,7 @@ class OrtInferSession(object):
|
||||
EP_list = [(cuda_ep, config[cuda_ep])]
|
||||
EP_list.append((cpu_ep, cpu_provider_options))
|
||||
|
||||
self._verify_model(config['model_path'])
|
||||
self.session = InferenceSession(config['model_path'],
|
||||
sess_options=sess_opt,
|
||||
providers=EP_list)
|
||||
@@ -46,9 +48,22 @@ class OrtInferSession(object):
|
||||
def get_output_name(self, output_idx=0):
|
||||
return self.session.get_outputs()[output_idx].name
|
||||
|
||||
def get_metadata(self):
|
||||
meta_dict = self.session.get_modelmeta().custom_metadata_map
|
||||
return meta_dict
|
||||
def get_character_list(self, key: str = 'character'):
|
||||
return self.meta_dict[key].splitlines()
|
||||
|
||||
def have_key(self, key: str = 'character') -> bool:
|
||||
self.meta_dict = self.session.get_modelmeta().custom_metadata_map
|
||||
if key in self.meta_dict.keys():
|
||||
return True
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def _verify_model(model_path):
|
||||
model_path = Path(model_path)
|
||||
if not model_path.exists():
|
||||
raise FileNotFoundError(f'{model_path} does not exists.')
|
||||
if not model_path.is_file():
|
||||
raise FileExistsError(f'{model_path} is not a file.')
|
||||
|
||||
|
||||
def read_yaml(yaml_path):
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
# 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.
|
||||
from pathlib import Path
|
||||
import yaml
|
||||
from openvino.runtime import Core
|
||||
|
||||
@@ -18,10 +19,20 @@ from openvino.runtime import Core
|
||||
class OpenVINOInferSession(object):
|
||||
def __init__(self, config):
|
||||
ie = Core()
|
||||
|
||||
self._verify_model(config['model_path'])
|
||||
model_onnx = ie.read_model(config['model_path'])
|
||||
compile_model = ie.compile_model(model=model_onnx, device_name='CPU')
|
||||
self.session = compile_model.create_infer_request()
|
||||
|
||||
@staticmethod
|
||||
def _verify_model(model_path):
|
||||
model_path = Path(model_path)
|
||||
if not model_path.exists():
|
||||
raise FileNotFoundError(f'{model_path} does not exists.')
|
||||
if not model_path.is_file():
|
||||
raise FileExistsError(f'{model_path} is not a file.')
|
||||
|
||||
|
||||
def read_yaml(yaml_path):
|
||||
with open(yaml_path, 'rb') as f:
|
||||
|
||||
@@ -16,27 +16,35 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# @Author: SWHL
|
||||
# @Contact: liekkaskono@163.com
|
||||
from __future__ import (absolute_import, division, print_function,
|
||||
unicode_literals)
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
import pyclipper
|
||||
import six
|
||||
from shapely.geometry import Polygon
|
||||
import yaml
|
||||
from openvino.runtime import Core
|
||||
from shapely.geometry import Polygon
|
||||
|
||||
|
||||
class OpenVINOInferSession(object):
|
||||
def __init__(self, config):
|
||||
ie = Core()
|
||||
|
||||
self._verify_model(config['model_path'])
|
||||
model_onnx = ie.read_model(config['model_path'])
|
||||
compile_model = ie.compile_model(model=model_onnx, device_name='CPU')
|
||||
self.session = compile_model.create_infer_request()
|
||||
|
||||
@staticmethod
|
||||
def _verify_model(model_path):
|
||||
model_path = Path(model_path)
|
||||
if not model_path.exists():
|
||||
raise FileNotFoundError(f'{model_path} does not exists.')
|
||||
if not model_path.is_file():
|
||||
raise FileExistsError(f'{model_path} is not a file.')
|
||||
|
||||
|
||||
def read_yaml(yaml_path):
|
||||
with open(yaml_path, 'rb') as f:
|
||||
|
||||
@@ -34,7 +34,7 @@ class TextRecognizer(object):
|
||||
self.rec_image_shape = config['rec_img_shape']
|
||||
self.rec_batch_num = config['rec_batch_num']
|
||||
|
||||
dict_path = str(Path(__file__).parent / './ppocr_keys_v1.txt')
|
||||
dict_path = str(Path(__file__).parent / 'ppocr_keys_v1.txt')
|
||||
self.character_dict_path = config.get( 'keys_path', dict_path)
|
||||
self.postprocess_op = CTCLabelDecode(self.character_dict_path)
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# @Author: SWHL
|
||||
# @Contact: liekkaskono@163.com
|
||||
from pathlib import Path
|
||||
import numpy as np
|
||||
import yaml
|
||||
from openvino.runtime import Core
|
||||
@@ -9,10 +10,19 @@ from openvino.runtime import Core
|
||||
class OpenVINOInferSession(object):
|
||||
def __init__(self, config):
|
||||
ie = Core()
|
||||
self._verify_model(config['model_path'])
|
||||
model_onnx = ie.read_model(config['model_path'])
|
||||
compile_model = ie.compile_model(model=model_onnx, device_name='CPU')
|
||||
self.session = compile_model.create_infer_request()
|
||||
|
||||
@staticmethod
|
||||
def _verify_model(model_path):
|
||||
model_path = Path(model_path)
|
||||
if not model_path.exists():
|
||||
raise FileNotFoundError(f'{model_path} does not exists.')
|
||||
if not model_path.is_file():
|
||||
raise FileExistsError(f'{model_path} is not a file.')
|
||||
|
||||
|
||||
def read_yaml(yaml_path):
|
||||
with open(yaml_path, 'rb') as f:
|
||||
|
||||
Reference in new issue
Block a user