| 
 參考這個網址有人分享Python2照片上數字的辨識 
數字辨識 Python 
 
   首先老男人用小畫家建立一張寫上數字0~9的圖片 圖片名稱number5.png 這張圖片是給Pythno的程式學習認字 程式是使用Python3與來源版本不同 有稍微修改 - import sys
 
 - import numpy as np
 
 - import cv2
 
  
- im = cv2.imread('1.jpg')
 
 - im3 = im.copy()
 
  
- gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
 
 - blur = cv2.GaussianBlur(gray,(5,5),0)
 
 - thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)
 
  
- #################      Now finding Contours         ###################
 
  
- contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
 
  
- samples =  np.empty((0,100))
 
 - responses = []
 
 - keys = [i for i in range(48,58)]
 
  
- 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,0,255),2)
 
 -             roi = thresh[y:y+h,x:x+w]
 
 -             roismall = cv2.resize(roi,(10,10))
 
 -             cv2.imshow('norm',im)
 
 -             key = cv2.waitKey(0)
 
  
-             if key == 27:  # (escape to quit)
 
 -                 sys.exit()
 
 -             elif key in keys:
 
 -                 responses.append(int(chr(key)))
 
 -                 sample = roismall.reshape((1,100))
 
 -                 samples = np.append(samples,sample,0)
 
  
- responses = np.array(responses,np.float32)
 
 - responses = responses.reshape((responses.size,1))
 
 - print("training complete")
 
  
- np.savetxt('generalsamples.data',samples)
 
 - np.savetxt('generalresponses.data',responses)
 
  複製代碼 
 
程式Run 指定number5.png這張圖學習 (數字的照片可自定,cv2.imread()這裡指定) 紅框在哪個字就按哪個字 一開始是0就按0 按之後紅框往左跳  
數字辨識 Python 
 
    
按一次跳一下 
數字辨識 Python 
 
    
全部按完 印字training complete 
數字辨識 Python 
 
    
產生下列兩個檔 generalsamples.data 與 generalresponses.data 學習資料與記錄放在裡面 學習完就可以開另一支程式做數字辨識  
第二支程式追加辨識完的數字字串存入List,然後寫入記事本保存 - import cv2
 
 - import numpy as 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.ml.KNearest_create()
 
 - model.train(samples, cv2.ml.ROW_SAMPLE, responses)
 
  
- ############################# testing part  #########################
 
  
- im = cv2.imread('a.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)
 
 - contours,_ = cv2.findContours(thresh, cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
 
 - count=0
 
 - num_str = 10*['0']
 
 - for cnt in contours:
 
 -     if cv2.contourArea(cnt)>50:
 
 -         [x,y,w,h] = cv2.boundingRect(cnt)
 
 -         if  h>32:
 
 -             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)
 
 -             retval, results, neigh_resp, dists = model.findNearest(roismall, k = 1)
 
 -             string = str(int((results[0][0])))
 
 -             print(string)
 
 -             num_str[count] = string
 
 -             count += 1
 
 -             cv2.putText(out,string,(x,y+h),0,1,(0,255,0))
 
  
 
- number = map(int, num_str)
 
 - cv2.imshow('im',im)
 
 - cv2.imshow('out',out)
 
 - temp = []
 
 - for i in num_str:
 
 -     temp.append(i)
 
 - temp.reverse()
 
 - temp_str = ''
 
 - num_data = temp_str.join(temp)
 
 - print(num_data)
 
  
- file=open('data.txt','a')
 
 - file.write(num_data + ';');
 
 - file.close()
 
  
- cv2.waitKey(0)
 
  複製代碼 
    
程式Run 先辨識原圖  
 
 
秀出兩個視窗 白底是辨識的照片 黑底是辨識結果 
數字辨識 Python 
 
    
辨識另一張number6.png 故意讓數字排列與前一張不同 (同上,在程式中cv2.imread()指定) 
數字辨識 Python 
 
    
程式Run 
數字辨識 Python 
 
    
辨識完數字資料存在名為data.txt的記事本 每筆資料以;間隔 
數字辨識 Python 
 
   數字辨識日常生活的運用越來越廣泛, 例如車牌號碼,水錶,電錶,瓦斯錶...等等  
  https://kobayasitenmei.pixnet.net/blog/post/226096343-%E7%85%A7%E7%89%87%E4%B8%8A%E7%9A%84%E6%95%B8%E5%AD%97%E8%BE%A8%E8%AD%98-python3 
 
網路上分享之瓦斯錶度數讀出範例  
程式  
  
 |