2022-07-28 21:52:18 +08:00
|
|
|
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
|
|
|
import os.path as osp
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
import cv2
|
|
|
|
|
|
|
|
|
|
from modelscope.msdatasets import MsDataset
|
|
|
|
|
from modelscope.pipelines import pipeline
|
2022-08-06 12:22:17 +08:00
|
|
|
from modelscope.utils.constant import Tasks
|
2022-08-23 14:27:19 +08:00
|
|
|
from modelscope.utils.cv.image_utils import draw_face_detection_result
|
2022-07-28 21:52:18 +08:00
|
|
|
from modelscope.utils.test_utils import test_level
|
|
|
|
|
|
|
|
|
|
|
2023-05-22 10:53:18 +08:00
|
|
|
class FaceDetectionTest(unittest.TestCase):
|
2022-07-28 21:52:18 +08:00
|
|
|
|
|
|
|
|
def setUp(self) -> None:
|
2022-09-08 14:08:51 +08:00
|
|
|
self.task = Tasks.face_detection
|
2022-07-28 21:52:18 +08:00
|
|
|
self.model_id = 'damo/cv_resnet_facedetection_scrfd10gkps'
|
|
|
|
|
|
2022-08-23 14:27:19 +08:00
|
|
|
def show_result(self, img_path, detection_result):
|
|
|
|
|
img = draw_face_detection_result(img_path, detection_result)
|
2022-07-28 21:52:18 +08:00
|
|
|
cv2.imwrite('result.png', img)
|
2022-08-23 14:27:19 +08:00
|
|
|
print(f'output written to {osp.abspath("result.png")}')
|
2022-07-28 21:52:18 +08:00
|
|
|
|
|
|
|
|
@unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
|
|
|
|
|
def test_run_with_dataset(self):
|
2022-10-12 19:58:50 +08:00
|
|
|
input_location = ['data/test/images/face_detection2.jpeg']
|
2022-07-28 21:52:18 +08:00
|
|
|
|
|
|
|
|
dataset = MsDataset.load(input_location, target='image')
|
2022-10-25 09:28:01 +08:00
|
|
|
face_detection = pipeline(Tasks.face_detection, model=self.model_id)
|
2022-07-28 21:52:18 +08:00
|
|
|
# note that for dataset output, the inference-output is a Generator that can be iterated.
|
|
|
|
|
result = face_detection(dataset)
|
|
|
|
|
result = next(result)
|
2022-08-23 14:27:19 +08:00
|
|
|
self.show_result(input_location[0], result)
|
2022-07-28 21:52:18 +08:00
|
|
|
|
|
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
|
|
|
|
def test_run_modelhub(self):
|
2022-10-25 09:28:01 +08:00
|
|
|
face_detection = pipeline(Tasks.face_detection, model=self.model_id)
|
2022-10-12 19:58:50 +08:00
|
|
|
img_path = 'data/test/images/face_detection2.jpeg'
|
2022-07-28 21:52:18 +08:00
|
|
|
|
|
|
|
|
result = face_detection(img_path)
|
2022-08-23 14:27:19 +08:00
|
|
|
self.show_result(img_path, result)
|
2022-07-28 21:52:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|