Skip to content

Commit 41d8d8b

Browse files
committed
feat(VisualRecognitionV4): add support to get training usage data
1 parent 651d2b4 commit 41d8d8b

File tree

7 files changed

+251
-5
lines changed

7 files changed

+251
-5
lines changed

Scripts/Services/VisualRecognition/V4/Model/Image.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*
1616
*/
1717

18+
using System.Collections.Generic;
1819
using Newtonsoft.Json;
1920

2021
namespace IBM.Watson.VisualRecognition.V4.Model
@@ -40,9 +41,9 @@ public class Image
4041
[JsonProperty("objects", NullValueHandling = NullValueHandling.Ignore)]
4142
public DetectedObjects Objects { get; set; }
4243
/// <summary>
43-
/// Details about an error.
44+
/// A container for the problems in the request.
4445
/// </summary>
4546
[JsonProperty("errors", NullValueHandling = NullValueHandling.Ignore)]
46-
public Error Errors { get; set; }
47+
public List<Error> Errors { get; set; }
4748
}
4849
}

Scripts/Services/VisualRecognition/V4/Model/ImageDetails.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*
1616
*/
1717

18+
using System.Collections.Generic;
1819
using Newtonsoft.Json;
1920
using System;
2021

@@ -51,10 +52,10 @@ public class ImageDetails
5152
[JsonProperty("dimensions", NullValueHandling = NullValueHandling.Ignore)]
5253
public ImageDimensions Dimensions { get; set; }
5354
/// <summary>
54-
/// Details about an error.
55+
/// Gets or Sets Errors
5556
/// </summary>
5657
[JsonProperty("errors", NullValueHandling = NullValueHandling.Ignore)]
57-
public Error Errors { get; set; }
58+
public List<Error> Errors { get; set; }
5859
/// <summary>
5960
/// Training data for all objects.
6061
/// </summary>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Copyright 2018, 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using Newtonsoft.Json;
19+
using System;
20+
21+
namespace IBM.Watson.VisualRecognition.V4.Model
22+
{
23+
/// <summary>
24+
/// Details about the training event.
25+
/// </summary>
26+
public class TrainingEvent
27+
{
28+
/// <summary>
29+
/// Trained object type. Only `objects` is currently supported.
30+
/// </summary>
31+
public class TypeValue
32+
{
33+
/// <summary>
34+
/// Constant OBJECTS for objects
35+
/// </summary>
36+
public const string OBJECTS = "objects";
37+
38+
}
39+
40+
/// <summary>
41+
/// Training status of the training event.
42+
/// </summary>
43+
public class StatusValue
44+
{
45+
/// <summary>
46+
/// Constant FAILED for failed
47+
/// </summary>
48+
public const string FAILED = "failed";
49+
/// <summary>
50+
/// Constant SUCCEEDED for succeeded
51+
/// </summary>
52+
public const string SUCCEEDED = "succeeded";
53+
54+
}
55+
56+
/// <summary>
57+
/// Trained object type. Only `objects` is currently supported.
58+
/// Constants for possible values can be found using TrainingEvent.TypeValue
59+
/// </summary>
60+
[JsonProperty("type", NullValueHandling = NullValueHandling.Ignore)]
61+
public string Type { get; set; }
62+
/// <summary>
63+
/// Training status of the training event.
64+
/// Constants for possible values can be found using TrainingEvent.StatusValue
65+
/// </summary>
66+
[JsonProperty("status", NullValueHandling = NullValueHandling.Ignore)]
67+
public string Status { get; set; }
68+
/// <summary>
69+
/// Identifier of the trained collection.
70+
/// </summary>
71+
[JsonProperty("collection_id", NullValueHandling = NullValueHandling.Ignore)]
72+
public string CollectionId { get; set; }
73+
/// <summary>
74+
/// Date and time in Coordinated Universal Time (UTC) that training on the collection finished.
75+
/// </summary>
76+
[JsonProperty("completion_time", NullValueHandling = NullValueHandling.Ignore)]
77+
public DateTime? CompletionTime { get; set; }
78+
/// <summary>
79+
/// The total number of images that were used in training for this training event.
80+
/// </summary>
81+
[JsonProperty("image_count", NullValueHandling = NullValueHandling.Ignore)]
82+
public long? ImageCount { get; set; }
83+
}
84+
}

Scripts/Services/VisualRecognition/V4/Model/TrainingEvent.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright 2018, 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using System.Collections.Generic;
19+
using Newtonsoft.Json;
20+
using System;
21+
22+
namespace IBM.Watson.VisualRecognition.V4.Model
23+
{
24+
/// <summary>
25+
/// Details about the training events.
26+
/// </summary>
27+
public class TrainingEvents
28+
{
29+
/// <summary>
30+
/// The starting day for the returned training events in Coordinated Universal Time (UTC). If not specified in
31+
/// the request, it identifies the earliest training event.
32+
/// </summary>
33+
[JsonProperty("start_time", NullValueHandling = NullValueHandling.Ignore)]
34+
public DateTime? StartTime { get; set; }
35+
/// <summary>
36+
/// The ending day for the returned training events in Coordinated Universal Time (UTC). If not specified in the
37+
/// request, it lists the current time.
38+
/// </summary>
39+
[JsonProperty("end_time", NullValueHandling = NullValueHandling.Ignore)]
40+
public DateTime? EndTime { get; set; }
41+
/// <summary>
42+
/// The total number of training events in the response for the start and end times.
43+
/// </summary>
44+
[JsonProperty("completed_events", NullValueHandling = NullValueHandling.Ignore)]
45+
public long? CompletedEvents { get; set; }
46+
/// <summary>
47+
/// The total number of images that were used in training for the start and end times.
48+
/// </summary>
49+
[JsonProperty("trained_images", NullValueHandling = NullValueHandling.Ignore)]
50+
public long? TrainedImages { get; set; }
51+
/// <summary>
52+
/// The completed training events for the start and end time.
53+
/// </summary>
54+
[JsonProperty("events", NullValueHandling = NullValueHandling.Ignore)]
55+
public List<TrainingEvent> Events { get; set; }
56+
}
57+
}

Scripts/Services/VisualRecognition/V4/Model/TrainingEvents.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Services/VisualRecognition/V4/VisualRecognitionService.cs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,9 @@ private void OnDeleteImageResponse(RESTConnector.Request req, RESTConnector.Resp
900900
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
901901
/// <param name="collectionId">The identifier of the collection.</param>
902902
/// <param name="imageId">The identifier of the image.</param>
903-
/// <param name="size">Specify the image size. (optional, default to full)</param>
903+
/// <param name="size">The image size. Specify `thumbnail` to return a version that maintains the original
904+
/// aspect ratio but is no larger than 200 pixels in the larger dimension. For example, an original 800 x 1000
905+
/// image is resized to 160 x 200 pixels. (optional, default to full)</param>
904906
/// <returns><see cref="byte[]" />byte[]</returns>
905907
public bool GetJpegImage(Callback<byte[]> callback, string collectionId, string imageId, string size = null)
906908
{
@@ -1115,6 +1117,85 @@ private void OnAddImageTrainingDataResponse(RESTConnector.Request req, RESTConne
11151117
((RequestObject<TrainingDataObjects>)req).Callback(response, resp.Error);
11161118
}
11171119
/// <summary>
1120+
/// Get training usage.
1121+
///
1122+
/// Information about the completed training events. You can use this information to determine how close you are
1123+
/// to the training limits for the month.
1124+
/// </summary>
1125+
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
1126+
/// <param name="startTime">The earliest day to include training events. Specify dates in YYYY-MM-DD format. If
1127+
/// empty or not specified, the earliest training event is included. (optional)</param>
1128+
/// <param name="endTime">The most recent day to include training events. Specify dates in YYYY-MM-DD format.
1129+
/// All events for the day are included. If empty or not specified, the current day is used. Specify the same
1130+
/// value as `start_time` to request events for a single day. (optional)</param>
1131+
/// <returns><see cref="TrainingEvents" />TrainingEvents</returns>
1132+
public bool GetTrainingUsage(Callback<TrainingEvents> callback, string startTime = null, string endTime = null)
1133+
{
1134+
if (callback == null)
1135+
throw new ArgumentNullException("`callback` is required for `GetTrainingUsage`");
1136+
1137+
RequestObject<TrainingEvents> req = new RequestObject<TrainingEvents>
1138+
{
1139+
Callback = callback,
1140+
HttpMethod = UnityWebRequest.kHttpVerbGET,
1141+
DisableSslVerification = DisableSslVerification
1142+
};
1143+
1144+
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
1145+
{
1146+
req.Headers.Add(kvp.Key, kvp.Value);
1147+
}
1148+
1149+
ClearCustomRequestHeaders();
1150+
1151+
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("watson_vision_combined", "V4", "GetTrainingUsage"))
1152+
{
1153+
req.Headers.Add(kvp.Key, kvp.Value);
1154+
}
1155+
1156+
req.Parameters["version"] = VersionDate;
1157+
if (!string.IsNullOrEmpty(startTime))
1158+
{
1159+
req.Parameters["start_time"] = startTime;
1160+
}
1161+
if (!string.IsNullOrEmpty(endTime))
1162+
{
1163+
req.Parameters["end_time"] = endTime;
1164+
}
1165+
1166+
req.OnResponse = OnGetTrainingUsageResponse;
1167+
1168+
Connector.URL = GetServiceUrl() + "/v4/training_usage";
1169+
Authenticator.Authenticate(Connector);
1170+
1171+
return Connector.Send(req);
1172+
}
1173+
1174+
private void OnGetTrainingUsageResponse(RESTConnector.Request req, RESTConnector.Response resp)
1175+
{
1176+
DetailedResponse<TrainingEvents> response = new DetailedResponse<TrainingEvents>();
1177+
foreach (KeyValuePair<string, string> kvp in resp.Headers)
1178+
{
1179+
response.Headers.Add(kvp.Key, kvp.Value);
1180+
}
1181+
response.StatusCode = resp.HttpResponseCode;
1182+
1183+
try
1184+
{
1185+
string json = Encoding.UTF8.GetString(resp.Data);
1186+
response.Result = JsonConvert.DeserializeObject<TrainingEvents>(json);
1187+
response.Response = json;
1188+
}
1189+
catch (Exception e)
1190+
{
1191+
Log.Error("VisualRecognitionService.OnGetTrainingUsageResponse()", "Exception: {0}", e.ToString());
1192+
resp.Success = false;
1193+
}
1194+
1195+
if (((RequestObject<TrainingEvents>)req).Callback != null)
1196+
((RequestObject<TrainingEvents>)req).Callback(response, resp.Error);
1197+
}
1198+
/// <summary>
11181199
/// Delete labeled data.
11191200
///
11201201
/// Deletes all data associated with a specified customer ID. The method has no effect if no data is associated

0 commit comments

Comments
 (0)