###環境
OS:win10
言語:python(anaconda)
###サンプル
lang
1import cv2 2import numpy as np 3 4####### training part ############### 5samples = np.loadtxt('generalsamples.data',np.float32) 6responses = np.loadtxt('generalresponses.data',np.float32) 7responses = responses.reshape((responses.size,1)) 8 9model = cv2.ml.KNearest_create() 10model.train(samples,responses) 11 12############################# testing part ######################### 13 14im = cv2.imread('pi.png') 15out = np.zeros(im.shape,np.uint8) 16gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) 17thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2) 18 19contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) 20 21for cnt in contours: 22 if cv2.contourArea(cnt)>50: 23 [x,y,w,h] = cv2.boundingRect(cnt) 24 if h>28: 25 cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2) 26 roi = thresh[y:y+h,x:x+w] 27 roismall = cv2.resize(roi,(10,10)) 28 roismall = roismall.reshape((1,100)) 29 roismall = np.float32(roismall) 30 retval, results, neigh_resp, dists = model.find_nearest(roismall, k = 1) 31 string = str(int((results[0][0]))) 32 cv2.putText(out,string,(x,y+h),0,1,(0,255,0)) 33 34cv2.imshow('im',im) 35cv2.imshow('out',out) 36cv2.waitKey(0) 37
###わからないこと
pythonのopencvで数字認識をしようと、
スタックオーバフローにあったサンプルを動かそうとしたところ、
lang
1AttributeError: module 'cv2' has no attribute 'KNearest'
このようなエラーが出てしまい進捗が停滞しています。
opencvが2系から3系になるときにいろいろとモジュールが削除されていることはわかりましたが、それ以上はわかりませんでした。
どなたか教えてください。
cv2.ml~に変えて実行してみたところ
Traceback (most recent call last): File "suji_test.py", line 10, in <module> model.train(samples,responses) TypeError: only size-1 arrays can be converted to Python scalars
このようになりました
これはどういういみですか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。