-
Notifications
You must be signed in to change notification settings - Fork 0
/
customvision.csx
119 lines (103 loc) · 3.51 KB
/
customvision.csx
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
#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
#r "System.Web"
using System;
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using System.Xml;
using System.Web;
using Newtonsoft.Json;
/// <summary>
/// Custom Visionで画像を識別
/// </summary>
/// <returns>Stream</returns>
static async Task<HttpResponseMessage> GetVisionData(Stream stream)
{
var client = new HttpClient();
// リクエストヘッダ
client.DefaultRequestHeaders.Add("Prediction-key", Environment.GetEnvironmentVariable("CustomVisionPredictionKey"));
// リクエストパラメータ
var uri = Environment.GetEnvironmentVariable("CustomVisionUri");
// リクエストボディ
var content = new StreamContent(stream);
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
HttpResponseMessage response = await client.PostAsync(uri, content);
return response;
}
/// <summary>
/// Custom Visionのレスポンスメッセージをパース
/// </summary>
/// <returns>Stream</returns>
static async Task<string> GetParseString(HttpResponseMessage visionResponse, TraceWriter log)
{
var jsonContent = await visionResponse.Content.ReadAsStringAsync();
Image_Response image_data = JsonConvert.DeserializeObject<Image_Response>(jsonContent);
string words = String.Empty;
if (image_data.predictions.Any())
{
foreach (var prediction in image_data.predictions)
{
words = words + prediction.TagName + Environment.NewLine + " - " + Convert.ToDouble(prediction.Probability).ToString("P") + Environment.NewLine;
}
}
else
{
words = "画像が認識できませんでした";
}
return words;
}
/// <summary>
/// Custom Visionのレスポンスから最大確率のタグを取得
/// </summary>
/// <returns>Stream</returns>
static async Task<string> GetMaxProbability(HttpResponseMessage visionResponse, TraceWriter log)
{
// Custom VisionのレスポンスからJSONをパースして予測結果を抽出
var jsonContent = await visionResponse.Content.ReadAsStringAsync();
log.Info(jsonContent);
Image_Response image_data = JsonConvert.DeserializeObject<Image_Response>(jsonContent);
if (image_data.predictions.Any())
{
double threshold = 0.8; // 識別確率の閾値(この値以上の確からしさがあるタグをreturnする)
double maxProb = 0; // 現時点の最大確率を保存する一時変数
string maxTag = "";
foreach (var prediction in image_data.predictions)
{
if (Convert.ToDouble(prediction.Probability) > maxProb)
{
maxTag = prediction.TagName;
maxProb = Convert.ToDouble(prediction.Probability);
}
}
if (maxProb > threshold)
{
log.Info(Convert.ToString(maxProb));
return maxTag;
}
else
{
return "failed";
}
}
else
{
return "error";
}
return "error";
}
// Custom Visionから返却されたデータ
public class Image_Response
{
public string Id { get; set; }
public string Project { get; set; }
public string Iteration { get; set; }
public string Created { get; set; }
public List<Prediction> predictions { get; set; }
}
public class Prediction
{
public string TagId { get; set; }
public string TagName { get; set; }
public string Probability { get; set; }
}