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

KYCDeepFace Integration Plugin #30

Draft
wants to merge 1 commit into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
"@tradle/schema-graphql": "github:tradle/schema-graphql",
"@tradle/schema-joi": "github:tradle/schema-joi",
"@tradle/test-helpers": "github:tradle/test-helpers",
"@tradle/urlsafe-base64": "^1.0.0",
"@tradle/validate-resource": "^4.3.5",
"@tradle/web3-provider-engine": "^14.0.6",
"JSONStream": "^1.3.5",
Expand Down
5 changes: 5 additions & 0 deletions serverless-uncompiled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,11 @@ functions:
# method: put
# cors: ${{self:custom.cors}}

kycdeepface:
image: public.ecr.aws/h7s8u8m7/kycdeepface:latest
timeout: 899
memorySize: 1024

# 1. generates temporary credentials (STS) for new connections,
# and assumes IotClientRole on them
# 2. creates an unauthenticated session,
Expand Down
1 change: 1 addition & 0 deletions src/in-house-bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ export const loadComponentsAndPlugins = ({
'leasingQuotes',
'giinCheck',
'vatCheck',
'kycdeepface-checks',
// 'invoicing'
].forEach((name) => attachPlugin({ name }))
;[
Expand Down
122 changes: 122 additions & 0 deletions src/in-house-bot/kycdeepface/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { safe as b64 } from '@tradle/urlsafe-base64'
import fs from 'fs'
import { resolve } from 'path'
import debug from 'debug'

const { readFile } = fs.promises
const log = debug('tradle:kycdeepface:index')

export type Point = [
number,
number
]

export interface Embedding {
bounds: {
topLeft: Point,
bottomRight: Point
}
landmarks: {
outline: Point[]
left_brows: Point[]
right_brows: Point[]
nose_back: Point[]
nostrils: Point[]
left_eye: Point[]
right_eye: Point[]
mouth: Point[]
},
angles: {
pitch: number
yaw: number
roll: number
}
embedding: string
}

export interface Embeddings {
faces: Embedding[]
timings: { [type: string]: number }
}

export interface Match {
similarity: number
timings: { [type: string]: number }
}

export interface InputBytes {
image_bytes: Buffer
}

export interface InputFile {
image_file: string
}

export interface InputURL {
image_url: string
}

export interface InputS3 {
image_s3: {
bucket: string
key: string
version?: string
}
}

export interface InputBase64 {
image_urlsafe_b64: string
}

export type Input = InputS3 | InputURL | InputBytes | InputFile | InputBase64

export function isInputFile (input: Input): input is InputFile {
return 'image_file' in input
}

export function isInputBytes (input: Input): input is InputBytes {
return 'image_bytes' in input
}

async function normalizeInput (input: Input): Promise<InputS3 | InputURL | InputBase64> {
if (isInputFile(input)) {
const pth = resolve(input.image_file)
try {
return normalizeInput({
image_bytes: await readFile(pth)
})
} catch (err) {
throw Object.assign(new Error(`Error while loading file ${pth}: ${err.message}`), err)
}
}
if (isInputBytes(input)) {
return {
image_urlsafe_b64: b64.encode(input.image_bytes)
}
}
return input
}

export interface Exec {
description: string,
run: (input: any) => Promise<any>
}

async function exec<T>(name: string, execFn: Exec, input: any): Promise<T> {
log(name, execFn.description, input)
input = { [name]: input }
try {
return await execFn.run(input)
} catch (err) {
log(`${name}:retry`, err)
return await execFn.run(input)
}
}

export async function face_embeddings (execFn: Exec, input: Input): Promise<Embeddings> {
return await exec<Embeddings>('face_embeddings', execFn, await normalizeInput(input))
}

export async function face_match (execFn: Exec, embedding_a: string, embedding_b: string): Promise<Match> {
return await exec<Match>('face_match', execFn, { embedding_a, embedding_b })
}