Last active
December 29, 2020 21:40
-
-
Save rktalusani/3b0bb3c61bc6d5b6020612f189e644fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. sendScreenShot (front-end js) method uses subsciber.getImgData() method to capture screenshot of the subscriber (remote participant) and post it to the server. | |
2. On the server we use detectFace() method to detect the facial features in this image and we get a identifier for the detected face (id1) | |
3. we use detectFace() again with the image we want to compare with and we get identifier for the detected face (id2) | |
4. we use verifyFace() method and pass id1 and id2 as the inputs. Microsoft face API compares these two faces and provides a result that includes match/mismatch as well as a score. | |
Reference - | |
1. getImgData() - https://tokbox.com/developer/sdks/js/reference/Subscriber.html#getImgData | |
2. Microsoft Face API - https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236 | |
function sendScreenShot() { | |
var imgdata = undefined; | |
if (subscriber) { | |
imgdata = subscriber.getImgData(); | |
} | |
if (imgdata != undefined) { | |
try { | |
var blob = this.b64toBlob(imgdata, "image/png"); | |
let formData = new FormData(); | |
formData.append('customer', blob); | |
let res = await $HTTPDEMO.post('/faceIDDemo.php', | |
formData, { | |
headers: { | |
'Content-Type': 'multipart/form-data' | |
} | |
} | |
); | |
console.log(res.data); | |
if (res.data.status != "success") { | |
alert("Error uploading the file"); | |
} else { | |
} | |
} catch (error) { | |
alert("error posting screenshot"); | |
console.log(error); | |
} | |
} | |
} | |
function b64toBlob(b64Data, contentType, sliceSize) { | |
contentType = contentType || ''; | |
sliceSize = sliceSize || 512; | |
var byteCharacters = atob(b64Data); | |
var byteArrays = []; | |
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) { | |
var slice = byteCharacters.slice(offset, offset + sliceSize); | |
var byteNumbers = new Array(slice.length); | |
for (var i = 0; i < slice.length; i++) { | |
byteNumbers[i] = slice.charCodeAt(i); | |
} | |
var byteArray = new Uint8Array(byteNumbers); | |
byteArrays.push(byteArray); | |
} | |
var blob = new Blob(byteArrays, { | |
type: contentType | |
}); | |
return blob; | |
} | |
$faceid_endpoint = "https://southeastasia.api.cognitive.microsoft.com"; | |
$faceid_key = "your-key"; | |
function detectFace($img){ | |
global $faceid_endpoint, $data_dir_url,$faceid_key; | |
$client = new GuzzleHttp\Client([ | |
'base_uri' => $faceid_endpoint | |
]); | |
$resp = $client->request('POST', 'face/v1.0/detect?recognitionModel=recognition_02&detectionModel=detection_02', [ | |
'headers' => [ | |
'Content-Type' => 'application/json', | |
'Ocp-Apim-Subscription-Key' => $faceid_key | |
], | |
'json' => ['url'=> $data_dir_url.$img] | |
]); | |
$json = json_decode($resp->getBody(),true); | |
//echo $resp->getBody(); | |
return $json[0]; | |
} | |
function verifyFace($id1,$id2){ | |
global $faceid_endpoint, $data_dir_url,$faceid_key; | |
$client = new GuzzleHttp\Client([ | |
'base_uri' => $faceid_endpoint | |
]); | |
$resp = $client->request('POST', 'face/v1.0/verify', [ | |
'headers' => [ | |
'Content-Type' => 'application/json', | |
'Ocp-Apim-Subscription-Key' => $faceid_key | |
], | |
'json' => [ | |
'faceid1'=>$id1, | |
'faceid2'=>$id2 | |
] | |
]); | |
return $resp->getBody(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment