def test_detect_by_area_one_face(self): image = VLImage.load(filename=ONE_FACE) area = Rect(0, 0, 100, 100) detection = TestDetector.detector.detectOne(image=image, detectArea=area) assert detection is None area = Rect(100, 100, image.rect.width - 100, image.rect.height - 100) detection = TestDetector.detector.detectOne(image=image, detectArea=area) isinstance(detection, FaceDetection)
def test_detect_by_area_and_not(self): image = VLImage.load(filename=ONE_FACE) area1 = Rect(0, 0, 100, 100) area2 = Rect(100, 100, image.rect.width - 100, image.rect.height - 100) detections = TestDetector.detector.detect( images=[ImageForDetection(image, area1), ImageForDetection(image, area2), image] ) assert 3 == len(detections) assert 0 == len(detections[0]) assert 1 == len(detections[1]) assert 1 == len(detections[1])
def test_redetect_by_area_without_face(self): """ Test re-detection by area without face """ for detector in self.detectors: with self.subTest(detectorType=detector.detectorType): redetectOne = detector.redetectOne(image=VLIMAGE_ONE_FACE, bBox=Rect(0, 0, 100, 100)) redetect = detector.redetect( images=[ImageForRedetection(image=VLIMAGE_ONE_FACE, bBoxes=[Rect(0, 0, 100, 100)])] )[0][0] assert redetectOne is None, "excepted None but found {}".format(redetectOne) assert redetect is None, "excepted None but found {}".format(redetectOne)
def test_redetect_by_area_without_human(self): """ Test re-detection by area without human """ redetectOne = self.detector.redetectOne(image=VLIMAGE_ONE_FACE, bBox=Rect(0, 0, 100, 100)) redetect = self.detector.redetect(images=[ ImageForRedetection(image=VLIMAGE_ONE_FACE, bBoxes=[Rect(0, 0, 100, 100)]) ])[0][0] assert redetectOne is None, "excepted None but found {}".format( redetectOne) assert redetect is None, "excepted None but found {}".format( redetectOne)
def detectFaces(): """ Redect faces on images. """ faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V1) imageWithOneFace = VLImage.load(filename=EXAMPLE_O) pprint.pprint( detector.detectOne(imageWithOneFace, detect5Landmarks=False, detect68Landmarks=False).asDict()) detection = detector.detectOne(imageWithOneFace, detect5Landmarks=False, detect68Landmarks=False) pprint.pprint( detector.redetectOne(image=imageWithOneFace, detection=detection)) pprint.pprint( detector.redetectOne(image=imageWithOneFace, bBox=detection.boundingBox.rect)) imageWithSeveralFaces = VLImage.load(filename=EXAMPLE_SEVERAL_FACES) severalFaces = detector.detect([imageWithSeveralFaces], detect5Landmarks=False, detect68Landmarks=False) pprint.pprint( detector.redetect(images=[ ImageForRedetection( imageWithSeveralFaces, [face.boundingBox.rect for face in severalFaces[0]]), ImageForRedetection(imageWithOneFace, [detection.boundingBox.rect]), ImageForRedetection(imageWithOneFace, [Rect(0, 0, 1, 1)]) ]))
def detectHumanBody(): """ Detect one human body on an image. """ faceEngine = VLFaceEngine() detector = faceEngine.createHumanDetector() imageWithOneHuman = VLImage.load(filename=EXAMPLE_O) pprint.pprint( detector.detectOne(imageWithOneHuman, detectLandmarks=False).asDict()) imageWithSeveralHumans = VLImage.load(filename=EXAMPLE_SEVERAL_FACES) pprint.pprint( detector.detectOne(imageWithSeveralHumans, detectLandmarks=False).asDict()) severalHumans = detector.detect([imageWithSeveralHumans], detectLandmarks=True) pprint.pprint([human.asDict() for human in severalHumans[0]]) imageWithoutHuman = VLImage.load(filename=EXAMPLE_WITHOUT_FACES) pprint.pprint( detector.detectOne(imageWithoutHuman, detectLandmarks=False) is None) severalHumans = detector.detect( [ImageForDetection(imageWithSeveralHumans, Rect(1, 1, 300.0, 300.0))]) pprint.pprint(severalHumans)
def test_detect_one_invalid_rectangle(self): """ Test detection of one human with an invalid rect """ with pytest.raises(LunaSDKException) as exceptionInfo: self.detector.detectOne(image=VLIMAGE_ONE_FACE, detectArea=Rect()) self.assertLunaVlError(exceptionInfo, LunaVLError.InvalidRect)
def test_batch_redetect_invalid_rectangle(self): """ Test batch re-detection with an invalid rect """ for detector in self.detectors: with self.subTest(detectorType=detector.detectorType): with pytest.raises(LunaSDKException) as exceptionInfo: detector.redetect(images=[ ImageForRedetection(image=VLIMAGE_ONE_FACE, bBoxes=[INVALID_RECT]), ImageForRedetection(image=VLIMAGE_ONE_FACE, bBoxes=[Rect(0, 0, 100, 100)]), ]) self.assertLunaVlError( exceptionInfo, LunaVLError.BatchedInternalError.format( "Failed validation.")) assert len(exceptionInfo.value.context ) == 2, "Expect two error in exception context" self.assertReceivedAndRawExpectedErrors( exceptionInfo.value.context[0], LunaVLError.InvalidRect.format("Invalid rectangle")) self.assertReceivedAndRawExpectedErrors( exceptionInfo.value.context[1], LunaVLError.Ok.format("Ok"))
def detectFaces(): """ Detect one face on an image. """ faceEngine = VLFaceEngine() detector = faceEngine.createFaceDetector(DetectorType.FACE_DET_V1) imageWithOneFace = VLImage.load(filename=EXAMPLE_O) pprint.pprint( detector.detectOne(imageWithOneFace, detect5Landmarks=False, detect68Landmarks=False).asDict()) imageWithSeveralFaces = VLImage.load(filename=EXAMPLE_SEVERAL_FACES) pprint.pprint( detector.detectOne(imageWithSeveralFaces, detect5Landmarks=False, detect68Landmarks=False).asDict()) severalFaces = detector.detect([imageWithSeveralFaces], detect5Landmarks=False, detect68Landmarks=False) pprint.pprint([face.asDict() for face in severalFaces[0]]) imageWithoutFace = VLImage.load(filename=EXAMPLE_WITHOUT_FACES) pprint.pprint( detector.detectOne(imageWithoutFace, detect5Landmarks=False, detect68Landmarks=False) is None) severalFaces = detector.detect( [ImageForDetection(imageWithSeveralFaces, Rect(1, 1, 300.0, 300.0))], detect5Landmarks=False, detect68Landmarks=False) pprint.pprint(severalFaces)
def detectHumans(): """ Redetect human body on images. """ faceEngine = VLFaceEngine() detector = faceEngine.createHumanDetector() imageWithOneHuman = VLImage.load(filename=EXAMPLE_O) detection = detector.detectOne(imageWithOneHuman, detectLandmarks=False) pprint.pprint(detector.redetectOne(image=imageWithOneHuman, bBox=detection)) pprint.pprint( detector.redetectOne(image=imageWithOneHuman, bBox=detection.boundingBox.rect)) imageWithSeveralHumans = VLImage.load(filename=EXAMPLE_SEVERAL_FACES) severalHumans = detector.detect([imageWithSeveralHumans], detectLandmarks=False) pprint.pprint( detector.redetect(images=[ ImageForRedetection( imageWithSeveralHumans, [human.boundingBox.rect for human in severalHumans[0]]), ImageForRedetection(imageWithOneHuman, [detection.boundingBox.rect]), ImageForRedetection(imageWithOneHuman, [Rect(0, 0, 1, 1)]), ]))
def test_load_image_from_file(self): """ Test load image from file """ imageWithOneFace = VLImage.load(filename=ONE_FACE) assert imageWithOneFace.isValid() assert imageWithOneFace.rect == Rect(0, 0, 912, 1080) assert imageWithOneFace.filename == "one_face.jpg"
def test_load_image_from_url(self): """ Test load image from url """ url = "https://st.kp.yandex.net/im/kadr/3/1/4/kinopoisk.ru-Keira-Knightley-3142930.jpg" imageWithOneFace = VLImage.load(url=url) assert imageWithOneFace.isValid() assert imageWithOneFace.rect == Rect(0, 0, 1000, 1288) assert imageWithOneFace.filename == url
def test_detect_one_invalid_rectangle(self): """ Test detection of one face with an invalid rect """ for detector in self.detectors: with self.subTest(detectorType=detector.detectorType): with pytest.raises(LunaSDKException) as exceptionInfo: detector.detectOne(image=VLIMAGE_ONE_FACE, detectArea=Rect()) self.assertLunaVlError(exceptionInfo, LunaVLError.InvalidRect)
def test_batch_detect_invalid_rectangle(self): """ Test batch face detection with an invalid rect """ for detector in self.detectors: with self.subTest(detectorType=detector.detectorType): with pytest.raises(LunaSDKException) as exceptionInfo: detector.detect(images=[ ImageForDetection(image=VLIMAGE_ONE_FACE, detectArea=Rect()) ]) self.assertLunaVlError(exceptionInfo, LunaVLError.InvalidRect)
def test_redetect_invalid_rectangle(self): """ Test batch re-detection with an invalid rect """ with pytest.raises(LunaSDKException) as exceptionInfo: self.detector.redetect(images=[ ImageForRedetection(image=VLIMAGE_ONE_FACE, bBoxes=[INVALID_RECT]), ImageForRedetection(image=VLIMAGE_ONE_FACE, bBoxes=[Rect(0, 0, 100, 100)]), ]) self.assertLunaVlError(exceptionInfo, LunaVLError.InvalidRect)
def test_batch_detect_invalid_rectangle(self): """ Test batch human detection with an invalid rect """ with pytest.raises(LunaSDKException) as exceptionInfo: self.detector.detect(images=[ ImageForDetection(image=VLIMAGE_ONE_FACE, detectArea=Rect()) ]) self.assertLunaVlError(exceptionInfo, LunaVLError.BatchedInternalError) assert len(exceptionInfo.value.context ) == 1, "Expect one error in exception context" self.assertReceivedAndRawExpectedErrors(exceptionInfo.value.context[0], LunaVLError.InvalidRect)
def checkRectAttr(defaultRect: Rect): """ Validate attributes of Rect Args: defaultRect: rect object """ for rectType in ("coreRectI", "coreRectF"): assert all( isinstance( getattr(defaultRect.__getattribute__(rectType), f"{coordinate}"), float if rectType == "coreRectF" else int, ) for coordinate in ("x", "y", "height", "width") )
def test_redetect_invalid_rectangle(self): """ Test batch re-detection with an invalid rect """ with pytest.raises(LunaSDKException) as exceptionInfo: self.detector.redetect(images=[ ImageForRedetection(image=VLIMAGE_ONE_FACE, bBoxes=[INVALID_RECT]), ImageForRedetection(image=VLIMAGE_ONE_FACE, bBoxes=[Rect(0, 0, 100, 100)]), ]) self.assertLunaVlError(exceptionInfo, LunaVLError.BatchedInternalError) assert len(exceptionInfo.value.context ) == 2, "Expect two errors in exception context" self.assertReceivedAndRawExpectedErrors(exceptionInfo.value.context[0], LunaVLError.InvalidRect) self.assertReceivedAndRawExpectedErrors(exceptionInfo.value.context[1], LunaVLError.Ok)
def test_image_initialize(self): path = Path(ONE_FACE) with path.open("rb") as file: binaryBody = file.read() img = Image.open(ONE_FACE) npBody = np.asarray(img) coreBody = fe.Image() coreBody.load(ONE_FACE) InitCase = namedtuple("InitCase", ("initType", "body")) cases = (InitCase("bytes", binaryBody), InitCase("numpy array", npBody), InitCase("core", coreBody)) for case in cases: with self.subTest(initType=case.initType): image = VLImage(body=case.body, filename=case.initType) assert case.initType == image.filename assert image.isValid() assert image.rect == Rect(0, 0, 912, 1080)
def test_batch_detect_invalid_rectangle(self): """ Test batch face detection with an invalid rect """ for detector in self.detectors: with self.subTest(detectorType=detector.detectorType): with pytest.raises(LunaSDKException) as exceptionInfo: detector.detect(images=[ ImageForDetection(image=VLIMAGE_ONE_FACE, detectArea=Rect()) ]) self.assertLunaVlError( exceptionInfo, LunaVLError.BatchedInternalError.format( "Failed validation.")) assert len(exceptionInfo.value.context ) == 1, "Expect one error in exception context" self.assertReceivedAndRawExpectedErrors( exceptionInfo.value.context[0], LunaVLError.InvalidRect.format("Invalid rectangle"))
def test_image_initialize(self): """ Test create VLImage with image body """ binaryBody = Path(ONE_FACE).read_bytes() bytearrayBody = bytearray(binaryBody) imageWithOneFace = Image.open(ONE_FACE) coreBody = fe.Image() coreBody.load(ONE_FACE) InitCase = namedtuple("InitCase", ("initType", "body")) cases = ( InitCase("bytes", binaryBody), InitCase("byte array", bytearrayBody), InitCase("core", coreBody), InitCase("pillow img", imageWithOneFace), ) for case in cases: with self.subTest(initType=case.initType): imageVl = VLImage(body=case.body, filename=case.initType) assert imageVl.isValid() assert case.initType == imageVl.filename assert imageVl.rect == Rect(0, 0, 912, 1080)
def test_detect_by_bad_area(self): image = VLImage.load(filename=ONE_FACE) area = Rect(100, 100, image.rect.width, image.rect.height) detections = TestDetector.detector.detectOne(image=image, detectArea=area)
from lunavl.sdk.detectors.base import BaseDetection from lunavl.sdk.detectors.facedetector import FaceDetection, FaceDetector, Landmarks5, Landmarks68 from lunavl.sdk.detectors.humandetector import HumanDetection, HumanDetector, Landmarks17 from lunavl.sdk.faceengine.engine import DetectorType from lunavl.sdk.image_utils.geometry import Point from lunavl.sdk.image_utils.geometry import Rect from lunavl.sdk.image_utils.image import VLImage from tests.base import BaseTestClass from tests.resources import ONE_FACE, SEVERAL_FACES, SMALL_IMAGE, BAD_IMAGE, LARGE_IMAGE VLIMAGE_SMALL = VLImage.load(filename=SMALL_IMAGE) VLIMAGE_ONE_FACE = VLImage.load(filename=ONE_FACE) VLIMAGE_BAD_IMAGE = VLImage.load(filename=BAD_IMAGE) VLIMAGE_SEVERAL_FACE = VLImage.load(filename=SEVERAL_FACES) VLIMAGE_LARGE_IMAGE = VLImage.load(filename=LARGE_IMAGE) GOOD_AREA = Rect(100, 100, VLIMAGE_ONE_FACE.rect.width - 100, VLIMAGE_ONE_FACE.rect.height - 100) OUTSIDE_AREA = Rect(100, 100, VLIMAGE_ONE_FACE.rect.width, VLIMAGE_ONE_FACE.rect.height) SMALL_AREA = Rect(50, 50, 52, 52) LARGE_AREA = Rect(1, 1, VLIMAGE_LARGE_IMAGE.rect.width - 1, VLIMAGE_LARGE_IMAGE.rect.height - 1) AREA_LARGER_IMAGE = Rect(100, 100, VLIMAGE_ONE_FACE.rect.width + 100, VLIMAGE_ONE_FACE.rect.height + 100) AREA_OUTSIDE_IMAGE = Rect( VLIMAGE_ONE_FACE.rect.width, VLIMAGE_ONE_FACE.rect.height, VLIMAGE_ONE_FACE.rect.width + 100, VLIMAGE_ONE_FACE.rect.height + 100, ) AREA_WITHOUT_FACE = Rect(50, 50, 100, 100) INVALID_RECT = Rect(0, 0, 0, 0)
def test_image_load_from_url(self): url = "https://cdn1.savepice.ru/uploads/2019/4/15/194734af15c4fcd06dec6db86bbeb7cd-full.jpg" image = VLImage.load(url=url) assert image.isValid() assert image.rect == Rect(0, 0, 497, 640) assert image.source == url
def test_image_load_from_file(self): image = VLImage.load(filename=ONE_FACE) assert image.rect == Rect(0, 0, 912, 1080) assert image.isValid() assert image.source == "one_face.jpg"
from lunavl.sdk.base import BoundingBox, LandmarkWithScore from lunavl.sdk.detectors.base import BaseDetection from lunavl.sdk.detectors.facedetector import FaceDetection, FaceDetector, Landmarks5, Landmarks68 from lunavl.sdk.detectors.humandetector import HumanDetection, HumanDetector, Landmarks17 from lunavl.sdk.faceengine.engine import DetectorType from lunavl.sdk.image_utils.geometry import Point from lunavl.sdk.image_utils.geometry import Rect from lunavl.sdk.image_utils.image import VLImage from tests.base import BaseTestClass from tests.resources import ONE_FACE, SEVERAL_FACES, SMALL_IMAGE SINGLE_CHANNEL_IMAGE = Image.open(ONE_FACE).convert("L") VLIMAGE_SMALL = VLImage.load(filename=SMALL_IMAGE) VLIMAGE_ONE_FACE = VLImage.load(filename=ONE_FACE) VLIMAGE_SEVERAL_FACE = VLImage.load(filename=SEVERAL_FACES) GOOD_AREA = Rect(100, 100, VLIMAGE_ONE_FACE.rect.width - 100, VLIMAGE_ONE_FACE.rect.height - 100) OUTSIDE_AREA = Rect(100, 100, VLIMAGE_ONE_FACE.rect.width, VLIMAGE_ONE_FACE.rect.height) AREA_WITHOUT_FACE = Rect(50, 50, 100, 100) INVALID_RECT = Rect(0, 0, 0, 0) ERROR_CORE_RECT = Rect(0.1, 0.1, 0.1, 0.1) # anything out of range (0.1, 1) class BaseDetectorTestClass(BaseTestClass): """ Base class for detectors tests """ #: detection class detectionClass: Type[BaseDetection]