C# my Azure API(翻译文本、计算机视觉)的终结点不可访问(错误代码404)
我遵循了Microsoft提供的Hololens教程,在301()和302()中遇到了一些有关Azure服务的问题。我认为问题与端点有关,因为检查时会出现错误代码404 我使用Hololens第一代、Unity 2017.4.27f1、HoloToolkit-Unity-Examples-2017.4.3.0和Visual Studio Community 2017。我试过一些全息镜头教程,这些版本都很好用。我的代码是Microsoft为教程301和302提供的代码。我已将Azure中的位置从更改为 “global”到“Western Europe”,通过创建一个新的ressource组,现在作为我的端点,我还为API创建了新的身份验证密钥,并在代码中进行了更新 我插入了相应API(翻译文本、计算机视觉)的第一个键,而不是myKey。下面的代码来自教程302和类“VisionManager.cs”(它似乎负责与计算机视觉API的服务进行通信)C# my Azure API(翻译文本、计算机视觉)的终结点不可访问(错误代码404),c#,azure,hololens,C#,Azure,Hololens,我遵循了Microsoft提供的Hololens教程,在301()和302()中遇到了一些有关Azure服务的问题。我认为问题与端点有关,因为检查时会出现错误代码404 我使用Hololens第一代、Unity 2017.4.27f1、HoloToolkit-Unity-Examples-2017.4.3.0和Visual Studio Community 2017。我试过一些全息镜头教程,这些版本都很好用。我的代码是Microsoft为教程301和302提供的代码。我已将Azure中的位置从更
公共静态VisionManager实例;
//您必须在此处插入服务密钥!
私有字符串authorizationKey=“myKey”;
私有常量字符串ocpApimSubscriptionKeyHeader=“Ocp Apim订阅密钥”;
专用字符串visionAnalysisEndpoint=”https://westcentralus.api.cognitive.microsoft.com/vision/v2.0"; // 如果将位置设置为美国西部以外的其他位置,则需要在此处更新端点。
内部字节[]图像字节;
内部字符串图像路径;
///
///致电计算机视觉服务提交图像。
///
公共IEnumerator AnalyseLastImageCaptured()
{
WWWForm webForm=新的WWWForm();
使用(UnityWebRequest UnityWebRequest=UnityWebRequest.Post(visionAnalysisEndpoint,webForm))
{
//从保存的图像中获取字节数组
imageBytes=GetImageAsByteArray(imagePath);
unityWebRequest.SetRequestHeader(“内容类型”、“应用程序/八位字节流”);
unityWebRequest.SetRequestHeader(ocpApimSubscriptionKeyHeader,authorizationKey);
//下载处理程序将帮助从Azure接收分析
unityWebRequest.downloadHandler=新的DownloadHandlerBuffer();
//上载处理程序将帮助上载带有请求的字节数组
unityWebRequest.uploadHandler=新的UploadHandlerRaw(imageBytes);
unityWebRequest.uploadHandler.contentType=“应用程序/八位字节流”;
产生返回unityWebRequest.SendWebRequest();
长响应代码=unityWebRequest.responseCode;
尝试
{
字符串jsonResponse=null;
jsonResponse=unityWebRequest.downloadHandler.text;
//响应将采用Json格式
//因此,需要将其反序列化到AnalysizedObject和TagData类中
AnalysedObject AnalysedObject=新的AnalysedObject();
analysedObject=JsonUtility.FromJson(jsonResponse);
if(analysedObject.tags==null)
{
Log(“analysedObject.tagData为null”);
}
其他的
{
Dictionary tagsDictionary=新字典();
foreach(analysedObject.tags中的TagData td)
{
TagData tag=td作为TagData;
tagsDictionary.Add(tag.name,tag.confidence);
}
ResultsLabel.instance.SetTagsToLastLabel(tagsDictionary);
}
}
捕获(异常)
{
Log(“Json exception.Message:+exception.Message”);
}
收益返回空;
}
}
[系统可序列化]
公共类标记数据
{
公共字符串名称;
公众信心;
}
[系统可序列化]
公共类分析对象
{
公共TagData[]标签;
公共字符串请求ID;
公共对象元数据;
}
//用于初始化
无效开始(){
}
私人空间
{
//允许此实例的行为类似于单例
实例=此;
}
//每帧调用一次更新
无效更新(){
}
///
///以字节数组的形式返回指定文件的内容。
///
私有静态字节[]GetImageAsByteArray(字符串imageFilePath)
{
FileStream FileStream=新的FileStream(imageFilePath,FileMode.Open,FileAccess.Read);
BinaryReader BinaryReader=新的BinaryReader(文件流);
返回binaryReader.ReadBytes((int)fileStream.Length);
}
我希望输出与教程中的示例类似,但是我得到了相同的GUI结果,没有任何更新。在301中,我可以看到相同的屏幕,但文本字段“You just said”和“Translation”在我发言时从未更新。在302中,我可以空中点击并制作一个屏幕截图,可以看到最初的“分析”和后来的“我看到了”,但没有任何检测到物体的结果。因此,在我看来,Azure API服务无法联系,我也没有得到任何分析数据作为回报(可能你会注意到,我对整个主题都很陌生,所以可能我的假设是错误的)
public static VisionManager instance;
// you must insert your service key here!
private string authorizationKey = "myKey";
private const string ocpApimSubscriptionKeyHeader = "Ocp-Apim-Subscription-Key";
private string visionAnalysisEndpoint = "https://westcentralus.api.cognitive.microsoft.com/vision/v2.0"; // This is where you need to update your endpoint, if you set your location to something other than west-us.
internal byte[] imageBytes;
internal string imagePath;
/// <summary>
/// Call the Computer Vision Service to submit the image.
/// </summary>
public IEnumerator AnalyseLastImageCaptured()
{
WWWForm webForm = new WWWForm();
using (UnityWebRequest unityWebRequest = UnityWebRequest.Post(visionAnalysisEndpoint, webForm))
{
// gets a byte array out of the saved image
imageBytes = GetImageAsByteArray(imagePath);
unityWebRequest.SetRequestHeader("Content-Type", "application/octet-stream");
unityWebRequest.SetRequestHeader(ocpApimSubscriptionKeyHeader, authorizationKey);
// the download handler will help receiving the analysis from Azure
unityWebRequest.downloadHandler = new DownloadHandlerBuffer();
// the upload handler will help uploading the byte array with the request
unityWebRequest.uploadHandler = new UploadHandlerRaw(imageBytes);
unityWebRequest.uploadHandler.contentType = "application/octet-stream";
yield return unityWebRequest.SendWebRequest();
long responseCode = unityWebRequest.responseCode;
try
{
string jsonResponse = null;
jsonResponse = unityWebRequest.downloadHandler.text;
// The response will be in Json format
// therefore it needs to be deserialized into the classes AnalysedObject and TagData
AnalysedObject analysedObject = new AnalysedObject();
analysedObject = JsonUtility.FromJson<AnalysedObject>(jsonResponse);
if (analysedObject.tags == null)
{
Debug.Log("analysedObject.tagData is null");
}
else
{
Dictionary<string, float> tagsDictionary = new Dictionary<string, float>();
foreach (TagData td in analysedObject.tags)
{
TagData tag = td as TagData;
tagsDictionary.Add(tag.name, tag.confidence);
}
ResultsLabel.instance.SetTagsToLastLabel(tagsDictionary);
}
}
catch (Exception exception)
{
Debug.Log("Json exception.Message: " + exception.Message);
}
yield return null;
}
}
[System.Serializable]
public class TagData
{
public string name;
public float confidence;
}
[System.Serializable]
public class AnalysedObject
{
public TagData[] tags;
public string requestId;
public object metadata;
}
// Use this for initialization
void Start () {
}
private void Awake()
{
// allows this instance to behave like a singleton
instance = this;
}
// Update is called once per frame
void Update () {
}
/// <summary>
/// Returns the contents of the specified file as a byte array.
/// </summary>
private static byte[] GetImageAsByteArray(string imageFilePath)
{
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}