Reading Text from Images Using C#
Introduction
By using Optical Character Recognition (OCR), you can detect and extract handwritten and printed text present in an image. The API works with different surfaces and backgrounds. To use OCR as a service, you have to get a subscription key from the Microsoft Cognitive Service portal. Steps of registration and obtaining the API keys are mentioned in my previous article, "Analyzing Image Content Programmatically: Using the Microsoft Cognitive Vision API."
In this post, I will demonstrate handwriting and text recognition by uploading a locally stored image consuming the Cognitive API.
Embedded Text Recognition Inside an Image
Step 1
Create a new ASP.NET Web application in Visual Studio. Add a new ASPX page named TextRecognition.aspx. Then, replace the HTML code of TextRecognition.aspx with the following code.
In the following ASPX code, I have created an ASP panel. Inside that panel, I have added an Image, TextBox, a file upload control, and Submit button. Locally browsed images with handwritten or printed text will be displayed in the image control. A textbox will display text present in the image after the Submit button is clicked.
- <%@ Page Language="C#" AutoEventWireup="true"
- CodeBehind="TextRecognition.aspx.cs"
- Inherits="TextRecognition.TextRecognition" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>OCR Recognition</title>
- </head>
- <body>
- <form id="myform" runat="server">
- <div>
- </div>
- <asp:Panel ID="ImagePanel" runat="server"
- Height="375px">
- <asp:Image ID="MyImage" runat="server"
- Height="342px" Width="370px" />
- <asp:TextBox ID="MyTestBox" runat="server"
- Height="337px" Width="348px"></asp:TextBox>
- <br />
- <input id="MyFileUpload" type="file"
- runat="server" />
- <input id="btnSubmit" runat ="server"
- type="submit" value="Submit"
- onclick="btnSubmit_Click" />
- <br />
- </asp:Panel>
- </form>
- </body>
- </html>
Step 2
Add the following namespaces in your TextRecognition.aspx.cs code file.
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Diagnostics;
Step 3
Add the following app setting entries in your Web.config file. I have used an existing subscription key generated long back; you have to add your own subscription key registered from Azure Portal. The APIuri parameter key value is the endpoint of the ODR cognitive service.
- <appSettings>
- <add key="RequestParameters"
- value="language=unk&detectOrientation =true"/>
- <add key="APIuri"
- value="https://westcentralus.api.cognitive.microsoft.com/
- vision/v1.0/ocr?"/>
- <add key="Subscription-Key"
- value="ce765f110a1e4a1c8eb5d2928a765c61"/>
- <add key ="Contenttypes" value="application/json"/>
- </appSettings>
Step 4
Now, add the following code in your TextRecognition.aspx.cs codebehind file. All static functions will return appSettings key values, as mentioned in Step 3. The BtnSubmit_Click event will occur once the Submit button is clicked. It will call the CallAPIforOCR async function. By using the GetByteArray function, the local image will be converted to bytes and that would be passed to Cognitive Services for recognition.
- public partial class TextRecognition : System.Web.UI.Page
- {
- static string responsetext;
- static string responsehandwritting;
- static string Subscriptionkey()
- {
- return System.Configuration.ConfigurationManager
- .AppSettings["Subscription-Key"];
- }
- static string RequestParameters()
- {
- return System.Configuration.ConfigurationManager
- .AppSettings["RequestParameters"];
- }
- static string ReadURI()
- {
- return System.Configuration.ConfigurationManager
- .AppSettings["APIuri"];
- }
- static string Contenttypes()
- {
- return System.Configuration.ConfigurationManager
- .AppSettings["Contenttypes"];
- }
- protected void btnSubmit_Click(object sender, EventArgs e)
- {
- string fileName = System.IO.Path.GetFileName
- (MyFileUpload.PostedFile.FileName);
- MyFileUpload.PostedFile.SaveAs(Server.MapPath
- ("~/images/" + fileName));
- MyImage.ImageUrl = "~/images/" + fileName;
- CallAPIforOCR("~/images/" + fileName);
- MyTestBox.Text = responsetext;
- }
- static byte[] GetByteArray(string LocalimageFilePath)
- {
- FileStream ImagefileStream = new
- FileStream(LocalimageFilePath, FileMode.Open,
- FileAccess.Read);
- BinaryReader ImagebinaryReader = new
- BinaryReader(ImagefileStream);
- return ImagebinaryReader.ReadBytes((int)
- ImagefileStream.Length);
- }
- // Optical Character Reader
- static async void CallAPIforOCR(string LocalimageFilePath)
- {
- var ComputerVisionAPIclient = new HttpClient();
- try {
- ComputerVisionAPIclient.DefaultRequestHeaders
- .Add("Ocp-Apim-Subscription- Key",
- Subscriptionkey());
- string requestParameters = RequestParameters();
- string APIuri = ReadURI() + requestParameters;
- HttpResponseMessage myresponse;
- byte[] byteData = GetByteArray(LocalimageFilePath);
- var content = new ByteArrayContent(byteData);
- content.Headers.ContentType = new
- MediaTypeHeaderValue(Contenttypes());
- myresponse = await
- ComputerVisionAPIclient.PostAsync
- (APIuri, content);
- myresponse.EnsureSuccessStatusCode();
- responsetext = await myresponse.Content
- .ReadAsStringAsync();
- }
- catch (Exception e)
- {
- EventLog.WriteEntry("Text Recognition Error",
- e.Message + "Trace" + e.StackTrace,
- EventLogEntryType.Error, 121, short.MaxValue);
- }
- }
Step 5
Now, set TextRecognition.aspx as the default page and execute the Web application. After the page is displayed, click the browser button and open an local image with printed text on it. Click the Submit button to see the output.
To show the demo, I have used the following image downloaded from the Internet. Successful execution of the Cognitive Services API call returns OCR, including text, a bounding box for regions, lines, and words. On the right side of the panel, you can see the embedded test displayed in the text box.
Figure 1: Output of OCR recognition
Following is the JSON response from Cognitive Services.
- {
- "TextAngle": 0.0,
- "Orientation": "NotDetected",
- "Language": "en",
- "Regions": [
- {
- "BoundingBox": "21,246,550,132",
- "Lines": [
- {
- "BoundingBox": "21,246,550,33",
- "Words": [
- {
- "BoundingBox": "21,247,85,32",
- "Text": "How"
- },
- {
- "BoundingBox": "118,246,140,33",
- "Text": "Sundar"
- },
- {
- "BoundingBox": "273,246,121,33",
- "Text": "Pichai"
- },
- {
- "BoundingBox": "410,247,161,32",
- "Text": "Became"
- }
- ]
- },
- {
- "BoundingBox": "39,292,509,33",
- "Words": [
- {
- "BoundingBox": "39,293,72,32",
- "Text": "The"
- },
- {
- "BoundingBox": "126,293,99,32",
- "Text": "Most"
- },
- {
- "BoundingBox": "240,292,172,33",
- "Text": "Powerful"
- },
- {
- "BoundingBox": "428,292,120,33",
- "Text": "Indian"
- }
- ]
- },
- {
- "BoundingBox": "155,338,294,40",
- "Words": [
- {
- "BoundingBox": "155,338,118,33",
- "Text": "Inside"
- },
- {
- "BoundingBox": "287,338,162,40",
- "Text": "Google?"
- }
- ]
- }
- ]
- }
- ]
- }
Recognize Handwriting
For handwriting recognition from text present in an image, I have used the same application, but you have to change the APIuri path to point to the correct endpoint and update the RequestParameters key value added in the previous step.
- <appSettings>
- <add key="RequestParameters" value="handwriting=true"/>
- <add key="APIuri"
- value="https://westcentralus.api.cognitive
- .microsoft.com/vision/v1.0/recognizeText?"/>
- <add key="Subscription-Key"
- value="ce765f110a1e4a1c8eb5d2928a765c61"/>
- <add key ="Contenttypes" value="application/json"/>
- </appSettings>
Also, add the following ReadHandwritttingFromImage async method. This function will replace the CallAPIforOCR function call present in the btnSubmit_Click event.
- static async void ReadHandwritttingFromImage(string LocalimageFilePath)
- {
- HttpResponseMessage myresponse = null;
- IEnumerable<string> myresponseValues = null;
- string operationLocation = null;
- var ComputerVisionAPIclient = new HttpClient();
- ComputerVisionAPIclient.DefaultRequestHeaders.Add
- ("Ocp-Apim-Subscription-Key", Subscriptionkey());
- string requestParameters = RequestParameters();
- string APIuri = ReadURI() + requestParameters;
- byte[] byteData = GetByteArray(LocalimageFilePath);
- var content = new ByteArrayContent(byteData);
- content.Headers.ContentType = new
- MediaTypeHeaderValue(Contenttypes());
- try
- {
- myresponse = await ComputerVisionAPIclient
- .PostAsync(APIuri, content);
- myresponseValues = myresponse.Headers
- .GetValues("Operation-Location");
- }
- catch (Exception e)
- {
- EventLog.WriteEntry("Handwritting Recognition Error",
- e.Message + "Trace" + e.StackTrace,
- EventLogEntryType.Error, 121, short.MaxValue);
- }
- foreach (var value in myresponseValues)
- {
- operationLocation = value;
- break;
- }
- try
- {
- myresponse = await ComputerVisionAPIclient
- .GetAsync(operationLocation);
- responsehandwritting = await myresponse.Content
- .ReadAsStringAsync();
- }
- catch (Exception e)
- {
- EventLog.WriteEntry("Handwritting Recognition Error",
- e.Message + "Trace" + e.StackTrace,
- EventLogEntryType.Error, 121, short.MaxValue);
- }
- }
Now, execute the Web application again. After the page is displayed, click the browser button and open an local image with handwritten text on it. Click the Submit button to see the output.
Figure 2: Output of handwriting recognition
Reading Text from Images Using C#的更多相关文章
- Suspended Animation——《The Economist》阅读积累(考研英语二·2010 Reading Text 1)
[知识小百科] Damien Hirst(达米恩●赫斯特):生于1965年,是新一代英国艺术家的主要代表人物之一.他主导了90年代英国艺术发展并享有很高的国际声誉.赫斯特在1986年9月就读于伦敦大学 ...
- read content in a text file in python
** read text file in pythoncapability: reading =text= from a text file 1. open the IDLE text editor ...
- 【深度学习Deep Learning】资料大全
最近在学深度学习相关的东西,在网上搜集到了一些不错的资料,现在汇总一下: Free Online Books by Yoshua Bengio, Ian Goodfellow and Aaron C ...
- (转) Awesome - Most Cited Deep Learning Papers
转自:https://github.com/terryum/awesome-deep-learning-papers Awesome - Most Cited Deep Learning Papers ...
- python3.4 build in functions from 官方文档 翻译中
2. Built-in Functions https://docs.python.org/3.4/library/functions.html?highlight=file The Python i ...
- ios异常(crash)输出
最近突然想起友盟的sdk附带的一个功能:将闪退异常情况上报服务器,(stackflow,github)找了一些资料,自己写了一个demo,想起来好久没有写过blog了,顺便分享. 其实不止是ios,a ...
- python3的文件操作
open的原型定义在bultin.py中,是一种内建函数,用于处理文件 open(file, mode='r', buffering=None, encoding=None, errors=None, ...
- python27读书笔记0.3
#-*- coding:utf-8 -*- ##D.has_key(k): A predicate that returns True if D has a key k.##D.items(): Re ...
- LOAD DATA INFILE Syntax--官方
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name' [REPLACE | IGNORE] INTO TABLE tbl_n ...
随机推荐
- P1365 WJMZBMR打osu! / Easy-洛谷luogu
传送门 题目背景 原 维护队列 参见P1903 题目描述 某一天WJMZBMR在打osu~~~但是他太弱逼了,有些地方完全靠运气:( 我们来简化一下这个游戏的规则 有nn次点击要做,成功了就是o,失败 ...
- 20175310 《Java程序设计》第6周学习总结
20175310 <Java程序设计>第6周学习总结 本周博客: <20175310 类定义 - 20175310xcy - 博客园 >https://www.cnblogs. ...
- Linux内核入门到放弃-进程管理和调度-《深入Linux内核架构》笔记
进程优先级 硬实时进程 软实时进程 普通进程 O(1)调度.完全公平调度器 抢占式多任务处理(preemptive multitasking):各个进程都分配到一定的时间段可以执行.时间段到期后,内核 ...
- Java线程安全与锁优化
线程安全的严谨定义: 当多个线程访问一个对象时,如果不用考虑这些线程在运行时环境下的调度和交题执行,也不需要进行额外的同步,或者调用方法进行其他任何操作,调用这个对象的行为都可以或者正确的结果,那么这 ...
- mysql的模糊匹配
实例: SQL模糊查询,使用like比较关键字,加上SQL里的通配符,请参考以下: 1.LIKE'Mc%' 将搜索以字母 Mc 开头的所有字符串(如 McBadden). 2.LIKE'%inger' ...
- 我们都被GitHub出卖了!逃跑吧兄弟!
周一突然间爆出微软以75亿收购GitHub可真是一颗重磅炸弹,一下轰动整个软件业.如果你不是搞开发的这篇文章几本不会引起你的共鸣:如果你没有用源代码管理这个消息也只不过是个新闻:如果你是微软系的朋友那 ...
- lambda函数
1.lambda函数是语法简短的匿名函数 2.lambda函数可以接受一个或多个参数 3.lambda函数只能有一个表达式 4.一般用于非重用的代码块 1)g = lambda x : x**2 g( ...
- CSS颜色代码 颜色值 颜色名字大全
颜色值 CSS 颜色使用组合了红绿蓝颜色值 (RGB) 的十六进制 (hex) 表示法进行定义.对光源进行设置的最低值可以是 0(十六进制 00).最高值是 255(十六进制 FF).从 0 到 25 ...
- C++实现算法常用的STL---整理
algorithm min(a,b)和max(a,b) #include<iostream> #include<algorithm> using namespace std; ...
- XManager&XShell如何保存登录用户和登录密码
Xshell配置ssh免密码登录 - qingfeng2556的博客 - CSDN博客https://blog.csdn.net/wuhenzhangxing/article/details/7948 ...