ocr roi of a scan in python

To OCR (Optical Character Recognition) ROI (Region of Interest) of a scan in Python, you can follow the below steps:

  1. Install required packages: pytesseract, opencv-python

    main.py
    !pip install pytesseract opencv-python
    
    39 chars
    2 lines
  2. Import the necessary modules:

    main.py
    import cv2
    import pytesseract
    
    30 chars
    3 lines
  3. Load the input image:

    main.py
    img = cv2.imread("input.jpg")
    
    30 chars
    2 lines
  4. Extract the ROI from the image:

    main.py
    x = 100
    y = 100
    w = 200
    h = 100
    roi = img[y:y + h, x:x + w]
    
    60 chars
    6 lines

    In the above code, x, y, w, and h are the position and size of the ROI.

  5. Apply OCR on the ROI:

    main.py
    text = pytesseract.image_to_string(roi)
    print(text)
    
    52 chars
    3 lines

This will print the recognized text from the ROI.

related categories

gistlibby LogSnag