发帖
1 0 0

【安信可小安派BW21-CBV-Kit】物品识别控制舵机

sujingliang
论坛元老

14

主题

15

回帖

5347

积分

论坛元老

积分
5347
小安派·BW21-CBV-KIt 24 1 昨天 21:00
本帖最后由 sujingliang 于 2025-3-12 10:59 编辑

用BW21-CBV-Kit开发板使用摄像头可以监测80种不同类型的物体,如人、自行车、汽车、笔记本电脑等。

ObjectClassList.h:

  1. ObjectDetectionItem itemList[80] = {
  2.     {0,  "person",         1},
  3.     {1,  "bicycle",        1},
  4.     {2,  "car",            1},
  5.     {3,  "motorbike",      1},
  6.     {4,  "aeroplane",      1},
  7.     {5,  "bus",            1},
  8.     {6,  "train",          1},
  9.     {7,  "truck",          1},
  10.     {8,  "boat",           1},
  11.     {9,  "traffic light",  1},
  12.     {10, "fire hydrant",   1},
  13.     {11, "stop sign",      1},
  14.     {12, "parking meter",  1},
  15.     {13, "bench",          1},
  16.     {14, "bird",           1},
  17.     {15, "cat",            1},
  18.     {16, "dog",            1},
  19.     {17, "horse",          1},
  20.     {18, "sheep",          1},
复制代码


在“文件”->“示例”->“AmebaNN”->“ObjectDetectionLoop”中打开一个对象检测示例。
9.png


  1. /*

  2. Example guide:
  3. https://www.amebaiot.com/en/amebapro2-arduino-neuralnework-object-detection/

  4. NN Model Selection
  5. Select Neural Network(NN) task and models using .modelSelect(nntask, objdetmodel, facedetmodel, facerecogmodel).
  6. Replace with NA_MODEL if they are not necessary for your selected NN Task.

  7. NN task
  8. =======
  9. OBJECT_DETECTION/ FACE_DETECTION/ FACE_RECOGNITION

  10. Models
  11. =======
  12. YOLOv3 model         DEFAULT_YOLOV3TINY   / CUSTOMIZED_YOLOV3TINY
  13. YOLOv4 model         DEFAULT_YOLOV4TINY   / CUSTOMIZED_YOLOV4TINY
  14. YOLOv7 model         DEFAULT_YOLOV7TINY   / CUSTOMIZED_YOLOV7TINY
  15. SCRFD model          DEFAULT_SCRFD        / CUSTOMIZED_SCRFD
  16. MobileFaceNet model  DEFAULT_MOBILEFACENET/ CUSTOMIZED_MOBILEFACENET
  17. No model             NA_MODEL
  18. */

  19. #include "WiFi.h"
  20. #include "StreamIO.h"
  21. #include "VideoStream.h"
  22. #include "RTSP.h"
  23. #include "NNObjectDetection.h"
  24. #include "VideoStreamOverlay.h"
  25. #include "ObjectClassList.h"
  26. #include <AmebaServo.h>

  27. #define CHANNEL   0
  28. #define CHANNELNN 3

  29. // Lower resolution for NN processing
  30. #define NNWIDTH  576
  31. #define NNHEIGHT 320


  32. AmebaServo myservo;

  33. VideoSetting config(VIDEO_FHD, 30, VIDEO_H264, 0);
  34. VideoSetting configNN(NNWIDTH, NNHEIGHT, 10, VIDEO_RGB, 0);
  35. NNObjectDetection ObjDet;
  36. RTSP rtsp;
  37. StreamIO videoStreamer(1, 1);
  38. StreamIO videoStreamerNN(1, 1);

  39. char ssid[] = "ssid";    // your network SSID (name)
  40. char pass[] = "pass";        // your network password
  41. int status = WL_IDLE_STATUS;
  42. int timeout=0;

  43. IPAddress ip;
  44. int rtsp_portnum;

  45. void setup()
  46. {
  47.     Serial.begin(115200);
  48.     myservo.attach(8);

  49.     // attempt to connect to Wifi network:
  50.     while (status != WL_CONNECTED) {
  51.         Serial.print("Attempting to connect to WPA SSID: ");
  52.         Serial.println(ssid);
  53.         status = WiFi.begin(ssid, pass);

  54.         // wait 2 seconds for connection:
  55.         delay(2000);
  56.     }
  57.     ip = WiFi.localIP();

  58.     // Configure camera video channels with video format information
  59.     // Adjust the bitrate based on your WiFi network quality
  60.     config.setBitrate(2 * 1024 * 1024);    // Recommend to use 2Mbps for RTSP streaming to prevent network congestion
  61.     Camera.configVideoChannel(CHANNEL, config);
  62.     Camera.configVideoChannel(CHANNELNN, configNN);
  63.     Camera.videoInit();

  64.     // Configure RTSP with corresponding video format information
  65.     rtsp.configVideo(config);
  66.     rtsp.begin();
  67.     rtsp_portnum = rtsp.getPort();

  68.     // Configure object detection with corresponding video format information
  69.     // Select Neural Network(NN) task and models
  70.     ObjDet.configVideo(configNN);
  71.     ObjDet.setResultCallback(ODPostProcess);
  72.     ObjDet.modelSelect(OBJECT_DETECTION, DEFAULT_YOLOV4TINY, NA_MODEL, NA_MODEL);
  73.     ObjDet.begin();

  74.     // Configure StreamIO object to stream data from video channel to RTSP
  75.     videoStreamer.registerInput(Camera.getStream(CHANNEL));
  76.     videoStreamer.registerOutput(rtsp);
  77.     if (videoStreamer.begin() != 0) {
  78.         Serial.println("StreamIO link start failed");
  79.     }

  80.     // Start data stream from video channel
  81.     Camera.channelBegin(CHANNEL);

  82.     // Configure StreamIO object to stream data from RGB video channel to object detection
  83.     videoStreamerNN.registerInput(Camera.getStream(CHANNELNN));
  84.     videoStreamerNN.setStackSize();
  85.     videoStreamerNN.setTaskPriority();
  86.     videoStreamerNN.registerOutput(ObjDet);
  87.     if (videoStreamerNN.begin() != 0) {
  88.         Serial.println("StreamIO link start failed");
  89.     }

  90.     // Start video channel for NN
  91.     Camera.channelBegin(CHANNELNN);

  92.     // Start OSD drawing on RTSP video channel
  93.     OSD.configVideo(CHANNEL, config);
  94.     OSD.begin();
  95. }

  96. void loop()
  97. {
  98.     // Do nothing
  99.     if(timeout>0){
  100.         timeout--;
  101.         if(timeout==0) myservo.write(0);
  102.     }
  103.     delay(1000);
  104. }

  105. // User callback function for post processing of object detection results
  106. void ODPostProcess(std::vector<ObjectDetectionResult> results)
  107. {
  108.     uint16_t im_h = config.height();
  109.     uint16_t im_w = config.width();

  110.     Serial.print("Network URL for RTSP Streaming: ");
  111.     Serial.print("rtsp://");
  112.     Serial.print(ip);
  113.     Serial.print(":");
  114.     Serial.println(rtsp_portnum);
  115.     Serial.println(" ");

  116.     printf("Total number of objects detected = %d\r\n", ObjDet.getResultCount());
  117.     OSD.createBitmap(CHANNEL);

  118.     if (ObjDet.getResultCount() > 0) {
  119.         for (int i = 0; i < ObjDet.getResultCount(); i++) {
  120.             int obj_type = results[i].type();
  121.             if (itemList[obj_type].filter) {    // check if item should be ignored

  122.                 ObjectDetectionResult item = results[i];
  123.                 // Result coordinates are floats ranging from 0.00 to 1.00
  124.                 // Multiply with RTSP resolution to get coordinates in pixels
  125.                 int xmin = (int)(item.xMin() * im_w);
  126.                 int xmax = (int)(item.xMax() * im_w);
  127.                 int ymin = (int)(item.yMin() * im_h);
  128.                 int ymax = (int)(item.yMax() * im_h);

  129.                 // Draw boundary box
  130.                 printf("Item %d %s:\t%d %d %d %d\n\r", i, itemList[obj_type].objectName, xmin, xmax, ymin, ymax);
  131.                 OSD.drawRect(CHANNEL, xmin, ymin, xmax, ymax, 3, OSD_COLOR_WHITE);

  132.                 // Print identification text
  133.                 char text_str[20];
  134.                 snprintf(text_str, sizeof(text_str), "%s %d", itemList[obj_type].objectName, item.score());
  135.                 OSD.drawText(CHANNEL, xmin, ymin - OSD.getTextHeight(CHANNEL), text_str, OSD_COLOR_CYAN);

  136.                 if(itemList[obj_type].index==7){
  137.                     myservo.write(180);
  138.                     timeout=3;
  139.                 }
  140.             }
  141.         }
  142.     }
  143.     OSD.update(CHANNEL);
  144. }
复制代码
#include <AmebaServo.h>
AmebaServo myservo;

myservo.attach(8);   //舵机控制io
servoP01.png


在监测到index=7的物品(truck)时,转动舵机180度。
if(itemList[obj_type].index==7){
    myservo.write(180);
    timeout=3;
}
loop中3秒钟后转动舵机为0度
void loop()
{
    // Do nothing
    if(timeout>0){
        timeout--;
        if(timeout==0) myservo.write(0);
    }
    delay(1000);
}


8.png
这个玩具车的底盘被识别为truck。


运行效果:摄像头对准玩具车,在玩具车被识别为truck后,其上舵机转动180度,3秒钟后,舵机恢复到0度。

如果用于识别其他物品,舵机不会有反映。


ezgif-73770340ee326b.gif

──── 0人觉得很赞 ────

使用道具 举报

添加一个效果视频
您需要登录后才可以回帖 立即登录
高级模式
返回
统计信息
  • 会员数: 28064 个
  • 话题数: 39617 篇