OCR

FREEMIUM
By API 4 AI | Updated לפני חודש | Visual Recognition
Popularity

9.6 / 10

Latency

1,846ms

Service Level

100%

Health Check

100%

Back to All Tutorials (8)

How to recognize a medicine name with OCR API

There is an extensive variety of pharmaceutical drugs available. Some medications are unique, while others may have numerous alternatives. These alternatives can differ in cost, dosage, and other factors. Furthermore, the same medicinal product may have varying prices across different retail networks.

Today, we are pleased to introduce an OCR-based tutorial that enables automated extraction of the name of a pharmaceutical drug from a photograph of its packaging. Subsequently, this drug name can be utilized for online searches to find equivalents, access instructions, and locate the best pricing offers.

To implement this technology, one can easily utilize the OCR API, available at https://api4.ai/apis/ocr.
By sending a request to the OCR API and appropriately handling the response, seamlessly integrating medicine name detection into your systems or products becomes remarkably simple, allowing you to access its numerous benefits.

Python implementation

Searching for a name of a medicine

Send a request with algo=simple-words query parameter to get bounding boxes for each word.
Usually, words with the biggest font are a name.

import argparse
import sys
from pathlib import Path

import requests
from requests.adapters import Retry, HTTPAdapter

API_URL = 'https://ocr43.p.rapidapi.com'


def get_medicine_name(photo_path: Path, api_key: str):
    """Get a VIN from a photo."""
    # We strongly recommend you use exponential backoff.
    error_statuses = (408, 409, 429, 500, 502, 503, 504)
    s = requests.Session()
    retries = Retry(backoff_factor=1.5, status_forcelist=error_statuses)

    s.mount('https://', HTTPAdapter(max_retries=retries))

    url = f'{API_URL}/v1/results?algo=simple-words'
    with photo_path.open('rb') as f:
        api_res = s.post(url, files={'image': f},
                         headers={'X-RapidAPI-Key': api_key}, timeout=20)
    api_res_json = api_res.json()

    # Handle processing failure.
    if (api_res.status_code != 200 or
            api_res_json['results'][0]['status']['code'] == 'failure'):
        print('Image processing failed.')
        sys.exit(1)

    objs = api_res_json['results'][0]['entities'][0]['objects']

    def close_equal(num1, num2, eps):
        return abs(num1-num2) <= eps

    # Usually, the line with the biggest font is a name of a medicine.
    biggest_obj = max(objs, key=lambda obj: obj['box'][3])
    # There are two approaches to find medicine name.
    # The first is to search words on the same line. The second is to search words with the same font (for example if medicine name is written on few lines like calcium carbonate.)
    # name_objs = [obj for obj in objs if close_equal(obj['box'][1], biggest_obj['box'][1], 0.03)]  # find other words on the same line.
    name_objs = [obj for obj in objs if close_equal(obj['box'][3], biggest_obj['box'][3], 0.01)]  # find other words with the same font.
    return ' '.join([obj['entities'][0]['text'] for obj in name_objs])

Parse command-line arguments

RAPID API key and path to a photo of a medicine are passed as command-line arguments.

def parse_args():
    """Parse command line arguments."""
    parser = argparse.ArgumentParser()
    parser.add_argument('--api-key', help='Rapid API token.', required=True)  # Get your token at https://rapidapi.com/api4ai-api4ai-default/api/ocr43/pricing
    parser.add_argument('photo', type=Path,
                        help='Path to a photo of a medicine.')
    return parser.parse_args()

Main function

In main function all code written before is put together.

def main():
    """
    Script entry point.

    Write recognized VINs in folder to a csv file.
    """
    args = parse_args()
    name = get_medicine_name(args.photo, args.api_key)
    print(name)

Test the script

Let’s run the script with the image from beginning: python3 main.py --api-key "YOUR_API_KEY" Atorvastatin.jpg.
As the result, the script will return name of the medicine.

Conclusion

In this tutorial, we’ve demonstrated a practical way to use the OCR API for recognizing the names of medicine packages from their photos. This method helps businesses in the pharmaceutical field streamline their operations and ensure accurate product identification. By following these steps, you can easily integrate OCR technology to improve efficiency and accuracy in your pharmaceutical processes.

For users seeking a more advanced medicine name recognition solution beyond the capabilities of the aforementioned code, we extend an invitation to collaborate with us.
By visiting our official website at https://api4.ai/get-started, you can reach out to us directly.
We are committed to tailoring solutions that cater specifically to your unique requirements, ensuring seamless integration and optimal outcomes.
Our team stands ready to engage in constructive consultations and deliver bespoke solutions tailored to your specific needs.