c# - Project oxford vision API ocr exception -
got problem project oxford vision api. example project oxford git works fine , recognise text on images. code throws exception:
exception of type 'microsoft.projectoxford.vision.clientexception' thrown. @ microsoft.projectoxford.vision.visionserviceclient.handleexception(exception exception) @ microsoft.projectoxford.vision.visionserviceclient.b__39_1[trequest,tresponse](exception e) @ system.aggregateexception.handle(func
2 predicate) @ microsoft.projectoxford.vision.visionserviceclient.<sendasync>d__39
2.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ microsoft.projectoxford.vision.visionserviceclient.d__32.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter1.getresult() @ ..ocrworker.<uploadandrecognizeimageasync>d__15.movenext() in ..\\ocrworker.cs:line 165\r\n --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter
1.getresult() @ ..ocrworker.d__14.movenext() in ..\ocrworker.cs:line 127
class code:
public string subscriptionkey { get; set; } public string ocrresulttext { { 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 { { 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 = 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 = 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 work scenario /// </summary> /// <param name="imageuri">the uri of image run against scenario</param> /// <param name="upload">upload image project oxford if [true]; submit uri remote url if [false];</param> /// <returns></returns> private async task<iterationitem> doworkforiterationasync(bitmap image, int iterationnumber) { var _status = "performing ocr..."; // // upload image // ocrresults ocrresult = await uploadandrecognizeimageasync(image, recognizelanguage.shortcode); _status = "ocr done"; // // log analysis result in log window // return new iterationitem() { number = iterationnumber, ocrresult = ocrresult }; } /// <summary> /// uploads image project oxford , performs ocr /// </summary> /// <param name="imagefilepath">the image file path.</param> /// <param name="language">the language code 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 created"); using (stream imagememorystream = new memorystream()) { image.save(imagememorystream, imageformat.bmp); // // upload image , 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 { { return "en"; } } public static string longname { { return "english"; } } }
did have same problem , how can solve it?
solved! correct work should use imagememorystream.seek(0, seekorigin.begin); after copy stream image
Comments
Post a Comment