-
Notifications
You must be signed in to change notification settings - Fork 0
/
FamilyFaceAPI.cs
120 lines (90 loc) · 3.56 KB
/
FamilyFaceAPI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace Family_Detection
{
class recognizeResponse
{
public bool success { get; set; }
public Face[] predictions { get; set; }
}
class registerResponse
{
public bool success { get; set; }
public string message { get; set; }
public string error { get; set; }
}
class deleteResponse
{
public bool success { get; set; }
}
class listResponse
{
public bool success { get; set; }
public string[] faces { get; set; }
}
class Face
{
public string userid { get; set; }
public float confidence { get; set; }
public int y_min { get; set; }
public int x_min { get; set; }
public int y_max { get; set; }
public int x_max { get; set; }
}
class FamilyFaceAPI
{
private HttpClient client;
private string server;
public FamilyFaceAPI(string server)
{
client = new HttpClient();
this.server = server;
}
public async Task<registerResponse> registerFace(string family, string name, string[] images_path)
{
var request = new MultipartFormDataContent();
for(int i = 0; i < images_path.Length; i++)
{
var image_data = File.OpenRead(images_path[i]);
request.Add(new StreamContent(image_data), "image"+(i+1), Path.GetFileName(images_path[i]));
}
request.Add(new StringContent(family+"_"+name), "userid");
var output = await client.PostAsync(server + "/v1/vision/face/register", request);
var jsonString = await output.Content.ReadAsStringAsync();
registerResponse response = JsonConvert.DeserializeObject<registerResponse>(jsonString);
return response;
}
public async Task<recognizeResponse> recognizeFace(string image_path)
{
var request = new MultipartFormDataContent();
var image_data = File.OpenRead(image_path);
request.Add(new StreamContent(image_data), "image", Path.GetFileName(image_path));
var output = await client.PostAsync(server + "/v1/vision/face/recognize", request);
var jsonString = await output.Content.ReadAsStringAsync();
recognizeResponse response = JsonConvert.DeserializeObject<recognizeResponse>(jsonString);
return response;
}
public listResponse listFaces()
{
var output = client.PostAsync(server + "/v1/vision/face/list", null).Result;
var jsonString = output.Content.ReadAsStringAsync().Result;
listResponse response = JsonConvert.DeserializeObject<listResponse>(jsonString);
return response;
}
public async Task<deleteResponse> deleteFace(string userid)
{
var request = new MultipartFormDataContent();
request.Add(new StringContent(userid), "userid");
var output = await client.PostAsync(server + "/v1/vision/face/delete", request);
var jsonString = await output.Content.ReadAsStringAsync();
deleteResponse response = JsonConvert.DeserializeObject<deleteResponse>(jsonString);
return response;
}
}
}