33_mnistCameraTest.py 4.9 KB

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