135018浏览
查看: 135018|回复: 0

[项目] 行空板 ESP32智能眼镜

[复制链接]
行空板 ESP32智能眼镜图3

【项目背景】
阿尔茨海默病(AD)是老年期痴呆最常见的一种类型,患者思维、记忆和独立性会因此受损,影响生活质量以及死亡,FDA说“这是一种毁灭性疾病”。从全球来看,据国际阿尔茨海默病协会(ADI)发布的《世界阿尔茨海默病2018年报告》显示,目前全世界至少有5000万的痴呆患者,到2050年预计将达到1.52亿,其中约60%-70%为阿尔茨海默病患者。
面对眼前的亲人,而不知他/她是谁,是让人最痛苦的。
本项目提供了一种用于阿尔茨海默症病人的智能眼镜,人脸识别功能和智能语音功能可帮助病人分辨熟人。
【项目设计】
行空板 ESP32智能眼镜图1

利用ESP32 EYE采集视频图像,将图像通过TCP传送到行空板上显示,通过按键截取图像传到时百度云进行人脸识别,将识别的文字通过pyttsx3库进行语音合成,行空板通过蓝牙连接蓝牙耳机,告知眼前的这个亲人是谁。
【项目硬件】
ESP32 EYE、行空板
行空板 ESP32智能眼镜图2

【ESP32 EYE程序代码】
Arduino IDE中修改ESP32摄像头示例:
1.行空板连接WIFI,获取IP地址,修改下面代码中的IP地址。
  1. // 用于上传照片的服务器地址,行空板IP地址
  2. const char *serverName = "http://192.168.43.120:9000/upload";
复制代码
2.注册Easy IOT物联网平台,获取相关参数,修改物联网配置程序代码。
  1. EspMQTTClient client(
  2.   "wifi名",
  3.   "wifi密码",
  4.   "182.254.130.180",  // MQTT Broker server ip
  5.   "X8jykxFnR",   // Iot_id(user)
  6.   "u8jskbFngz",   // Iot_pwd(password)
  7.   "yuntian365",     // Client name that uniquely identify your device
  8.   1883              // The MQTT port, default to 1883.
  9. );
复制代码
3.接收行空板通过物联网平台发送的指令,设置标识,主循环体程序根据标识执行任务。
  1. void onConnectionEstablished()
  2. {
  3.   // Subscribe to "mytopic/wildcardtest/#" and display received message to Serial
  4.   client.subscribe("1DXAmWJ4g", [](const String & topic, const String & payload) {
  5.     Serial.println("(From wildcard) topic: " + topic + ", payload: " + payload);
  6.     if(payload=="a"){
  7.       bs=1;
  8.     }
  9.     if(payload=="b"){
  10.       bs=0;
  11.     }
  12.   });
复制代码
4.主循环体程序,开启和关闭向行空板传送图像。
  1. void loop() {
  2.   if(bs==1){
  3. // 拍摄照片
  4.     camera_fb_t *fb = esp_camera_fb_get();
  5.     if (!fb)
  6.     {
  7.       Serial.println("获取摄像头帧缓冲失败");
  8.       return;
  9.     }
  10.     // 建立HTTP客户端
  11.     HTTPClient http;
  12.     // 将照片上传到服务器
  13.     http.begin(serverName);
  14.     http.addHeader("Content-Type", "image/jpeg");
  15.     int httpResponseCode = http.POST(fb->buf, fb->len);
  16.     if (httpResponseCode > 0)
  17.     {
  18.       Serial.printf("照片上传成功,服务器返回代码:%d\n", httpResponseCode);
  19.       // 再闪一下提示上传成功
  20.    
  21.     }
  22.     else
  23.     {
  24.       Serial.printf("照片上传失败,错误代码:%s\n", http.errorToString(httpResponseCode).c_str());
  25.     }
  26.     http.end();
  27.     // 释放帧缓冲
  28.     esp_camera_fb_return(fb);
  29.   }
  30.    client.loop();
  31. }
复制代码
5.完整程序
  1. #include "esp_camera.h"
  2. #include <WiFi.h>
  3. #include <HTTPClient.h>
  4. // 用于上传照片的服务器地址,行空板IP地址
  5. const char *serverName = "http://192.168.43.120:9000/upload";
  6. // ===================
  7. // Select camera model
  8. #define CAMERA_MODEL_ESP_EYE // Has PSRAM
  9. #include "camera_pins.h"
  10. #include "EspMQTTClient.h"
  11. EspMQTTClient client(
  12.   "sxs",
  13.   "smj080823",
  14.   "182.254.130.180",  // MQTT Broker server ip
  15.   "X8jykxFnR",   // Can be omitted if not needed
  16.   "u8jskbFngz",   // Can be omitted if not needed
  17.   "yuntian365",     // Client name that uniquely identify your device
  18.   1883              // The MQTT port, default to 1883. this line can be omitted
  19. );
  20. // ===========================
  21. // Enter your WiFi credentials
  22. // ===========================
  23. const char* ssid = "sxs";
  24. const char* password = "smj080823";
  25. int bs;
  26. void setup() {
  27.   Serial.begin(115200);
  28.   Serial.setDebugOutput(true);
  29.   Serial.println();
  30.   camera_config_t config;
  31.   config.ledc_channel = LEDC_CHANNEL_0;
  32.   config.ledc_timer = LEDC_TIMER_0;
  33.   config.pin_d0 = Y2_GPIO_NUM;
  34.   config.pin_d1 = Y3_GPIO_NUM;
  35.   config.pin_d2 = Y4_GPIO_NUM;
  36.   config.pin_d3 = Y5_GPIO_NUM;
  37.   config.pin_d4 = Y6_GPIO_NUM;
  38.   config.pin_d5 = Y7_GPIO_NUM;
  39.   config.pin_d6 = Y8_GPIO_NUM;
  40.   config.pin_d7 = Y9_GPIO_NUM;
  41.   config.pin_xclk = XCLK_GPIO_NUM;
  42.   config.pin_pclk = PCLK_GPIO_NUM;
  43.   config.pin_vsync = VSYNC_GPIO_NUM;
  44.   config.pin_href = HREF_GPIO_NUM;
  45.   config.pin_sccb_sda = SIOD_GPIO_NUM;
  46.   config.pin_sccb_scl = SIOC_GPIO_NUM;
  47.   config.pin_pwdn = PWDN_GPIO_NUM;
  48.   config.pin_reset = RESET_GPIO_NUM;
  49.   config.xclk_freq_hz = 20000000;
  50.   config.frame_size = FRAMESIZE_UXGA;
  51.   config.pixel_format = PIXFORMAT_JPEG; // for streaming
  52.   //config.pixel_format = PIXFORMAT_RGB565; // for face detection/recognition
  53.   config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
  54.   config.fb_location = CAMERA_FB_IN_PSRAM;
  55.   config.jpeg_quality = 12;
  56.   config.fb_count = 1;
  57.   
  58.   // if PSRAM IC present, init with UXGA resolution and higher JPEG quality
  59.   //                      for larger pre-allocated frame buffer.
  60.   if(config.pixel_format == PIXFORMAT_JPEG){
  61.     if(psramFound()){
  62.       config.jpeg_quality = 10;
  63.       config.fb_count = 2;
  64.       config.grab_mode = CAMERA_GRAB_LATEST;
  65.     } else {
  66.       // Limit the frame size when PSRAM is not available
  67.       config.frame_size = FRAMESIZE_SVGA;
  68.       config.fb_location = CAMERA_FB_IN_DRAM;
  69.     }
  70.   } else {
  71.     // Best option for face detection/recognition
  72.     config.frame_size = FRAMESIZE_240X240;
  73. #if CONFIG_IDF_TARGET_ESP32S3
  74.     config.fb_count = 2;
  75. #endif
  76.   }
  77. #if defined(CAMERA_MODEL_ESP_EYE)
  78.   pinMode(13, INPUT_PULLUP);
  79.   pinMode(14, INPUT_PULLUP);
  80. #endif
  81.   // camera init
  82.   esp_err_t err = esp_camera_init(&config);
  83.   if (err != ESP_OK) {
  84.     Serial.printf("Camera init failed with error 0x%x", err);
  85.     return;
  86.   }
  87.   sensor_t * s = esp_camera_sensor_get();
  88.   // initial sensors are flipped vertically and colors are a bit saturated
  89.   if (s->id.PID == OV3660_PID) {
  90.     s->set_vflip(s, 1); // flip it back
  91.     s->set_brightness(s, 1); // up the brightness just a bit
  92.     s->set_saturation(s, -2); // lower the saturation
  93.   }
  94.   // drop down frame size for higher initial frame rate
  95.   if(config.pixel_format == PIXFORMAT_JPEG){
  96.     s->set_framesize(s, FRAMESIZE_QVGA);
  97.   }
  98. #if defined(CAMERA_MODEL_M5STACK_WIDE) || defined(CAMERA_MODEL_M5STACK_ESP32CAM)
  99.   s->set_vflip(s, 1);
  100.   s->set_hmirror(s, 1);
  101. #endif
  102. #if defined(CAMERA_MODEL_ESP32S3_EYE)
  103.   s->set_vflip(s, 1);
  104. #endif
  105.   WiFi.begin(ssid, password);
  106.   WiFi.setSleep(false);
  107.   while (WiFi.status() != WL_CONNECTED) {
  108.     delay(500);
  109.     Serial.print(".");
  110.   }
  111.   Serial.println("");
  112.   Serial.println("WiFi connected");
  113.   bs=0;
  114.   client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
  115.   client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overridded with enableHTTPWebUpdater("user", "password").
  116.   client.enableOTA(); // Enable OTA (Over The Air) updates. Password defaults to MQTTPassword. Port is the default OTA port. Can be overridden with enableOTA("password", port).
  117.   client.enableLastWillMessage("MNpA1p_4R", "I am going offline");  // You can activate the retain flag by setting the third parameter to true
  118. }
  119. void onConnectionEstablished()
  120. {
  121.   // Subscribe to "mytopic/wildcardtest/#" and display received message to Serial
  122.   client.subscribe("1DXAmWJ4g", [](const String & topic, const String & payload) {
  123.     Serial.println("(From wildcard) topic: " + topic + ", payload: " + payload);
  124.     if(payload=="a"){
  125.       bs=1;
  126.     }
  127.     if(payload=="b"){
  128.       bs=0;
  129.     }
  130.   });
  131.   // Publish a message to "mytopic/test"
  132.   client.publish("k_eT7HUVR", "This is a message"); // You can activate the retain flag by setting the third parameter to true
  133.   // Execute delayed instructions
  134.   client.executeDelayed(5 * 1000, []() {
  135.     client.publish("k_eT7HUVR", "This is a message sent 5 seconds later");
  136.   });
  137. }
  138. void loop() {
  139.   if(bs==1){
  140. // 拍摄照片
  141.     camera_fb_t *fb = esp_camera_fb_get();
  142.     if (!fb)
  143.     {
  144.       Serial.println("获取摄像头帧缓冲失败");
  145.       return;
  146.     }
  147.     // 建立HTTP客户端
  148.     HTTPClient http;
  149.     // 将照片上传到服务器
  150.     http.begin(serverName);
  151.     http.addHeader("Content-Type", "image/jpeg");
  152.     int httpResponseCode = http.POST(fb->buf, fb->len);
  153.     if (httpResponseCode > 0)
  154.     {
  155.       Serial.printf("照片上传成功,服务器返回代码:%d\n", httpResponseCode);
  156.       // 再闪一下提示上传成功
  157.    
  158.     }
  159.     else
  160.     {
  161.       Serial.printf("照片上传失败,错误代码:%s\n", http.errorToString(httpResponseCode).c_str());
  162.     }
  163.     http.end();
  164.     // 释放帧缓冲
  165.     esp_camera_fb_return(fb);
  166.   }
  167.    client.loop();
  168. }
复制代码
【百度智能云】
1.申请账号,获取相关参数。
行空板 ESP32智能眼镜图4
2.可视人脸库,建用户,添加人脸。
行空板 ESP32智能眼镜图5
3.增加用户信息
  1. from aip import AipFace
  2. """ 你的 APPID AK SK """
  3. APP_ID = '17893916'
  4. API_KEY = 'uEunHuOhiSTeYHfZcdcUIq60'
  5. SECRET_KEY = 'oOPx8FerIBGx4wVOYE3khD9rNLzPRGGy'
  6. client = AipFace(APP_ID, API_KEY, SECRET_KEY)
  7. image = "37c0a46ef66c11b09452956dfd653820"
  8. imageType = "FACE_TOKEN"
  9. groupId = "AI1"
  10. userId = "yuntian"
  11. options = {}
  12. options["user_info"] = "yuntian"
  13. """ 调用人脸更新 """
  14. res=client.updateUser(image, imageType, groupId, userId, options)
  15. print(res)
复制代码

【行空板程序】
1.测试接收图片,传百度云,打印信息
  1. from aip import AipFace
  2. import myimg
  3. from flask import Flask, request
  4. from unihiker import GUI
  5. import siot
  6. import time
  7. import base64
  8. u_gui=GUI()
  9. i=0
  10. 显图=u_gui.draw_image(image="back.png",h=320,x=0,y=0)
  11. app = Flask(__name__)
  12. @app.route('/upload', methods=['POST'])
  13. def upload():
  14.    try:
  15.         image = request.data
  16.         # 保存照片到指定目录
  17.       
  18.         with open('base.png', 'wb') as f:
  19.             f.write(image)
  20.             f.close()
  21.         显图.config(image="base.png")      
  22.         return "照片上传成功", 200
  23.         
  24.    except Exception as e:
  25.        return "照片上传失败", 500      
  26.      
  27. # 事件回调函数
  28. def on_buttona_click_callback():
  29.     global i
  30.     if i==0:
  31.        i=1
  32.        siot.publish(topic="1DXAmWJ4g", data="a")
  33. # 事件回调函数
  34. def on_buttonb_click_callback():
  35.     global i
  36.     if i==1:
  37.        i=0
  38.       
  39.        siot.publish(topic="1DXAmWJ4g", data="b")
  40.       
  41.        with open('base.png', 'rb') as f:
  42.             encoded_string = base64.b64encode(f.read())
  43.             encoded_string = encoded_string.decode("utf-8")
  44.             f.close()
  45.        image = encoded_string
  46.        """ 调用人脸检测 """
  47.        imageType = "BASE64"
  48.        res=client.detect(image, imageType);
  49.        print(res)
  50.        if(res['error_code']==0):
  51.         if(res['result']['face_num']==1):
  52.             print(res['result']['face_list'][0]['face_token'])
  53.             print(res['result']['face_list'][0]['location'])
  54.        显图.config(image="base.png")
  55. """https://ai.baidu.com/ai-doc/FACE/ek37c1qiz"""
  56. """ 你的 APPID AK SK """
  57. APP_ID = '17893916'
  58. API_KEY = 'uEunHuOhiSTeYHfZcdcUIq60'
  59. SECRET_KEY = 'oOPx8FerIBGx4wVOYE3khD9rNLzPRGGy'
  60. client = AipFace(APP_ID, API_KEY, SECRET_KEY)
  61. if __name__ == '__main__':
  62.     siot.init(client_id="yuntian367",server="iot.dfrobot.com.cn",port=1883,user="X8jykxFnR",password="u8jskbFngz")
  63.     siot.connect()
  64.     siot.loop()
  65.    
  66.     u_gui.on_a_click(on_buttona_click_callback)
  67.     u_gui.on_b_click(on_buttonb_click_callback)
  68.     try:
  69.        app.run(host='192.168.31.71', port=9000)
  70.     except Exception as e:
  71.        print("照片上传失败:", str(e))
复制代码

2.增加开机画面,并在图像上画识别到人脸方框
  1. from aip import AipFace
  2. import myimg
  3. from flask import Flask, request
  4. from unihiker import GUI
  5. import siot
  6. import time
  7. import base64
  8. import cv2
  9. u_gui=GUI()
  10. i=0
  11. 显图=u_gui.draw_image(image="back.png",h=320,x=0,y=0)
  12. 画框=u_gui.draw_rect(x=0,y=0,w=0,h=0,width=4,color="#0000FF")
  13. app = Flask(__name__)
  14. @app.route('/upload', methods=['POST'])
  15. def upload():
  16.    try:
  17.         image = request.data
  18.         # 保存照片到指定目录
  19.       
  20.         with open('base.png', 'wb') as f:
  21.             f.write(image)
  22.         
  23.             f.close()
  24.         img = cv2.imread('base.png')
  25.         #img_re = cv2.resize(img, (240, 320))
  26.         height, width = img.shape[:2]
  27.         rotation_matrix = cv2.getRotationMatrix2D((120,120), -90, 1)
  28.         rotated_image = cv2.warpAffine(img, rotation_matrix, (240,320))
  29.         cv2.imwrite("base.png",rotated_image)
  30.         显图.config(image="base.png")
  31.         return "照片上传成功", 200
  32.         
  33.    except Exception as e:
  34.        print(str(e))
  35.        return "照片上传失败", 500      
  36.      
  37. # 事件回调函数
  38. def on_buttona_click_callback():
  39.     global i
  40.     if i==0:
  41.        i=1
  42.       
  43.        画框.config(width=0)
  44.        siot.publish(topic="1DXAmWJ4g", data="a")
  45. # 事件回调函数
  46. def on_buttonb_click_callback():
  47.     global i
  48.     if i==1:
  49.        i=0
  50.       
  51.        siot.publish(topic="1DXAmWJ4g", data="b")
  52.       
  53.        with open('base.png', 'rb') as f:
  54.             encoded_string = base64.b64encode(f.read())
  55.             encoded_string = encoded_string.decode("utf-8")
  56.             f.close()
  57.        image = encoded_string
  58.        """ 调用人脸检测 """
  59.        imageType = "BASE64"
  60.        res=client.detect(image, imageType);
  61.        print(res)
  62.        if(res['error_code']==0):
  63.         if(res['result']['face_num']==1):
  64.             print(res['result']['face_list'][0]['face_token'])
  65.             print(res['result']['face_list'][0]['location'])
  66.             left=res['result']['face_list'][0]['location']['left']
  67.             top=res['result']['face_list'][0]['location']['top']
  68.             width=res['result']['face_list'][0]['location']['width']
  69.             height=res['result']['face_list'][0]['location']['height']
  70.             显图.config(image="base.png")
  71.             画框.config(width=3)
  72.             画框.config(x=left-height)
  73.             画框.config(y=top)
  74.             画框.config(w=height)
  75.             画框.config(h=width)
  76. """https://ai.baidu.com/ai-doc/FACE/ek37c1qiz"""
  77. """ 你的 APPID AK SK """
  78. APP_ID = '17893916'
  79. API_KEY = 'uEunHuOhiSTeYHfZcdcUIq60'
  80. SECRET_KEY = 'oOPx8FerIBGx4wVOYE3khD9rNLzPRGGy'
  81. client = AipFace(APP_ID, API_KEY, SECRET_KEY)
  82. if __name__ == '__main__':
  83.     siot.init(client_id="yuntian367",server="iot.dfrobot.com.cn",port=1883,user="X8jykxFnR",password="u8jskbFngz")
  84.     siot.connect()
  85.     siot.loop()
  86.    
  87.     u_gui.on_a_click(on_buttona_click_callback)
  88.     u_gui.on_b_click(on_buttonb_click_callback)
  89.     try:
  90.        app.run(host='192.168.31.71', port=9000)
  91.     except Exception as e:
  92.        print("照片上传失败:", str(e))
复制代码

3.识别人脸信息并打印
  1. from aip import AipFace
  2. import name
  3. from flask import Flask, request
  4. from unihiker import GUI
  5. import siot
  6. import time
  7. import base64
  8. import cv2
  9. u_gui=GUI()
  10. i=0
  11. 显图=u_gui.draw_image(image="back.png",h=320,x=0,y=0)
  12. 画框=u_gui.draw_rect(x=0,y=0,w=0,h=0,width=4,color="#0000FF")
  13. app = Flask(__name__)
  14. @app.route('/upload', methods=['POST'])
  15. def upload():
  16.    try:
  17.         image = request.data
  18.         # 保存照片到指定目录
  19.       
  20.         with open('base.png', 'wb') as f:
  21.             f.write(image)
  22.         
  23.             f.close()
  24.         img = cv2.imread('base.png')
  25.         #img_re = cv2.resize(img, (240, 320))
  26.         height, width = img.shape[:2]
  27.         rotation_matrix = cv2.getRotationMatrix2D((120,120), -90, 1)
  28.         rotated_image = cv2.warpAffine(img, rotation_matrix, (240,320))
  29.         cv2.imwrite("base.png",rotated_image)
  30.         显图.config(image="base.png")
  31.         return "照片上传成功", 200
  32.         
  33.    except Exception as e:
  34.        print(str(e))
  35.        return "照片上传失败", 500      
  36.      
  37. # 事件回调函数
  38. def on_buttona_click_callback():
  39.     global i
  40.     if i==0:
  41.        i=1
  42.       
  43.        画框.config(width=0)
  44.        siot.publish(topic="1DXAmWJ4g", data="a")
  45. # 事件回调函数
  46. def on_buttonb_click_callback():
  47.     global i
  48.     if i==1:
  49.        i=0
  50.       
  51.        siot.publish(topic="1DXAmWJ4g", data="b")
  52.       
  53.        with open('base.png', 'rb') as f:
  54.             encoded_string = base64.b64encode(f.read())
  55.             encoded_string = encoded_string.decode("utf-8")
  56.             f.close()
  57.        image = encoded_string
  58.        """ 调用人脸检测 """
  59.        imageType = "BASE64"
  60.        res=client.detect(image, imageType);
  61.        print(res)
  62.        if(res['error_code']==0):
  63.         if(res['result']['face_num']==1):
  64.             print(res['result']['face_list'][0]['face_token'])
  65.             print(res['result']['face_list'][0]['location'])
  66.             left=res['result']['face_list'][0]['location']['left']
  67.             top=res['result']['face_list'][0]['location']['top']
  68.             width=res['result']['face_list'][0]['location']['width']
  69.             height=res['result']['face_list'][0]['location']['height']
  70.             显图.config(image="base.png")
  71.             画框.config(width=3)
  72.             画框.config(x=abs(height-left))
  73.             画框.config(y=top)
  74.             画框.config(w=height)
  75.             画框.config(h=width)
  76.             image=res['result']['face_list'][0]['face_token']
  77.             groupIdList = "AI1"
  78.             imageType = "FACE_TOKEN"
  79.             """ 如果有可选参数 """
  80.             options = {}
  81.             options["match_threshold"] = 70
  82.             options["quality_control"] = "NORMAL"
  83.             options["liveness_control"] = "LOW"
  84.             
  85.             options["max_user_num"] =1
  86.             """ 带参数调用人脸搜索 """
  87.             res1=client.search(image, imageType, groupIdList, options)
  88.             if(res1['error_code']==0):
  89.                 user_id=res1['result']['user_list'][0]['user_id']
  90.                 if(user_id in name.name_id):
  91.                     print(name.name_id[user_id])
  92.            
  93. """https://ai.baidu.com/ai-doc/FACE/ek37c1qiz"""
  94. """ 你的 APPID AK SK """
  95. APP_ID = '17893916'
  96. API_KEY = 'uEunHuOhiSTeYHfZcdcUIq60'
  97. SECRET_KEY = 'oOPx8FerIBGx4wVOYE3khD9rNLzPRGGy'
  98. client = AipFace(APP_ID, API_KEY, SECRET_KEY)
  99. if __name__ == '__main__':
  100.     siot.init(client_id="yuntian367",server="iot.dfrobot.com.cn",port=1883,user="X8jykxFnR",password="u8jskbFngz")
  101.     siot.connect()
  102.     siot.loop()
  103.    
  104.     u_gui.on_a_click(on_buttona_click_callback)
  105.     u_gui.on_b_click(on_buttonb_click_callback)
  106.     try:
  107.        app.run(host='192.168.31.71', port=9000)
  108.     except Exception as e:
  109.        print("照片上传失败:", str(e))
复制代码

4.行空板配置连接蓝牙音箱
论坛有配置文章,下面备注一下自己的操作记录。
  1. #可以通过bluetoothctl实现,蓝牙设备之间的配对,bluetoothctl工具,类似一个shell工具,提供许多子命令集合。
  2. #使用命令 agent on (推荐)选择要连接的设备类型或者去选择一个特定的类型: 如果你在 agent 命令后按下两次tab键, 你应该看到一些可以使用的类型列表,比如DisplayOnly KeyboardDisplay NoInputNoOutput DisplayYesNo KeyboardOnly off on.
  3. #使用命令 default-agent 去确认并完成要连接的设备类型
  4. #power on蓝牙适配器上电,打开蓝牙,蓝牙默认是关闭的,重启后默认也是关闭的
  5. #scan on/off开启蓝牙适配的扫描过程,扫描周边的蓝牙设备。
  6. bluetoothctl
  7. agent on
  8. default-agent
  9. power on
  10. #依次键入以下命令,进行设备配对,trust 授信蓝牙设备,pair配对设备,connect连接设备
  11. trust 41:42:48:46:68:C7
  12. pair 41:42:48:46:68:C7
  13. connect 41:42:48:46:68:C7
  14. #退出
  15. exit
复制代码

5.连接蓝牙音箱,识别成功后,语音输出亲人信息。(最终完整程序)
  1. from aip import AipFace
  2. import name
  3. from flask import Flask, request
  4. from unihiker import GUI
  5. import siot
  6. import time
  7. import base64
  8. import cv2
  9. #语音播放
  10. import pyttsx3
  11. u_gui=GUI()
  12. i=0
  13. 显图=u_gui.draw_image(image="back.png",h=320,x=0,y=0)
  14. 画框=u_gui.draw_rect(x=0,y=0,w=0,h=0,width=4,color="#0000FF")
  15. app = Flask(__name__)
  16. @app.route('/upload', methods=['POST'])
  17. def upload():
  18.    try:
  19.         image = request.data
  20.         # 保存照片到指定目录
  21.       
  22.         with open('base.png', 'wb') as f:
  23.             f.write(image)
  24.         
  25.             
  26.         img = cv2.imread('base.png')
  27.         #img_re = cv2.resize(img, (240, 320))
  28.         height, width = img.shape[:2]
  29.         rotation_matrix = cv2.getRotationMatrix2D((120,120), -90, 1)
  30.         rotated_image = cv2.warpAffine(img, rotation_matrix, (240,320))
  31.         cv2.imwrite("base.png",rotated_image)
  32.         显图.config(image="base.png")
  33.         return "照片上传成功", 200
  34.         
  35.    except Exception as e:
  36.        print(str(e))
  37.        return "照片上传失败", 500      
  38.      
  39. # 事件回调函数
  40. def on_buttona_click_callback():
  41.     global i
  42.     if i==0:
  43.        i=1
  44.       
  45.        画框.config(width=0)
  46.        siot.publish(topic="1DXAmWJ4g", data="a")
  47. # 事件回调函数
  48. def on_buttonb_click_callback():
  49.     global i
  50.     if i==1:
  51.        i=0
  52.       
  53.        siot.publish(topic="1DXAmWJ4g", data="b")
  54.       
  55.        with open('base.png', 'rb') as f:
  56.             encoded_string = base64.b64encode(f.read())
  57.             encoded_string = encoded_string.decode("utf-8")
  58.            
  59.        image = encoded_string
  60.        """ 调用人脸检测 """
  61.        imageType = "BASE64"
  62.        res=client.detect(image, imageType);
  63.        print(res)
  64.        if(res['error_code']==0):
  65.         if(res['result']['face_num']==1):
  66.             print(res['result']['face_list'][0]['face_token'])
  67.             print(res['result']['face_list'][0]['location'])
  68.             left=res['result']['face_list'][0]['location']['left']
  69.             top=res['result']['face_list'][0]['location']['top']
  70.             width=res['result']['face_list'][0]['location']['width']
  71.             height=res['result']['face_list'][0]['location']['height']
  72.             显图.config(image="base.png")
  73.             画框.config(width=3)
  74.             画框.config(x=abs(height-left))
  75.             画框.config(y=top)
  76.             画框.config(w=height)
  77.             画框.config(h=width)
  78.             image=res['result']['face_list'][0]['face_token']
  79.             groupIdList = "AI1"
  80.             imageType = "FACE_TOKEN"
  81.             """ 如果有可选参数 """
  82.             options = {}
  83.             options["match_threshold"] = 70
  84.             options["quality_control"] = "NORMAL"
  85.             options["liveness_control"] = "LOW"
  86.             
  87.             options["max_user_num"] =1
  88.             """ 带参数调用人脸搜索 """
  89.             res1=client.search(image, imageType, groupIdList, options)
  90.             print(res1)
  91.             if(res1['error_code']==0):
  92.                 user_id=res1['result']['user_list'][0]['user_id']
  93.                 score=res1['result']['user_list'][0]['score']
  94.                 if score>60:
  95.                  print(user_id)
  96.                  if user_id in name.name_id:
  97.                     print(name.name_id[user_id])
  98.                     pyttsx3.speak("这个人是:"+name.name_id[user_id])
  99.                     
  100.                 else:
  101.                     pyttsx3.speak("请重新识别")
  102.             elif res1['error_code']==223120:
  103.                 pyttsx3.speak("请不要使用照片进行识别")
  104. """https://ai.baidu.com/ai-doc/FACE/ek37c1qiz"""
  105. """ 你的 APPID AK SK """
  106. APP_ID = '17893916'
  107. API_KEY = 'uEunHuOhiSTeYHfZcdcUIq60'
  108. SECRET_KEY = 'oOPx8FerIBGx4wVOYE3khD9rNLzPRGGy'
  109. client = AipFace(APP_ID, API_KEY, SECRET_KEY)
  110. if __name__ == '__main__':
  111.     siot.init(client_id="yuntian367",server="iot.dfrobot.com.cn",port=1883,user="X8jykxFnR",password="u8jskbFngz")
  112.     siot.connect()
  113.     siot.loop()
  114.    
  115.     u_gui.on_a_click(on_buttona_click_callback)
  116.     u_gui.on_b_click(on_buttonb_click_callback)
  117.     try:
  118.        app.run(host='192.168.31.70', port=9000)
  119.     except Exception as e:
  120.        print("照片上传失败:", str(e))
复制代码


IMG_20230923_150010.jpg
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail