Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
OcrService.cs
1using System.Net.Http.Headers;
2using IdApp.Cv;
3using Waher.Content;
5
7{
11 [Singleton]
12 public class OcrService : IOcrService
13 {
17 public OcrService()
18 : base()
19 {
20 }
21
29 public async Task<string[]> ProcessImage(IMatrix Image, string Language, PageSegmentationMode? PageSegmentationMode)
30 {
31 byte[] Png = Bitmaps.EncodeAsPng(Image);
32 string? Token = await ServiceRef.XmppService.GetApiToken(60);
33
34 Uri Uri = new("https://" + ServiceRef.TagProfile.Domain + "/Tesseract/Api");
35 using HttpClient HttpClient = new();
36 using HttpRequestMessage Request = new()
37 {
38 RequestUri = Uri,
39 Method = HttpMethod.Post,
40 Content = new ByteArrayContent(Png)
41 };
42
43 Request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("image/png");
44 Request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
45
46 if (!string.IsNullOrEmpty(Language))
47 Request.Headers.Add("X-LANGUAGE", Language);
48
49 if (PageSegmentationMode.HasValue)
50 Request.Headers.Add("X-PSM", PageSegmentationMode.Value.ToString());
51
52 HttpResponseMessage Response = await HttpClient.SendAsync(Request);
53 Response.EnsureSuccessStatusCode();
54
55 byte[] Bin = await Response.Content.ReadAsByteArrayAsync();
56 string? ContentType = Response.Content.Headers.ContentType?.ToString();
57 object Obj = await InternetContent.DecodeAsync(ContentType, Bin, Uri);
58
59 if (Obj is not string Text)
60 throw new Exception("Unexpected response.");
61
62 if (string.IsNullOrEmpty(Text))
63 return [];
64 else
65 return Text.Split(CommonTypes.CRLF, StringSplitOptions.RemoveEmptyEntries);
66 }
67
68 }
69}
Static methods managing conversion to and from bitmap representations.
Definition: Bitmaps.cs:12
static byte[] EncodeAsPng(IMatrix M)
Encodes an image in a matrix using PNG.
Definition: Bitmaps.cs:318
Optical Character Recognition (OCR) Service.
Definition: OcrService.cs:13
async Task< string[]> ProcessImage(IMatrix Image, string Language, PageSegmentationMode? PageSegmentationMode)
Processes an image and tries to extract strings of characters from it.
Definition: OcrService.cs:29
OcrService()
Optical Character Recognition (OCR) Service.
Definition: OcrService.cs:17
Base class that references services in the app.
Definition: ServiceRef.cs:31
static IXmppService XmppService
The XMPP service for XMPP communication.
Definition: ServiceRef.cs:67
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static readonly char[] CRLF
Contains the CR LF character sequence.
Definition: CommonTypes.cs:17
Static class managing encoding and decoding of internet content.
static Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
Interface for matrices.
Definition: IMatrix.cs:9
Interface for the Optical Character Recognition (OCR) Service.
Definition: IOcrService.cs:11
Definition: Abs.cs:2
PageSegmentationMode
Page Segmentation Mode