OpenCV Python Error in simple digit recognition -
import cv2 import numpy np ####### training part ############### samples = np.loadtxt('generalsamples.data',np.float32) responses = np.loadtxt('generalresponses.data',np.float32) responses = responses.reshape((responses.size,1)) model = cv2.knearest() model.train(samples,responses) ############################# testing part ######################### im = cv2.imread('/home/manoj/pictures/untitled-1.jpg') out = np.zeros(im.shape,np.uint8) gray = cv2.cvtcolor(im,cv2.color_bgr2gray) thresh = cv2.adaptivethreshold(gray,255,1,1,11,2) contours,hierarchy = cv2.findcontours(thresh,cv2.retr_list,cv2.chain_approx_simple) 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)
i used python code opencv
character recognition error while running code.
attributeerror: 'module' object has no attribute 'knearest'
since new opencv's version need replace
model = cv2.knearest()
by:
model = cv2.ml.knearest_create()
moreover, line:
model.train(samples,responses)
is going raise error, should fix it:
model.train(samples,cv2.ml.row_sample,responses) # might adapted
hope help.
Comments
Post a Comment