C# 使用Angular 7的Azure人脸Api进行人脸识别
我需要用Angular和Azure face API开发一个人脸识别系统。但是,Azure Face API的文档是C#。有人能帮我把它改写成打字稿吗 是Azure face API中人脸识别的准则C# 使用Angular 7的Azure人脸Api进行人脸识别,c#,angular,typescript,azure,face-recognition,C#,Angular,Typescript,Azure,Face Recognition,我需要用Angular和Azure face API开发一个人脸识别系统。但是,Azure Face API的文档是C#。有人能帮我把它改写成打字稿吗 是Azure face API中人脸识别的准则 https://westus.api.cognitive.microsoft.com/face/v1.0/detect[?returnFaceId][&returnFaceLandmarks][&returnFaceAttributes] &subscription-key=
https://westus.api.cognitive.microsoft.com/face/v1.0/detect[?returnFaceId][&returnFaceLandmarks][&returnFaceAttributes]
&subscription-key=<Subscription key>
faceServiceClient = new FaceServiceClient("<Subscription Key>");
// Create an empty PersonGroup
string personGroupId = "myfriends";
await faceServiceClient.CreatePersonGroupAsync(personGroupId, "My Friends");
// Define Anna
CreatePersonResult friend1 = await faceServiceClient.CreatePersonAsync(
// Id of the PersonGroup that the person belonged to
personGroupId,
// Name of the person
"Anna"
);
// Directory contains image files of Anna
const string friend1ImageDir = @"D:\Pictures\MyFriends\Anna\";
foreach (string imagePath in Directory.GetFiles(friend1ImageDir, "*.jpg"))
{
using (Stream s = File.OpenRead(imagePath))
{
// Detect faces in the image and add to Anna
await faceServiceClient.AddPersonFaceAsync(
personGroupId, friend1.PersonId, s);
}
}
https://westus.api.cognitive.microsoft.com/face/v1.0/detect[?returnFaceId][&returnFaceLandmarks][&returnFaceAttributes]
&订阅密钥=
faceServiceClient=新的faceServiceClient(“”);
//创建一个空的PersonGroup
字符串personGroupId=“myfriends”;
等待faceServiceClient.CreatePersonGroupAsync(personGroupId,“我的朋友”);
//定义安娜
CreatePersonResult friend1=等待faceServiceClient.CreatePersonalSync(
//此人所属的个人组的Id
personGroupId,
//姓名
“安娜”
);
//目录包含安娜的图像文件
常量字符串friend1ImageDir=@“D:\Pictures\MyFriends\Anna\”;
foreach(Directory.GetFiles(friend1ImageDir,*.jpg)中的字符串imagePath)
{
使用(streams=File.OpenRead(imagePath))
{
//检测图像中的人脸并添加到
等待faceServiceClient.AddPersonFaceAsync(
personGroupId,friend1.PersonId,s);
}
}
以下是用于人脸识别的typescript中的示例代码
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from '../../environments/environment';
@Injectable()
export class FaceRecognitionService {
constructor(private httpClient: HttpClient) { }
scanImage(subscriptionKey: string, base64Image: string) {
const headers = this.getHeaders(subscriptionKey);
const params = this.getParams();
const blob = this.makeblob(base64Image);
return this.httpClient.post<FaceRecognitionResponse>(
environment.endpoint,
blob,
{
params,
headers
}
);
}
private makeblob(dataURL) {
const BASE64_MARKER = ';base64,';
const parts = dataURL.split(BASE64_MARKER);
const contentType = parts[0].split(':')[1];
const raw = window.atob(parts[1]);
const rawLength = raw.length;
const uInt8Array = new Uint8Array(rawLength);
for (let i = 0; i < rawLength; ++i) {
uInt8Array[i] = raw.charCodeAt(i);
}
return new Blob([uInt8Array], { type: contentType });
}
private getHeaders(subscriptionKey: string) {
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/octet-stream');
headers = headers.set('Ocp-Apim-Subscription-Key', subscriptionKey);
return headers;
}
private getParams() {
const httpParams = new HttpParams()
.set('returnFaceId', 'true')
.set('returnFaceLandmarks', 'false')
.set(
'returnFaceAttributes',
'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'
);
return httpParams;
}
}
从'@angular/common/http'导入{HttpClient,HttpHeaders,HttpParams};
从“@angular/core”导入{Injectable};
从“../../environments/environment”导入{environment};
@可注射()
导出类FaceRecognitionService{
构造函数(私有httpClient:httpClient){}
scanImage(subscriptionKey:string,Base64图像:string){
const headers=this.getHeaders(subscriptionKey);
const params=this.getParams();
const blob=this.makeblob(base64Image);
返回this.httpClient.post(
environment.endpoint,
水滴,
{
params,
标题
}
);
}
私有makeblob(dataURL){
常量BASE64_标记=';BASE64';
const parts=dataURL.split(BASE64_标记);
常量contentType=parts[0]。拆分(“:”)[1];
const raw=window.atob(第[1]部分);
const rawLength=原始长度;
const uInt8Array=新uInt8Array(rawLength);
for(设i=0;i
有关完整的代码参考,请参阅:
希望这有帮助。添加示例代码和代码回购路径,因为您是SO新成员,但请确保检查