###環境
OS:windows10
pythonを使用しています。
###やりたいこと
現在、文字(数字)認識をやろうとしているのですが、
認識したい文字列の特徴が
0~9999の数字+末尾にアルファベット
という特徴をしています。
後々、物体として検知するときに
おそらく、一桁ごとに検知されると思うのですが、
どのように、物体検知させればいいのか
わかりません。
できれば、Opencvで検知させたいです。
どなたか教えてください。
###2018年7月11日追記
なぜ、文字を物体認識にかけたいかというと、
本来は、機械学習で文字認識をしたいと検討していました。
ただ、認識させたい文字列は数が異常に多く、画像データは準備できても、
ラベルの作成の時に、単純に計算しただけでも、
260000次元の配列を必要としています。
(下記で質問しています)
↓機械学習でラベルの作り方
https://teratail.com/questions/135207
それは実に、無茶であるために、
前処理の段階で、
文字列を桁ごとに分割して、仮想的に画像化することで、
そこで、初めて機械学習で認識にかけることができるということです。
(下記で質問しています)
↓機械学習 数字列の桁ごとに画像認識させたい
https://teratail.com/questions/135292
###数字のみの桁ごとに認識するものはあった
桁ごとの数字認識
https://stackoverflow.com/questions/9413216/simple-digit-recognition-ocr-in-opencv-python
上記のプログラムを、今のバージョン用に書き換えて実行したところ、
うまく動きました。
学習プログラム
lang
1import sys 2 3import numpy as np 4import cv2 5 6im = cv2.imread('pitrain.png') 7im3 = im.copy() 8 9gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) 10blur = cv2.GaussianBlur(gray,(5,5),0) 11thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2) 12 13################# Now finding Contours ################### 14 15image, cnts, hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) 16 17samples = np.empty((0,100)) 18responses = [] 19keys = [i for i in range(48,58)] 20 21for cnt in cnts: 22 if cv2.contourArea(cnt)>50: 23 [x,y,w,h] = cv2.boundingRect(cnt) 24 25 if h>28: 26 cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),2) 27 roi = thresh[y:y+h,x:x+w] 28 roismall = cv2.resize(roi,(10,10)) 29 cv2.imshow('norm',im) 30 key = cv2.waitKey(0) 31 32 if key == 27: # (escape to quit) 33 sys.exit() 34 elif key in keys: 35 responses.append(int(chr(key))) 36 sample = roismall.reshape((1,100)) 37 samples = np.append(samples,sample,0) 38 39responses = np.array(responses,np.float32) 40responses = responses.reshape((responses.size,1)) 41print("training complete") 42 43np.savetxt('generalsamples.data',samples) 44np.savetxt('generalresponses.data',responses)
テスト用
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, cv2.ml.ROW_SAMPLE, responses) 11 12############################# testing part ######################### 13 14im = cv2.imread('PS.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 19imgs, contours,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.findNearest(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(10000)
結果は上記のサイトにありますのでそちらを見てください。
###画像の数字にアルファベットを混ぜてみた
上記のプログラムを全く変えないで、
画像を以下のものにしました。
PS.png
それで実行してみたところ、
案の定、アルファベットも数字と認識してしまい、
失敗に終わりました。
上記のプログラムに何か付け足すことで、
アルファベットを認識できる方法があるのであれば、
知りたいです。
そのほか、何か方法があれば教えてください。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/11 02:09