16、JETSON NANO 对特定目标进行识别和gpio输出高低电平报警
生活随笔
收集整理的這篇文章主要介紹了
16、JETSON NANO 对特定目标进行识别和gpio输出高低电平报警
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
基本思想:需要對特定目標進行識別和報警,TensorRT做的的效果會極其好,但是沒時間,先使用ncnn的yolo-fastest做個例子吧,后期修改為TensorRT框架識別吧,該博客主要學習如何控制gpio的輸入輸出電平
首先JETSON NANO裝opencv和基本的配置庫;
創建一個工程,然后引入NCNN的基本庫,
ubuntu@ubuntu:~/yolo5$ tree -L 2 . ├── build │?? ├── CMakeCache.txt │?? ├── CMakeFiles │?? ├── cmake_install.cmake │?? ├── main │?? └── Makefile ├── CMakeLists.txt ├── include │?? └── ncnn ├── lib │?? ├── cmake │?? └── libncnn.a ├── main.cpp └── model├── yolo-fastest.bin└── yolo-fastest.param7 directories, 9 filesCMakelists.txt的設置
cmake_minimum_required(VERSION 3.10) project(untitled3)set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp")include_directories(${CMAKE_SOURCE_DIR}/include/ncnn/) #導入ncnn add_library(libncnn STATIC IMPORTED) set_target_properties(libncnn PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/libncnn.a) find_package(OpenCV REQUIRED) set(CMAKE_CXX_STANDARD 11)add_executable(main main.cpp) target_link_libraries(main libncnn ${OpenCV_LIBS})JETSON NANO 的源碼
#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> #include "benchmark.h" #include "cpu.h" #include "datareader.h" #include "net.h" #include "gpu.h"#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <stdio.h> #include <vector> #include <algorithm> using namespace std; using namespace cv; using namespace ncnn; int demo(cv::Mat& image, ncnn::Net &detector, int detector_size_width, int detector_size_height) {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 bgr = image.clone();int img_w = bgr.cols;int img_h = bgr.rows;ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR2RGB,\bgr.cols, bgr.rows, detector_size_width, detector_size_height);//數據預處理const float mean_vals[3] = {0.f, 0.f, 0.f};const float norm_vals[3] = {1/255.f, 1/255.f, 1/255.f};in.substract_mean_normalize(mean_vals, norm_vals);ncnn::Extractor ex = detector.create_extractor();ex.set_num_threads(8);ex.input("data", in);ncnn::Mat out;ex.extract("output", out);for (int i = 0; i < out.h; i++){int label;float x1, y1, x2, y2, score;float pw,ph,cx,cy;const float* values = out.row(i);x1 = values[2] * img_w;y1 = values[3] * img_h;x2 = values[4] * img_w;y2 = values[5] * img_h;score = values[1];label = values[0];//處理坐標越界問題if(x1<0) x1=0;if(y1<0) y1=0;if(x2<0) x2=0;if(y2<0) y2=0;if(x1>img_w) x1=img_w;if(y1>img_h) y1=img_h;if(x2>img_w) x2=img_w;if(y2>img_h) y2=img_h;cv::rectangle (image, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(255, 255, 0), 1, 1, 0);char text[256];sprintf(text, "%s %.1f%%", class_names[label], score * 100);int baseLine = 0;cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);cv::putText(image, text, cv::Point(x1, y1 + label_size.height),cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));std::cout<<class_names[label]<<endl;if( class_names[label]=="dog") //識別到狗就點亮 檢測不到狗 就不點亮{std::cout<<class_names[label]<<endl;system("echo 1 > /sys/class/gpio/gpio79/value");system("echo 0 > /sys/class/gpio/gpio79/value");}}return 0; }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) + ", format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink"; }//攝像頭測試 int test_cam(){//定義yolo-fastest VOC檢測器ncnn::Net detector;detector.load_param("/home/li/yolo5/model/yolo-fastest.param");detector.load_model("/home/li/yolo5/model/yolo-fastest.bin");int detector_size_width = 320;int detector_size_height = 320;cv::Mat frame;int capture_width = 1280 ;int capture_height = 720 ;int display_width = 1280 ;int display_height = 720 ;int framerate = 60 ;int flip_method = 0 ;string pipeline = gstreamer_pipeline(capture_width,capture_height,display_width,display_height,framerate,flip_method);std::cout << "使用gstreamer管道: \n\t" << pipeline << "\n";//管道與視頻流綁定cv::VideoCapture cap(pipeline, CAP_GSTREAMER); // 該操作是外接 CSI 普通攝像頭system("echo 79 > /sys/class/gpio/export");system("echo out > /sys/class/gpio/gpio79/direction");// 對應NVIDIA Jetson Nano 對應pin腳12cv::VideoCapture cap(1); // 該操作是外接 usb 普通攝像頭while (true){cap >> frame;double start = ncnn::get_current_time();demo(frame, detector, detector_size_width, detector_size_height);double end = ncnn::get_current_time();double time = end - start;printf("Time:%7.2f \n",time);cv::imshow("demo", frame);//system("echo 1 > /sys/class/gpio/gpio79/value");//system("echo 0 > /sys/class/gpio/gpio79/value");cv::waitKey(1);}return 0; } int main() {test_cam();return 0; }后期改TensorRT,,,,原諒我~~? 主要想引入一個例子 如何在nano上控制gpio口
附錄接口圖
總結
以上是生活随笔為你收集整理的16、JETSON NANO 对特定目标进行识别和gpio输出高低电平报警的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css特效文件,CSS样式特效演示文件修
- 下一篇: 计算机控制系统硬件组成实验报告,计算机硬