import pytesseract
from PIL import Image, ImageEnhance, ImageFilter
pytesseract.pytesseract.tesseract_cmd="C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe"
im = Image.open("C:\\1.png") # the second one
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1')
im.save('temp2.png')
#im.show()
text = pytesseract.image_to_string(Image.open('temp2.png'),config='-psm 8')
print(text)
Hi to all
I'm trying to extract text from image(captcha) so it's the code above i'am coding so far.
I don't think there is any problem so far since there is any error when i run it. but output is so poor.
when i run this it shows nothing but i change -psm 8 to -psm 5, it shows ';«'.
Would you give me some advice to fix it?
It's done.
I changed other pic for test and there was no problem at least for this pic.
but i think this module is too poor.. it will be way better to find other module..
Related
i am newbie to python. I am trying to create a Python Program to image dehazing using dcp. I have an image that need to view at console at first and need to do some dehazing method. unfortunately, here i unable to upload or view the image and it saying Image data cannot be converted to float. I am getting the following error when I try running it.
import cv2
import math
import numpy as np
import matplotlib.pyplot as plt
def DarkChannel(im,sz):
b,g,r = cv2.split(img)
dc = cv2.min(cv2.min(r,g),b)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (sz,sz))
dark = cv2.erode(dc,kernel)
return dark
img = cv2.imread("C:/Users/User/Documents/sypder/img/bird.jpg", 1)
plt.imshow(img)
It seems your file path is wrong since your sample code worked perfectly for me. If you are struggling with file paths you can pass it as a raw string.
img = cv2.imread(r"C:\Users\User\Documents\sypder\img\bird.jpg", 1)
If you fix it like this, it should work. I copied the object name from properties while doing it.
try to read this post :
https://www.pythonfixing.com/2021/10/fixed-typeerror-image-data-can-not.html
it's jupiter based article but you can change them accordingly
I'm a newbie too that come across your question but those might be a help
goodluck!
I am using python3.6 and Tesseract-OCR on my mac. I have pictures containing the text which is clearly readable. However, despite that it is super clear to the human eyes, the Tesseract can't extract them correctly. The attached one is the extreme case that nothing is returned
Below is the snapshot of the code I am using
import cv2
import pytesseract
img = cv2.imread('frame40.jpg')
img = cv2.resize(img, (600, 450))
text = pytesseract.image_to_string(img)
print(text)
What am I missing here?
I have some sample images. How to extract tabular data from images and store it into JSON format?
Use pytesseract. The code will be something like this.
You can try different modifications .
My code may not solve the whole problem .It is just an example code ,this will work for text in black but for blue and any other colour you will have to create a mask accordingly and then extract that data.
import pytesseract
from PIL import Image, ImageEnhance, ImageFilter
im = Image.open("temp.jpg")
maxsize = (2024, 2024)
im=im.thumbnail(maxsize, PIL.Image.ANTIALIAS)
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1')
im.save('mod_file.jpg')
text = pytesseract.image_to_string(Image.open('mod_file.jpg'))
print(text)
For example for red colour detection you can refer to this post.
After getting the red text binarize the image and then run
text = pytesseract.image_to_string(Image.open('red_text_file.jpg'))
Similerly you will have to do the same process for blue and so on.
I believe you can easily try to do it yorself, just play around with some values.
The following code does not display the image lists.jpg (in current dir):
print(dir(Image)) displays components; im.size, im.filename, im.format all return correct values.
What have I not done to display this jpg file?
from PIL import Image
im = Image.open("lists.jpg")
im.show() # did not work - perhaps due to the environment Jupyter Notebooks
Solution: replaced module with another with immediate results.
from IPython.display import Image
Image(filename='lists.jpg')
I know it is quite late to post but I will do it for new readers.
This problem arises in case of Jupyter Notebooks. Using show() does not display the image. So discard calling show() like in the code below. This will display the image in the output of the cell.
from PIL import Image
im = Image.open("lists.jpg")
im
To display the image on screen:
from PIL import Image
im = Image.open("lists.jpg")
im.show()
See also http://pillow.readthedocs.io/en/4.0.x/reference/Image.html
I am trying to detect bangla character from image using python, so i decided to use pytesseract. For this purpose i have used below code:
import pytesseract
from PIL import Image, ImageEnhance, ImageFilter
im = Image.open("input.png") # the second one
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1')
im.save('temp2.png')
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
text = pytesseract.image_to_string(Image.open('temp2.png'),lang="ben")
print text
The problem is that if i gave a image of english character is detects. But when i am writing lang="ben" and detecting from image of bengali characters my code is running for endless time or like forever.
P.S: I have downloaded bengali language train data to tessdata folder and i am trying to run it in PyCharm.
Can anyone help me to solve this problem?
sample of input.png
I added Bangla(india) language to Windows. Downloaded ben.traineddata to TESSDATA_PREFIX which equals to C:\Program Files\Tesseract 4.0.0\tessdata in my PC. Then run,
> tesseract -l ben bangla.jpg bangla_out
in command prompt and got the result below in 2 seconds. The result looks fine even I don't understand the language.
Have you tried to run tesseract in command prompt to verify if it works for -l ben?
EDIT:
Used Spyder, similar to PyCharm, which comes with Anaconda to test
it. Modified your code to call Tesseract as below.
pytesseract.pytesseract.tesseract_cmd = "C:/Program Files/Tesseract 4.0.0/tesseract.exe"
Test Code in Spyder:
import pytesseract
from PIL import Image, ImageEnhance, ImageFilter
import os
im = Image.open("bangla.jpg") # the second one
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1')
im.save("bangla_pp.jpg")
pytesseract.pytesseract.tesseract_cmd = "C:/Program Files/Tesseract 4.0.0/tesseract.exe"
text = pytesseract.image_to_string(Image.open("bangla_pp.jpg"),lang="ben")
print text
It works and produced result below on the processed image. Apparently, the OCR result of the processed image is not as good as the original one.
Result from the processed bangla_pp.jpg:
প্রত্যাবর্তনকারীরা
তাঁদের দেশে গিয়ে
-~~-<~~~~--
প্রত্যাবর্তন-পরবর্তী
আর্থিক সহায়তা
= পাবেন তার
Result from original image, directly feed to Tesseract.
Code:
from PIL import Image
import pytesseract as tess
print tess.image_to_string(Image.open('bangla.jpg'), lang='ben')
Output:
প্রত্যাবর্তনকারীরা
তাঁদের দেশে গিয়ে
প্রত্যাবর্তন-পরবর্তী
আর্থিক সহায়তা
পাবেন তার
I have installed some fonts in windows from here
https://www.omicronlab.com/bangla-fonts.html
After that, it worked perfectly fine for me in Pycharm.