-
Notifications
You must be signed in to change notification settings - Fork 9
/
MaskRecognition.cshtml.cs
141 lines (116 loc) · 5.11 KB
/
MaskRecognition.cshtml.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
//using Microsoft.Azure.CognitiveServices.Vision.Face;
//using Microsoft.Azure.CognitiveServices.Vision.Face.Models;
namespace CogServicesVisionSamples_202107.Pages
{
public class MaskRecognitionModel : PageModel
{
[BindProperty]
public IFormFile ImageFile { get; set; }
public string ImageFileUrl { get; set; }
public string Result { get; set; }
public Mask MaskRecognition { get; set; }
// Setting for using Face API
private const string faceSubscriptionKey = "YOUR_SUBSCRIPTION_KEY";
private const string faceEndpoint = "YOUR_SUBSCRIPTION_ENDPOINT"; //"https://japaneast.api.cognitive.microsoft.com/"
// Setting for FileIO
private readonly IWebHostEnvironment _host;
public MaskRecognitionModel(IWebHostEnvironment host)
{
_host = host;
}
static readonly HttpClient httpClient = new HttpClient();
public async Task<IActionResult> OnPostAsync()
{
// Show image on web page
var imageFilePath = Path.Combine(_host.WebRootPath, "images\\uploadedImage.jpg");
using (var fileStream = new FileStream(imageFilePath, FileMode.OpenOrCreate))
{
await ImageFile.CopyToAsync(fileStream);
}
ImageFileUrl = "/images/uploadedImage.jpg";
//var faceClient = new FaceClient(new ApiKeyServiceClientCredentials(faceSubscriptionKey))
//{
// Endpoint = faceEndpoint
//};
try
{
//var faceResult = await faceClient.Face.DetectWithStreamAsync(ImageFile.OpenReadStream(),
// returnFaceId: false,
// returnFaceLandmarks: false,
// returnFaceAttributes: FaceAttributeType.Mask,
// detectionModel: "detection_03"
// );
// Call API direct without using Face API Libraries
var content = new StreamContent(ImageFile.OpenReadStream());
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
var url = faceEndpoint + "face/v1.0/detect?returnFaceAttributes=mask&detectionModel=detection_03";
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Headers.Add(@"Ocp-Apim-Subscription-Key", faceSubscriptionKey);
request.Content = content;
var response = await httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
var faceResponseJson = await response.Content.ReadAsStringAsync();
var faceResponse = JsonSerializer.Deserialize<MaskRecognitionResult[]>(faceResponseJson);
Result = "結果";
MaskRecognition = faceResponse[0].faceAttributes.mask;
// Draw rectangle on detected image
var detectedImagePath = Path.Combine(_host.WebRootPath, "images\\detectedImage.jpg");
var rectangleImagePath = Path.Combine(_host.WebRootPath, "images\\rectangle.png");
using (var detectedImage = new Bitmap(imageFilePath))
using (var graphics = Graphics.FromImage(detectedImage))
{
graphics.DrawImage(new Bitmap(rectangleImagePath),
faceResponse[0].faceRectangle.left, faceResponse[0].faceRectangle.top,
faceResponse[0].faceRectangle.width, faceResponse[0].faceRectangle.height);
detectedImage.Save(detectedImagePath, ImageFormat.Jpeg);
}
ImageFileUrl = "/images/detectedImage.jpg";
}
else
{
Result = "判定できませんでした";
}
}
catch (ApplicationException e)
{
Result = "エラー: " + e.Message;
}
return Page();
}
// Custom Class for Mask Recognition
public class MaskRecognitionResult
{
public string faceId { get; set; }
public FaceRectangle faceRectangle { get; set; }
public FaceAttributes faceAttributes { get; set; }
}
public class FaceRectangle
{
public int top { get; set; }
public int left { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class FaceAttributes
{
public Mask mask { get; set; }
}
public class Mask
{
public string type { get; set; }
public bool noseAndMouthCovered { get; set; }
}
}
}