通过ELF 1开发板实现读取摄像头视频进行目标检测

当前,将AI或深度学习算法(如分类、目标检测和轨迹追踪)部署到嵌入式设备,进而实现边缘计算,正成为轻量级深度学习算法发展的一个重要趋势。今天将与各位小伙伴分享一个实际案例:利用ChatGPT在ELF 1开发板上成功部署深度学习模型的项目,该项目能够实时读取摄像头视频流并实现对画面中的物体进行精准的目标检测。

项目所需的硬件设备:1、基于NXP(恩智浦)i.MX6ULL的ELF 1开发板,2、网线,3、USB摄像头。

获取开发板摄像头文件路径

本次项目开发使用的为普通的USB摄像头,将摄像头插在开发板任一USB口均可。

图片

在Linux开发板中使用USB摄像头,通常会涉及到一些基本的命令行操作。这些操作主要是通过 Video4Linux (V4L2)内核框架API进行的。以下是一些常用的命令和概念:

1. 列出所有摄像头设备: 使用 ls /dev/video* 命令可以列出所有已连接的视频设备。这些设备通常显示为 /dev/video0 , /dev/video1 等。如下图,开发板中对应的摄像头为/dev/video2(插入哪个USB口都是一样的)。

2. 查看摄像头信息: 使用v4l2-ctl --all -d /dev/video2可以查看特定摄像头(例如/dev/video2)的所有信息,包括支持的格式、帧率等。可以看到图象尺寸为640×480, 为了和后续的目标检测输入图像大小匹配,需要在程序中进行resize。

编写程序,读取取摄像头视频进行检测

并传递检测结果到上位机

编写在开发板中运行的程序,开发板中运行的程序主要有两个功能:

1. 读取摄像头捕捉的视频并进行检测

2. 将检测结果通过网络通信传递到上位机中

首先是第一个功能,因为一边要读取视频,一边要进行图片检测,为了提高检测速度,使用多线程来编写相应的程序。线程间通讯采用队列,为避免多线程间的竞态,在访问共享资源时需要添加互斥锁。

第二个功能,采用socket通信,将检测后的图像发送到上位机中即可。

下面是完整的程序实现:

/*命名为 squeezenetssd_thread.cpp */
#include "net.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <vector>
#include <chrono>
/*增加多线程代码*/
#include <thread>
#include <mutex>
#include <queue>
#include <condition_variable>
/*队列通信,全局变量*/
std::queue<cv::Mat> frameQueue;
std::mutex queueMutex;
std::condition_variable queueCondVar;
bool finished = false;
const size_t MAX_QUEUE_SIZE = 2; // 设为两个,因为检测速度实在太慢,多了意义不大
struct Object {
cv::Rect_<float> rect;
int label;
float prob;
};
ncnn::Net squeezenet;
int client_sock;
static int detect_squeezenet(const cv::Mat& bgr, std::vector<Object>& objects)
{
const int target_size = 300;
int img_w = bgr.cols;
int img_h = bgr.rows;
ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR,
bgr.cols, bgr.rows, target_size, target_size);
const float mean_vals[3] = {104.f, 117.f, 123.f};
in.substract_mean_normalize(mean_vals, 0);
ncnn::Extractor ex = squeezenet.create_extractor();
ex.input("data", in);
ncnn::Mat out;
ex.extract("detection_out", out);
// printf("%d %d %d
", out.w, out.h, out.c);
objects.clear();
for (int i = 0; i < out.h; i++)
{
const float* values = out.row(i);
Object object;
object.label = values[0];
object.prob = values[1];
object.rect.x = values[2] * img_w;
object.rect.y = values[3] * img_h;
object.rect.width = values[4] * img_w - object.rect.x;
object.rect.height = values[5] * img_h - object.rect.y;
objects.push_back(object);
}
return 0;
}
static void draw_objects(cv::Mat& bgr, const std::vector<Object>& objects)
{
static const char* class_names[] = {"background",
"aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair",
"cow", "diningtable", "dog", "horse",
"motorbike", "person", "pottedplant",
"sheep", "sofa", "train", "tvmonitor"
};
//cv::Mat image = bgr.clone();
//cv::Mat& image = bgr;
for (size_t i = 0; i < objects.size(); i++)
{
const Object& obj = objects[i];
fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f
", obj.label,
obj.prob,
obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);
cv::rectangle(bgr, obj.rect, cv::Scalar(255, 0, 0));
char text[256];
sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);
int baseLine = 0;
cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX,
0.5, 1, &baseLine);
int x = obj.rect.x;
int y = obj.rect.y - label_size.height - baseLine;
if (y < 0)
y = 0;
if (x + label_size.width > bgr.cols)
x = bgr.cols - label_size.width;
cv::rectangle(bgr, cv::Rect(cv::Point(x, y), cv::Size(label_size.width,
label_size.height + baseLine)),
cv::Scalar(255, 255, 255), -1);
cv::putText(bgr, text, cv::Point(x, y + label_size.height),
cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
}
// cv::imshow("image", image);
// cv::waitKey(0);
}
void send_to_client(const cv::Mat& image, int client_sock) {
std::vector<uchar> buffer;
std::vector<int> params = {cv::IMWRITE_JPEG_QUALITY, 80};
cv::imencode(".jpg", image, buffer, params);
uint32_t len = htonl(buffer.size());
send(client_sock, &len, sizeof(len), 0);
send(client_sock, buffer.data(), buffer.size(), 0);
}
/*线程1工作函数,此线程是用来采集相机图像的*/
static void captureThreadFunction(cv::VideoCapture& cap) {
while (true) {
cv::Mat frame;
cap >> frame;
if (frame.empty()) {
finished = true;
queueCondVar.notify_all();
break;
}
cv::rotate(frame, frame, cv::ROTATE_90_COUNTERCLOCKWISE); // 图像旋转90度
std::unique_lock<std::mutex> lock(queueMutex);
if (frameQueue.size() >= MAX_QUEUE_SIZE) {
frameQueue.pop(); // 丢弃最旧的帧
}
frameQueue.push(frame);
queueCondVar.notify_one();
}
}
/* 线程2工作函数,此线程是用来检测图像*/
void processThreadFunction() {
int frameCount = 0;
while (true) {
cv::Mat frame;
{
std::unique_lock<std::mutex> lock(queueMutex);
queueCondVar.wait(lock, []{ return !frameQueue.empty() || finished;
});
if (finished && frameQueue.empty()) {
// 退出前释放锁
return; // 使用 return 替代 break 来确保在持有锁时不退出循环
}
frame = frameQueue.front();
frameQueue.pop();
} // 锁在这里被释放
// 检测代码...
std::vector<Object> objects;
// if (++frameCount % 5 == 0) {
// detect_squeezenet(frame, objects);
// frameCount = 0;
// }
detect_squeezenet(frame,objects);
draw_objects(frame, objects);
send_to_client(frame, client_sock);
if (cv::waitKey(1) >= 0) {
break;
}
}
}
int main() {
int server_sock = socket(AF_INET, SOCK_STREAM, 0);
if (server_sock < 0) {
perror("socket 创建失败");
return -1;
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(12345);
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) <
0) {
perror("bind 失败");
close(server_sock);
return -1;
}
if (listen(server_sock, 1) < 0) {
perror("listen 失败");
close(server_sock);
return -1;
}
printf("等待客户端连接...
");
struct sockaddr_in client_addr;
socklen_t client_len = sizeof(client_addr);
client_sock = accept(server_sock, (struct sockaddr*)&client_addr,
&client_len);
if (client_sock < 0) {
perror("accept 失败");
close(server_sock);
return -1;
}
printf("客户端已连接
");
// ... [模型加载和初始化代码] ...
cv::VideoCapture cap("/dev/video2");
if (!cap.isOpened()) {
fprintf(stderr, "摄像头打开失败
");
return -1;
}
squeezenet.opt.use_vulkan_compute = true;
// original pretrained model from https://github.com/chuanqi305/SqueezeNetSSD
// squeezenet_ssd_voc_deploy.prototxt
// https://drive.google.com/open?id=0B3gersZ2cHIxdGpyZlZnbEQ5Snc
// the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
if (squeezenet.load_param("squeezenet_ssd_voc.param"))
exit(-1);
if (squeezenet.load_model("squeezenet_ssd_voc.bin"))
exit(-1);
std::thread captureThread(captureThreadFunction, std::ref(cap));
std::thread processThread(processThreadFunction);
captureThread.join();
processThread.join();
cap.release();
close(client_sock);
close(server_sock);
return 0;
}

将上述程序,拷贝到ncnn目录下,并更改CMakeLists.txt文件。

1、拷贝程序

2、更改CMakeLists.txt文件

做好以上工作后,我们直接进入ncnn-master/build/examples/ 文件夹下进行编译。编译之前直接切换到ncnn-master/build 目录输入:

cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/arm-linux-gnueabihf.toolchain.cmake -
DNCNN_SIMPLEOCV=ON -DNCNN_BUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release ..

然后切换到ncnn-master/build/examples/ 目录下输入 make -j4 即可。

可见编译成功,拷贝到开发板中就行。

编写上位机软件

上位机软件较为简单,使用ChatGPT编写即可,下面附上源码:

import socket
import cv2
import numpy as np

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('192.168.0.232', 12345))  # Connect to the server

while True:
    # Receive size of the frame
    size = client_socket.recv(4)
    size = int.from_bytes(size, byteorder='big')

    # Receive the frame
    buffer = b''
    while len(buffer) < size:
        buffer += client_socket.recv(size - len(buffer))

    # Decode and display the frame
    frame = np.frombuffer(buffer, dtype=np.uint8)
    frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
    cv2.imshow('Received Frame', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

client_socket.close()
cv2.destroyAllWindows()