Skip to content

Commit f9dede3

Browse files
committed
feat(VisualRecognitionV4): add support for download model file
1 parent 9998de9 commit f9dede3

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2018, 2019 IBM Corp. All Rights Reserved.
2+
* (C) Copyright IBM Corp. 2018, 2020.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,7 +52,7 @@ public class ImageDetails
5252
[JsonProperty("dimensions", NullValueHandling = NullValueHandling.Ignore)]
5353
public ImageDimensions Dimensions { get; set; }
5454
/// <summary>
55-
/// Gets or Sets Errors
55+
/// Details about the errors.
5656
/// </summary>
5757
[JsonProperty("errors", NullValueHandling = NullValueHandling.Ignore)]
5858
public List<Error> Errors { get; set; }

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public class ObjectTrainingStatus
4545
[JsonProperty("latest_failed", NullValueHandling = NullValueHandling.Ignore)]
4646
public bool? LatestFailed { get; set; }
4747
/// <summary>
48+
/// Whether the model can be downloaded after the training status is `ready`.
49+
/// </summary>
50+
[JsonProperty("rscnn_ready", NullValueHandling = NullValueHandling.Ignore)]
51+
public bool? RscnnReady { get; set; }
52+
/// <summary>
4853
/// Details about the training. If training is in progress, includes information about the status. If training
4954
/// is not in progress, includes a success message or information about why training failed.
5055
/// </summary>

Scripts/Services/VisualRecognition/V4/VisualRecognitionService.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,84 @@ private void OnDeleteCollectionResponse(RESTConnector.Request req, RESTConnector
575575
((RequestObject<object>)req).Callback(response, resp.Error);
576576
}
577577
/// <summary>
578+
/// Get a model.
579+
///
580+
/// Download a model that you can deploy to detect objects in images. The collection must include a generated
581+
/// model, which is indicated in the response for the collection details as `"rscnn_ready": true`. If the value
582+
/// is `false`, train or retrain the collection to generate the model.
583+
///
584+
/// Currently, the model format is specific to Android apps. For more information about how to deploy the model
585+
/// to your app, see the [Watson Visual Recognition on Android](https://github.com/matt-ny/rscnn) project in
586+
/// GitHub.
587+
/// </summary>
588+
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
589+
/// <param name="collectionId">The identifier of the collection.</param>
590+
/// <param name="feature">The feature for the model.</param>
591+
/// <param name="modelFormat">The format of the returned model.</param>
592+
/// <returns><see cref="byte[]" />byte[]</returns>
593+
public bool GetModelFile(Callback<byte[]> callback, string collectionId, string feature, string modelFormat)
594+
{
595+
if (callback == null)
596+
throw new ArgumentNullException("`callback` is required for `GetModelFile`");
597+
if (string.IsNullOrEmpty(collectionId))
598+
throw new ArgumentNullException("`collectionId` is required for `GetModelFile`");
599+
if (string.IsNullOrEmpty(feature))
600+
throw new ArgumentNullException("`feature` is required for `GetModelFile`");
601+
if (string.IsNullOrEmpty(modelFormat))
602+
throw new ArgumentNullException("`modelFormat` is required for `GetModelFile`");
603+
604+
RequestObject<byte[]> req = new RequestObject<byte[]>
605+
{
606+
Callback = callback,
607+
HttpMethod = UnityWebRequest.kHttpVerbGET,
608+
DisableSslVerification = DisableSslVerification
609+
};
610+
611+
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
612+
{
613+
req.Headers.Add(kvp.Key, kvp.Value);
614+
}
615+
616+
ClearCustomRequestHeaders();
617+
618+
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("watson_vision_combined", "V4", "GetModelFile"))
619+
{
620+
req.Headers.Add(kvp.Key, kvp.Value);
621+
}
622+
623+
req.Parameters["version"] = VersionDate;
624+
if (!string.IsNullOrEmpty(feature))
625+
{
626+
req.Parameters["feature"] = feature;
627+
}
628+
if (!string.IsNullOrEmpty(modelFormat))
629+
{
630+
req.Parameters["model_format"] = modelFormat;
631+
}
632+
633+
req.OnResponse = OnGetModelFileResponse;
634+
635+
Connector.URL = GetServiceUrl() + string.Format("/v4/collections/{0}/model", collectionId);
636+
Authenticator.Authenticate(Connector);
637+
638+
return Connector.Send(req);
639+
}
640+
641+
private void OnGetModelFileResponse(RESTConnector.Request req, RESTConnector.Response resp)
642+
{
643+
DetailedResponse<byte[]> response = new DetailedResponse<byte[]>();
644+
foreach (KeyValuePair<string, string> kvp in resp.Headers)
645+
{
646+
response.Headers.Add(kvp.Key, kvp.Value);
647+
}
648+
response.StatusCode = resp.HttpResponseCode;
649+
650+
response.Result = resp.Data;
651+
652+
if (((RequestObject<byte[]>)req).Callback != null)
653+
((RequestObject<byte[]>)req).Callback(response, resp.Error);
654+
}
655+
/// <summary>
578656
/// Add images.
579657
///
580658
/// Add images to a collection by URL, by file, or both.

0 commit comments

Comments
 (0)