2022-08-10 16:42:09 +08:00
|
|
|
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
import PIL
|
|
|
|
|
|
|
|
|
|
from modelscope.pipelines import pipeline
|
|
|
|
|
from modelscope.pipelines.base import Pipeline
|
|
|
|
|
from modelscope.utils.constant import Tasks
|
2022-09-08 14:08:51 +08:00
|
|
|
from modelscope.utils.demo_utils import DemoCompatibilityCheck
|
2022-08-10 16:42:09 +08:00
|
|
|
from modelscope.utils.test_utils import test_level
|
|
|
|
|
|
|
|
|
|
|
2022-09-08 14:08:51 +08:00
|
|
|
class OCRRecognitionTest(unittest.TestCase, DemoCompatibilityCheck):
|
2022-08-10 16:42:09 +08:00
|
|
|
|
|
|
|
|
def setUp(self) -> None:
|
2023-02-10 02:11:59 +00:00
|
|
|
self.model_id = 'damo/cv_crnn_ocr-recognition-general_damo'
|
2022-08-10 16:42:09 +08:00
|
|
|
self.test_image = 'data/test/images/ocr_recognition.jpg'
|
2022-09-08 14:08:51 +08:00
|
|
|
self.task = Tasks.ocr_recognition
|
2022-08-10 16:42:09 +08:00
|
|
|
|
|
|
|
|
def pipeline_inference(self, pipeline: Pipeline, input_location: str):
|
|
|
|
|
result = pipeline(input_location)
|
|
|
|
|
print('ocr recognition results: ', result)
|
|
|
|
|
|
2022-08-19 19:02:27 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
2022-08-10 16:42:09 +08:00
|
|
|
def test_run_with_model_from_modelhub(self):
|
2023-02-10 02:11:59 +00:00
|
|
|
ocr_recognition = pipeline(
|
|
|
|
|
Tasks.ocr_recognition,
|
|
|
|
|
model=self.model_id,
|
|
|
|
|
model_revision='v1.0.0')
|
2022-08-10 16:42:09 +08:00
|
|
|
self.pipeline_inference(ocr_recognition, self.test_image)
|
|
|
|
|
|
2022-08-19 19:02:27 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
|
2022-08-10 16:42:09 +08:00
|
|
|
def test_run_with_model_from_modelhub_PILinput(self):
|
2023-02-10 02:11:59 +00:00
|
|
|
ocr_recognition = pipeline(
|
|
|
|
|
Tasks.ocr_recognition,
|
|
|
|
|
model=self.model_id,
|
|
|
|
|
model_revision='v1.0.0')
|
2022-08-10 16:42:09 +08:00
|
|
|
imagePIL = PIL.Image.open(self.test_image)
|
|
|
|
|
self.pipeline_inference(ocr_recognition, imagePIL)
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
|
|
|
|
|
def test_run_modelhub_default_model(self):
|
2023-02-10 02:11:59 +00:00
|
|
|
ocr_recognition = pipeline(
|
|
|
|
|
Tasks.ocr_recognition, model_revision='v2.0.0')
|
2022-08-10 16:42:09 +08:00
|
|
|
self.pipeline_inference(ocr_recognition, self.test_image)
|
|
|
|
|
|
2022-09-09 14:56:15 +08:00
|
|
|
@unittest.skip('demo compatibility test is only enabled on a needed-basis')
|
2022-09-08 14:08:51 +08:00
|
|
|
def test_demo_compatibility(self):
|
|
|
|
|
self.compatibility_check()
|
|
|
|
|
|
2022-08-10 16:42:09 +08:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|