BLE.html 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>BLE</title>
  6. </head>
  7. <body>
  8. <h1>BLE</h1>
  9. <button id="connectButton">Connect</button>
  10. <button id="sendButton" disabled>Send Data</button>
  11. <h1></h1>
  12. <textarea id="textarea" cols="30" rows="10">import json
  13. from maix import mjpg
  14. from maix import utils
  15. import base64
  16. import time
  17. import os
  18. import sys
  19. sys.path.append("/root/")
  20. import http.client
  21. import smbus2
  22. from CocoPi import singleRgb
  23. singleRgb=singleRgb()
  24. import requests
  25. from CocoPi import multiFuncGpio
  26. import _coco_mfcc
  27. from maix import camera
  28. from maix import display
  29. from maix import image
  30. from maix import nn
  31. from maix.nn.app import face
  32. from maix.nn.app.face import FaceRecognize
  33. from maix.nn import decoder
  34. class Face_Recognizer:
  35. def __init__(self, threshold = 0.5, nms = 0.3, max_face_num = 1):
  36. model = "/root/preset/model/retinaface.mud"
  37. model_fe = "/root/preset/model/fe_resnet.mud"
  38. self.input_size = (224, 224, 3)
  39. input_size_fe = (128, 128, 3)
  40. self.feature_len = 256
  41. self.features = []
  42. print("-- load model:", model)
  43. m = nn.load(model)
  44. print("-- load ok")
  45. print("-- load model:", model_fe)
  46. m_fe = nn.load(model_fe)
  47. print("-- load ok")
  48. self.recognizer = FaceRecognize(m, m_fe, self.feature_len, self.input_size, threshold, nms, max_face_num)
  49. print("-- init end")
  50. def get_faces(self, img, std_img = False):
  51. faces = self.recognizer.get_faces(img, std_img)
  52. return faces
  53. def __len__(self):
  54. return len(self.features)
  55. def add_user(self, name, feature):
  56. self.features.append([name, feature])
  57. return True
  58. def remove_user(self, name_del):
  59. rm = None
  60. for name, feature in self.features:
  61. if name_del == name:
  62. rm = [name, feature]
  63. if rm:
  64. self.features.remove(rm)
  65. return True
  66. return False
  67. def recognize(self, feature):
  68. max_score = 0
  69. uid = -1
  70. for i, user in enumerate(self.features):
  71. score = self.recognizer.compare(user[1], feature)
  72. if score > max_score:
  73. max_score = score
  74. uid = i
  75. if uid >= 0:
  76. return self.features[uid][0], max_score
  77. return None, 0
  78. def get_input_size(self):
  79. return self.input_size
  80. def get_feature_len(self):
  81. return self.feature_len
  82. def darw_info(self, img, box, points, disp_str, bg_color=(255, 0, 0, 255), font_color=(255, 255, 255, 255), font_size=32):
  83. font_wh = image.get_string_size(disp_str)
  84. for p in points:
  85. img.draw_rectangle(p[0] - 1, p[1] -1, p[0] + 1, p[1] + 1, color=bg_color)
  86. img.draw_rectangle(box[0], box[1], box[0] + box[2], box[1] + box[3], color=bg_color, thickness=2)
  87. if disp_str:
  88. img.draw_rectangle(box[0], box[1] - font_wh[1], box[0] + font_wh[0], box[1], color=bg_color, thickness = -1)
  89. img.draw_string(box[0], box[1] - font_wh[1], disp_str, color=font_color)
  90. def map_face(self,box,points,IMAGEFACESHOW):
  91. # print(box,points)
  92. for p in points:
  93. IMAGEFACESHOW.draw_rectangle(p[0] - 1, p[1] -1, p[0] + 1, p[1] + 1, color=(255,200,255))
  94. #return box,points
  95. def darw_title(self, img, dis_size ,key_l = None, key_r =None):
  96. if key_C:
  97. key_l = "| "+ key_l
  98. img.draw_string( 1, 2 ,key_l , scale = 1, color = (255, 255, 255), thickness = 2)
  99. if key_D:
  100. key_r = key_r+" |"
  101. w = int(dis_size[0] - 4 - image.get_string_size(key_r)[0] * 1)
  102. img.draw_string( w, 2 ,key_r , scale = 1, color = (255, 255, 255), thickness = 2)
  103. FACERECGNIZER = Face_Recognizer(0.5, 0.3, max_face_num = 4)
  104. try:
  105. with open("/root/user/model/recorded_face_features.py", "r") as file:
  106. FACERECGNIZER.features = json.loads(file.read())
  107. except:
  108. pass
  109. def v831_display_show_img_recognized(displayShow):
  110. global _img_recognized_y,_img_recognized_x,ScreenOrientation,cameraSize
  111. CANVASSHOWIMGAGE = ""
  112. if ScreenOrientation:
  113. displayShowCanvas = image.new(size = (240, 320))
  114. displayShowCanvas.draw_rectangle(0,0,240,320, color=(0,0,0), thickness=-1)
  115. displayShowCanvas.draw_image(displayShow,_img_recognized_x,_img_recognized_y,alpha=1)
  116. displayShowVER = displayShowCanvas.crop(0,0,240,320)
  117. displayShowVER = displayShowVER.rotate(-90, adjust=1)
  118. display.show(displayShowVER)
  119. else:
  120. displayShowCanvas = image.new(size = (320, 240))
  121. displayShowCanvas.draw_rectangle(0,0,320,240, color=(0,0,0), thickness=-1)
  122. displayShowCanvas.draw_image(displayShow,_img_recognized_x,_img_recognized_y,alpha=1)
  123. display.show(displayShowCanvas)
  124. def _E4_BA_BA_E8_87_89_E8_AD_98_E5_88_A5():
  125. global img_recognized, i, state, canvas, people_name_list, _img_recognized_x, _img_recognized_y, _canvas_x, _canvas_y, _COCOCLOUD_SEND_REQUEST, RecordState, recordFrequency, recordResultData, SETVFLIP, SETHMIRROT, ScreenOrientation, score_threshold, CLASSNAMEFACELIST
  126. FACESRECOGNITONRESULT = FACERECGNIZER.get_faces(img_recognized)
  127. if len(FACESRECOGNITONRESULT):
  128. for i in FACESRECOGNITONRESULT:
  129. FACERECGNIZER.map_face(i[1],i[2],img_recognized)
  130. if FACERECGNIZER.recognize(i[3])[1] > 70:
  131. img_recognized.draw_string(0,0, FACERECGNIZER.recognize(i[3])[0], scale = 1, color = (51,204,0) , thickness = 1)
  132. img_recognized.draw_rectangle(i[1][0],i[1][1], i[1][0]+i[1][2],i[1][1]+ i[1][3], color=(51,204,0), thickness=1)
  133. state = "歡迎語"
  134. else:
  135. img_recognized.draw_rectangle(i[1][0],i[1][1], i[1][0]+i[1][2],i[1][1]+ i[1][3], color=(255,255,255), thickness=1)
  136. v831_display_show_img_recognized(img_recognized)
  137. def getNetworkDate_noexit():
  138. global getDateNum
  139. try:
  140. coon = http.client.HTTPConnection("www.baidu.com")
  141. coon.request("GET","/")
  142. r = coon.getresponse()
  143. ts = r.getheader("date")
  144. GMT_time = time.strptime(ts[5:25],"%d %b %Y %H:%M:%S")
  145. BeiJing_time = time.localtime(time.mktime(GMT_time) + 8*60*60)
  146. format_time = time.strftime("%Y-%m-%d %H:%M:%S",BeiJing_time)
  147. command = "date -s "+"\"{}\"".format(format_time)
  148. os.system(command)
  149. getDateNum = 1
  150. # sys.exit()
  151. except:
  152. pass
  153. def wifi_is_content():
  154. global getDateNum
  155. cmd = "ping -c 4 www.baidu.com"
  156. res = os.popen(cmd).read()
  157. data = False
  158. if res:
  159. data = True
  160. return data
  161. def Wi_Fi_E9_80_A3_E6_8E_A5():
  162. global img_recognized, i, state, canvas, people_name_list, _img_recognized_x, _img_recognized_y, _canvas_x, _canvas_y, _COCOCLOUD_SEND_REQUEST, RecordState, recordFrequency, recordResultData, SETVFLIP, SETHMIRROT, ScreenOrientation, score_threshold, CLASSNAMEFACELIST
  163. WiFiSSID = "CocoRobo_SZ"
  164. WiFiPSD = "cocorobo2019"
  165. os.system("wifi_disconnect_ap_test")
  166. os.system("wifi_connect_chinese_ap_test "+WiFiSSID+" "+WiFiPSD+"")
  167. #CLIENT = ntplib.NTPClient()
  168. #RESPONSE = CLIENT.request('127.0.0.1')
  169. getNetworkDate_noexit()
  170. while not (wifi_is_content()):
  171. singleRgb.setBrightness(5)
  172. singleRgb.setColor(255,0,0)
  173. time.sleep(0.05)
  174. singleRgb.show()
  175. singleRgb.setBrightness(5)
  176. singleRgb.setColor(0,255,0)
  177. time.sleep(0.05)
  178. singleRgb.show()
  179. def v831_display_show_canvas(displayShow):
  180. global _canvas_y,_canvas_x,ScreenOrientation,cameraSize
  181. CANVASSHOWIMGAGE = ""
  182. if ScreenOrientation:
  183. displayShowCanvas = image.new(size = (240, 320))
  184. displayShowCanvas.draw_rectangle(0,0,240,320, color=(0,0,0), thickness=-1)
  185. displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
  186. displayShowVER = displayShowCanvas.crop(0,0,240,320)
  187. displayShowVER = displayShowVER.rotate(-90, adjust=1)
  188. display.show(displayShowVER)
  189. else:
  190. displayShowCanvas = image.new(size = (320, 240))
  191. displayShowCanvas.draw_rectangle(0,0,320,240, color=(0,0,0), thickness=-1)
  192. displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
  193. display.show(displayShowCanvas)
  194. def _E8_AA_9E_E9_9F_B3_E8_AD_98_E5_88_A5():
  195. global img_recognized, i, state, canvas, people_name_list, _img_recognized_x, _img_recognized_y, _canvas_x, _canvas_y, _COCOCLOUD_SEND_REQUEST, RecordState, recordFrequency, recordResultData, SETVFLIP, SETHMIRROT, ScreenOrientation, score_threshold, CLASSNAMEFACELIST
  196. if RecordState == 0:
  197. mfcc.recognize()
  198. recordResultData = None
  199. time.sleep(1)
  200. RecordState = 1
  201. if RecordState == 1:
  202. recordData = mfcc.state()
  203. if recordData == mfcc._mfcc_result:
  204. recordResultData = mfcc.result()
  205. time.sleep(1)
  206. RecordState = 0
  207. canvas.clear()
  208. if recordResultData == 0:
  209. canvas.draw_string(0,30, "I want to borrow a ball.", scale = 1, color = (51,204,0) , thickness = 1)
  210. v831_display_show_canvas(canvas)
  211. state = "借球提示語"
  212. elif recordResultData == 1:
  213. canvas.draw_string(0,30, "Here's the ball back!", scale = 1, color = (51,204,0) , thickness = 1)
  214. v831_display_show_canvas(canvas)
  215. time.sleep(1)
  216. _E9_82_84_E7_90_83_E6_8F_90_E7_A4_BA_E8_AA_9E()
  217. time.sleep(2)
  218. state = "體育用品識別"
  219. elif recordResultData == 2:
  220. canvas.draw_string(0,30, "Basketball", scale = 1, color = (51,204,0) , thickness = 1)
  221. v831_display_show_canvas(canvas)
  222. _E7_B1_83_E7_90_83_E6_AB_83_E9_96_80_E6_8E_A7_E5_88_B6()
  223. _COCOCLOUD_SEND_ENDPOINT = "http://api.cocorobo.cn/iot/data/eventAPIKeyJson/3314aaaae706e22e79f625b6be4bac17"
  224. _COCOCLOUD_SEND_DATA = {"Basketball":0}
  225. try:
  226. _COCOCLOUD_SEND_REQUEST = requests.post(_COCOCLOUD_SEND_ENDPOINT, json = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 60)
  227. print(str(_COCOCLOUD_SEND_REQUEST.status_code)+", "+str(_COCOCLOUD_SEND_REQUEST.content))
  228. except BaseException as e:
  229. print(e)
  230. pass
  231. state = "人臉識別"
  232. elif recordResultData == 3:
  233. canvas.draw_string(0,30, "Volleyball", scale = 1, color = (51,204,0) , thickness = 1)
  234. v831_display_show_canvas(canvas)
  235. _E6_8E_92_E7_90_83_E6_AB_83_E9_96_80_E6_8E_A7_E5_88_B6()
  236. _COCOCLOUD_SEND_ENDPOINT = "http://api.cocorobo.cn/iot/data/eventAPIKeyJson/3314aaaae706e22e79f625b6be4bac17"
  237. _COCOCLOUD_SEND_DATA = {"Volleyball":0}
  238. try:
  239. _COCOCLOUD_SEND_REQUEST = requests.post(_COCOCLOUD_SEND_ENDPOINT, json = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 60)
  240. print(str(_COCOCLOUD_SEND_REQUEST.status_code)+", "+str(_COCOCLOUD_SEND_REQUEST.content))
  241. except BaseException as e:
  242. print(e)
  243. pass
  244. state = "人臉識別"
  245. def _E9_82_84_E7_90_83_E6_8F_90_E7_A4_BA_E8_AA_9E():
  246. global img_recognized, i, state, canvas, people_name_list, _img_recognized_x, _img_recognized_y, _canvas_x, _canvas_y, _COCOCLOUD_SEND_REQUEST, RecordState, recordFrequency, recordResultData, SETVFLIP, SETHMIRROT, ScreenOrientation, score_threshold, CLASSNAMEFACELIST
  247. canvas.clear()
  248. if ScreenOrientation:
  249. canvas = image.new(size = (240, 320), color = (255,255,255), mode = "RGB")
  250. else:
  251. canvas = image.new(size = (320, 240), color = (255,255,255), mode = "RGB")
  252. canvas.draw_string(2,60, "請將體育用品", scale = 2.8, color = (102,0,204) , thickness = 1)
  253. canvas.draw_string(2,140, "放在鏡頭前面", scale = 2.8, color = (102,51,255) , thickness = 1)
  254. canvas.draw_string(2,220, "進行識別", scale = 2.8, color = (102,51,255) , thickness = 1)
  255. v831_display_show_canvas(canvas)
  256. def _E6_AD_A1_E8_BF_8E_E8_AA_9E():
  257. global img_recognized, i, state, canvas, people_name_list, _img_recognized_x, _img_recognized_y, _canvas_x, _canvas_y, _COCOCLOUD_SEND_REQUEST, RecordState, recordFrequency, recordResultData, SETVFLIP, SETHMIRROT, ScreenOrientation, score_threshold, CLASSNAMEFACELIST
  258. canvas.draw_string(0,0, str("Hi,") + str(FACERECGNIZER.recognize(i[3])[0]), scale = 3, color = (255,255,255) , thickness = 1)
  259. canvas.draw_string(5,50, "歡迎使用CocoPi", scale = 2, color = (255,255,255) , thickness = 1)
  260. canvas.draw_string(5,90, "體育用品共享櫃", scale = 2, color = (255,255,255) , thickness = 1)
  261. canvas.draw_rectangle(0,140, 0+240,140+ 180, color=(255,255,255), thickness=-1)
  262. canvas.draw_string(0,150, "請說出", scale = 2, color = (51,102,255) , thickness = 1)
  263. canvas.draw_string(0,200, "I want to borrow a ball.", scale = 1.4, color = (51,102,255) , thickness = 1)
  264. canvas.draw_string(120,240, "或", scale = 1.5, color = (51,102,255) , thickness = 1)
  265. canvas.draw_string(0,280, "Here's the ball back!", scale = 1.4, color = (51,102,255) , thickness = 1)
  266. v831_display_show_canvas(canvas)
  267. S1= multiFuncGpio(0,1)
  268. S2= multiFuncGpio(1,1)
  269. mfcc = _coco_mfcc.MFCC(is_reply=False)
  270. cameraSize = True
  271. def CAMERATYPE():
  272. global cameraSize
  273. if os.path.exists("/etc/cameraSize.cfg"):
  274. cameraSize = True
  275. else:
  276. cameraSize = False
  277. CAMERATYPE()
  278. image.load_freetype("/root/preset/fonts/simhei.ttf")
  279. max_face_num = 4
  280. detect_threshold = 0.5
  281. detect_nms = 0.3
  282. #score_threshold = 70
  283. FEATURES = []
  284. def lcdRotation(inputImg):
  285. global SETVFLIP,SETHMIRROT,cameraSize,ScreenOrientation
  286. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  287. if ScreenOrientation:
  288. imgRotationAim = image.new(size = (240, 320))
  289. rotationAngle = 180
  290. else:
  291. imgRotationAim = image.new(size = (320, 240))
  292. rotationAngle = 90
  293. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  294. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  295. if SETVFLIP and not SETHMIRROT:
  296. GETROTATIONs = GETROTATION.flip(0)
  297. if SETHMIRROT and not SETVFLIP:
  298. GETROTATIONs = GETROTATION.flip(1)
  299. if SETVFLIP and SETHMIRROT:
  300. GETROTATION1 = GETROTATION.flip(0)
  301. GETROTATION = GETROTATION1.flip(1)
  302. return GETROTATION
  303. def lcdRotationNew(inputImg):
  304. global SETVFLIP,SETHMIRROT,cameraSize,ScreenOrientation
  305. imageRotationBuffer = inputImg.crop(0, 0, 320, 240)
  306. if ScreenOrientation:
  307. imgRotationAim = image.new(size = (240, 320))
  308. rotationAngle = 90
  309. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  310. else:
  311. imgRotationAim = image.new(size = (320, 240))
  312. GETROTATION = imageRotationBuffer
  313. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  314. if SETVFLIP and not SETHMIRROT:
  315. GETROTATIONs = GETROTATION.flip(0)
  316. elif SETHMIRROT and not SETVFLIP:
  317. GETROTATIONs = GETROTATION.flip(1)
  318. elif SETVFLIP and SETHMIRROT:
  319. GETROTATION1 = GETROTATION.flip(0)
  320. GETROTATION = GETROTATION1.flip(1)
  321. return GETROTATION
  322. def getLcdRotation(cameraCapture):
  323. global cameraSize
  324. if cameraSize:
  325. return lcdRotationNew(cameraCapture)
  326. else:
  327. return lcdRotation(cameraCapture)
  328. def _E9_AB_94_E8_82_B2_E7_94_A8_E5_93_81_E8_AD_98_E5_88_A5():
  329. global img_recognized, i, state, canvas, people_name_list, _img_recognized_x, _img_recognized_y, _canvas_x, _canvas_y, _COCOCLOUD_SEND_REQUEST, RecordState, recordFrequency, recordResultData, SETVFLIP, SETHMIRROT, ScreenOrientation, score_threshold, CLASSNAMEFACELIST
  330. out = Yolo.model.forward(img_recognized, quantize=True, layout="hwc")
  331. boxes, probs = Yolo.decoder.run(out, nms=0.3, threshold=0.5, img_size=(224, 224))
  332. if len(boxes):
  333. for boxesi, box in enumerate(boxes):
  334. boxes[boxesi].append(probs[boxesi])
  335. if len(boxes):
  336. for i in (boxes):
  337. img_recognized.draw_string((i[0]),(i[1]), (Yolo.labels[i[4][0]]), scale = 1, color = (255,0,0) , thickness = 1)
  338. img_recognized.draw_rectangle((i[0]),(i[1]),(i[0]+i[2]),(i[1]+i[3]), color=(255,0,0), thickness=1)
  339. if (Yolo.labels[i[4][0]]) == "lanqiu":
  340. _E7_B1_83_E7_90_83_E6_AD_B8_E9_82_84()
  341. _E7_B1_83_E7_90_83_E6_AB_83_E9_96_80_E6_8E_A7_E5_88_B6()
  342. _COCOCLOUD_SEND_ENDPOINT = "http://api.cocorobo.cn/iot/data/eventAPIKeyJson/3314aaaae706e22e79f625b6be4bac17"
  343. _COCOCLOUD_SEND_DATA = {"Basketball":1}
  344. try:
  345. _COCOCLOUD_SEND_REQUEST = requests.post(_COCOCLOUD_SEND_ENDPOINT, json = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 60)
  346. print(str(_COCOCLOUD_SEND_REQUEST.status_code)+", "+str(_COCOCLOUD_SEND_REQUEST.content))
  347. except BaseException as e:
  348. print(e)
  349. pass
  350. canvas.clear()
  351. state = "人臉識別"
  352. elif (Yolo.labels[i[4][0]]) == "paiqiu":
  353. _E6_8E_92_E7_90_83_E6_AD_B8_E9_82_84()
  354. _E6_8E_92_E7_90_83_E6_AB_83_E9_96_80_E6_8E_A7_E5_88_B6()
  355. _COCOCLOUD_SEND_ENDPOINT = "http://api.cocorobo.cn/iot/data/eventAPIKeyJson/3314aaaae706e22e79f625b6be4bac17"
  356. _COCOCLOUD_SEND_DATA = {"Volleyball":1}
  357. try:
  358. _COCOCLOUD_SEND_REQUEST = requests.post(_COCOCLOUD_SEND_ENDPOINT, json = _COCOCLOUD_SEND_DATA , headers = { "Content-type": "application/json" }, timeout = 60)
  359. print(str(_COCOCLOUD_SEND_REQUEST.status_code)+", "+str(_COCOCLOUD_SEND_REQUEST.content))
  360. except BaseException as e:
  361. print(e)
  362. pass
  363. canvas.clear()
  364. state = "人臉識別"
  365. v831_display_show_img_recognized(img_recognized)
  366. def _E7_B1_83_E7_90_83_E6_AB_83_E9_96_80_E6_8E_A7_E5_88_B6():
  367. global img_recognized, i, state, canvas, people_name_list, _img_recognized_x, _img_recognized_y, _canvas_x, _canvas_y, _COCOCLOUD_SEND_REQUEST, RecordState, recordFrequency, recordResultData, SETVFLIP, SETHMIRROT, ScreenOrientation, score_threshold, CLASSNAMEFACELIST
  368. S1.servoCtrl(180)
  369. time.sleep(2)
  370. S1.servoCtrl(75)
  371. def _E6_8E_92_E7_90_83_E6_AB_83_E9_96_80_E6_8E_A7_E5_88_B6():
  372. global img_recognized, i, state, canvas, people_name_list, _img_recognized_x, _img_recognized_y, _canvas_x, _canvas_y, _COCOCLOUD_SEND_REQUEST, RecordState, recordFrequency, recordResultData, SETVFLIP, SETHMIRROT, ScreenOrientation, score_threshold, CLASSNAMEFACELIST
  373. S2.servoCtrl(180)
  374. time.sleep(2)
  375. S2.servoCtrl(70)
  376. def _E5_80_9F_E7_90_83_E6_8F_90_E7_A4_BA_E8_AA_9E():
  377. global img_recognized, i, state, canvas, people_name_list, _img_recognized_x, _img_recognized_y, _canvas_x, _canvas_y, _COCOCLOUD_SEND_REQUEST, RecordState, recordFrequency, recordResultData, SETVFLIP, SETHMIRROT, ScreenOrientation, score_threshold, CLASSNAMEFACELIST
  378. if ScreenOrientation:
  379. canvas = image.new(size = (240, 320), color = (255,255,255), mode = "RGB")
  380. else:
  381. canvas = image.new(size = (320, 240), color = (255,255,255), mode = "RGB")
  382. canvas.draw_string(10,50, "請說出", scale = 3, color = (102,0,204) , thickness = 1)
  383. canvas.draw_string(10,120, "你要借的體", scale = 3, color = (102,51,255) , thickness = 1)
  384. canvas.draw_string(10,190, "育用品英文", scale = 3, color = (102,51,255) , thickness = 1)
  385. v831_display_show_canvas(canvas)
  386. def _E6_8E_92_E7_90_83_E6_AD_B8_E9_82_84():
  387. global img_recognized, i, state, canvas, people_name_list, _img_recognized_x, _img_recognized_y, _canvas_x, _canvas_y, _COCOCLOUD_SEND_REQUEST, RecordState, recordFrequency, recordResultData, SETVFLIP, SETHMIRROT, ScreenOrientation, score_threshold, CLASSNAMEFACELIST
  388. if ScreenOrientation:
  389. canvas = image.new(size = (240, 320), color = (0,100,0), mode = "RGB")
  390. else:
  391. canvas = image.new(size = (320, 240), color = (0,100,0), mode = "RGB")
  392. canvas.draw_image((image.open("/root/user/img/paiqiu.png")),20,0,alpha=1)
  393. canvas.draw_string(10,200, "排球已歸還", scale = 3, color = (255,255,255) , thickness = 1)
  394. canvas.draw_string(5,260, "歡迎下次使用", scale = 2.8, color = (255,255,255) , thickness = 1)
  395. v831_display_show_canvas(canvas)
  396. def _E7_B1_83_E7_90_83_E6_AD_B8_E9_82_84():
  397. global img_recognized, i, state, canvas, people_name_list, _img_recognized_x, _img_recognized_y, _canvas_x, _canvas_y, _COCOCLOUD_SEND_REQUEST, RecordState, recordFrequency, recordResultData, SETVFLIP, SETHMIRROT, ScreenOrientation, score_threshold, CLASSNAMEFACELIST
  398. canvas.clear()
  399. if ScreenOrientation:
  400. canvas = image.new(size = (240, 320), color = (0,100,0), mode = "RGB")
  401. else:
  402. canvas = image.new(size = (320, 240), color = (0,100,0), mode = "RGB")
  403. canvas.draw_image((image.open("/root/user/img/lanqiu.png")),20,0,alpha=1)
  404. canvas.draw_string(10,200, "籃球已歸還", scale = 3, color = (255,255,255) , thickness = 1)
  405. canvas.draw_string(5,260, "歡迎下次使用", scale = 2.8, color = (255,255,255) , thickness = 1)
  406. v831_display_show_canvas(canvas)
  407. _img_recognized_x = 0
  408. _img_recognized_y = 0
  409. _canvas_x = 0
  410. _canvas_y = 0
  411. _COCOCLOUD_SEND_REQUEST = None
  412. RecordState = 0
  413. recordFrequency = 0
  414. recordResultData = None
  415. SETVFLIP = False
  416. SETHMIRROT = False
  417. ScreenOrientation = True
  418. score_threshold = 70
  419. Wi_Fi_E9_80_A3_E6_8E_A5()
  420. S1.servoCtrl(75)
  421. S2.servoCtrl(70)
  422. SETVFLIP = True
  423. if cameraSize==True:
  424. camera.camera.config(size=(320,240))
  425. else:
  426. camera.camera.config(size=(240,320))
  427. canvas = image.new(size = (240, 320))
  428. img_recognized = image.new(size = (240, 320))
  429. _img_recognized_x, _img_recognized_y = 8,48
  430. people_name_list = ["張三", "李四", "王五"]
  431. CLASSNAMEFACELIST = people_name_list
  432. class Yolo:
  433. labels = ["ganlanqiu", "lanqiu", "paiqiu", "wangqiu", "yumaoqiu", "zuqiu"]
  434. anchors = [1.19, 1.98, 2.79, 4.59, 4.53, 8.92, 8.06, 5.29, 10.32, 10.65]
  435. m = {
  436. "param": "/root/user/model/ball.param",
  437. "bin": "/root/user/model/ball.bin"
  438. }
  439. options = {
  440. "model_type": "awnn",
  441. "inputs": {
  442. "input0": (224, 224, 3)
  443. },
  444. "outputs": {
  445. "output0": (7, 7, (1+4+len(labels))*5)
  446. },
  447. "mean": [127.5, 127.5, 127.5],
  448. "norm": [0.0078125, 0.0078125, 0.0078125],
  449. }
  450. def __init__(self):
  451. from maix import nn
  452. from maix.nn import decoder
  453. self.model = nn.load(self.m, opt=self.options)
  454. self.decoder = decoder.Yolo2(len(self.labels), self.anchors, net_in_size=(224, 224), net_out_size=(7, 7))
  455. def __del__(self):
  456. del self.model
  457. del self.decoder
  458. Yolo = Yolo()
  459. state = "人臉識別"
  460. while True:
  461. img_recognized = getLcdRotation(camera.capture())
  462. img_recognized = img_recognized.crop(0, 0,224, 224)
  463. if state == "人臉識別":
  464. _E4_BA_BA_E8_87_89_E8_AD_98_E5_88_A5()
  465. elif state == "歡迎語":
  466. _E6_AD_A1_E8_BF_8E_E8_AA_9E()
  467. _E8_AA_9E_E9_9F_B3_E8_AD_98_E5_88_A5()
  468. elif state == "借球提示語":
  469. _E5_80_9F_E7_90_83_E6_8F_90_E7_A4_BA_E8_AA_9E()
  470. _E8_AA_9E_E9_9F_B3_E8_AD_98_E5_88_A5()
  471. elif state == "體育用品識別":
  472. _E9_AB_94_E8_82_B2_E7_94_A8_E5_93_81_E8_AD_98_E5_88_A5()
  473. </textarea>
  474. <button id="sendfile">Send File</button>
  475. <div id="output"></div>
  476. <div id="test"></div>
  477. <script src="crc32.js"></script>
  478. <script src="JSZip.js"></script>
  479. <script>
  480. // 获取连接按钮和发送按钮
  481. const connectButton = document.getElementById('connectButton');
  482. const sendButton = document.getElementById('sendButton');
  483. const outputDiv = document.getElementById('output');
  484. const sendfile = document.getElementById('sendfile');
  485. const test = document.getElementById('test')
  486. let bluetoothDevice;
  487. let service;
  488. let characteristic;
  489. let timeoutId;
  490. connectButton.addEventListener('click', async () => {
  491. try {
  492. // 请求用户授权访问蓝牙设备
  493. bluetoothDevice = await navigator.bluetooth.requestDevice({
  494. filters: [{ services: ['0000fff0-0000-1000-8000-00805f9b34fb'] }]
  495. });
  496. // 连接到所选的蓝牙设备
  497. const server = await bluetoothDevice.gatt.connect();
  498. console.log('Connected to Bluetooth Device');
  499. // 启用发送按钮
  500. sendButton.disabled = false;
  501. // 读取来自蓝牙设备的数据
  502. server.getPrimaryService('0000fff0-0000-1000-8000-00805f9b34fb').then(service => {
  503. return service.getCharacteristic('0000fff2-0000-1000-8000-00805f9b34fb');
  504. })
  505. .then(characteristic => {
  506. characteristic.startNotifications();
  507. characteristic.addEventListener('characteristicvaluechanged', handleCharacteristicValueChanged);
  508. return characteristic.startNotifications();
  509. })
  510. .catch(error => {
  511. console.error('Error reading data:', error);
  512. });
  513. } catch (error) {
  514. console.error('Error connecting to Bluetooth Device:', error);
  515. }
  516. service = await bluetoothDevice.gatt.getPrimaryService('0000fff0-0000-1000-8000-00805f9b34fb');
  517. characteristic = await service.getCharacteristic('0000fff1-0000-1000-8000-00805f9b34fb');
  518. });
  519. sendButton.addEventListener('click', async () => {
  520. senddata('test')
  521. });
  522. async function blefilesend(event, datas, len) {
  523. const returnarr = new Uint8Array(event.target.value.buffer);
  524. //判断信号是否是发送文件的信号
  525. if (returnarr[0] === returnarr[1] && returnarr[0] === 0xFF) {
  526. try {
  527. let split = uint8ArrayToInt(returnarr.slice(2, 8));
  528. let crc = returnarr.slice(8, 16);
  529. //已经传输完毕无需再上传
  530. if (split == len && Uint8ArraysEqual(crc, sendcrc)) {
  531. senddata(new Uint8Array([0xFF, 0xFF, 0xFF]));
  532. }
  533. //断点续传,满足是同一个文件的情况
  534. else if (split != 0 && split < len) {
  535. const splitcrc = new TextEncoder().encode((CRC32.buf(new Uint8Array([...datas.subarray(0, split)])) >>> 0).toString(16).padStart(8, '0'));
  536. if (Uint8ArraysEqual(crc, splitcrc)) {
  537. for (let i = split; i < datas.length; i += 240) {
  538. if (i + 240 >= datas.length) {
  539. timeoutId = setTimeout(() => { senddata(new Uint8Array([0xFF, 0xFF, 0xFF])); }, 2000);
  540. window.blechangedcallback = async function (event) {
  541. window.blechangedcallback = null;
  542. clearTimeout(timeoutId);
  543. };
  544. }
  545. if (i == split) {
  546. await senddata(new Uint8Array([
  547. ...new Uint8Array([0xFF, 0xFF, 0x01]),
  548. ...datas.subarray(i, i + 240)]));
  549. } else {
  550. await senddata(new Uint8Array([...datas.subarray(i, i + 240)]));
  551. }
  552. }
  553. } else {
  554. for (let i = 0; i < datas.length; i += 240) {
  555. if (i + 240 >= datas.length) {
  556. timeoutId = setTimeout(() => { senddata(new Uint8Array([0xFF, 0xFF, 0xFF])); }, 2000);
  557. window.blechangedcallback = async function (event) {
  558. window.blechangedcallback = null;
  559. clearTimeout(timeoutId);
  560. };
  561. }
  562. if (i == 0) {
  563. await senddata(new Uint8Array([
  564. ...new Uint8Array([0xFF, 0xFF, 0xF0]),
  565. ...datas.subarray(i, i + 240)]));
  566. } else {
  567. await senddata(new Uint8Array([...datas.subarray(i, i + 240)]));
  568. }
  569. }
  570. }
  571. }
  572. //文件直接上传
  573. else {
  574. for (let i = 0; i < datas.length; i += 240) {
  575. if (i + 240 >= datas.length) {
  576. timeoutId = setTimeout(() => { senddata(new Uint8Array([0xFF, 0xFF, 0xFF])); }, 2000);
  577. window.blechangedcallback = async function (event) {
  578. window.blechangedcallback = null;
  579. clearTimeout(timeoutId);
  580. };
  581. }
  582. if (i == 0) {
  583. await senddata(new Uint8Array([
  584. ...new Uint8Array([0xFF, 0xFF, 0xF0]),
  585. ...datas.subarray(i, i + 240)]));
  586. } else {
  587. await senddata(new Uint8Array([...datas.subarray(i, i + 240)]));
  588. }
  589. }
  590. }
  591. return true;
  592. }
  593. catch (error) {
  594. console.error('发送消息错误:', error);
  595. return false;
  596. }
  597. }
  598. }
  599. sendfile.addEventListener('click', async () => {
  600. const textarea = document.getElementById('textarea');
  601. const zip = new JSZip();
  602. zip.file('user_latest_code.py', textarea.value, { compression: 'DEFLATE', compressionOptions: { level: 9 } }); // Use DEFLATE compression with highest level (9)
  603. const temp = await zip.generateAsync({ type: 'uint8array' })
  604. console.log(temp.length);
  605. const textec = new TextEncoder();
  606. sendcrc = textec.encode((CRC32.buf(temp) >>> 0).toString(16).padStart(8, '0'));
  607. const sendarr = new Uint8Array([
  608. ...new Uint8Array([0xFF, 0xFF]),
  609. ...textec.encode('/root/user_latest_code.py'),
  610. ...intToUint8Array(temp.length),
  611. ...sendcrc,
  612. ]);
  613. window.blechangedcallback = async function (event) {
  614. window.blechangedcallback = null;
  615. blefilesend(event, temp, temp.length).then(res=>{
  616. console.log(res,'1111111111111111')
  617. });
  618. };
  619. console.log(sendarr);
  620. await senddata(sendarr);
  621. })
  622. async function senddata(value) {
  623. if (value) {
  624. if (typeof value === 'string') {
  625. value = new TextEncoder().encode(value);
  626. }
  627. console.log("characteristic",characteristic)
  628. await characteristic.writeValue(value);
  629. }
  630. }
  631. function handleCharacteristicValueChanged(event) {
  632. const value = new Uint8Array(event.target.value.buffer);
  633. if (value[0] === value[1] && value[0] === 0xFF) {
  634. window.blechangedcallback && window.blechangedcallback(event);
  635. }
  636. }
  637. function Uint8ArraysEqual(arr1, arr2) {
  638. if (arr1.length !== arr2.length) {
  639. return false;
  640. }
  641. for (let i = 0; i < arr1.length; i++) {
  642. if (arr1[i] !== arr2[i]) {
  643. return false;
  644. }
  645. }
  646. return true;
  647. }
  648. function intToUint8Array(num) {
  649. num = BigInt(num)
  650. const array = new Uint8Array(6);
  651. for (let i = 5; i >= 0; i--) {
  652. array[i] = Number(num % 256n);
  653. num = num >> 8n;
  654. }
  655. return array;
  656. }
  657. function uint8ArrayToInt(array) {
  658. let num = 0n;
  659. for (let i = 0; i < array.length; i++) {
  660. num += BigInt(array[i]) << BigInt((array.length - 1 - i) * 8);
  661. }
  662. return Number(num);
  663. }
  664. </script>
  665. </body>
  666. </html>