31_training_deploy_model.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #!/usr/bin/env python
  2. #version : 2023.12.31
  3. #language : ch
  4. from maix import camera, display, zbar, image
  5. import socket
  6. import os
  7. import sys
  8. sys.path.append('/root/')
  9. import http.client
  10. from CocoPi import BUTTON
  11. import time
  12. import json
  13. import requests
  14. key_A = BUTTON(14)
  15. key_B = BUTTON(8)
  16. key_C = BUTTON(13)
  17. key_D = BUTTON(7)
  18. ScreenOrientation = False
  19. image.load_freetype("/root/preset/fonts/CascadiaCodePL-Italic.ttf")
  20. camera.camera.config(size=(320,240))
  21. def getWifiConnectState():
  22. cmd = "wifi_get_connection_info_test 1"
  23. res = os.popen(cmd).read()
  24. wifiInfo = {}
  25. if res.find('get connection infomation successfully!') != -1:
  26. wifiInfo["state"]=True
  27. wifiInfo["AP"] = res[res.find("Connected AP: ")+13:res.find("IP address: ")-1]
  28. wifiInfo["IP"] = res[res.find("IP address: ")+12:res.find("frequency")-1]
  29. wifiInfo["frequency"] = res[res.find("frequency: ")+10:res.find("RSSI")-1]
  30. wifiInfo["RSSI"] = res[res.find("RSSI: ")+6:res.find("link_speed")-1]
  31. wifiInfo["link_speed"] = res[res.find("link_speed: ")+10:res.find("IP address: ")-1]
  32. wifiInfo["noise"] = res[res.find("noise: ")+6:res.find("noise: ")+11]
  33. else:
  34. wifiInfo["state"]=False
  35. wifiInfo["AP"] = "N/A"
  36. wifiInfo["IP"] = "N/A"
  37. wifiInfo["frequency"] = "N/A"
  38. wifiInfo["RSSI"] = "N/A"
  39. wifiInfo["link_speed"] = "N/A"
  40. wifiInfo["noise"] = "N/A"
  41. return wifiInfo
  42. if ScreenOrientation:
  43. CAMERAROTATE = +180
  44. else:
  45. CAMERAROTATE = +90
  46. def urldownload(url,filepath=None,filename=None,canvas1=image.new(size = (320, 40))):
  47. """
  48. 下载文件到指定目录
  49. :param url: 文件下载的url
  50. :param filename: 要存放的目录及文件名,例如:./test.xls
  51. :return:
  52. """
  53. headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"}
  54. down_res = requests.get(url,headers=headers,stream=True)
  55. chunk_size = 1024 * 1024 # 单次请求最大值
  56. content_size = int(down_res.headers['content-length']) # 内容体总大小
  57. data_count = 0
  58. with open(filepath,'wb') as file:
  59. for data in down_res.iter_content(chunk_size=chunk_size):
  60. file.write(data)
  61. data_count = data_count + len(data)
  62. now_jd = (data_count / content_size) * 100
  63. # print("\r 文件下载进度:%d%%(%d/%d) - %s" % (now_jd, data_count, content_size, filename), end=" ")
  64. canvas1.clear()
  65. canvas1.draw_string(0,0,str("下载中: ") + filename, scale = 1, color = (255, 0, 0), thickness = 2)
  66. canvas1.draw_string(0,20 , str("进度: %d%%(%d/%d)" % (now_jd, data_count, content_size)), scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  67. display.show(canvas1)
  68. # time.sleep(1)
  69. # return down_res.code
  70. canvas1 = image.new(size = (320, 40))
  71. startConnect=False
  72. param = ''
  73. bin = ''
  74. py = ''
  75. def getWifiConnectState():
  76. cmd = "wifi_get_connection_info_test 1"
  77. res = os.popen(cmd).read()
  78. wifiInfo = {}
  79. if res.find('get connection infomation successfully!') != -1:
  80. wifiInfo["state"]=True
  81. wifiInfo["AP"] = res[res.find("Connected AP: ")+13:res.find("IP address: ")-1]
  82. wifiInfo["IP"] = res[res.find("IP address: ")+12:res.find("frequency")-1]
  83. wifiInfo["frequency"] = res[res.find("frequency: ")+10:res.find("RSSI")-1]
  84. wifiInfo["RSSI"] = res[res.find("RSSI: ")+6:res.find("link_speed")-1]
  85. wifiInfo["link_speed"] = res[res.find("link_speed: ")+10:res.find("IP address: ")-1]
  86. wifiInfo["noise"] = res[res.find("noise: ")+6:res.find("noise: ")+11]
  87. else:
  88. wifiInfo["state"]=False
  89. wifiInfo["AP"] = "N/A"
  90. wifiInfo["IP"] = "N/A"
  91. wifiInfo["frequency"] = "N/A"
  92. wifiInfo["RSSI"] = "N/A"
  93. wifiInfo["link_speed"] = "N/A"
  94. wifiInfo["noise"] = "N/A"
  95. return wifiInfo
  96. def getPrivateIp():
  97. st = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
  98. try:
  99. st.connect(("10.255.255.255",1))
  100. IP = st.getsockname()[0]
  101. except Exception:
  102. IP = "127.0.0.1"
  103. finally:
  104. st.close()
  105. return IP
  106. ssidInfo = ""
  107. passwordInfo = ""
  108. checkConnectState = False
  109. connectText = ""
  110. startConnectWifi=False
  111. wifiConnectState = False
  112. # PublicIp=""
  113. PrivateIP=""
  114. runConnectSig=True
  115. connectSuccessSig=False
  116. while True:
  117. canvas = camera.capture()
  118. IP = getPrivateIp()
  119. if ssidInfo!="" and passwordInfo!="":
  120. startConnectWifi=True
  121. #connectText = "Waitting for Connection..."
  122. #canvas.draw_string(10,40, ssidInfo+" "+str(len(ssidInfo))+" "+str(type(ssidInfo)), scale = 1.5, color = (0,0,0), thickness = 1)
  123. canvas.draw_string(10,50, "正在连接WIFI...", scale = 1.5, color = (0,0,0), thickness = 1)
  124. display.show(canvas)
  125. if startConnectWifi==True:
  126. canvas_1 = image.new(size = (320, 320), color = (255,255,255), mode = "RGB")
  127. canvas.draw_image(canvas_1,0,0, alpha=0.4)
  128. if wifiConnectState == True:
  129. if key_B.is_pressed():
  130. while (key_B.is_pressed() == True):
  131. time.sleep(0.001)
  132. startConnectWifi= False
  133. connectText = ""
  134. checkConnectState = False
  135. connectSuccessSig = False
  136. wifiConnectState = False
  137. runConnectSig== True
  138. ssidInfo=""
  139. passwordInfo=""
  140. if checkConnectState == True:
  141. if connectSuccessSig == False:
  142. # PublicIp=getPublicIp()
  143. PrivateIP=getPrivateIp()
  144. connectSuccessSig = True
  145. connectText = "WiFi连接成功!"
  146. canvas.draw_string(10,50, "WiFi连接成功!", scale = 1.5, color = (0,0,0) , thickness = 1)
  147. canvas.draw_string(10,80, "局域网IP地址:" + PrivateIP, scale = 1.5, color = (0,0,0), thickness = 1)
  148. canvas.draw_image((image.open("/root/preset/img/restart_ff0000_24x24.png")).rotate(0, adjust=0),8,216,alpha=1)
  149. ssidInfo=""
  150. passwordInfo=""
  151. display.show(canvas)
  152. time.sleep(3)
  153. canvas.clear()
  154. break
  155. else:
  156. pass
  157. else:
  158. if checkConnectState == False:
  159. os.system("wifi_disconnect_ap_test")
  160. os.system('wifi_connect_chinese_ap_test '+ssidInfo+' '+passwordInfo+'')
  161. wifiConnectState=getWifiConnectState()["state"]
  162. checkConnectState = True
  163. if checkConnectState == True:
  164. if key_B.is_pressed():
  165. while (key_B.is_pressed() == True):
  166. time.sleep(0.001)
  167. startConnectWifi= False
  168. connectText = ""
  169. checkConnectState = False
  170. connectSuccessSig = False
  171. wifiConnectState = False
  172. runConnectSig== True
  173. passwordInfo = ""
  174. ssidInfo = ""
  175. else:
  176. if runConnectSig== True:
  177. connectText = "正在连接WIFI..."
  178. runConnectSig= False
  179. else:
  180. passwordInfo = ""
  181. ssidInfo = ""
  182. connectText = "WiFi连接失败!"
  183. canvas.draw_image((image.open("/root/preset/img/restart_ff0000_24x24.png")).rotate(0, adjust=0),8,216,alpha=1)
  184. canvas.draw_string(10,50, connectText, scale = 1.5, color = (0,0,0) , thickness = 1)
  185. else:
  186. mks = canvas.find_qrcodes()
  187. for mk in mks:
  188. #外框数据
  189. X = mk['x']
  190. Y = mk['y']
  191. W = mk['w']
  192. H = mk['h']
  193. #二维码信息
  194. string = mk['payload']
  195. codeData = string.split(";")
  196. ssidInfo = codeData[0].split(":")[1]
  197. passwordInfo = codeData[1].split(":")[1]
  198. #画外框
  199. canvas.draw_rectangle(X, Y, X + W, Y + H, color=(0, 0, 255), thickness = 2)
  200. #打印信息
  201. canvas.draw_string(int(X) , int(Y - 45) , "SSID:"+ssidInfo, scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  202. canvas.draw_string(int(X) , int(Y - 25) , "PASSWORD:"+passwordInfo, scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  203. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  204. #canvas.draw_image((image.open("/root/preset/img/camera_bfbfbf_24x24.png")).rotate(0, adjust=0),292,2,alpha=1)
  205. display.show(canvas)
  206. while True:
  207. canvas = lcdRotation(camera.capture(),CAMERAROTATE)
  208. # IP = extract_ip()
  209. if(str(param) != '' and str(bin) != '' and str(py) != ''):
  210. startConnect=True
  211. if startConnect==True:
  212. canvas.clear()
  213. time.sleep(5)
  214. urldownload("https://" + bin.split("//")[1],"/root/user/model/" + (str(bin).split("newModels/")[1].split("/")[0]) + ".bin",(str(bin).split("newModels/")[1].split("/")[0]) + ".bin",canvas1)
  215. urldownload("https://" + param.split("//")[1],"/root/user/model/" + (str(param).split("newModels/")[1].split("/")[0]) + ".param", (str(param).split("newModels/")[1].split("/")[0]) + ".param",canvas1)
  216. urldownload("https://" + py.split("//")[1],"/root/" + (str(py).split("newModels/")[1].split("/")[1]),"user_latest_code.py",canvas1)
  217. break
  218. else:
  219. mks = canvas.find_qrcodes()
  220. for mk in mks:
  221. #外框数据
  222. X = mk['x']
  223. Y = mk['y']
  224. W = mk['w']
  225. H = mk['h']
  226. #二维码信息
  227. try:
  228. string = mk['payload']
  229. codeData = json.loads(string)
  230. param = codeData['param']
  231. bin = codeData['bin']
  232. py = codeData['py']
  233. except:
  234. pass
  235. #画外框
  236. canvas.draw_rectangle(X, Y, X + W, Y + H, color=(0, 0, 255), thickness = 2)
  237. #打印信息
  238. canvas.draw_string(int(X) , int(Y - 35) , str(string), scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  239. canvas.draw_string(0, 0 , "扫描二维码来下载模型", scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  240. # canvas.draw_image((image.open("/root/preset/img/camera_ff0000_24x24.png")).rotate(0, adjust=0),280,2,alpha=1)
  241. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),290,208,alpha=1)
  242. display.show(canvas)
  243. os.system("ln -sf %s /tmp/event && touch /tmp/start" % "/root/user_latest_code.py")