31_training_deploy_model.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. import os
  15. ScreenOrientation = False
  16. try:
  17. if os.path.exists("/etc/cameraSize.cfg"):
  18. cameraSize = True
  19. else:
  20. cameraSize = False
  21. except:
  22. cameraSize = False
  23. def getLcdRotation(cameraCapture):
  24. global cameraSize
  25. if cameraSize:
  26. return lcdRotationNew(cameraCapture)
  27. else:
  28. return lcdRotation(cameraCapture)
  29. def lcdRotationNew(inputImg):
  30. global cameraSize,ScreenOrientation
  31. imageRotationBuffer = inputImg.crop(0, 0, 320, 240)
  32. if ScreenOrientation:
  33. imgRotationAim = image.new(size = (240, 320))
  34. rotationAngle = 90
  35. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  36. else:
  37. imgRotationAim = image.new(size = (320, 240))
  38. GETROTATION = imageRotationBuffer
  39. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  40. return GETROTATION
  41. def lcdRotation(inputImg):
  42. global cameraSize,ScreenOrientation
  43. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  44. if ScreenOrientation:
  45. imgRotationAim = image.new(size = (240, 320))
  46. rotationAngle = 180
  47. else:
  48. imgRotationAim = image.new(size = (320, 240))
  49. rotationAngle = 90
  50. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  51. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  52. return GETROTATION
  53. key_A = BUTTON(14)
  54. key_B = BUTTON(8)
  55. key_C = BUTTON(13)
  56. key_D = BUTTON(7)
  57. image.load_freetype("/root/preset/fonts/CascadiaCodePL-Italic.ttf")
  58. if cameraSize==True:
  59. camera.camera.config(size=(320,240))
  60. else:
  61. camera.camera.config(size=(240,320))
  62. def getWifiConnectState():
  63. cmd = "wifi_get_connection_info_test 1"
  64. res = os.popen(cmd).read()
  65. wifiInfo = {}
  66. if res.find('get connection infomation successfully!') != -1:
  67. wifiInfo["state"]=True
  68. wifiInfo["AP"] = res[res.find("Connected AP: ")+13:res.find("IP address: ")-1]
  69. wifiInfo["IP"] = res[res.find("IP address: ")+12:res.find("frequency")-1]
  70. wifiInfo["frequency"] = res[res.find("frequency: ")+10:res.find("RSSI")-1]
  71. wifiInfo["RSSI"] = res[res.find("RSSI: ")+6:res.find("link_speed")-1]
  72. wifiInfo["link_speed"] = res[res.find("link_speed: ")+10:res.find("IP address: ")-1]
  73. wifiInfo["noise"] = res[res.find("noise: ")+6:res.find("noise: ")+11]
  74. else:
  75. wifiInfo["state"]=False
  76. wifiInfo["AP"] = "N/A"
  77. wifiInfo["IP"] = "N/A"
  78. wifiInfo["frequency"] = "N/A"
  79. wifiInfo["RSSI"] = "N/A"
  80. wifiInfo["link_speed"] = "N/A"
  81. wifiInfo["noise"] = "N/A"
  82. return wifiInfo
  83. def urldownload(url,filepath=None,filename=None,canvas1=image.new(size = (320, 40))):
  84. """
  85. 下载文件到指定目录
  86. :param url: 文件下载的url
  87. :param filename: 要存放的目录及文件名,例如:./test.xls
  88. :return:
  89. """
  90. 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"}
  91. down_res = requests.get(url,headers=headers,stream=True)
  92. chunk_size = 1024 * 1024 # 单次请求最大值
  93. content_size = int(down_res.headers['content-length']) # 内容体总大小
  94. data_count = 0
  95. with open(filepath,'wb') as file:
  96. for data in down_res.iter_content(chunk_size=chunk_size):
  97. file.write(data)
  98. data_count = data_count + len(data)
  99. now_jd = (data_count / content_size) * 100
  100. # print("\r 文件下载进度:%d%%(%d/%d) - %s" % (now_jd, data_count, content_size, filename), end=" ")
  101. canvas1.clear()
  102. canvas1.draw_string(0,0,str("Loading: ") + filename, scale = 1, color = (255, 0, 0), thickness = 2)
  103. canvas1.draw_string(0,20 , str("Speed: %d%%(%d/%d)" % (now_jd, data_count, content_size)), scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  104. display.show(canvas1)
  105. # time.sleep(1)
  106. # return down_res.code
  107. canvas1 = image.new(size = (320, 40))
  108. startConnect=False
  109. param = ''
  110. bin = ''
  111. py = ''
  112. def getWifiConnectState():
  113. cmd = "wifi_get_connection_info_test 1"
  114. res = os.popen(cmd).read()
  115. wifiInfo = {}
  116. if res.find('get connection infomation successfully!') != -1:
  117. wifiInfo["state"]=True
  118. wifiInfo["AP"] = res[res.find("Connected AP: ")+13:res.find("IP address: ")-1]
  119. wifiInfo["IP"] = res[res.find("IP address: ")+12:res.find("frequency")-1]
  120. wifiInfo["frequency"] = res[res.find("frequency: ")+10:res.find("RSSI")-1]
  121. wifiInfo["RSSI"] = res[res.find("RSSI: ")+6:res.find("link_speed")-1]
  122. wifiInfo["link_speed"] = res[res.find("link_speed: ")+10:res.find("IP address: ")-1]
  123. wifiInfo["noise"] = res[res.find("noise: ")+6:res.find("noise: ")+11]
  124. else:
  125. wifiInfo["state"]=False
  126. wifiInfo["AP"] = "N/A"
  127. wifiInfo["IP"] = "N/A"
  128. wifiInfo["frequency"] = "N/A"
  129. wifiInfo["RSSI"] = "N/A"
  130. wifiInfo["link_speed"] = "N/A"
  131. wifiInfo["noise"] = "N/A"
  132. return wifiInfo
  133. def getPrivateIp():
  134. st = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
  135. try:
  136. st.connect(("10.255.255.255",1))
  137. IP = st.getsockname()[0]
  138. except Exception:
  139. IP = "127.0.0.1"
  140. finally:
  141. st.close()
  142. return IP
  143. ssidInfo = ""
  144. passwordInfo = ""
  145. checkConnectState = False
  146. connectText = ""
  147. startConnectWifi=False
  148. wifiConnectState = False
  149. # PublicIp=""
  150. PrivateIP=""
  151. runConnectSig=True
  152. connectSuccessSig=False
  153. while True:
  154. canvas = getLcdRotation(camera.capture())
  155. IP = getPrivateIp()
  156. if ssidInfo!="" and passwordInfo!="":
  157. startConnectWifi=True
  158. #connectText = "Waitting for Connection..."
  159. #canvas.draw_string(10,40, ssidInfo+" "+str(len(ssidInfo))+" "+str(type(ssidInfo)), scale = 1.5, color = (0,0,0), thickness = 1)
  160. canvas.draw_string(10,50, "Connect WIFI...", scale = 1.5, color = (0,0,0), thickness = 1)
  161. display.show(canvas)
  162. if startConnectWifi==True:
  163. canvas_1 = image.new(size = (320, 320), color = (255,255,255), mode = "RGB")
  164. canvas.draw_image(canvas_1,0,0, alpha=0.4)
  165. if wifiConnectState == True:
  166. if key_B.is_pressed():
  167. while (key_B.is_pressed() == True):
  168. time.sleep(0.001)
  169. startConnectWifi= False
  170. connectText = ""
  171. checkConnectState = False
  172. connectSuccessSig = False
  173. wifiConnectState = False
  174. runConnectSig== True
  175. ssidInfo=""
  176. passwordInfo=""
  177. if checkConnectState == True:
  178. if connectSuccessSig == False:
  179. # PublicIp=getPublicIp()
  180. PrivateIP=getPrivateIp()
  181. connectSuccessSig = True
  182. connectText = "WiFi Connect Success!"
  183. canvas.draw_string(10,50, "WiFi Connect Success!", scale = 1.5, color = (0,0,0) , thickness = 1)
  184. canvas.draw_string(10,80, "IP:" + PrivateIP, scale = 1.5, color = (0,0,0), thickness = 1)
  185. canvas.draw_image((image.open("/root/preset/img/restart_ff0000_24x24.png")).rotate(0, adjust=0),8,216,alpha=1)
  186. ssidInfo=""
  187. passwordInfo=""
  188. display.show(canvas)
  189. time.sleep(3)
  190. canvas.clear()
  191. break
  192. else:
  193. pass
  194. else:
  195. if checkConnectState == False:
  196. os.system("wifi_disconnect_ap_test")
  197. os.system('wifi_connect_chinese_ap_test '+ssidInfo+' '+passwordInfo+'')
  198. wifiConnectState=getWifiConnectState()["state"]
  199. checkConnectState = True
  200. if checkConnectState == True:
  201. if key_B.is_pressed():
  202. while (key_B.is_pressed() == True):
  203. time.sleep(0.001)
  204. startConnectWifi= False
  205. connectText = ""
  206. checkConnectState = False
  207. connectSuccessSig = False
  208. wifiConnectState = False
  209. runConnectSig== True
  210. passwordInfo = ""
  211. ssidInfo = ""
  212. else:
  213. if runConnectSig== True:
  214. connectText = "Connect WIFI..."
  215. runConnectSig= False
  216. else:
  217. passwordInfo = ""
  218. ssidInfo = ""
  219. connectText = "WiFi Connect Failed!"
  220. canvas.draw_image((image.open("/root/preset/img/restart_ff0000_24x24.png")).rotate(0, adjust=0),8,216,alpha=1)
  221. canvas.draw_string(10,50, connectText, scale = 1.5, color = (0,0,0) , thickness = 1)
  222. else:
  223. mks = canvas.find_qrcodes()
  224. for mk in mks:
  225. #外框数据
  226. X = mk['x']
  227. Y = mk['y']
  228. W = mk['w']
  229. H = mk['h']
  230. #二维码信息
  231. string = mk['payload']
  232. codeData = string.split(";")
  233. ssidInfo = codeData[0].split(":")[1]
  234. passwordInfo = codeData[1].split(":")[1]
  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 - 45) , "SSID:"+ssidInfo, scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  239. canvas.draw_string(int(X) , int(Y - 25) , "PASSWORD:"+passwordInfo, scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  240. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  241. #canvas.draw_image((image.open("/root/preset/img/camera_bfbfbf_24x24.png")).rotate(0, adjust=0),292,2,alpha=1)
  242. display.show(canvas)
  243. while True:
  244. canvas = getLcdRotation(camera.capture())
  245. # IP = extract_ip()
  246. if(str(param) != '' and str(bin) != '' and str(py) != ''):
  247. startConnect=True
  248. if startConnect==True:
  249. canvas.clear()
  250. time.sleep(5)
  251. 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)
  252. 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)
  253. urldownload("https://" + py.split("//")[1],"/root/" + (str(py).split("newModels/")[1].split("/")[1]),"user_latest_code.py",canvas1)
  254. break
  255. else:
  256. mks = canvas.find_qrcodes()
  257. for mk in mks:
  258. #外框数据
  259. X = mk['x']
  260. Y = mk['y']
  261. W = mk['w']
  262. H = mk['h']
  263. #二维码信息
  264. try:
  265. string = mk['payload']
  266. codeData = json.loads(string)
  267. param = codeData['param']
  268. bin = codeData['bin']
  269. py = codeData['py']
  270. except:
  271. pass
  272. #画外框
  273. canvas.draw_rectangle(X, Y, X + W, Y + H, color=(0, 0, 255), thickness = 2)
  274. #打印信息
  275. canvas.draw_string(int(X) , int(Y - 35) , str(string), scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  276. canvas.draw_string(0, 0 , "Scan the QR code to download the model", scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  277. # canvas.draw_image((image.open("/root/preset/img/camera_ff0000_24x24.png")).rotate(0, adjust=0),280,2,alpha=1)
  278. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),290,208,alpha=1)
  279. display.show(canvas)
  280. os.system("ln -sf %s /tmp/event && touch /tmp/start" % "/root/user_latest_code.py")