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_face_detection.py 2.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import os.path as osp
  3. import unittest
  4. import cv2
  5. import numpy as np
  6. from modelscope.msdatasets import MsDataset
  7. from modelscope.outputs import OutputKeys
  8. from modelscope.pipelines import pipeline
  9. from modelscope.utils.constant import Tasks
  10. from modelscope.utils.cv.image_utils import draw_face_detection_result
  11. from modelscope.utils.test_utils import test_level
  12. class FaceDetectionTest(unittest.TestCase):
  13. def setUp(self) -> None:
  14. self.model_id = 'damo/cv_resnet_facedetection_scrfd10gkps'
  15. def show_result(self, img_path, detection_result):
  16. img = draw_face_detection_result(img_path, detection_result)
  17. cv2.imwrite('result.png', img)
  18. print(f'output written to {osp.abspath("result.png")}')
  19. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  20. def test_run_with_dataset(self):
  21. input_location = ['data/test/images/face_detection.png']
  22. dataset = MsDataset.load(input_location, target='image')
  23. face_detection = pipeline(Tasks.face_detection, model=self.model_id)
  24. # note that for dataset output, the inference-output is a Generator that can be iterated.
  25. result = face_detection(dataset)
  26. result = next(result)
  27. self.show_result(input_location[0], result)
  28. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  29. def test_run_modelhub(self):
  30. face_detection = pipeline(Tasks.face_detection, model=self.model_id)
  31. img_path = 'data/test/images/face_detection.png'
  32. result = face_detection(img_path)
  33. self.show_result(img_path, result)
  34. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  35. def test_run_modelhub_default_model(self):
  36. face_detection = pipeline(Tasks.face_detection)
  37. img_path = 'data/test/images/face_detection.png'
  38. result = face_detection(img_path)
  39. self.show_result(img_path, result)
  40. if __name__ == '__main__':
  41. unittest.main()