プロジェクトoxfordビジョンAPIで問題が発生しました。 project oxford gitの例はうまく動作し、画像のテキストを認識します。しかし、私のコードで例外がスローされます:プロジェクトoxfordビジョンAPI ocr例外
'Microsoft.ProjectOxford.Vision.ClientException'型の例外がスローされました。 System.AggregateException.Handle(機能
2 predicate) at Microsoft.ProjectOxford.Vision.VisionServiceClient.<SendAsync>d__39
2.MoveNextでMicrosoft.ProjectOxford.Vision.VisionServiceClient.b__39_1 [TRequest、たTResponse](例外e) でMicrosoft.ProjectOxford.Vision.VisionServiceClient.HandleException(例外例外) で()例外がスローされた以前の場所からのスタックトレースの ---終わり--- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccessで (タスクタスク) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotificationで(タスクのタスク) でMicrosoft.ProjectOxford.Vision.VisionServiceClient.d__32.MoveNext() ---例外がスローされた前の場所からのスタックトレースの終了--- at System.Runtime.CompilerServices.Tas ..OcrWorker.d__14.MoveNextでSystem.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotificationでkAwaiter.ThrowForNonSuccess(タスクタスク)1.GetResult() at ..OcrWorker.<UploadAndRecognizeImageAsync>d__15.MoveNext() in ..\\OcrWorker.cs:line 165\r\n --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter
System.Runtime.CompilerServices.TaskAwaiter 1.GetResultに(タスクのタスク) () ()です。 。\ OcrWorker.cs:ライン127
クラスコード:
public string SubscriptionKey { get; set; }
public string OcrResultText
{
get
{
if (FullOcrResult == null)
{
FullOcrResult = new StringBuilder();
}
string response = string.Empty;
if (OcrDone)
{
response = FullOcrResult.ToString();
}
else
{
response = null;
}
return response;
}
}
private bool OcrDone = true;
public bool IsOcrDone { get { return OcrDone; } }
private StringBuilder FullOcrResult;
public OcrWorker(string appKey)
{
SubscriptionKey = appKey;
FullOcrResult = new StringBuilder();
}
public string DoWorkSync(List<Bitmap> images)
{
if (OcrDone)
{
List<IterationItem> iteartionItems = new List<IterationItem>();
int i = 0;
OcrDone = false;
foreach (var image in images)
{
IterationItem ocrIterationItem = new IterationItem();
try
{
Task<IterationItem> o = DoWorkForIterationAsync(image, i);
o.Wait();
ocrIterationItem = o.Result;
}
catch (Exception ex)
{
var a = ex.GetBaseException();
}
iteartionItems.Add(ocrIterationItem);
i++;
}
GetOcrResultFromIterations(iteartionItems);
OcrDone = true;
}
return OcrResultText;
}
public void WriteResultToFile(string path)
{
if (OcrDone)
{
if (File.Exists(path))
{
File.Delete(path);
}
File.AppendAllText(path, OcrResultText);
}
}
private void GetOcrResultFromIterations(List<IterationItem> iterationResults)
{
iterationResults = iterationResults.OrderBy(item => item.Number).ToList();
foreach (var iterationItem in iterationResults)
{
var results = iterationItem.OcrResult;
FullOcrResult.AppendLine();
foreach (var item in results.Regions)
{
foreach (var line in item.Lines)
{
foreach (var word in line.Words)
{
FullOcrResult.Append(word.Text);
FullOcrResult.Append(" ");
}
FullOcrResult.AppendLine();
}
FullOcrResult.AppendLine();
}
}
}
/// <summary>
/// Perform the work for this scenario
/// </summary>
/// <param name="imageUri">The URI of the image to run against the scenario</param>
/// <param name="upload">Upload the image to Project Oxford if [true]; submit the Uri as a remote url if [false];</param>
/// <returns></returns>
private async Task<IterationItem> DoWorkForIterationAsync(Bitmap image, int iterationNumber)
{
var _status = "Performing OCR...";
//
// Upload an image
//
OcrResults ocrResult = await UploadAndRecognizeImageAsync(image, RecognizeLanguage.ShortCode);
_status = "OCR Done";
//
// Log analysis result in the log window
//
return new IterationItem()
{
Number = iterationNumber,
OcrResult = ocrResult
};
}
/// <summary>
/// Uploads the image to Project Oxford and performs OCR
/// </summary>
/// <param name="imageFilePath">The image file path.</param>
/// <param name="language">The language code to recognize for</param>
/// <returns></returns>
private async Task<OcrResults> UploadAndRecognizeImageAsync(Bitmap image, string language)
{
// -----------------------------------------------------------------------
// KEY SAMPLE CODE STARTS HERE
// -----------------------------------------------------------------------
//
// Create Project Oxford Vision API Service client
//
VisionServiceClient VisionServiceClient = new VisionServiceClient(SubscriptionKey);
Log("VisionServiceClient is created");
using (Stream imageMemoryStream = new MemoryStream())
{
image.Save(imageMemoryStream, ImageFormat.Bmp);
//
// Upload an image and perform OCR
//
Log("Calling VisionServiceClient.RecognizeTextAsync()...");
OcrResults ocrResult = await VisionServiceClient.RecognizeTextAsync(imageMemoryStream, language);
return ocrResult;
}
// -----------------------------------------------------------------------
// KEY SAMPLE CODE ENDS HERE
// -----------------------------------------------------------------------
}
//get ocred text
class IterationItem
{
public int Number { get; set; }
public OcrResults OcrResult { get; set; }
}
public static class RecognizeLanguage
{
public static string ShortCode { get { return "en"; } }
public static string LongName { get { return "English"; } }
}
誰もが同じ問題を抱えていましたし、どのように私はそれを解決することができますか?
解決済み! UploadAndRecognizeImageAsync方法で File.OpenRead(imageFilePath)の代わりに:あなたが使うべき正しい仕事のため のMemoryStreamを作成し、コピービットマップそれに は、だからSDK – Nemo
の** RecognizeTextAsync **方法のFileStreamにメモリストリームを渡しません解決策はありますが、何らかの理由でMemoryStreamを機能させたい場合(たとえば、OCRを呼び出す前に画像をトリミングまたは操作する必要がある場合)は、まずimageMemoryStream.Position = 0 'となる。さもなければMemoryStreamの 'read 'ポインタは' image.Save() 'の後の最後にあります。 – cthrash