30_train_data_collect.py 12 KB

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