You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_card_detection.py 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import os.path as osp
  3. import unittest
  4. import cv2
  5. from modelscope.msdatasets import MsDataset
  6. from modelscope.pipelines import pipeline
  7. from modelscope.utils.constant import Tasks
  8. from modelscope.utils.cv.image_utils import draw_card_detection_result
  9. from modelscope.utils.demo_utils import DemoCompatibilityCheck
  10. from modelscope.utils.test_utils import test_level
  11. class CardDetectionTest(unittest.TestCase, DemoCompatibilityCheck):
  12. def setUp(self) -> None:
  13. self.task = Tasks.card_detection
  14. self.model_id = 'damo/cv_resnet_carddetection_scrfd34gkps'
  15. def show_result(self, img_path, detection_result):
  16. img_list = draw_card_detection_result(img_path, detection_result)
  17. for i, img in enumerate(img_list):
  18. if i == 0:
  19. cv2.imwrite('result.jpg', img_list[0])
  20. print(
  21. f'Found {len(img_list)-1} cards, output written to {osp.abspath("result.jpg")}'
  22. )
  23. else:
  24. cv2.imwrite(f'card_{i}.jpg', img_list[i])
  25. save_path = osp.abspath(f'card_{i}.jpg')
  26. print(f'detect card_{i}: {save_path}')
  27. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  28. def test_run_with_dataset(self):
  29. input_location = ['data/test/images/card_detection.jpg']
  30. dataset = MsDataset.load(input_location, target='image')
  31. card_detection = pipeline(Tasks.card_detection, model=self.model_id)
  32. # note that for dataset output, the inference-output is a Generator that can be iterated.
  33. result = card_detection(dataset)
  34. result = next(result)
  35. self.show_result(input_location[0], result)
  36. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  37. def test_run_modelhub(self):
  38. card_detection = pipeline(Tasks.card_detection, model=self.model_id)
  39. img_path = 'data/test/images/card_detection.jpg'
  40. result = card_detection(img_path)
  41. self.show_result(img_path, result)
  42. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  43. def test_run_modelhub_default_model(self):
  44. card_detection = pipeline(Tasks.card_detection)
  45. img_path = 'data/test/images/card_detection.jpg'
  46. result = card_detection(img_path)
  47. self.show_result(img_path, result)
  48. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  49. def test_demo_compatibility(self):
  50. self.compatibility_check()
  51. if __name__ == '__main__':
  52. unittest.main()