34_CocoGPT.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #!/usr/bin/env python
  2. #version : 2024.08.08
  3. #language : en
  4. from maix import display
  5. from maix import image
  6. from maix import camera
  7. from maix import mjpg
  8. from maix import utils
  9. import base64
  10. import time
  11. import requests
  12. import json
  13. from evdev import InputDevice, categorize, ecodes
  14. import os
  15. import sys
  16. sys.path.append("/root/")
  17. sys.path.append("/root/preset/drivers/pylib/")
  18. from CocoPi import BUTTON
  19. import os
  20. ScreenOrientation = False
  21. try:
  22. if os.path.exists("/etc/cameraSize.cfg"):
  23. cameraSize = True
  24. else:
  25. cameraSize = False
  26. except:
  27. cameraSize = False
  28. def getLcdRotation(cameraCapture):
  29. global cameraSize
  30. if cameraSize:
  31. return lcdRotationNew(cameraCapture)
  32. else:
  33. return lcdRotation(cameraCapture)
  34. def lcdRotationNew(inputImg):
  35. global cameraSize,ScreenOrientation
  36. imageRotationBuffer = inputImg.crop(0, 0, 320, 240)
  37. if ScreenOrientation:
  38. imgRotationAim = image.new(size = (240, 320))
  39. rotationAngle = 90
  40. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  41. else:
  42. imgRotationAim = image.new(size = (320, 240))
  43. GETROTATION = imageRotationBuffer
  44. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  45. return GETROTATION
  46. def lcdRotation(inputImg):
  47. global cameraSize,ScreenOrientation
  48. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  49. if ScreenOrientation:
  50. imgRotationAim = image.new(size = (240, 320))
  51. rotationAngle = 180
  52. else:
  53. imgRotationAim = image.new(size = (320, 240))
  54. rotationAngle = 90
  55. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  56. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  57. return GETROTATION
  58. camera.camera.config(size=(240,320))
  59. image.load_freetype("/root/preset/fonts/simhei.ttf")
  60. canvas = image.new(size = (320, 240))
  61. try:
  62. # 获取键盘设备
  63. keyboard = InputDevice('/dev/input/event1') # 替换成实际的键盘设备路径
  64. except:
  65. keyboard = ""
  66. if keyboard=="":
  67. canvas.draw_string(0,12, "Keyboard not detected", scale = 1.3, color = (255,255,255) , thickness = 1)
  68. display.show(canvas)
  69. time.sleep(3)
  70. sys.exit()
  71. def v831_display_show_canvas(displayShow):
  72. global _canvas_y,_canvas_x
  73. CANVASSHOWIMGAGE = ""
  74. if ScreenOrientation:
  75. displayShowCanvas = image.new(size = (240, 320))
  76. displayShowCanvas.draw_rectangle(0,0,240,320, color=(0,0,0), thickness=-1)
  77. displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
  78. displayShowVER = displayShowCanvas.crop(0,0,240,320)
  79. displayShowVER = displayShowVER.rotate(-90, adjust=1)
  80. CANVASSHOWIMGAGE = displayShowVER
  81. display.show(displayShowVER)
  82. else:
  83. displayShowCanvas = image.new(size = (320, 240))
  84. displayShowCanvas.draw_rectangle(0,0,320,240, color=(0,0,0), thickness=-1)
  85. displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
  86. CANVASSHOWIMGAGE = displayShowCanvas
  87. display.show(displayShowCanvas)
  88. def get_post_chatgpt(datas):
  89. global list_num
  90. # 目标URL
  91. url = "https://gpt.cocorobo.cn/askForTopic"
  92. payload = json.dumps({"topic": datas})
  93. headers={
  94. "Content-Type":"application/json"
  95. }
  96. response=requests.request("POST", url, headers=headers, data=payload)
  97. # try:
  98. # keyboardStr[list_num] = "result"
  99. # except:
  100. # keyboardStr[list_num] = "fail"
  101. # pass
  102. if response.status_code == 200:
  103. # keyboardStr[list_num] = json.loads(response.text)["FunctionResponse"]["result"]
  104. result_num = len(json.loads(response.text)["FunctionResponse"]["result"])//20
  105. for i in range(result_num+1):
  106. keyboardStr.append("")
  107. keyboardStr[list_num] = json.loads(response.text)["FunctionResponse"]["result"][i*20:(i+1)*20]
  108. list_num = list_num+1
  109. else:
  110. keyboardStr[list_num] = "fail"
  111. _canvas_x = 0
  112. _canvas_y = 0
  113. keyboardStr = ["Input:",""]
  114. list_num = 1
  115. # 判断是否联网
  116. key_B = BUTTON(8)
  117. key_C = BUTTON(13)
  118. key_D = BUTTON(7)
  119. def wifi_is_content():
  120. global getDateNum
  121. cmd = "ifconfig"
  122. res = os.popen(cmd).read()
  123. data = False
  124. if res.rfind("inet addr:")!=48:
  125. data = True
  126. return data
  127. ssidInfo = ""
  128. passwordInfo = ""
  129. checkConnectState = False
  130. connectText = ""
  131. startConnect=False
  132. wifiConnectState = False
  133. # PublicIp=""
  134. PrivateIP=""
  135. runConnectSig=True
  136. connectSuccessSig=False
  137. while True:
  138. if wifi_is_content():
  139. break
  140. else:
  141. canvas = getLcdRotation(camera.capture())
  142. if ssidInfo!="" and passwordInfo!="":
  143. startConnect=True
  144. #connectText = "Waitting for Connection..."
  145. #canvas.draw_string(10,40, ssidInfo+" "+str(len(ssidInfo))+" "+str(type(ssidInfo)), scale = 1.5, color = (0,0,0), thickness = 1)
  146. canvas.draw_string(10,50, "Connect WIFI...", scale = 1.5, color = (0,0,0), thickness = 1)
  147. display.show(canvas)
  148. if startConnect==True:
  149. canvas_1 = image.new(size = (320, 320), color = (255,255,255), mode = "RGB")
  150. canvas.draw_image(canvas_1,0,0, alpha=0.4)
  151. if wifiConnectState == True:
  152. if key_B.is_pressed():
  153. while (key_B.is_pressed() == True):
  154. time.sleep(0.001)
  155. startConnect= False
  156. connectText = ""
  157. checkConnectState = False
  158. connectSuccessSig = False
  159. wifiConnectState = False
  160. runConnectSig== True
  161. ssidInfo=""
  162. passwordInfo=""
  163. if checkConnectState == True:
  164. if connectSuccessSig == False:
  165. # PublicIp=getPublicIp()
  166. connectSuccessSig = True
  167. connectText = "Wifi connection successfully!"
  168. # canvas.draw_string(10,80, "The WLAN PUBLIC IP:" + PublicIp, scale = 1.5, color = (0,0,0), thickness = 1)
  169. canvas.draw_string(10,110, "IP:" + PrivateIP, scale = 1.5, color = (0,0,0), thickness = 1)
  170. # canvas.draw_image((image.open("/root/preset/img/restart_ff0000_24x24.png")).rotate(0, adjust=0),8,216,alpha=1)
  171. ssidInfo=""
  172. passwordInfo=""
  173. canvas.clear()
  174. break
  175. else:
  176. pass
  177. else:
  178. if checkConnectState == False:
  179. os.system("wifi_disconnect_ap_test")
  180. os.system('wifi_connect_chinese_ap_test '+ssidInfo+' '+passwordInfo+'')
  181. checkConnectState = True
  182. if checkConnectState == True:
  183. if key_B.is_pressed():
  184. while (key_B.is_pressed() == True):
  185. time.sleep(0.001)
  186. startConnect= False
  187. connectText = ""
  188. checkConnectState = False
  189. connectSuccessSig = False
  190. wifiConnectState = False
  191. runConnectSig== True
  192. passwordInfo = ""
  193. ssidInfo = ""
  194. else:
  195. if runConnectSig== True:
  196. connectText = "Connect WIFI..."
  197. runConnectSig= False
  198. else:
  199. passwordInfo = ""
  200. ssidInfo = ""
  201. connectText = "WIFI Connect Failed!"
  202. canvas.draw_image((image.open("/root/preset/img/restart_ff0000_24x24.png")).rotate(0, adjust=0),8,216,alpha=1)
  203. canvas.draw_string(10,50, connectText, scale = 1.5, color = (0,0,0) , thickness = 1)
  204. else:
  205. mks = canvas.find_qrcodes()
  206. for mk in mks:
  207. #外框数据
  208. X = mk['x']
  209. Y = mk['y']
  210. W = mk['w']
  211. H = mk['h']
  212. #二维码信息
  213. string = mk['payload']
  214. codeData = string.split(";")
  215. ssidInfo = codeData[0].split(":")[1]
  216. passwordInfo = codeData[1].split(":")[1]
  217. #画外框
  218. canvas.draw_rectangle(X, Y, X + W, Y + H, color=(0, 0, 255), thickness = 2)
  219. #打印信息
  220. canvas.draw_string(int(X) , int(Y - 45) , "WIFI:"+ssidInfo, scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  221. canvas.draw_string(int(X) , int(Y - 25) , "PSD:"+passwordInfo, scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  222. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  223. v831_display_show_canvas(canvas)
  224. # 键盘事件
  225. canvas.draw_string(0,0, str("Input:"), scale = 1, color = (255,255,255) , thickness = 1)
  226. v831_display_show_canvas(canvas)
  227. for event in keyboard.read_loop():
  228. if event.type == ecodes.EV_KEY:
  229. key_event = categorize(event)
  230. if key_event.keystate == key_event.key_down:
  231. # print(keyboardStr,key_event.keycode[4:].lower())
  232. if key_event.keycode[4:] == "SPACE":
  233. keyboardStr[list_num] = keyboardStr[list_num] + " "
  234. elif key_event.keycode[4:] == "BACKSPACE":
  235. keyboardStr[list_num] = keyboardStr[list_num][:-1]
  236. if len(keyboardStr[list_num]) ==0 and list_num>1:
  237. list_num = list_num - 1
  238. elif key_event.keycode[4:] == "ENTER":
  239. keyboardStr.append("")
  240. list_num = list_num + 1
  241. get_post_chatgpt(keyboardStr[list_num-1])
  242. else:
  243. keyboardStr[list_num] = keyboardStr[list_num] + key_event.keycode[4:].lower()
  244. # print(list_num,keyboardStr)
  245. if len(keyboardStr[list_num]) >40:
  246. keyboardStr.append("")
  247. list_num = list_num + 1
  248. canvas.clear()
  249. if list_num>16:
  250. for i in range(list_num,list_num-16,-1):
  251. canvas.draw_string(0,15*(i-(list_num-15)), str(keyboardStr[i]), scale = 1, color = (255,255,255) , thickness = 1)
  252. else:
  253. for i in range(list_num+1):
  254. canvas.draw_string(0,15*i, str(keyboardStr[i]), scale = 1, color = (255,255,255) , thickness = 1)
  255. canvas.draw_string(0,0, str("Input:"), scale = 1, color = (255,255,255) , thickness = 1)
  256. v831_display_show_canvas(canvas)