Managing downloadable resources

By default, Aspose.OCR for Python via .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 methods of Resources class.

Setting up the repository

To return the URL of the online repository from which Aspose.OCR for Python via .NET resources are downloaded, use Resources.get_repository() 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 Python via .NET does not support authentication. You may use:

    • Public GitHub repository.
    • Private GitLab.
    • A folder on a web site.
  3. Provide a link to Resources.set_repository() 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, Resources.set_repository("https://github.com/aspose-ocr/resources/blob/main").
    • A link to GitLab raw files in form https://{domain}/{project}/{repository}/-/raw/{branch}/. For example, Resources.set_repository("https://aspose.com/ocr/resources/-/raw/master/").
    • A link to the directory on a web site (either intranet or public). For example Resources.set_repository("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 Resources.set_local_path() method. Pass false to the create parameter to prevent the directory from being created automatically. For example: Resources.set_local_path("aspose/ocr", false)

To get the full path where the resources will be downloaded, use Resources.get_local_path() 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 Resources.list_remote() 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. Resources.fetch_all()
  • 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. Resources.fetch_resource()
  • 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. Resources.fetch_resources()
  • 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 Resources.allow_automatic_downloads(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 Resources.list_local() method. The resource names are returned as a string array.

To delete a locally stored resource, use Resources.remove_local() 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
Resources.set_local_path("aspose/ocr")
Resources.fetch_resource("aspose-ocr-cyrillic-v1");
# Instantiate Aspose.OCR API
api = AsposeOcr()
# Add image to the recognition batch
input = OcrInput(InputType.SINGLE_IMAGE)
input.add("source.png")
# Recognize Ukrainian text
recognitionSettings = RecognitionSettings()
recognitionSettings.language = Language.UKR
# Recognize the image
result = api.recognize(input, recognitionSettings)
# Print recognition result
print(result[0].recognition_text)
input("Press Enter to continue...")