Python-tesseract does not recognize anything - python

This is the image that I will import
My python code
try:
import Image
except ImportError:
from PIL import Image
import pytesseract
print(pytesseract.image_to_string(Image.open('/home/milenko/Pictures/Screenshot from 2018-03-06 19-03-19.png')))
When I run code
python a72.py
As an output I got empty line.It does not make any sense.
Why?

Try to tweak your command a little bit using e.g.: other Page Segmentation Method As you can see the default value is "Fully automatic page segmentation, but no OSD." so it does not perform orientation and script detection (OSD).
This one gives me some output:
print(pytesseract.image_to_string(Image.open('image.png'), config='-psm 12'))
You can use OpenCV to prepare this image for OCR, e.g:
#!/usr/bin/python
import cv2 as cv
import numpy as np
import pytesseract
import Image
from matplotlib import pyplot as plt
img = cv.imread('/tmp/image.png',0)
ret,thresh = cv.threshold(img, 220, 255, cv.THRESH_BINARY)
plt.axis('off')
plt.imshow(thresh, 'gray')
plt.show()
print(pytesseract.image_to_string(thresh, config='-psm 12'))
In the next step you could divide this image into some parts (x-axis, y-axis, trend line) and use OCR for each part separately with the proper PSM value set for each one of them.

Related

Why does the Variance of Laplace very different for OpenCV and scikit-image?

TL;DR: How can I use skimage.filters.laplace(image).var() in a way to get the same value as cv2.Laplacian(image, CV_64F).var() and skimage.filters.sobel(image) to get same value as cv2.Sobel(image) ?
I have the following code to find the Laplace Variance for blur detection
import numpy as np
import cv2
import pandas as pd
import requests
from PIL import Image
import urllib
from skimage.filters import laplace, sobel, roberts
from io import BytesIO
from skimage import color
from skimage import io as sk_io
def url_to_image(url): # Get image from url
resp = urllib.request.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_GRAYSCALE)
return image
def simple_blur(gray:np.ndarray)->float:
'''
Use Laplacian Variance to find if an image has blur or not. It is very critical to find the threshold and is vert data specific
args:
gray: Grayscale Image
'''
return cv2.Laplacian(gray, CV_64F).var()
So when I try to find the Laplace variance from OpenCV and scikit-image, it gives me two different values:
laplace(color.rgb2gray(sk_io.imread(url))).var()
>> 1.1086769139613736e-05
simple_blur(url_to_image(url))
>> 0.6622495826224196
Which one should I use or how can I get same number from both the functions?
Also, How can I use the cv2.Sobel(image) given in OpenCV EXACTLY to get the results from sobel(image) # given in scikit-image?
This is the Image Link

Displaying Hole Filled image in python with OpenCV

I am working on some image analysis in python using OpenCV. I want to display an image that I filled in holes with using scipy.ndimage.binary_filled_holes. Upon doing this I could not see anything being displayed when I used cv2.imshow, so I used plt.imshow and saw that the holes in my original image were filled. I want to use the cv2.imshow function to display the image. I did convert the image so that the datatype is uint8, yet still, nothing shows up. Any help would be appreciated.
import cv2
import matplotlib.pyplot as plt
import numpy as np
import scipy.ndimage
img = cv2.imread('Funky 647.jpg', cv2.IMREAD_GRAYSCALE)
dst = cv2.fastNlMeansDenoising(img,None,10,7,21)
ret, thresh2 = cv2.threshold(dst, 40, 255, cv2.THRESH_BINARY)
hole_filled= np.uint8(scipy.ndimage.binary_fill_holes(thresh2))
# plt.imshow(hole_filled)
cv2.imshow('No Holes', hole_filled)
cv2.waitKey(0)
cv2.destroyAllWindows()
Hole Filled Image via matplotlib:

How do I fix this cv2.imshow() syntax error?

I cannot get the following code to run:
import cv2 # import Open Cv moudle
import numpy as np
import face_recognition
import matplotlib as plt
imgEloun=face_recognition.load_image_file ("image/elounMask.jpg") # load Image
imgEloun=cv2.cvtColor(imgEloun,cv2.COLOR_BGR2RGB) # changing BGR clr to RGB
imgTest=face_recognition.load_image_file ("image/steve jobs.jpg") # Load Image
imgTest=cv2.cvtColor(imgTest,cv2.COLOR_BGR2RGB) # changing BGR clr to RGB
faceLocation=face_recognition.face_locations(imgEloun)[0] #Detect face loaction in image
encodeELOUN=face_recognition.face_encodings(imgEloun)[0] #encoding face location
cv2.rectangle(imgEloun,(faceLocation[3],(faceLocation[0]),(faceLocation[1],(faceLocation[2]),(255,0,255))
cv2.imshow('elounMask',imgEloun) ######## here is error
cv2.imshow('steve jobs',imgTest) # Show Image as output
cv2.waitKey(0)
Here is a Screenshot of my error in PyCharm.
Is anyone able to help out?
cv2.rectangle(imgEloun,((faceLocation[3],(faceLocation[0])),((faceLocation[1],(faceLocation[2])),(255,0,255))
you missed the parenthesis when giving the coordinates for rectangle. It should be in this format,
eg: cv2.rectangle(image, (5,5), (200,200), (255,255,0), 2)

Open CV: Problems with cv2.rectangle() method

Im just trying some things with OpenCV. Once i try to to put a rectangle on my image, I always receive the following error: Expected Ptr<cv::UMat> for argument 'img'
This happens while using the following code:
!pip install opencv-python
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import cv2
img = cv2.imread("C:/..../.../.../.../.....jpg")[...,::-1]
cv2.rectangle(img,(400,200),(500,500),(0,0,255),20)
plt.imshow(img)
plt.show()
Is there anybody who faced the same issue once?
Thanks for your help in advance
Best regards
Sascha
If you want to display with matplotlib, the best way to change from BGR to RGB is to use
img = cv2.imread("C:/..../.../.../.../.....jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
instead of
img = cv2.imread("C:/..../.../.../.../.....jpg")[...,::-1]
I tried it and it works.

Counting Objects in an image using OPENCV and Python

I'm currently in the pursue of counting the number of shrimps in a given image. I'm using this test image:
The code I have used so far is the following:
import cv2
import numpy as np
from matplotlib import pyplot as plt
#Load img
path = r'C:\Users\...' #the path to the image
original=cv2.imread(path, cv2.COLOR_BGR2RGB)
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
#Hist to proceed with the binarizarion
hist = cv2.calcHist([img],[0],None,[256],[0,256])
#do the threshold
ret,thresh = cv2.threshold(img,60,255,cv2.THRESH_BINARY_INV)
From this point I have tried different morphological transformations such a erode, dilate, open and close but they don't seem to be working and separating the objects as I want.
I've read that I can apply a Watershed transformation so separate touching elements, but I donĀ“t have experience in this (working at this point at the moment).
After that I am planning on using a Simple Blob Detector to count the blobs, I don't know if these steps are correct.
Any help is very welcomed!

Categories

Resources