mardi 17 mai 2016

error about 'NoneType' object has no attribute 'shape' in ocr

hello i have a question now i'm doing about OCR using python-opencv , and after training part i've started testing part. But it didn't work This is my code i wrote.


`import sys

import cv2

import numpy as np

# training part ###############

samples = np.loadtxt('C:/Python27/generalsamples1.data',np.float32)

responses = np.loadtxt('C:/Python27/generalresponses1.data',np.float32)

responses = responses.reshape((responses.size,1))

model = cv2.ml.KNearest_create()

model.train(samples,cv2.ml.ROW_SAMPLE,responses)

####################### testing part

im = cv2.imread('C:/Python27/pi.png')

out = np.zeros(im.shape, np.uint8)

gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)

image,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:

if cv2.contourArea(cnt)>50:

    [x,y,w,h] = cv2.boundingRect(cnt)

    if  h>28:

        cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)

        roi = thresh[y:y+h,x:x+w]

        roismall = cv2.resize(roi,(10,10))

        roismall = roismall.reshape((1,100))

        roismall = np.float32(roismall)

        retval, results, neigh_resp, dists = model.find_nearest(roismall, k = 1)
        string = str(int((results[0][0])))

        cv2.putText(out,string,(x,y+h),0,1,(0,255,0))

cv2.imshow('im',im)

cv2.imshow('out',out)

cv2.waitKey(0)`


And i got this error


`Warning (from warnings module):

File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 893 warnings.warn('loadtxt: Empty input file: "%s"' % fname)

UserWarning: loadtxt: Empty input file: "C:/Python27/generalresponses1.data"

Traceback (most recent call last):

File "C:/Python27/sk_5_18_1(digit)_part2_1.py", line 18, in

out = np.zeros(im.shape, np.uint8)

AttributeError: 'NoneType' object has no attribute 'shape'`


And I've already check the path where 'pi.png' is. It is in the folder same as my code. I don't know how to do... plz help me soving this problem . I need your help!

Aucun commentaire:

Enregistrer un commentaire