-
Notifications
You must be signed in to change notification settings - Fork 55
Example of Aspose OCR #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nielsbosma
merged 4 commits into
Ivy-Interactive:main
from
adit-dhananjay:feat/aspose-ocr
Sep 29, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| bin/ | ||
| obj/ | ||
| *.sln.iml | ||
| .idea/**/* | ||
| **/.idea/* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| using Ivy.Aspose.OCR.Examples.Connections.OCR; | ||
| using System.IO; | ||
|
|
||
| namespace Ivy.Aspose.OCR.Examples.Apps; | ||
|
|
||
| [App(icon: Icons.FileImage, path: ["Apps"])] | ||
| public class ImageToTextApp : ViewBase | ||
| { | ||
| public override object? Build() | ||
| { | ||
| var outputText = this.UseState<string>(""); | ||
| var ocrService = UseService<IOCRService>(); | ||
|
|
||
| var error = UseState<string?>(() => null); | ||
| var files = UseState<FileInput?>(() => null); | ||
| var fileBytes = UseState<byte[]?>(() => null); // Store the actual file bytes | ||
|
|
||
| var uploadUrl = this.UseUpload( | ||
| uploadedBytes => | ||
| { | ||
| if (uploadedBytes.Length > 1 * 1024 * 1024) // 1MB limit | ||
| { | ||
| error.Set("File size must be less than 1MB"); | ||
| fileBytes.Set((byte[]?)null); // Clear stored bytes on error | ||
| return; | ||
| } | ||
|
|
||
| error.Set((string?)null); | ||
| fileBytes.Set(uploadedBytes); // Store the file bytes for later use | ||
| }, | ||
| "image/jpeg", | ||
| "uploaded-image" | ||
| ); | ||
|
|
||
| return Layout.Vertical( | ||
| Text.H1("Convert Image to text online").Color(Colors.Green), | ||
| Text.Block("Free OCR software to convert images or screenshots to text online"), | ||
| error.Value != null | ||
| ? new Callout(error.Value, variant: CalloutVariant.Error) | ||
| : null, | ||
| files.ToFileInput(uploadUrl, "Upload Image").Accept("image/*"), | ||
| new Button("Recognize", _ => | ||
| { | ||
| if (error.Value == null && fileBytes.Value != null) | ||
| { | ||
| // Use the stored file bytes instead of file.Content | ||
| using var ms = new MemoryStream(fileBytes.Value); | ||
| outputText.Value = ocrService.ExtractText(ms); | ||
| fileBytes.Set((byte[]?)null); // Clear stored bytes once completed | ||
| } | ||
| }), | ||
| Text.Block("Output Text:"), | ||
| new ObservableView<string>(outputText) | ||
| ).Align(Align.Center); | ||
| } | ||
| } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| using Aspose.OCR; | ||
|
|
||
| namespace Ivy.Aspose.OCR.Examples.Connections.OCR.Aspose; | ||
|
|
||
| public class AsposeOcrService: IOCRService | ||
| { | ||
| private readonly AsposeOcr recognitionEngine; | ||
|
|
||
| public AsposeOcrService() | ||
| { | ||
| recognitionEngine = new AsposeOcr(); | ||
| } | ||
|
|
||
| public string ExtractText(MemoryStream imageStream) | ||
| { | ||
| // Add image to the recognition batch | ||
| var source = new OcrInput(InputType.SingleImage); | ||
| source.Add(imageStream); | ||
|
|
||
| // Perform OCR | ||
| List<RecognitionResult> results | ||
| = recognitionEngine.Recognize(source); | ||
|
|
||
| // OCR processing | ||
| string result = string.Empty; | ||
| if (results.Count > 0) | ||
| { | ||
| result = results[0].RecognitionText; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace Ivy.Aspose.OCR.Examples.Connections.OCR; | ||
|
|
||
| public interface IOCRService | ||
| { | ||
| string ExtractText(MemoryStream imageStream); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| using Ivy.Aspose.OCR.Examples.Connections.OCR.Aspose; | ||
| using Ivy.Connections; | ||
|
|
||
| namespace Ivy.Aspose.OCR.Examples.Connections.OCR; | ||
|
|
||
| public class OCRConnection : IConnection | ||
| { | ||
| public string GetConnectionType() | ||
| { | ||
| return typeof(OCRConnection).ToString(); | ||
| } | ||
|
|
||
| public string GetContext(string connectionPath) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| public ConnectionEntity[] GetEntities() | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| public string GetName() => nameof(OCRConnection); | ||
|
|
||
| public string GetNamespace() => typeof(OCRConnection).Namespace; | ||
|
|
||
| public void RegisterServices(IServiceCollection services) | ||
| { | ||
| services.AddScoped<IOCRService, AsposeOcrService>(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| global using Ivy; | ||
| global using Ivy.Apps; | ||
| global using Ivy.Auth; | ||
| global using Ivy.Chrome; | ||
| global using Ivy.Client; | ||
| global using Ivy.Core; | ||
| global using Ivy.Core.Hooks; | ||
| global using Ivy.Helpers; | ||
| global using Ivy.Hooks; | ||
| global using Ivy.Shared; | ||
| global using Ivy.Services; | ||
| global using Ivy.Views; | ||
| global using Ivy.Views.Alerts; | ||
| global using Ivy.Views.Blades; | ||
| global using Ivy.Views.Builders; | ||
| global using Ivy.Views.Charts; | ||
| global using Ivy.Views.Dashboards; | ||
| global using Ivy.Views.Forms; | ||
| global using Ivy.Views.Tables; | ||
| global using Ivy.Widgets.Inputs; | ||
| global using Microsoft.Extensions.Configuration; | ||
| global using Microsoft.Extensions.DependencyInjection; | ||
| global using Microsoft.Extensions.Logging; | ||
| global using System.Collections.Immutable; | ||
| global using System.ComponentModel.DataAnnotations; | ||
| global using System.Globalization; | ||
| global using System.Reactive.Linq; | ||
|
|
||
| namespace Ivy.Aspose.OCR.Examples; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <NoWarn>CS8618;CS8603;CS8602;CS8604;CS9113</NoWarn> | ||
| <RootNamespace>Ivy.Aspose.OCR.Examples</RootNamespace> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <EmbeddedResource Include="Assets/**/*" /> | ||
| </ItemGroup> | ||
|
|
||
|
|
||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Aspose.OCR" Version="25.9.0" /> | ||
| <PackageReference Include="Ivy" Version="1.0.113.0" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 17 | ||
| VisualStudioVersion = 17.14.36429.23 d17.14 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ivy.Aspose.OCR.Examples", "Ivy.Aspose.OCR.Examples.csproj", "{BA2A13B0-78C9-203B-1462-034AD42E4D37}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {BA2A13B0-78C9-203B-1462-034AD42E4D37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {BA2A13B0-78C9-203B-1462-034AD42E4D37}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {BA2A13B0-78C9-203B-1462-034AD42E4D37}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {BA2A13B0-78C9-203B-1462-034AD42E4D37}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {24B5F0F2-14B2-4944-9817-4606E84F19E2} | ||
| EndGlobalSection | ||
| EndGlobal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
|
|
||
| using Ivy.Aspose.OCR.Examples.Apps; | ||
|
|
||
| CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US"); | ||
| var server = new Server(); | ||
| #if DEBUG | ||
| server.UseHotReload(); | ||
| #endif | ||
| server.AddAppsFromAssembly(); | ||
| server.AddConnectionsFromAssembly(); | ||
| var chromeSettings = new ChromeSettings() | ||
| .DefaultApp<ImageToTextApp>() | ||
| .UseTabs(preventDuplicates: true); | ||
| server.UseChrome(chromeSettings); | ||
| await server.RunAsync(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Ivy.Aspose.OCR.Examples | ||
|
|
||
| Web application created using [Ivy](https://github.com/Ivy-Interactive/Ivy-Framework). | ||
|
|
||
| Ivy is a web framework for building interactive web applications using C# and .NET. | ||
|
|
||
| ## How it works | ||
|
|
||
| This web application demonstrates OCR (Optical Character Recognition) using Ivy and Aspose.OCR. Users can upload an image (JPEG, up to 1MB), and the app extracts text from the image using an OCR service. The workflow is: | ||
|
|
||
| 1. Upload an image using the provided form. | ||
| 2. The app validates the file size and type. | ||
| 3. On clicking "Recognize", the image is processed and text is extracted. | ||
| 4. The recognized text is displayed on the page. | ||
|
|
||
| ## Run | ||
|
|
||
| ``` | ||
| dotnet watch | ||
| ``` | ||
|
|
||
| ## Output Screenshot | ||
|
|
||
|  |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.