本帖最后由 sujingliang 于 2025-3-12 10:59 编辑
用BW21-CBV-Kit开发板使用摄像头可以监测80种不同类型的物体,如人、自行车、汽车、笔记本电脑等。
ObjectClassList.h:
- ObjectDetectionItem itemList[80] = {
- {0, "person", 1},
- {1, "bicycle", 1},
- {2, "car", 1},
- {3, "motorbike", 1},
- {4, "aeroplane", 1},
- {5, "bus", 1},
- {6, "train", 1},
- {7, "truck", 1},
- {8, "boat", 1},
- {9, "traffic light", 1},
- {10, "fire hydrant", 1},
- {11, "stop sign", 1},
- {12, "parking meter", 1},
- {13, "bench", 1},
- {14, "bird", 1},
- {15, "cat", 1},
- {16, "dog", 1},
- {17, "horse", 1},
- {18, "sheep", 1},
复制代码
在“文件”->“示例”->“AmebaNN”->“ObjectDetectionLoop”中打开一个对象检测示例。
- /*
- Example guide:
- https://www.amebaiot.com/en/amebapro2-arduino-neuralnework-object-detection/
- NN Model Selection
- Select Neural Network(NN) task and models using .modelSelect(nntask, objdetmodel, facedetmodel, facerecogmodel).
- Replace with NA_MODEL if they are not necessary for your selected NN Task.
- NN task
- =======
- OBJECT_DETECTION/ FACE_DETECTION/ FACE_RECOGNITION
- Models
- =======
- YOLOv3 model DEFAULT_YOLOV3TINY / CUSTOMIZED_YOLOV3TINY
- YOLOv4 model DEFAULT_YOLOV4TINY / CUSTOMIZED_YOLOV4TINY
- YOLOv7 model DEFAULT_YOLOV7TINY / CUSTOMIZED_YOLOV7TINY
- SCRFD model DEFAULT_SCRFD / CUSTOMIZED_SCRFD
- MobileFaceNet model DEFAULT_MOBILEFACENET/ CUSTOMIZED_MOBILEFACENET
- No model NA_MODEL
- */
- #include "WiFi.h"
- #include "StreamIO.h"
- #include "VideoStream.h"
- #include "RTSP.h"
- #include "NNObjectDetection.h"
- #include "VideoStreamOverlay.h"
- #include "ObjectClassList.h"
- #include <AmebaServo.h>
- #define CHANNEL 0
- #define CHANNELNN 3
- // Lower resolution for NN processing
- #define NNWIDTH 576
- #define NNHEIGHT 320
- AmebaServo myservo;
- VideoSetting config(VIDEO_FHD, 30, VIDEO_H264, 0);
- VideoSetting configNN(NNWIDTH, NNHEIGHT, 10, VIDEO_RGB, 0);
- NNObjectDetection ObjDet;
- RTSP rtsp;
- StreamIO videoStreamer(1, 1);
- StreamIO videoStreamerNN(1, 1);
- char ssid[] = "ssid"; // your network SSID (name)
- char pass[] = "pass"; // your network password
- int status = WL_IDLE_STATUS;
- int timeout=0;
- IPAddress ip;
- int rtsp_portnum;
- void setup()
- {
- Serial.begin(115200);
- myservo.attach(8);
- // attempt to connect to Wifi network:
- while (status != WL_CONNECTED) {
- Serial.print("Attempting to connect to WPA SSID: ");
- Serial.println(ssid);
- status = WiFi.begin(ssid, pass);
- // wait 2 seconds for connection:
- delay(2000);
- }
- ip = WiFi.localIP();
- // Configure camera video channels with video format information
- // Adjust the bitrate based on your WiFi network quality
- config.setBitrate(2 * 1024 * 1024); // Recommend to use 2Mbps for RTSP streaming to prevent network congestion
- Camera.configVideoChannel(CHANNEL, config);
- Camera.configVideoChannel(CHANNELNN, configNN);
- Camera.videoInit();
- // Configure RTSP with corresponding video format information
- rtsp.configVideo(config);
- rtsp.begin();
- rtsp_portnum = rtsp.getPort();
- // Configure object detection with corresponding video format information
- // Select Neural Network(NN) task and models
- ObjDet.configVideo(configNN);
- ObjDet.setResultCallback(ODPostProcess);
- ObjDet.modelSelect(OBJECT_DETECTION, DEFAULT_YOLOV4TINY, NA_MODEL, NA_MODEL);
- ObjDet.begin();
- // Configure StreamIO object to stream data from video channel to RTSP
- videoStreamer.registerInput(Camera.getStream(CHANNEL));
- videoStreamer.registerOutput(rtsp);
- if (videoStreamer.begin() != 0) {
- Serial.println("StreamIO link start failed");
- }
- // Start data stream from video channel
- Camera.channelBegin(CHANNEL);
- // Configure StreamIO object to stream data from RGB video channel to object detection
- videoStreamerNN.registerInput(Camera.getStream(CHANNELNN));
- videoStreamerNN.setStackSize();
- videoStreamerNN.setTaskPriority();
- videoStreamerNN.registerOutput(ObjDet);
- if (videoStreamerNN.begin() != 0) {
- Serial.println("StreamIO link start failed");
- }
- // Start video channel for NN
- Camera.channelBegin(CHANNELNN);
- // Start OSD drawing on RTSP video channel
- OSD.configVideo(CHANNEL, config);
- OSD.begin();
- }
- void loop()
- {
- // Do nothing
- if(timeout>0){
- timeout--;
- if(timeout==0) myservo.write(0);
- }
- delay(1000);
- }
- // User callback function for post processing of object detection results
- void ODPostProcess(std::vector<ObjectDetectionResult> results)
- {
- uint16_t im_h = config.height();
- uint16_t im_w = config.width();
- Serial.print("Network URL for RTSP Streaming: ");
- Serial.print("rtsp://");
- Serial.print(ip);
- Serial.print(":");
- Serial.println(rtsp_portnum);
- Serial.println(" ");
- printf("Total number of objects detected = %d\r\n", ObjDet.getResultCount());
- OSD.createBitmap(CHANNEL);
- if (ObjDet.getResultCount() > 0) {
- for (int i = 0; i < ObjDet.getResultCount(); i++) {
- int obj_type = results[i].type();
- if (itemList[obj_type].filter) { // check if item should be ignored
- ObjectDetectionResult item = results[i];
- // Result coordinates are floats ranging from 0.00 to 1.00
- // Multiply with RTSP resolution to get coordinates in pixels
- int xmin = (int)(item.xMin() * im_w);
- int xmax = (int)(item.xMax() * im_w);
- int ymin = (int)(item.yMin() * im_h);
- int ymax = (int)(item.yMax() * im_h);
- // Draw boundary box
- printf("Item %d %s:\t%d %d %d %d\n\r", i, itemList[obj_type].objectName, xmin, xmax, ymin, ymax);
- OSD.drawRect(CHANNEL, xmin, ymin, xmax, ymax, 3, OSD_COLOR_WHITE);
- // Print identification text
- char text_str[20];
- snprintf(text_str, sizeof(text_str), "%s %d", itemList[obj_type].objectName, item.score());
- OSD.drawText(CHANNEL, xmin, ymin - OSD.getTextHeight(CHANNEL), text_str, OSD_COLOR_CYAN);
- if(itemList[obj_type].index==7){
- myservo.write(180);
- timeout=3;
- }
- }
- }
- }
- OSD.update(CHANNEL);
- }
复制代码 #include <AmebaServo.h>
AmebaServo myservo;
myservo.attach(8); //舵机控制io
在监测到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);
}
这个玩具车的底盘被识别为truck。
运行效果:摄像头对准玩具车,在玩具车被识别为truck后,其上舵机转动180度,3秒钟后,舵机恢复到0度。
如果用于识别其他物品,舵机不会有反映。
|