Managing downloadable resources

By default, Aspose.OCR for .NET automatically downloads the required resources when you call the method which depends on them.

Out of the box, the library is preconfigured to use the Aspose.OCR resource repository. The resources will be downloaded into the aspose_data folder in the application’s working directory; if the folder is missing, it is automatically created. No configuration is required.

However, you have an option to manually manage the resources for your project using the static methods of Aspose.OCR.Resources class.

Setting up the repository

To return the URL of the online repository from which Aspose.OCR for .NET resources are downloaded, use Aspose.OCR.Resources.GetRepository() method. By default, the resources are downloaded from https://github.com/aspose-ocr/resources/; however, you can use your own repository or even Internet or intranet web site:

  1. Copy the entire content of the main branch: https://github.com/aspose-ocr/resources/tree/main. You can use any of the following options:

    • Using Git command: git clone git@github.com:aspose-ocr/resources.git
    • Download and unpack the ZIP archive.
  2. Publish all downloaded files to your own HTTP resource. The resource must be must be freely accessible to the application without providing credentials; Aspose.OCR for .NET does not support authentication. You may use:

    • Public GitHub repository.
    • Private GitLab.
    • A folder on a web site.
  3. Provide a link to Aspose.OCR.Resources.SetRepository() method. Depending on the resource, use the following links:

    • A link to the root directory of the branch in a public GitHub repository in form https://github.com/{project}/{repository}/blob/{branch}/. For example, Aspose.OCR.Resources.SetRepository("https://github.com/aspose-ocr/resources/blob/main");.
    • A link to GitLab raw files in form https://{domain}/{project}/{repository}/-/raw/{branch}/. For example, Aspose.OCR.Resources.SetRepository("https://aspose.com/ocr/resources/-/raw/master/");.
    • A link to the directory on a web site (either intranet or public). For example Aspose.OCR.Resources.SetRepository("http://localhost/aspose-ocr-resources/");.

Specifying the local folder

To specify an absolute or relative path to the folder where the resources will be downloaded, use Aspose.OCR.Resources.SetLocalPath() method. Pass false to the create parameter to prevent the directory from being created automatically. For example: Aspose.OCR.Resources.SetLocalPath("aspose/ocr", false);

To get the full path where the resources will be downloaded, use Aspose.OCR.Resources.GetLocalPath() method. By default, the resources are downloaded into the aspose_data folder in the application’s working directory.

Downloading the resources

To get the full list of compatible resources from the online repository, use Aspose.OCR.Resources.ListRemote() method. To find more information on a specific resource, refer to the following link.

There are several ways to download the resources:

Option Method Benefits Drawbacks
Automatically download the resource when you call the method which depends on it. n/a
  • Default behavior which requires no coding.
  • Increased processing time when you first call a method that depends on the downloaded resource.
Download all resources at once. Aspose.OCR.Resources.FetchAll()
  • No further delays during the application’s lifetime.
  • Downloading all resource files may take a long time.
  • Unnecessary (unused) resources will consume network traffic and disk space.
Download a specific resource by name. Aspose.OCR.Resources.FetchResource()
  • No further delays during the application’s lifetime.
  • You only download and store what is necessary.
  • You have to manually maintain the required resources.
Bulk download of resources by names. Aspose.OCR.Resources.FetchResources()
  • No further delays during the application’s lifetime.
  • You only download and store what is necessary.
  • You have to manually maintain the required resources.

To prevent the resources from being automatically downloaded, use Aspose.OCR.Resources.AllowAutomaticDownloads(false) method. This will not affect the manual downloads.

Managing the resources

To get the list of Aspose.OCR resources stored in the local folder, use Aspose.OCR.Resources.ListLocal() method. The resource names are returned as a string array.

To delete a locally stored resource, use Aspose.OCR.Resources.RemoveLocal() method or simply remove the corresponding file.

Example

The code sample below illustrates how to manually install Cyrillic recognition model:

// Download Cyrillic OCR model to "aspose/ocr" directory in the application working directory
Aspose.OCR.Resources.SetLocalPath("aspose/ocr");
Aspose.OCR.Resources.FetchResource("aspose-ocr-cyrillic-v1");
// Initialize Aspose.OCR for .NET recognition API
Aspose.OCR.AsposeOcr recognitionEngine = new Aspose.OCR.AsposeOcr();
// Add images to OcrInput object
Aspose.OCR.OcrInput input = new Aspose.OCR.OcrInput(Aspose.OCR.InputType.SingleImage);
input.Add("source1.png");
input.Add("source2.jpg");
// Set recognition language
Aspose.OCR.RecognitionSettings recognitionSettings = new Aspose.OCR.RecognitionSettings();
recognitionSettings.Language = Aspose.OCR.Language.Ukr;
// Recognize image
List<Aspose.OCR.RecognitionResult> results = recognitionEngine.Recognize(input, recognitionSettings);
foreach(Aspose.OCR.RecognitionResult result in results)
{
    Console.WriteLine(result.RecognitionText);
}