Hi All.
I would like to make a photo using a photo camera and then feed the function to recognise a text! For testing i download images in main file(see below)!!!
Question: How to omit the mistake/error?
Thanks a lot
error
C:\py_programs\ps_>test.py
Traceback (most recent call last):
File "C:\py_programs\ps_\test.py", line 37, in <module>
res=findWords(imagelist[0]) # imagelist[0] is the ImgAuf (Image of the auftrag)
File "C:\py_programs\ps_\findWords.py", line 63, in findWords
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'cvtColor'
> Overload resolution failed:
> - src is not a numpy array, neither a scalar
> - Expected Ptr<cv::UMat> for argument 'src'
main file is:
from PIL import Image, ImageEnhance
# Loading TEST images form path
#---- Test data/images: delate after finishing
path_to_image2 = 'images/22_test.jpg'
path_to_image1 = 'images/c22.jpg'
Img1 = Image.open(path_to_image1)
Img2 = Image.open(path_to_image2)
imagelist=[Img1,Img2]
# finding text----- Extracting only numbers from the text
res=findWords(imagelist[0])
**findWords () Function **
from PIL import Image, ImageEnhance
import cv2
import re
import time
from pytesseract import pytesseract
from configparser import*
#Point tessaract_cmd to tessaract.exe
pytesseract.tesseract_cmd = path_to_tesseract
def findWords(img):
Auf_Num="Not known"
res = False
start = time.time()
dicList={}
foundWords=[]
while time.time()<start+timeout:
now = time.time()
## Searching for the numbers base on the gray color of image
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Extract text from image
text = pytesseract.image_to_string(img)
for search in searchList:
print("start main Loop from the List: ")
print(search)
for searchWord in search:
print('----------------------------------')
print('\niNFO: Search word:', searchWord) #searchWord
print('--> Grayscale is started')
## Text processing
#find an index of beginning of the searchWord
index = text.find(searchWord)
#Extract a word (number) which is found after "searchword"
Auf_Num=re.findall(r'\b\d+\b', text[index:index+17])
## Searching for the numbers base on the image contrast adjusting (applied if above section faild res==FALSE)
#verify if found word is number
if len(Auf_Num)>0:
res=str.isdigit(Auf_Num[0]) ## Auf_Num[0] - the first number in the list Auf_Num
end = time.time()
print('\n\n Search number is found:', Auf_Num[0])
print(search[0]+": "+ Auf_Num[0])
print('\nIdentification paramers are:','\n-> image processing:','grayscale', '\n-> scanning time:',round((end-start),4),'s')
print('\nScript is finished on:', time.ctime(time.time()),'\n\n')
foundWords.append(Auf_Num[0])
dicList[str(search[0])]=Auf_Num[0]
res = True
break
else:
print('\nFail:','Grayscale image was not recognised \n')
res = False
if res==False:
## Searching for the numbers base on the image contrast adjusting (applied if above section faild res==FALSE)
for searchWord in search:
print('----------------------------------')
print('\niNFO: Search word:', searchWord) #searchWord
print('--> Contrast variation loop is started')
if res == False:
for factor in np.arange(1,2.5,0.5):
img=Img
#make grayscale
imggray=ImageEnhance.Color(img)
img=imggray.enhance(0.0)
#image brightness enhancer
enhancer = ImageEnhance.Contrast(img)
img = enhancer.enhance(factor)
print("Contrast enhancment factor: " + str(factor)) #Contrast enhancment factor:
#Text processing
#Extract text from image
text = pytesseract.image_to_string(img)
#find an index of beginning of the searchWord
index = text.find(searchWord)
#Extract a word (number) which is found after "searchword"
Auf_Num=re.findall(r'\b\d+\b', text[index:index+17])
#verify if found word is number
if len(Auf_Num)>0:
res=str.isdigit(Auf_Num[0]) ## Auf_Num[0] - the first number in the list Auf_Num
end = time.time()
print('\n\nSearch number is found:', Auf_Num[0])
print(search[0]+": "+ Auf_Num[0])
print('Identification paramers are:','\n-> image processing:','contrast variation','\n-> enhancement factor:', factor, '\n-> scanning time:',round((end-start),4),'s')
print('\nScript is finished on:', time.ctime(time.time()),'\n\n')
foundWords.append(Auf_Num[0])
dicList[str(search[0])]=Auf_Num[0]
res = True
break
else:
print('\nFail:','Contrast-variation failed to recognise the searchword \n')
res = False
dicList[str(search[0])]="NaN"
if res==True:
break
if len(foundWords)==len(searchList):
print(dicList)
return(dicList)
break
elif len(foundWords)==1:
print("ERROR: Not all words were found")
print(dicList)
return(dicList)
break
else:
print("ERROR: The words were not found")
print(dicList) # returns {'Auftrag': 'NaN', 'Material': 'NaN'}
#return(res) # if all failed. it returns "False"
return(dicList) # if all failed. it returns {'Auftrag': 'NaN', 'Material': 'NaN'}
break