30_training_data_collect.py 13 KB

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