30_training_data_collect.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. if ScreenOrientation:
  22. CAMERAROTATE = +180
  23. else:
  24. CAMERAROTATE = +90
  25. def getWifiConnectState():
  26. cmd = "wifi_get_connection_info_test 1"
  27. res = os.popen(cmd).read()
  28. wifiInfo = {}
  29. if res.find('get connection infomation successfully!') != -1:
  30. wifiInfo["state"]=True
  31. wifiInfo["AP"] = res[res.find("Connected AP: ")+13:res.find("IP address: ")-1]
  32. wifiInfo["IP"] = res[res.find("IP address: ")+12:res.find("frequency")-1]
  33. wifiInfo["frequency"] = res[res.find("frequency: ")+10:res.find("RSSI")-1]
  34. wifiInfo["RSSI"] = res[res.find("RSSI: ")+6:res.find("link_speed")-1]
  35. wifiInfo["link_speed"] = res[res.find("link_speed: ")+10:res.find("IP address: ")-1]
  36. wifiInfo["noise"] = res[res.find("noise: ")+6:res.find("noise: ")+11]
  37. else:
  38. wifiInfo["state"]=False
  39. wifiInfo["AP"] = "N/A"
  40. wifiInfo["IP"] = "N/A"
  41. wifiInfo["frequency"] = "N/A"
  42. wifiInfo["RSSI"] = "N/A"
  43. wifiInfo["link_speed"] = "N/A"
  44. wifiInfo["noise"] = "N/A"
  45. return wifiInfo
  46. name = ''
  47. host = ''
  48. isCollect = False
  49. image_num = 0
  50. save_path = ""
  51. startConnect=False
  52. def getWifiConnectState():
  53. cmd = "wifi_get_connection_info_test 1"
  54. res = os.popen(cmd).read()
  55. wifiInfo = {}
  56. if res.find('get connection infomation successfully!') != -1:
  57. wifiInfo["state"]=True
  58. wifiInfo["AP"] = res[res.find("Connected AP: ")+13:res.find("IP address: ")-1]
  59. wifiInfo["IP"] = res[res.find("IP address: ")+12:res.find("frequency")-1]
  60. wifiInfo["frequency"] = res[res.find("frequency: ")+10:res.find("RSSI")-1]
  61. wifiInfo["RSSI"] = res[res.find("RSSI: ")+6:res.find("link_speed")-1]
  62. wifiInfo["link_speed"] = res[res.find("link_speed: ")+10:res.find("IP address: ")-1]
  63. wifiInfo["noise"] = res[res.find("noise: ")+6:res.find("noise: ")+11]
  64. else:
  65. wifiInfo["state"]=False
  66. wifiInfo["AP"] = "N/A"
  67. wifiInfo["IP"] = "N/A"
  68. wifiInfo["frequency"] = "N/A"
  69. wifiInfo["RSSI"] = "N/A"
  70. wifiInfo["link_speed"] = "N/A"
  71. wifiInfo["noise"] = "N/A"
  72. return wifiInfo
  73. def getPrivateIp():
  74. st = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
  75. try:
  76. st.connect(("10.255.255.255",1))
  77. IP = st.getsockname()[0]
  78. except Exception:
  79. IP = "127.0.0.1"
  80. finally:
  81. st.close()
  82. return IP
  83. ssidInfo = ""
  84. passwordInfo = ""
  85. checkConnectState = False
  86. connectText = ""
  87. startConnectWifi=False
  88. wifiConnectState = False
  89. # PublicIp=""
  90. PrivateIP=""
  91. runConnectSig=True
  92. connectSuccessSig=False
  93. while True:
  94. canvas = camera.capture()
  95. IP = getPrivateIp()
  96. if ssidInfo!="" and passwordInfo!="":
  97. startConnectWifi=True
  98. #connectText = "Waitting for Connection..."
  99. #canvas.draw_string(10,40, ssidInfo+" "+str(len(ssidInfo))+" "+str(type(ssidInfo)), scale = 1.5, color = (0,0,0), thickness = 1)
  100. canvas.draw_string(10,50, "正在连接WIFI,请等待...", scale = 1.5, color = (0,0,0), thickness = 1)
  101. display.show(canvas)
  102. if startConnectWifi==True:
  103. canvas_1 = image.new(size = (320, 320), color = (255,255,255), mode = "RGB")
  104. canvas.draw_image(canvas_1,0,0, alpha=0.4)
  105. if wifiConnectState == True:
  106. if key_B.is_pressed():
  107. while (key_B.is_pressed() == True):
  108. time.sleep(0.001)
  109. startConnectWifi= False
  110. connectText = ""
  111. checkConnectState = False
  112. connectSuccessSig = False
  113. wifiConnectState = False
  114. runConnectSig== True
  115. ssidInfo=""
  116. passwordInfo=""
  117. if checkConnectState == True:
  118. if connectSuccessSig == False:
  119. # PublicIp=getPublicIp()
  120. PrivateIP=getPrivateIp()
  121. connectSuccessSig = True
  122. connectText = "WiFi连接成功!"
  123. canvas.draw_string(10,50, "WiFi连接成功!", scale = 1.5, color = (0,0,0) , thickness = 1)
  124. canvas.draw_string(10,80, "局域网IP:" + PrivateIP, scale = 1.5, color = (0,0,0), thickness = 1)
  125. canvas.draw_image((image.open("/root/preset/img/restart_ff0000_24x24.png")).rotate(0, adjust=0),8,216,alpha=1)
  126. ssidInfo=""
  127. passwordInfo=""
  128. display.show(canvas)
  129. time.sleep(3)
  130. canvas.clear()
  131. break
  132. else:
  133. pass
  134. else:
  135. if checkConnectState == False:
  136. os.system("wifi_disconnect_ap_test")
  137. os.system('wifi_connect_chinese_ap_test '+ssidInfo+' '+passwordInfo+'')
  138. wifiConnectState=getWifiConnectState()["state"]
  139. checkConnectState = True
  140. if checkConnectState == True:
  141. if key_B.is_pressed():
  142. while (key_B.is_pressed() == True):
  143. time.sleep(0.001)
  144. startConnectWifi= False
  145. connectText = ""
  146. checkConnectState = False
  147. connectSuccessSig = False
  148. wifiConnectState = False
  149. runConnectSig== True
  150. passwordInfo = ""
  151. ssidInfo = ""
  152. else:
  153. if runConnectSig== True:
  154. connectText = "正在连接WIFI,请等待..."
  155. runConnectSig= False
  156. else:
  157. passwordInfo = ""
  158. ssidInfo = ""
  159. connectText = "WIFI连接失败!"
  160. canvas.draw_image((image.open("/root/preset/img/restart_ff0000_24x24.png")).rotate(0, adjust=0),8,216,alpha=1)
  161. canvas.draw_string(10,50, connectText, scale = 1.5, color = (0,0,0) , thickness = 1)
  162. else:
  163. mks = canvas.find_qrcodes()
  164. for mk in mks:
  165. #外框数据
  166. X = mk['x']
  167. Y = mk['y']
  168. W = mk['w']
  169. H = mk['h']
  170. #二维码信息
  171. string = mk['payload']
  172. codeData = string.split(";")
  173. ssidInfo = codeData[0].split(":")[1]
  174. passwordInfo = codeData[1].split(":")[1]
  175. #画外框
  176. canvas.draw_rectangle(X, Y, X + W, Y + H, color=(0, 0, 255), thickness = 2)
  177. #打印信息
  178. canvas.draw_string(int(X) , int(Y - 45) , "WIFI名:"+ssidInfo, scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  179. canvas.draw_string(int(X) , int(Y - 25) , "密码:"+passwordInfo, scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  180. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  181. #canvas.draw_image((image.open("/root/preset/img/camera_bfbfbf_24x24.png")).rotate(0, adjust=0),292,2,alpha=1)
  182. display.show(canvas)
  183. while True:
  184. canvas.clear()
  185. canvas = camera.capture()
  186. # IP = extract_ip()
  187. if str(name) != '' and str(host) != '':
  188. startConnect=True
  189. if startConnect==True:
  190. _COCOCLOUD_SEND_ENDPOINT = "https://" + host.split("//")[1] + "v831_update_code_connect_device"
  191. _COCOCLOUD_SEND_DATA = {"DatasetName":"" + str(name) + ""}
  192. try:
  193. _COCOCLOUD_SEND_REQUEST = requests.post(_COCOCLOUD_SEND_ENDPOINT, json = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 60)
  194. print(str(_COCOCLOUD_SEND_REQUEST.status_code)+", "+str(_COCOCLOUD_SEND_REQUEST.content))
  195. except BaseException as e:
  196. print(e)
  197. pass
  198. isCollect = True
  199. break
  200. else:
  201. mks = canvas.find_qrcodes()
  202. for mk in mks:
  203. #外框数据
  204. X = mk['x']
  205. Y = mk['y']
  206. W = mk['w']
  207. H = mk['h']
  208. #二维码信息
  209. string = mk['payload']
  210. try:
  211. codeData = string.split(";")
  212. name = codeData[0].split("=")[1]
  213. host = codeData[1].split("=")[1]
  214. except:
  215. pass
  216. #画外框
  217. canvas.draw_rectangle(X, Y, X + W, Y + H, color=(0, 0, 255), thickness = 2)
  218. #打印信息
  219. canvas.draw_string(int(X) , int(Y - 35) , str(string), scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  220. canvas.draw_string(0, 0 , "扫描二维码来访问图片标注服务器", scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  221. canvas.draw_string(0, 20 , "", scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  222. # canvas.draw_image((image.open("/root/preset/img/camera_ff0000_24x24.png")).rotate(0, adjust=0),290,2,alpha=1)
  223. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),290,208,alpha=1)
  224. display.show(canvas)
  225. isAlTake = False
  226. while True:
  227. canvas1 = camera.capture()
  228. if isCollect:
  229. if not isAlTake:
  230. if key_C.is_pressed() :
  231. while not (key_C.is_pressed() == False):
  232. time.sleep(0.01)
  233. image_num = image_num + 1
  234. save_path = ''.join([str(x) for x in ["/root/user/img/image", str(image_num), ".jpg"]])
  235. canvas1.save(save_path)
  236. isAlTake = True
  237. canvas1.draw_string(0, 0 , "请拍照来采集图片数据", scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  238. canvas1.draw_image((image.open("/root/preset/img/camera_ff0000_24x24.png")).rotate(0, adjust=0),290,2,alpha=1)
  239. canvas1.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),290,208,alpha=1)
  240. display.show(canvas1)
  241. if isAlTake:
  242. canvas1.draw_image((image.open(save_path)),0,0)
  243. # if key_D.is_pressed() :
  244. # while not (key_D.is_pressed() == False):
  245. # time.sleep(0.01)
  246. # img = open(save_path,'rb')
  247. # data = {"file": img}
  248. # param = {"name": str(name)}
  249. # res = requests.post("https://" + host.split("//")[1] + "v831_upload_image", files=data, data=param)
  250. # print(res.status_code)
  251. # if str(res.status_code) == "200":
  252. # isAlTake = False
  253. # if key_C.is_pressed() :
  254. # while not (key_C.is_pressed() == False):
  255. # time.sleep(0.01)
  256. # isAlTake = False
  257. img = open(save_path,'rb')
  258. data = {"file": img}
  259. param = {"name": str(name)}
  260. res = requests.post("https://" + host.split("//")[1] + "v831_upload_image", files=data, data=param)
  261. print(res.status_code)
  262. if str(res.status_code) == "200":
  263. isAlTake = False
  264. # canvas1.draw_image((image.open("/root/preset/img/upload_ff0000_24x24.png")).rotate(0, adjust=0),5,2,alpha=1)
  265. # canvas1.draw_image((image.open("/root/preset/img/cancel_ff0000_24x24.png")).rotate(0, adjust=0),280,2,alpha=1)
  266. canvas1.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),290,208,alpha=1)
  267. display.show(canvas1)
  268. display.show(canvas1)