How do I prepare images for OCR using OpenCV


My task is to read promo codes from bottle caps and boxes, the lids have different colors, and the promo code is printed differently. I want to prepare text using OpenCV on Android and read it using tesseract, please tell me if it is possible to dynamically process different images using OpenCV (examples below)? And how can I align the text from the bottle caps to a single line, so that, for example, the Tesseract can read it qualitatively? Thanks.

img1 img2 upd:

Hsv_counter

#!/usr/bin/env python

import cv2 as cv
import numpy as np

hsv_min = np.array((0, 77, 17), np.uint8)
hsv_max = np.array((208, 255, 255), np.uint8)

if __name__ == '__main__':
    fn = 'test.jpg'
    img = cv.imread(fn)
    img = cv.resize(img, (0, 0), fx=0.5, fy=0.5)

    hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
    thresh = cv.inRange(hsv, hsv_min, hsv_max)
    contours0, hierarchy = cv.findContours(thresh.copy(), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
    for cnt in contours0:
        if len(cnt) > 4:
            ellipse = cv.fitEllipse(cnt)
            cv.ellipse(img, ellipse, (0, 0, 255), 2)

    cv.imwrite("counter_cap.jpg", img)
    cv.imshow('contours', img)

    cv.waitKey()
    cv.destroyAllWindows()

Broken_text

import cv2
import numpy as np
import pytesseract
from PIL import Image
from pytesseract import image_to_string

img = cv2.imread("paper.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
src_gray = cv2.blur(gray, (3, 3))
canny_output = cv2.Canny(src_gray, 100, 100 * 2)

ret, thresh1 = cv2.threshold(canny_output, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
k = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], np.uint8)
closing = cv2.morphologyEx(thresh1, cv2.MORPH_CLOSE, k)
closing = cv2.adaptiveThreshold(closing, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 3, 1)

k1 = np.ones((4, 4), np.uint8)
erosion = cv2.erode(closing, k1, iterations=2)
small = cv2.resize(erosion, (0, 0), fx=0.5, fy=0.5)

# Write image
cv2.imwrite("erosion.jpg", small)

# Path
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

# Recognize text with tesseract for python
result = pytesseract.image_to_string(Image.open("erosion.jpg")).replace(" ", "")
print("--------- TADAM ITS-------------")
print(result)

# Show image
cv2.imshow("erosion", small)
cv2.waitKey(0)
Author: Jack, 2019-08-18

1 answers

The quality of your images is very poor.

I managed to prepare only the code from a piece of paper well.
Here's what I got:

Sample code

In order to get this image I used OpenCV and took the mask by black color.
Code in python-3. x:

import cv2
import numpy as np


def get_mask(img, lower=np.array([0, 0, 0]), upper=np.array([255, 255, 255])):
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    return cv2.inRange(hsv, lower, upper)


def main():
    img = cv2.imread('photo2.jpg')
    crop_img = img[320:400, 350:930]
    mask_img = get_mask(crop_img, lower=np.array([0, 0, 130]))
    cv2.imwrite('mask.png', mask_img)


if __name__ == '__main__':
    main()
 1
Author: nomnoms12, 2019-08-18 12:01:28