1 #include <iostream>
2 #include "alibabacloud/core/AlibabaCloud.h"
3 #include "alibabacloud/facebody/FacebodyClient.h"
4 #include "alibabacloud/core/CommonClient.h"
5 #include "model/common.h"
6
7 extern "C" {
8 using namespace std;
9 using namespace AlibabaCloud;
10 using namespace AlibabaCloud::Facebody;
11
compareFace(char * urlA,char * urlB,AIModelCBFunc cb)12 int compareFace(char *urlA, char *urlB, AIModelCBFunc cb)
13 {
14 InitializeSdk();
15 string key = getAccessKey();
16 string secret = getAccessSecret();
17 ClientConfiguration configuration;
18 configuration.setRegionId(CLOUD_AI_REGION_ID);
19 configuration.setEndpoint(CLOUD_AI_FACEBODY_ENDPOINT);
20 FacebodyClient client(key, secret, configuration);
21 Model::CompareFaceRequest request;
22 string tmpImageURLA, tmpImageURLB;
23 FacebodyResultStruct result;
24 float confidence;
25 int ret = 0;
26
27 tmpImageURLA = urlA;
28 tmpImageURLB = urlB;
29
30 request.setScheme("http");
31 request.setMethod(HttpRequest::Method::Post);
32 request.setImageURLA(tmpImageURLA);
33 request.setImageURLB(tmpImageURLB);
34
35 auto outcome = client.compareFace(request);
36 cout << endl << "facebody describeInstances returned:" << endl;
37 cout << "error code: " << outcome.error().errorCode() << endl;
38 cout << "requestId: " << outcome.result().requestId() << endl << endl;
39
40 cout << "confidence:" << outcome.result().getData().confidence << endl;
41 cout << "x:" << outcome.result().getData().rectAList[0] << endl;
42 cout << "y:" << outcome.result().getData().rectAList[1] << endl;
43 cout << "w:" << outcome.result().getData().rectAList[2] << endl;
44 cout << "h" << outcome.result().getData().rectAList[3] << endl;
45
46 if (cb) {
47 result.face.confidence = outcome.result().getData().confidence;
48 result.face.location.x = outcome.result().getData().rectAList[0];
49 result.face.location.y = outcome.result().getData().rectAList[1];
50 result.face.location.w = outcome.result().getData().rectAList[2];
51 result.face.location.h = outcome.result().getData().rectAList[3];
52 ret = cb((void *)&result);
53 }
54 ShutdownSdk();
55 cout << "facebody comparing done" << endl << endl;
56 return ret;
57 }
58
recognizeExpression(char * url,AIModelCBFunc cb)59 int recognizeExpression(char *url, AIModelCBFunc cb)
60 {
61 InitializeSdk();
62 string key = getAccessKey();
63 string secret = getAccessSecret();
64 ClientConfiguration configuration;
65 configuration.setRegionId(CLOUD_AI_REGION_ID);
66 configuration.setEndpoint(CLOUD_AI_FACEBODY_ENDPOINT);
67 FacebodyClient client(key, secret, configuration);
68 Model::RecognizeExpressionRequest request;
69 string tmpImageURL;
70 string expression;
71 float faceProbability;
72 FacebodyResultStruct result;
73 int ret = 0;
74
75 tmpImageURL = url;
76 request.setScheme("http");
77 request.setMethod(HttpRequest::Method::Post);
78 request.setImageURL(tmpImageURL);
79 auto outcome = client.recognizeExpression(request);
80 cout << endl << "facebody describeInstances returned:" << endl;
81 cout << "error code: " << outcome.error().errorCode() << endl;
82 cout << "requestId: " << outcome.result().requestId() << endl << endl;
83
84 cout << "expression:" << outcome.result().getData().elements[0].expression << endl;
85 cout << "face probablility:" << outcome.result().getData().elements[0].faceProbability << endl;
86 cout << "x:" << outcome.result().getData().elements[0].faceRectangle.left << endl;
87 cout << "y:" << outcome.result().getData().elements[0].faceRectangle.top << endl;
88 cout << "w:" << outcome.result().getData().elements[0].faceRectangle.width << endl;
89 cout << "h:" << outcome.result().getData().elements[0].faceRectangle.height << endl;
90
91 if (cb) {
92 expression = outcome.result().getData().elements[0].expression;
93 if (!expression.empty()) {
94 result.expression.expression = (char *)expression.c_str();
95 cout << "expression:" << outcome.result().getData().elements[0].expression << endl;
96 result.expression.probability = outcome.result().getData().elements[0].faceProbability;
97 result.expression.location.x = 20; // outcome.result().getData().elements[0].faceRectangle.left;
98 result.expression.location.y = 20; // outcome.result().getData().elements[0].faceRectangle.top;
99 result.expression.location.w = outcome.result().getData().elements[0].faceRectangle.width;
100 result.expression.location.h = outcome.result().getData().elements[0].faceRectangle.height;
101 ret = cb((void *)&result);
102 }
103 }
104 ShutdownSdk();
105 return ret;
106 }
107
generateHumanAnimeStyle(char * url,AIModelCBFunc cb)108 int generateHumanAnimeStyle(char *url, AIModelCBFunc cb)
109 {
110 InitializeSdk();
111 string key = getAccessKey();
112 string secret = getAccessSecret();
113 ClientConfiguration configuration;
114 configuration.setRegionId(CLOUD_AI_REGION_ID);
115 configuration.setEndpoint(CLOUD_AI_FACEBODY_ENDPOINT);
116 FacebodyClient client(key, secret, configuration);
117 Model::GenerateHumanAnimeStyleRequest request;
118 string tmpImageURL;
119 FacebodyResultStruct result;
120 string outImageUrl;
121 int len;
122 int ret = 0;
123
124 tmpImageURL = url;
125 request.setScheme("http");
126 request.setMethod(HttpRequest::Method::Post);
127 request.setImageURL(tmpImageURL);
128
129 auto outcome = client.generateHumanAnimeStyle(request);
130 cout << endl << "facebody describeInstances returned:" << endl;
131 cout << "error code: " << outcome.error().errorCode() << endl;
132 cout << "requestId: " << outcome.result().requestId() << endl << endl;
133 cout << "image url:" << outcome.result().getData().imageURL << endl;
134
135 outImageUrl = outcome.result().getData().imageURL;
136
137 if (outImageUrl.size() > 0) {
138 result.anime.url = (char *)outImageUrl.c_str();
139 result.anime.imageLen = getResponseBodyByUrl(result.anime.url, &result.anime.image);
140 }
141 if (result.anime.image && cb) {
142 ret = cb((void *)&result);
143 }
144 ShutdownSdk();
145 return ret;
146 }
147
148 }
149