33_mnistCameraTest.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/usr/bin/env python
  2. #version : 2023.12.31
  3. #language : ch
  4. from maix import camera
  5. from maix import display
  6. from maix import image
  7. from maix import nn
  8. from maix.nn import decoder
  9. import os
  10. import numpy as np
  11. import os
  12. ScreenOrientation = False
  13. try:
  14. if os.path.exists("/etc/cameraSize.cfg"):
  15. cameraSize = True
  16. else:
  17. cameraSize = False
  18. except:
  19. cameraSize = False
  20. def getLcdRotation(cameraCapture):
  21. global cameraSize
  22. if cameraSize:
  23. return lcdRotationNew(cameraCapture)
  24. else:
  25. return lcdRotation(cameraCapture)
  26. def lcdRotationNew(inputImg):
  27. global cameraSize,ScreenOrientation
  28. imageRotationBuffer = inputImg.crop(0, 0, 320, 240)
  29. if ScreenOrientation:
  30. imgRotationAim = image.new(size = (240, 320))
  31. rotationAngle = 90
  32. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  33. else:
  34. imgRotationAim = image.new(size = (320, 240))
  35. GETROTATION = imageRotationBuffer
  36. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  37. return GETROTATION
  38. def lcdRotation(inputImg):
  39. global cameraSize,ScreenOrientation
  40. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  41. if ScreenOrientation:
  42. imgRotationAim = image.new(size = (240, 320))
  43. rotationAngle = 180
  44. else:
  45. imgRotationAim = image.new(size = (320, 240))
  46. rotationAngle = 90
  47. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  48. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  49. return GETROTATION
  50. def sigmoid(x):
  51. return 1/(1+np.exp(-x))
  52. def grad(x):
  53. return x*(1-x)
  54. class NeuralNetwork:
  55. """
  56. 三层全连接前馈神经网络
  57. """
  58. def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate, active_function=sigmoid, gradient=grad, lambda_=0.1):
  59. """
  60. :param inputnodes: 输入层结点数
  61. :param hiddennodes: 隐藏层节点数
  62. :param outputnodes: 输出层节点数
  63. :param learningrate: 学习率
  64. :param active_function: 激活函数
  65. :param gradient: 激活函数的导数
  66. :param lambda_: L2正则化系数
  67. """
  68. self.inputnodes = inputnodes
  69. self.hiddennodes = hiddennodes
  70. self.outputnodes = outputnodes
  71. self.learningrate = learningrate
  72. self.active_function = active_function
  73. self.gradient = gradient
  74. self.lambda_ = lambda_
  75. # 权值矩阵
  76. self.weights_i_h = np.random.rand(self.hiddennodes, self.inputnodes) - 0.5
  77. self.weights_h_o = np.random.rand(self.outputnodes, self.hiddennodes) - 0.5
  78. def train_sgd(self, x, y):
  79. """梯度下降训练"""
  80. train_x = np.array(x).reshape(-1,1)
  81. target = np.zeros((self.outputnodes,1)) + 0.01
  82. target[y,0] = 0.99
  83. hiddeninputs = np.dot(self.weights_i_h, train_x)
  84. hiddenoutputs = self.active_function(hiddeninputs)
  85. outputinputs = np.dot(self.weights_h_o, hiddenoutputs)
  86. final_outputs = self.active_function(outputinputs)
  87. error = target - final_outputs
  88. hidden_error = np.dot(self.weights_h_o.transpose(), error)
  89. self.weights_h_o += self.learningrate * error * np.dot(self.gradient(final_outputs), hiddenoutputs.transpose())
  90. self.weights_i_h += self.learningrate * hidden_error * np.dot(self.gradient(hiddenoutputs), train_x.transpose())
  91. def fit(self, train_x, targets):
  92. train_x = np.array(train_x)
  93. for i in range(train_x.shape[0]):
  94. self.train_sgd(train_x[i], targets[i])
  95. def query(self, inputs, debug=False):
  96. """单个值预测"""
  97. inputs = np.array(inputs).reshape(-1,1)
  98. hidden_input = np.dot(self.weights_i_h, inputs)
  99. hidden_output = self.active_function(hidden_input)
  100. output_input = np.dot(self.weights_h_o, hidden_output)
  101. final_output = self.active_function(output_input)
  102. if debug:
  103. print('predict: ', final_output)
  104. return np.argmax(final_output)
  105. def predict(self,inputs):
  106. """批量预测"""
  107. res = []
  108. for x in inputs:
  109. res.append(self.query(x))
  110. return res
  111. def __str__(self):
  112. return "NeuralNetwork: \ninput_nodes = {0}, hidden_nodes = {1}, \noutputnodes = {2}, learningrate = {3}".format(
  113. self.inputnodes, self.hiddennodes, self.outputnodes, self.learningrate
  114. )
  115. image.load_freetype("/root/preset/fonts/CascadiaCodePL-Italic.ttf")
  116. import pickle
  117. if os.path.exists('/root/user/model/NN10.pkl'):
  118. with open('/root/user/model/NN10.pkl','rb') as f:
  119. b_data = pickle.load(f)
  120. net_model = pickle.loads(b_data)
  121. else:
  122. # 最佳模型,载入最佳模型
  123. with open('/root/user/NN10.pkl','rb') as f:
  124. b_data = pickle.load(f)
  125. net_model = pickle.loads(b_data)
  126. print(net_model)
  127. if cameraSize==True:
  128. camera.camera.config(size=(320,240))
  129. else:
  130. camera.camera.config(size=(240,320))
  131. img_path="/root/user/image_1.png"
  132. img_gray_path="/root/user/image_gray.png"
  133. while True:
  134. img = getLcdRotation(camera.capture()) #从摄像头中获取一张图像
  135. mk = img.crop(0, 0, 240, 240) #裁剪图像为320*240
  136. mk1=img.crop(0, 0, 240, 240).resize(224,224) #继续裁剪图像为224*224
  137. canvas= image.new(size = (320, 240), color = (0,255,0), mode = 'RGB') #创建背景图
  138. canvas.draw_image(mk1,58,8) #贴224*224的图片
  139. mk2=mk1.resize(28,28) #裁剪图片为28*28
  140. canvas.draw_image(mk2,8,8) #贴28*28的图
  141. mk2.save(img_path)
  142. from PIL import Image
  143. origin=Image.open(img_path).convert('L') #转为8位分辨率灰度图(0~255)
  144. origin.save(img_gray_path)
  145. origin_gray=image.open(img_gray_path)
  146. canvas.draw_image(origin_gray,8,48)
  147. img = np.array(origin).reshape(1, 784).astype('int')
  148. img = img / 255 * 0.99 + 0.01
  149. print(img.shape)
  150. tmp = net_model.query(img)
  151. print(tmp)
  152. if tmp:
  153. canvas.draw_string(12,80, str(tmp), scale = 5, color = (255,0,0) , thickness = 1)
  154. display.show(canvas) #将图像显示出来