TShopping

 找回密碼
 註冊
搜索
查看: 3535|回復: 0
打印 上一主題 下一主題

[教學] 照片上的數字辨識 Python3

[複製鏈接]
跳轉到指定樓層
1#
發表於 2020-11-24 22:05:47 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
參考這個網址有人分享Python2照片上數字的辨識
  
首先老男人用小畫家建立一張寫上數字0~9的圖片
圖片名稱number5.png
這張圖片是給Pythno的程式學習認字
程式是使用Python3與來源版本不同
有稍微修改
  1. import sys
  2. import numpy as np
  3. import cv2

  4. im = cv2.imread('1.jpg')
  5. im3 = im.copy()

  6. gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
  7. blur = cv2.GaussianBlur(gray,(5,5),0)
  8. thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)

  9. #################      Now finding Contours         ###################

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

  11. samples =  np.empty((0,100))
  12. responses = []
  13. keys = [i for i in range(48,58)]

  14. for cnt in contours:
  15.     if cv2.contourArea(cnt)>50:
  16.         [x,y,w,h] = cv2.boundingRect(cnt)

  17.         if  h>28:
  18.             cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),2)
  19.             roi = thresh[y:y+h,x:x+w]
  20.             roismall = cv2.resize(roi,(10,10))
  21.             cv2.imshow('norm',im)
  22.             key = cv2.waitKey(0)

  23.             if key == 27:  # (escape to quit)
  24.                 sys.exit()
  25.             elif key in keys:
  26.                 responses.append(int(chr(key)))
  27.                 sample = roismall.reshape((1,100))
  28.                 samples = np.append(samples,sample,0)

  29. responses = np.array(responses,np.float32)
  30. responses = responses.reshape((responses.size,1))
  31. print("training complete")

  32. np.savetxt('generalsamples.data',samples)
  33. np.savetxt('generalresponses.data',responses)
複製代碼


程式Run
指定number5.png這張圖學習
(數字的照片可自定,cv2.imread()這裡指定)
紅框在哪個字就按哪個字
一開始是0就按0
按之後紅框往左跳

  

按一次跳一下
  

全部按完
印字training complete
  

產生下列兩個檔
generalsamples.data 與 generalresponses.data
學習資料與記錄放在裡面
學習完就可以開另一支程式做數字辨識

第二支程式追加辨識完的數字字串存入List,然後寫入記事本保存
  1. import cv2
  2. import numpy as np

  3. #######   training part    ###############
  4. samples = np.loadtxt('generalsamples.data',np.float32)
  5. responses = np.loadtxt('generalresponses.data',np.float32)
  6. responses = responses.reshape((responses.size,1))

  7. model = cv2.ml.KNearest_create()
  8. model.train(samples, cv2.ml.ROW_SAMPLE, responses)

  9. ############################# testing part  #########################

  10. im = cv2.imread('a.jpg')
  11. out = np.zeros(im.shape,np.uint8)
  12. gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
  13. thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)
  14. #contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
  15. contours,_ = cv2.findContours(thresh, cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
  16. count=0
  17. num_str = 10*['0']
  18. for cnt in contours:
  19.     if cv2.contourArea(cnt)>50:
  20.         [x,y,w,h] = cv2.boundingRect(cnt)
  21.         if  h>32:
  22.             cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
  23.             roi = thresh[y:y+h,x:x+w]
  24.             roismall = cv2.resize(roi,(10,10))
  25.             roismall = roismall.reshape((1,100))
  26.             roismall = np.float32(roismall)
  27.             #retval, results, neigh_resp, dists = model.find_nearest(roismall, k = 1)
  28.             retval, results, neigh_resp, dists = model.findNearest(roismall, k = 1)
  29.             string = str(int((results[0][0])))
  30.             print(string)
  31.             num_str[count] = string
  32.             count += 1
  33.             cv2.putText(out,string,(x,y+h),0,1,(0,255,0))


  34. number = map(int, num_str)
  35. cv2.imshow('im',im)
  36. cv2.imshow('out',out)
  37. temp = []
  38. for i in num_str:
  39.     temp.append(i)
  40. temp.reverse()
  41. temp_str = ''
  42. num_data = temp_str.join(temp)
  43. print(num_data)

  44. file=open('data.txt','a')
  45. file.write(num_data + ';');
  46. file.close()

  47. cv2.waitKey(0)
複製代碼

  

程式Run
先辨識原圖



秀出兩個視窗
白底是辨識的照片
黑底是辨識結果
  

辨識另一張number6.png
故意讓數字排列與前一張不同
(同上,在程式中cv2.imread()指定)
  

程式Run
  

辨識完數字資料存在名為data.txt的記事本
每筆資料以;間隔
  
數字辨識日常生活的運用越來越廣泛,
例如車牌號碼,水錶,電錶,瓦斯錶...等等

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

網路上分享之瓦斯錶度數讀出範例

程式

網頁設計,網站架設 ,網路行銷,網頁優化,SEO - NetYea 網頁設計


 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-27 04:28 , Processed in 0.067066 second(s), 25 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表