Using multiple languages in Pytesser - python

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.

Related

Use imshow with Matlab Python engine

After building and installing the Python engine shipped with Matlab 2019b in Anaconda
(TestEnvironment) PS C:\Program Files\MATLAB\R2019b\extern\engines\python> C:\Users\USER\Anaconda3\envs\TestEnvironment\python.exe .\setup.py build -b C:\Users\USER\MATLAB\build_temp install
for Python 3.7 I wrote a simple script to test a couple of features I'm interested in:
import matlab.engine as ml_e
# Start Matlab engine
eng = ml_e.start_matlab()
# Load MAT file into engine. The result is a dictionary
mat_file = "samples/lena.mat"
lenaMat = eng.load("samples/lena.mat")
print("Variables found in \"" + mat_file + "\"")
for key in lenaMat.keys():
print(key)
# print(lenaMat["lena512"])
# Use the variable from the MAT file to display it as an image
eng.imshow(lenaMat["lena512"], [])
I have a problem with imshow() (or any similar function that displays a figure in the Matlab GUI on the screen) namely that it shows quickly and then disappears, which - I guess - at least confirms that it is possible to use it. The only possibility to keep it on the screen is to add an infinite loop at the end:
while True:
continue
For obvious reasons this is not a good solution. I am not looking for a conversion of Matlab data to NumPy or similar and displaying it using matplotlib or similar third party libraries (I am aware that SciPy can load MAT files for example). The reason is simple - I would like to use Matlab (including loading whole environments) and for debugging purposes I'd like to be able to show this and that result without having to go through loops and hoops of converting the data manually.

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)

pydicom is not defined

I'm trying to learn how to use pydicom for reading and processing dicom images. I'm using Python 3.
import dicom
import numpy
ds = pydicom.read_file(lstFilesDCM[0])
print(ds.pixel_array)`
I get an error NameError: name 'pydicom' is not defined. If I change
ds = pydicom.read_file(lstFilesDCM[0])
to
ds = dicom.read_file(lstFilesDCM[0])
(using dicom.read_file instead), I get the following error:
NotImplementedError: Pixel Data is compressed in a format
pydicom does not yet handle. Cannot return array
I also verified that pydicom is properly installed and updated.
How do i fix this?
You are trying to call a class that you have not imported before:
Use:
import pydicom
import numpy
ds = pydicom.read_file(lstFilesDCM[0])
print(ds.pixel_array)
or
import dicom
ds = dicom.read_file("the_name_of_file.dcm")
Documentation: http://pydicom.readthedocs.io/en/stable/pydicom_user_guide.html
If you want to get your hands on the pixel data, I suggest to use the convert program from the ImageMagick suite. You can either call this program from Python using the subprocess module. (See this example, where I convert them to JPEG format), or you can use one of the Python bindings.
If you want to manipulate the images, using the bindings might be preferable. But note that not all the bindings have been converted to ImageMagick version 7.

Python array visualizer for debugging

I'm working with image processing which means I'm doing operations on large matrices. I'm trying to debug which means I need to explore the elements, but it's a real pain doing it with print statements. Is there some kind of python plugin that will let me view arrays in a GUI for the purpose of debugging?
yes just use the python debugger and put a break point
or use something like q
$ easy_install q
import q
my_array = numpy.arange(1000)
q.d() #open a terminal where you have access to my_array
you will see something like below
Python console opened by q.d() in <some_module>
>>> print my_array[5]
you can also use pill to generate an image from the array (not sure if it will work right without tweaking)
>>> import Image
>>> img = Image.fromarray(my_array, 'RGB')
>>> img.save('test.png')
However, if you would like to display numpy array as an image, you can use OpenCV Image Viewer Plugin, which I've just released.
https://plugins.jetbrains.com/plugin/14371-opencv-image-viewer

python opencv imwrite ... can't find params

I am using opencv with python. I wanted to do an cv2.imwrte:
cv2.imwrite('myimage.png', my_im)
The only problem is that opencv does not recognize the params constants:
cv2.imwrite('myimage.png', my_im, cv2.CV_IMWRITE_PNG_COMPRESSION, 0)
It cannot find CV_IMWRITE_PNG_COMPRESSION at all. Any ideas?
I can't find key CV_XXXXX in the cv2 module:
Try cv2.XXXXX
Failing that, use cv2.cv.CV_XXXXX
In your case, cv2.cv.CV_IMWRITE_PNG_COMPRESSION.
More info.
The docs for OpenCV (cv2 interface) are a bit confusing.
Usually parameters that look like CV_XXXX are actually cv2.XXXX.
I use the following to search for the relevant cv2 constant name. Say I was looking for CV_MORPH_DILATE. I'll search for any constant with MORPH in it:
import cv2
nms = dir(cv2) # list of everything in the cv2 module
[m for m in nms if 'MORPH' in m]
# ['MORPH_BLACKHAT', 'MORPH_CLOSE', 'MORPH_CROSS', 'MORPH_DILATE',
# 'MORPH_ELLIPSE', 'MORPH_ERODE', 'MORPH_GRADIENT', 'MORPH_OPEN',
# 'MORPH_RECT', 'MORPH_TOPHAT']
From this I see that MORPH_DILATE is what I'm looking for.
However, sometimes the constants have not been moved from the cv interface to the cv2 interface yet.
In that case, you can find them under cv2.cv.CV_XXXX.
So, I looked for IMWRITE_PNG_COMPRESSION for you and couldn't find it (under cv2....), and so I looked under cv2.cv.CV_IMWRITE_PNG_COMPRESSION, and hey presto! It's there:
>>> cv2.cv.CV_IMWRITE_PNG_COMPRESSION
16
Expanding on mathematical.coffee to ignore case and look in both namespaces:
import cv2
import cv2.cv as cv
nms = [(n.lower(), n) for n in dir(cv)] # list of everything in the cv module
nms2 = [(n.lower(), n) for n in dir(cv2)] # list of everything in the cv2 module
search = 'imwrite'
print "in cv2\n ",[m[1] for m in nms2 if m[0].find(search.lower())>-1]
print "in cv\n ",[m[1] for m in nms if m[0].find(search.lower())>-1]
>>>
in cv2
['imwrite']
in cv
['CV_IMWRITE_JPEG_QUALITY', 'CV_IMWRITE_PNG_COMPRESSION', 'CV_IMWRITE_PXM_BINARY']
>>>
Hopefully this problem will go away in some later release of cv2...
the compression style is automatically chosen from the file extension. see the cv2.imwrite help here.
however you might still be interested to know all the possible flags used by all the possible functions in cv2 and cv modules.
look for cv2.txt and cv.txt on your computer. they will be where the opencv modules are installed. at the bottom of those text files are a list of the flags used by the respective modules.
just in case you don't find them, you can download the ones i have from here, though they are from august 2011:
cv2.txt
cv.txt
in fact, with cv2 style API, this constant is replaced with cv2.IMWRITE_PNG_COMPRESSION.

Categories