Can't create OCR using this path - python

I am trying to use pytesseract for OCR:
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd='C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
img=cv2.imread('numbers.png')
after running the code I get this message:
Unable to create process using 'C:\Users\Mostafa\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\python.exe C:/Users/Mostafa/PycharmProjects/pythonProject/main.py'
These are the installed packages:

The problem might be with 'Program Files' because since there is a space in the middle that makes the program unhappy. Inserting a double quotes should be enough to make the program understand it should ignore that space in the middle. I Once had a problem with that and adding double quotes worked like a charm.
"\Program Files"\

Related

Image moves or deletes when displaying with PIL

Recently, I have been working on one of my python3 programs and then I wanted to to open a picture. Here is the code that I used to do it:
from PIL import Image
r = Image.open('C:/Users/sudam/OneDrive/Desktop/programming/python/projects/good night app/morning.png' )
r.show()
But as soon as I run this code,the windows photo viewer opens and gives and error saying that the specified file was moved. I tried googling this question but all of the answers I got only worked for python2, but not for python3.
Because you have whitespace in your path you need to use the r"string" format.
Also you need to use:
PIL.ImageShow.show(r)
to show your image.
It's also recommended to check if the file exist before opening any file.
You can do like this:
from pathlib import Path
from PIL import Image, ImageShow
path =r"C:/Users/sudam/OneDrive/Desktop/programming/python/projects/good night app/morning.png"
if Path(path).is_file():
r = Image.open(path)
ImageShow.show(r)
else:
print(f'{path} not exist')

Failed to read images when folder has special characters on name

Basically I'm using locateOnScreen() function, which is from pyautogui to read an image and then find in the screen by:
import os
import pathlib
import pyautogui
Image = os.path.join(os.path.sep, pathlib.Path(__file__).parent.resolve(), 'static', 'img', 'game', 'image-btn.png')
if pyautogui.locateOnScreen(BossImg, grayscale=True, confidence=0.95) != None:
print(True)
The code above works prety fine, the problem is when some users, even me because my native language is Portuguese and we have special characters in the language, and we might have some in a folder name.
Let's use this example:
In english:
C:\Users\guilh\Desktop\Folder
In Portuguese:
C:\Users\guilh\Área de Trabalho\Folder
So for some cases when we get a folder with accented characters, I'm getting the error:
Failed to read C:\Users\guilh\Área de Trabalho\Folder\image-btn.png because file is missing, has improper permissions, or is an unsupported or invalid format
But why am I gettig this error with special characters if I'm passing the path correctly with pathlib and os? If I run the same script in the English example, works perfectly.
After digging a bit in the source code of PyAutoGUI on Github, it appears that PyScreeze is used to detect an element on the screen from an image of it, and it uses openCV's imread() function to load the image.
cv2.imread() currently does not support pathnames containing Non-ASCII characters on Windows.
A pull-request has already been opened on the PyScreeze repository to use cv2.imdecode() instead of cv2.imread().
To fix this issue while waiting for the support for non-ASCII characters,
Method 1
The first option would be to modify the PyScreeze package installed (This can be annoying if anyone needs to be able to run the script easily from their computer).
- Identify the location of the PyScreeze module:
python -c "import pyscreeze; print(pyscreeze.__path__)"
- Modify __init__.py located in this folder:
Line 21,
import numpy as np
Line 166,
img_cv = cv2.imdecode(np.fromfile(img, dtype=np.uint8), LOAD_GRAYSCALE)
Line 168,
img_cv = cv2.imdecode(np.fromfile(img, dtype=np.uint8), LOAD_COLOR)
- Finally install numpy
pip install numpy
Method 2
As #SiP explained, Another possibility could be to copy the image to a temporary folder.
Something like that:
import os
import pathlib
import tempfile
import shutil
import pyautogui
Image = os.path.join(os.path.sep, pathlib.Path(__file__).parent.resolve(), 'static', 'img', 'game', 'image-btn.png')
temp_path = os.path.join(tempfile.gettempdir(), "file_name_in_ascii")
shutil.copy2(Image, temp_path)
if pyautogui.locateOnScreen(temp_path, grayscale=True, confidence=0.95) is not None:
print(True)

Getting error when try to execute exe while of my script

I was getting some error and I don't know how to handle that error. I have created a script that is used to generate a barcode. Everything is working fine. But when I convert that script into exe. And try to use it then I am getting an error.
Error Image
Here is my code
import barcode
from barcode.writer import ImageWriter
EAN = barcode.get_barcode_class('code128')
with open('somefile.jpeg', 'wb') as f:
EAN("12347859450", writer=ImageWriter()).write(f)
Please help me out from this situation.
looks like your freeze method when creating the executable(cx_freeze?) is failing to include the needed dependency - in this case, looks like the FreeType library. I would investigate in that direction.

How to restrict the recognized characters in tesserocr?

When using tesserocr how do I limit the set of characters that Tesseract will recognize to just the digits?
I know from this that if I were using c++ I could set a tessedit_char_whitelist in the config file, but I don't know the analogous approach in tesserocr within Python.
In general, the tesserocr documentation gives help that works if the reader already knows the Tesseract API for c++. As I am not fluent in c++, I am hoping to avoid having to read the c++ source code in order to use tesserocr.
If anyone can give me what I actually need to write in python or a general rule for going from config settings to Python code that would be great. Thanks in advance.
Tesserocr works as the C++ API, you can set a whitelist with the function SetVariable.
An example:
from tesserocr import PyTessBaseAPI
from string import digits
with PyTessBaseAPI() as api:
api.SetVariable('tessedit_char_whitelist', digits)
api.SetImageFile('image.png')
print api.GetUTF8Text() # it will print only digits
If you want another approach that is more straightforward and independent from the C++ API, try with the pytesseract module.
An example with pytesseract:
import pytesseract
from PIL import Image
from string import digits
image = Image.open('image.png')
print pytesseract.image_to_string(
image, config='-c tessedit_char_whitelist=' + digits)

Using multiple languages in Pytesser

I have started to use Pytesser, which works great with both english and chinese, but is there a way to have both languages work at the same time? Would I have to make my own traineddata file? My code is:
import Image
from pytesser import *
print image_to_string(Image.open("chinese_and_english.jpg"), lang="eng")
#also want to have chinese be recognized
I'm not sure about Pytesser but using tesserocr you can specify multiple languages. For example:
import tesserocr
with tesserocr.PyTessBaseAPI(lang='eng+chi_tra') as api:
api.SetImageFile('eSXSz.jpg')
print api.GetUTF8Text()
# or simply
print tesserocr.file_to_text('eSXSz.jpg', lang='eng+chi_tra')
Example output for your image:
In [8]: print tesserocr.file_to_text('eSXSz.jpg', lang='eng+chi_tra')
Character, Chmese 動m川爬d
胸肌岫馴伽 H枷﹏ P﹏… …
〔Manda‥﹝ 二 Standard C…爬虯
一
口
X慣ng怕ng
Note that it's more efficient to initialize the API once as in the first example and re-use it for multiple images by calling SetImageFile (or SetImage with a PIL.Image object) to avoid re-initializing the API every time.

Categories