Jetson Nano使用CSI摄像头教程(c++)

本文详细介绍使用JetsonNano与CSI摄像头进行人脸识别、视频流读取及二维码检测识别的方法。通过C++编程实现,包括环境配置、代码示例及运行效果。

Jetson Nano使用CSI摄像头教程(c++)

一、 人脸检测

C++下开发Opencv需要进行一些额外的配置,先看一下opencv的文件位置。Jetson Nano预装的Opencv4.1.1的头文件位置如下图所示:

库文件位置如下图所示:

只需要在Qt的pro文件中将上述两个目录包含进来。另外注意头文件和lib文件的添加方法。
QT的pro文件如下:

QT -= gui
 
CONFIG += c++11 console
CONFIG -= app_bundle
CONFIG += C++11  # 添加对C++11的支持
 
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
 
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
 
INCLUDEPATH += /usr/include/opencv4 #添加头文件路径
 
LIBS += -L/usr/lib/aarch64-linux-gnu -lopencv_core -lopencv_imgcodecs -lopencv_imgproc -lopencv_highgui -lopencv_objdetect  #添加需要链接的库
 
 
SOURCES += main.cpp

重新生成整个项目,然后将test.jpeg和haarcascade_frontalface_default.xml文件放置在编译生成的build-QTtest-unknown-Debug文件夹中,运行项目效果图如下所示:

二、读取CSI摄像头

使用C++编程读取CSI摄像头,可以看到已经可以正常的显示视频流图像了,但是由于树莓派摄像头本身的原因,其图像中还有很多的噪点,颜色也有些失真(真实工业场景中建议购买更好的摄像头)。C++版本的main.cpp文件如下:


#include <iostream>
#include <string>
#include <opencv4/opencv2/opencv.hpp>
#include <opencv4/opencv2/core.hpp>
#include <opencv4/opencv2/highgui.hpp>
#include <opencv4/opencv2/imgproc.hpp>
#include <opencv4/opencv2/objdetect.hpp>
#include <opencv4/opencv2/imgproc/types_c.h>
#include <opencv4/opencv2/videoio.hpp>

using namespace std;
using namespace cv;

string gstreamer_pipeline (int capture_width, int capture_height, int display_width, int display_height, int framerate, int flip_method)
{
   
   
    return "nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)" + to_string(capture_width) + ", height=(int)" +
           to_string(capture_height) + ", format=(string)NV12, framerate=(fraction)" + to_string(framerate) +
           "/1 ! nvvidconv flip-method=" + to_string(flip_method) + " ! video/x-raw, width=(int)" + to_string(display_width) + ", height=(int)" +
           to_string(display_height<
Jetson Nano 使用 CSI (Camera Serial Interface) 接口连接摄像头。要在 Jetson Nano使用 CSI 摄像头,您需要使用 NVIDIA JetPack SDK 安装相应的软件和驱动程序,并编写相应的 C 代码来访问和控制摄像头。 以下是一些基本的步骤,可以帮助您开始使用 CSI 摄像头: 1. 安装 JetPack SDK:JetPack SDK 包含了 NVIDIA Jetson Nano 的所有软件和驱动程序。您可以从 NVIDIA 的官方网站上下载 JetPack SDK,并按照指南进行安装。 2. 连接 CSI 摄像头:将 CSI 摄像头插入 Jetson NanoCSI 接口。 3. 编写 C 代码:使用 GStreamer 库编写 C 代码来访问和控制摄像头。GStreamer 是一种流媒体框架,可以帮助您在 Jetson Nano 上捕获和处理视频流。您可以使用 GStreamer 的 nvarguscamerasrc 插件来访问 CSI 摄像头。 以下是一个简单的 C 代码示例,可以使用 nvarguscamerasrc 插件捕获 CSI 摄像头的视频流: ```c #include <gst/gst.h> int main(int argc, char* argv[]) { GstElement *pipeline, *source, *sink; GstBus *bus; GstMessage *msg; GstStateChangeReturn ret; /* Initialize GStreamer */ gst_init(&argc, &argv); /* Create the elements */ source = gst_element_factory_make("nvarguscamerasrc", "source"); sink = gst_element_factory_make("nveglglessink", "sink"); /* Create the empty pipeline */ pipeline = gst_pipeline_new("test-pipeline"); if (!pipeline || !source || !sink) { g_printerr("Not all elements could be created.\n"); return -1; } /* Build the pipeline */ gst_bin_add_many(GST_BIN(pipeline), source, sink, NULL); if (gst_element_link(source, sink) != TRUE) { g_printerr("Elements could not be linked.\n"); gst_object_unref(pipeline); return -1; } /* Start playing */ ret = gst_element_set_state(pipeline, GST_STATE_PLAYING); if (ret == GST_STATE_CHANGE_FAILURE) { g_printerr("Unable to set the pipeline to the playing state.\n"); gst_object_unref(pipeline); return -1; } /* Wait until error or EOS */ bus = gst_element_get_bus(pipeline); msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS); /* Parse message */ if (msg != NULL) { GError *err; gchar *debug_info; switch (GST_MESSAGE_TYPE(msg)) { case GST_MESSAGE_ERROR: gst_message_parse_error(msg, &err, &debug_info); g_printerr("Error received from element %s: %s\n", GST_OBJECT_NAME(msg->src), err->message); g_printerr("Debugging information: %s\n", debug_info ? debug_info : "none"); g_clear_error(&err); g_free(debug_info); break; case GST_MESSAGE_EOS: g_print("End-Of-Stream reached.\n"); break; default: /* We should not reach here because we only asked for ERRORs and EOS */ g_printerr("Unexpected message received.\n"); break; } gst_message_unref(msg); } /* Free resources */ gst_object_unref(bus); gst_element_set_state(pipeline, GST_STATE_NULL); gst_object_unref(pipeline); return 0; } ``` 4. 编译和运行代码:使用 gcc 编译上述 C 代码,并在 Jetson Nano 上运行它。您应该能够看到 CSI 摄像头的视频流在屏幕上显示出来。 请注意,上述代码仅仅是一个简单的示例。如果您需要更复杂的功能,例如捕获图像、处理视频流等,请参考 GStreamer 的官方文档,并编写相应的 C 代码来实现您的需求。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值