Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]feat: identity/face-detection #89

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions capabilities/crypto/exchange-rate/maps/binance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe(`crypto/exchange-rate/binance}`, () => {
let superface: SuperfaceTest;

beforeEach(() => {
superface = new SuperfaceTest();
superface = new SuperfaceTest();
});

describe('GetExchangeRate', () => {
Expand All @@ -18,10 +18,12 @@ describe(`crypto/exchange-rate/binance}`, () => {
input: {
from: 'BTC',
to: 'USDT',
}
},
})
).resolves
.toHaveProperty('value.rate', expect.stringMatching(/^\d{1,}\.?\d{0,}$/));
).resolves.toHaveProperty(
'value.rate',
expect.stringMatching(/^\d{1,}\.?\d{0,}$/)
);
});

// todo currently fails
Expand Down Expand Up @@ -58,4 +60,4 @@ describe(`crypto/exchange-rate/binance}`, () => {
// .toMatchSnapshot();
// });
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
profile = "identity/[email protected]"
provider = "azure-cognitive-services"

"""
FaceDetection map
"""
map FaceDetection {
imageUrl = input.imageUrl

http POST "/face/v1.0/detect" {
security "azure-subscription-key"

request {
query {
'detectionModel' = "detection_01"
'returnFaceLandmarks' = true
'returnFaceAttributes'='emotion'
}
headers {
"Content-Type" = "application/json"
}
body {
url = imageUrl
}
}

response {
return map error if (statusCode !== 200) {
code = body.error.code
message = body.error.message
}

faces = body.map((r) => {
const resolveLikelihood = (value) => {
if(value >= 0 && value <= 0.2) {
return 'veryUnlikely'
}

if(value > 0.2 && value <= 0.4) {
return 'unlikely'
}

if(value > 0.4 && value <= 0.6) {
return 'possible'
}

if(value > 0.6 && value <= 0.8) {
return 'likely'
}

if(value > 0.8 && value <= 1) {
return 'verylikely'
}

return 'unknown'
}

return {
faceRectangle: {
topLeft: {
x: r.faceRectangle.left,
y: r.faceRectangle.top,
},
topRight: {
x: r.faceRectangle.left + r.faceRectangle.width,
y: r.faceRectangle.top,
},
bottomLeft: {
x: r.faceRectangle.left,
y: r.faceRectangle.top + r.faceRectangle.height
},
bottomRight: {
x: r.faceRectangle.left + r.faceRectangle.width,
y: r.faceRectangle.top + r.faceRectangle.height
}
},
landmarks: [
{kind: 'leftPupil', x: r.faceLandmarks.pupilLeft.x, y: r.faceLandmarks.pupilLeft.y },
{kind: 'rightPupil', x: r.faceLandmarks.pupilRight.x, y: r.faceLandmarks.pupilRight.y },
{kind: 'noseTip', x: r.faceLandmarks.noseTip.x, y: r.faceLandmarks.noseTip.y },
{kind: 'mouthLeft', x: r.faceLandmarks.mouthLeft.x, y: r.faceLandmarks.mouthLeft.y },
{kind: 'mouthRight', x: r.faceLandmarks.mouthRight.x, y: r.faceLandmarks.mouthRight.y },
{kind: 'eyebrowLeftOuter', x: r.faceLandmarks.eyebrowLeftOuter.x, y: r.faceLandmarks.eyebrowLeftOuter.y },
{kind: 'eyebrowLeftInner', x: r.faceLandmarks.eyebrowLeftInner.x, y: r.faceLandmarks.eyebrowLeftInner.y },
{kind: 'eyeLeftOuter', x: r.faceLandmarks.eyeLeftOuter.x, y: r.faceLandmarks.eyeLeftOuter.y },
{kind: 'eyeLeftTop', x: r.faceLandmarks.eyeLeftTop.x, y: r.faceLandmarks.eyeLeftTop.y },
{kind: 'eyeLeftBottom', x: r.faceLandmarks.eyeLeftBottom.x, y: r.faceLandmarks.eyeLeftBottom.y },
{kind: 'eyeLeftInner', x: r.faceLandmarks.eyeLeftInner.x, y: r.faceLandmarks.eyeLeftInner.y },
{kind: 'eyebrowRightInner', x: r.faceLandmarks.eyebrowRightInner.x, y: r.faceLandmarks.eyebrowRightInner.y },
{kind: 'eyebrowRightOuter', x: r.faceLandmarks.eyebrowRightOuter.x, y: r.faceLandmarks.eyebrowRightOuter.y },
{kind: 'eyeRightInner', x: r.faceLandmarks.eyeRightInner.x, y: r.faceLandmarks.eyeRightInner.y },
{kind: 'eyeRightTop', x: r.faceLandmarks.eyeRightTop.x, y: r.faceLandmarks.eyeRightTop.y },
{kind: 'eyeRightBottom', x: r.faceLandmarks.eyeRightBottom.x, y: r.faceLandmarks.eyeRightBottom.y },
{kind: 'eyeRightOuter', x: r.faceLandmarks.eyeRightOuter.x, y: r.faceLandmarks.eyeRightOuter.y },
{kind: 'noseRootLeft', x: r.faceLandmarks.noseRootLeft.x, y: r.faceLandmarks.noseRootLeft.y },
{kind: 'noseRootRight', x: r.faceLandmarks.noseRootRight.x, y: r.faceLandmarks.noseRootRight.y },
],
emotions: {
happiness: resolveLikelihood(r.faceAttributes.emotion.happiness),
anger: resolveLikelihood(r.faceAttributes.emotion.anger),
sadness: resolveLikelihood(r.faceAttributes.emotion.sadness),
surprise: resolveLikelihood(r.faceAttributes.emotion.surprise)
}
}
})
return map result [{ faces: faces }]
}
}
}


114 changes: 114 additions & 0 deletions capabilities/identity/face-detection/maps/mock.suma
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
profile = "identity/[email protected]"
provider = "mock"

"""
FaceDetection map
"""
map FaceDetection {
map result [{
faces: [{
faceRectangle:{
topLeft: {
x: 1,
y: 1
},
topRight: {
x: 2,
y: 1
},
bottomLeft: {
x: 1,
y: 2
},
bottomRight: {
x: 2,
y: 2
}
},
landmarks: [
{
kind: `eyebrowLeftOuter`,
x: 1,
y: 2
},{
kind: `eyebrowLeftInner`,
x: 1,
y: 2
},{
kind: `eyebrowRightOuter`,
x: 1,
y: 2
},{
kind: `eyebrowRightInner`,
x: 1,
y: 2
},{
kind: `mouthLeft`,
x: 1,
y: 2
},{
kind: `mouthRight`,
x: 1,
y: 2
},{
kind: `noseRootLeft`,
x: 1,
y: 2
}, {
kind: `noseRootRight`,
x: 1,
y: 2
}, {
kind: `noseTip`,
x: 1,
y: 2
},{
kind: `leftPupil`,
x: 1,
y: 2
},{
kind: `eyeLeftTop`,
x: 1,
y: 2
},{
kind: `eyeLeftInner`,
x: 1,
y: 2
}, {
kind: `eyeLeftOuter`,
x: 1,
y: 2
}, {
kind: `eyeLeftBottom`,
x: 1,
y: 2
}, {
kind: `rightPupil`,
x: 1,
y: 2
}, {
kind: `eyeRightTop`,
x: 1,
y: 2
}, {
kind: `eyeRightInner`,
x: 1,
y: 2
},{
kind: `eyeRightOuter`,
x: 1,
y: 2
},{
kind: `eyeRightBottom`,
x: 1,
y: 2
}],
emotions: {
happiness: `possible`,
anger: `possible`,
sadness: `possible`,
surprise: `possible`,
}
}]
}]
}
46 changes: 46 additions & 0 deletions capabilities/identity/face-detection/maps/mock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { SuperfaceClient } from '../../../../superface/sdk';

describe('identity/face-detection/mock', () => {
it('should return mock data', async () => {
const client = new SuperfaceClient();
const profile = await client.getProfile('identity/face-detection');
const provider = await client.getProvider('mock');
const usecase = profile.useCases.FaceDetection;

expect(provider).not.toBeUndefined();
expect(usecase).not.toBeUndefined();

const result = await usecase.perform(
{
imageUrl: 'mock',
instance: 'mock',
},
{ provider: 'mock' }
);

result.unwrap();
expect(result.isOk()).toBeTruthy();

const faceAnnotations = result.unwrap();
expect(faceAnnotations[0]).toHaveProperty('faces');
expect(faceAnnotations[0].faces?.[0]).toHaveProperty('emotions');
expect(faceAnnotations[0].faces?.[0].emotions).toHaveProperty('sadness');
expect(faceAnnotations[0].faces?.[0].emotions).toHaveProperty('surprise');
expect(faceAnnotations[0].faces?.[0].emotions).toHaveProperty('happiness');
expect(faceAnnotations[0].faces?.[0].emotions).toHaveProperty('anger');
expect(faceAnnotations[0].faces?.[0]).toHaveProperty('landmarks');
expect(faceAnnotations[0].faces?.[0]).toHaveProperty('faceRectangle');
expect(faceAnnotations[0].faces?.[0].faceRectangle).toHaveProperty(
'topLeft'
);
expect(faceAnnotations[0].faces?.[0].faceRectangle).toHaveProperty(
'topRight'
);
expect(faceAnnotations[0].faces?.[0].faceRectangle).toHaveProperty(
'bottomLeft'
);
expect(faceAnnotations[0].faces?.[0].faceRectangle).toHaveProperty(
'bottomRight'
);
});
});
86 changes: 86 additions & 0 deletions capabilities/identity/face-detection/profile.supr
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
name = "identity/face-detection"
version = "1.0.0"

"""
FaceDetection usecase
"""
usecase FaceDetection {
input {
imageUrl! string!
//Azure specific instance - should be resolved as integration parameters
}

result [{
faces! [{
faceRectangle! faceRectangle!
landmarks! [landmark]!
emotions! emotions!
}]!
}]!

error {
message! string!
code! string!
}
}
model emotions {
happiness! likelihood!
anger! likelihood!
sadness! likelihood!
surprise! likelihood!
}

model faceRectangle {
topLeft! point!
topRight! point!
bottomLeft! point!
bottomRight! point!
}

model likelihood enum {
unknown
veryUnlikely
unlikely
possible
likely
veryLikely
}

model landmark {
kind! landmarkKind!
x! number!
y! number!
}

model landmarkKind enum {
//Left eye
leftPupil
eyeLeftOuter
eyeLeftTop
eyeLeftBottom
eyeLeftInner
//Right eye
rightPupil
eyeRightOuter
eyeRightTop
eyeRightBottom
eyeRightInner
//Left eyebrow
eyebrowLeftOuter
eyebrowLeftInner
//Right eyebrow
eyebrowRightInner
eyebrowRightOuter
//Nose
noseTip
noseRootLeft
noseRootRight
//Mouth
mouthLeft
mouthRight
}

model point {
x number
y number
}
Loading