This is a simple C# client for interacting with a gRPC OCR service. The client demonstrates how to call the OCR, health check, and documentation endpoints provided by the service.
- .NET 7.0 SDK or newer.
- A running instance of the gRPC OCR service / Docker (adjust
serverAddressif necessary)
-
Create a New Console Application
dotnet new console -o OcrClientApp cd OcrClientApp -
Add gRPC and Protobuf NuGet Packages
dotnet add package Grpc.Net.Client dotnet add package Google.Protobuf dotnet add package Grpc.Tools
-
Add the
OcrClient.csFileCreate a new file called
OcrClient.csand add the following content:using System; using System.IO; using System.Threading.Tasks; using Google.Protobuf; using Grpc.Net.Client; using OcrGrpcWrapper; class Program { static async Task Main(string[] args) { // The address of the gRPC server string serverAddress = "http://localhost:5000"; // Path of the input image file string imagePath = "example.png"; // Read image file as byte array byte[] imageBytes = await File.ReadAllBytesAsync(imagePath); // Create a channel and a client using var channel = GrpcChannel.ForAddress(serverAddress); var client = new OcrService.OcrServiceClient(channel); // Create an OcrRequest message var request = new OcrRequest { InputFile = ByteString.CopyFrom(imageBytes), Language = "eng", OutputFormat = "json", DetectAreasMode = "document", AutoDeskew = true, Rotate = 0, UpscaleSmallFont = true, AutoContrast = true, AutoDenoise = true }; // Call the Process method OcrResponse response = await client.ProcessAsync(request); // Display the result Console.WriteLine("OCR Result:"); Console.WriteLine(response.Output); // Call the HealthCheck method var healthCheckRequest = new EchoRequest { Message = "Are you there?" }; OcrResponse healthCheckResponse = await client.HealthCheckAsync(healthCheckRequest); // Display the health check response Console.WriteLine("Health Check Response:"); Console.WriteLine(healthCheckResponse.Output); // Call the Documentation method OcrResponse documentationResponse = await client.DocumentationAsync(new Empty()); // Display the documentation response Console.WriteLine("Documentation Response:"); Console.WriteLine(documentationResponse.Output); } }
-
Add the Proto File
Create a new directory called
Protosand add yourocr_service.protofile in it. Ensure it is included in your project file as follows:<ItemGroup> <Protobuf Include="Protos\ocr_service.proto" GrpcServices="Client" /> </ItemGroup>
-
Run the Application
Ensure that you have a valid image file named
example.pngin the project directory.dotnet run
Your console application should print the results of the OCR processing along with the health check and documentation responses.
- Adjust the
serverAddressin the code if your gRPC server is running on a different host or port. - The
input_filemust be a valid image file supported by the OCR service (PNG, JPEG, BMP, or TIFF).