-
-
Save akash-ch2812/ffcf6ce7e1e6137f323689157b233151 to your computer and use it in GitHub Desktop.
import pytesseract | |
pytesseract.pytesseract.tesseract_cmd = r'C:\Users\Akash.Chauhan1\AppData\Local\Tesseract-OCR\tesseract.exe' | |
# load the original image | |
image = cv2.imread('Original_Image.jpg') | |
# get co-ordinates to crop the image | |
c = line_items_coordinates[1] | |
# cropping image img = image[y0:y1, x0:x1] | |
img = image[c[0][1]:c[1][1], c[0][0]:c[1][0]] | |
plt.figure(figsize=(10,10)) | |
plt.imshow(img) | |
# convert the image to black and white for better OCR | |
ret,thresh1 = cv2.threshold(img,120,255,cv2.THRESH_BINARY) | |
# pytesseract image to string to get results | |
text = str(pytesseract.image_to_string(thresh1, config='--psm 6')) | |
print(text) |
@OmkarPatkar Thanks for your comment. How did you fix it ?
@Apache2021 I have not found out any workaround yet, but i have modified the code a bit to make it more user friendly. you can check it out here. please let me know if you fix the error.
I am running to trouble with understand how you know which cropped parts are what?
I am returning an error:
img = image[c[0][1]:c[1][1], c[0][0]:c[1][0]]
Type Error: 'bool' object is not subscriptable
I am not sure if this is because the cropped portion is not good enough quality for OCR to decipher or if there are problems cropping. Any advice?
Personally I tried running this code with the marks done with the Marking_ROI.py function but it only gave me the first section detected. This was because I was only giving one set of coordinates while multiple are needed to get the full text of the document cropped.
-> img = image[c[0][1]:c[1][1], c[0][0]:c[1][0]]
I fixed this by doing a for loop which instead of accesing the first value of the coordinates list, it actually access to the full list and then acces to item by item and finally coordinate by coordinate
Hi everyone,
This is my very first post in GitHub, so apologies if what I am going to share is not pertinent or does not belong to this place.
I had problems using the script, as it only extracted the very last line of the document, even though there were several lines marked. Therefore, I started browsing, and found a solution proposed in stackoverflow that, at least, worked for me. I don't understand why this worked and the previous one didn't, to be honest, as I am quite new in this field. I hope any of you can find it out, and that this code may help anyone with the same problem.
Of course, credits to Nathancy as the original solver of the problem: https://stackoverflow.com/users/11162165/nathancy
https://stackoverflow.com/questions/58824094/what-causes-pytesseract-to-read-either-the-top-or-bottom-text-line-of-a-dual-lin
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
data = pytesseract.image_to_string(thresh, lang='eng',config='--psm 6')
print(data)
cv2.imshow('thresh', thresh)
cv2.waitKey()
@Apache2021 I was facing the same error on line num 10, i think (lin_items_coordinates) it is not getting the right value when we try to crop it.