Issue
I tried to detect text from an image where I draw bounding boxes around select characters and stitch them together to form another image as below :
I used cv2 to draw bounding boxes around the characters using the following code :
cnts = cv2.findContours(inverted, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
(cnts, bounding_boxes) = sort_contours(cnts)
ROI_number = 0
for c in cnts:
x, y, w, h = cv2.boundingRect(c)
ROI = inverted[y:y + h, x:x + w]
# ROI = cv2.erode(ROI, kernel, iterations=1)
ROI = cv2.filter2D(ROI, -1, sharpen_kernel)
# ROI = cv2.GaussianBlur(ROI, (5, 5), 0)
# ROI = cv2.filter2D(ROI, -100, sharpen_kernel)
ROI = cv2.bitwise_not(ROI)
ht, wd = ROI.shape
ww = 26
hh = 30
result = np.full((hh, ww), 255, dtype=np.uint8)
xx = (ww - wd) // 2
yy = (hh - ht) // 2
result[yy:yy + ht, xx:xx + wd] = ROI
cv2.imwrite('ROI_{}.jpeg'.format(ROI_number), result)
cv2.rectangle(inverted, (x, y), (x + w, y + h), (36, 255, 12), 0)
ROI_number += 1
I have used hstack from numpy to stitch the image together using the following code:
def stitch_images(input_path):
imagePaths = []
for image_path in glob.glob(os.path.join(input_path, '*.jpeg')):
imagePaths.append(image_path)
sorted_paths = sorted(imagePaths)
list_im = [sorted_paths[0], sorted_paths[1], sorted_paths[2], sorted_paths[3], sorted_paths[4], sorted_paths[5]]
imgs = [Image.open(i) for i in list_im]
min_shape = sorted([(np.sum(i.size), i.size) for i in imgs])[0][1]
imgs_comb = np.hstack((np.asarray(i.resize(min_shape)) for i in imgs))
imgs_comb = Image.fromarray(imgs_comb)
imgs_comb.save('stitched.jpeg')
The stitched image however cannot be read using tesseract and gives the following error:
Tesseract Open Source OCR Engine v4.1.1-rc2-21-gf4ef with Leptonica
Warning: Invalid resolution 0 dpi. Using 70 instead.
Estimating resolution as 334
Empty page!!
Estimating resolution as 334
Empty page!
Solution
Loading the image, converting to grayscale, and using image_to_string
is working for me. Result from pytesseract:
418081
Code
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Perfrom OCR with Pytesseract
data = pytesseract.image_to_string(gray, lang='eng', config='--psm 6')
print(data)
------------------
System information
------------------
Python: 3.7.4
NumPy: 1.14.5
OpenCV: 4.1.0
------------------
Answered By - nathancy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.