1.代码

jetson nano使用一个USB摄像头和一个CSI摄像头,通过两个按键控制切换,程序开始自动打开USB摄像头,按K1键切换为CSI,按K2又返回USB。代码有两个版本。

使用前先配置jetson nano 的c++  GPIO库,方法如下,第一版代码作者也是链接主人

Jetson Nano GPIO C++库的安装使用_shing_star的博客-CSDN博客

第一版:goto  flag版

#include <memory>
#include <thread>#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>#include <chrono>
#include <signal.h>
#include <JetsonGPIO.h>using namespace GPIO;
using namespace std;
using namespace cv;static bool end_this_program = false;inline void delay(int s)
{this_thread::sleep_for(chrono::seconds(s));
}void signalHandler(int s)
{end_this_program = true;
}
// 创建CSI管道
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 main(int argc, char** argv)
{   int a=0;signal(SIGINT, signalHandler);int csi_to_usb = 16; int usb_to_csi = 18; GPIO::setmode(GPIO::BOARD);GPIO::setup(csi_to_usb, GPIO::IN);GPIO::setup(usb_to_csi, GPIO::IN);while(1){if(a==0){flag1:cv::VideoCapture cap(1);namedWindow("USB Camera", WINDOW_AUTOSIZE);Mat img;while (true){  if (!cap.read(img)){std::cout << "未能成功调用摄像头" << std::endl;return -1;}int new_width, new_height, width, height, channel;width = img.cols;height = img.rows;channel = img.channels();cout << width << "  " << height << "  " << channel << endl;new_width = 800;if (width > 800){new_height = int(new_width * 1.0 / width * height);}resize(img, img, cv::Size(new_width, new_height));cv::imshow("USB Camera", img);int exit_value1 = GPIO::input(usb_to_csi);if (exit_value1 == 0)break;//int keycode = cv::waitKey(1);int keycode = cv::waitKey(1) & 0xff;if (keycode == 27)break;}cap.release();destroyAllWindows();goto flag2;        }else if (a==1){flag2:int capture_width = 640;int capture_height = 480;int display_width = 640;int display_height = 480;int framerate = 25;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";//管道与视频流绑定VideoCapture cap(pipeline, CAP_GSTREAMER);if (!cap.isOpened()){std::cout << "打开摄像头失败." << std::endl;return (-1);}namedWindow("CSI Camera", WINDOW_AUTOSIZE);Mat img;//逐帧显示while (true){if (!cap.read(img)){std::cout << "捕获失败" << std::endl;break;}cv::imshow("CSI Camera", img);int exit_value2 = GPIO::input(csi_to_usb);if (exit_value2 == 0)break;int keycode = cv::waitKey(1) & 0xff;if (keycode == 27)break;}cap.release();destroyAllWindows();goto flag1;   }}GPIO::cleanup();return 0;
}

第二版 : a赋值版

#include <memory>
#include <thread>#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>#include <chrono>
#include <signal.h>
#include <JetsonGPIO.h>using namespace GPIO;
using namespace std;
using namespace cv;static bool end_this_program = false;inline void delay(int s)
{this_thread::sleep_for(chrono::seconds(s));
}void signalHandler(int s)
{end_this_program = true;
}
// 创建CSI管道
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 main(int argc, char** argv)
{   int a=0;signal(SIGINT, signalHandler);int csi_to_usb = 16; int usb_to_csi = 18; GPIO::setmode(GPIO::BOARD);GPIO::setup(csi_to_usb, GPIO::IN);GPIO::setup(usb_to_csi, GPIO::IN);while(1){if(a==0){flag1:cv::VideoCapture cap(1);namedWindow("USB Camera", WINDOW_AUTOSIZE);Mat img;while (true){  if (!cap.read(img)){std::cout << "未能成功调用摄像头" << std::endl;return -1;}int new_width, new_height, width, height, channel;width = img.cols;height = img.rows;channel = img.channels();cout << width << "  " << height << "  " << channel << endl;new_width = 800;if (width > 800){new_height = int(new_width * 1.0 / width * height);}resize(img, img, cv::Size(new_width, new_height));cv::imshow("USB Camera", img);int exit_value1 = GPIO::input(usb_to_csi);if (exit_value1 == 0)break;//int keycode = cv::waitKey(1);int keycode = cv::waitKey(1) & 0xff;if (keycode == 27)break;}cap.release();destroyAllWindows();a = 1;        }else if (a==1){flag2:int capture_width = 640;int capture_height = 480;int display_width = 640;int display_height = 480;int framerate = 25;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";//管道与视频流绑定VideoCapture cap(pipeline, CAP_GSTREAMER);if (!cap.isOpened()){std::cout << "打开摄像头失败." << std::endl;return (-1);}namedWindow("CSI Camera", WINDOW_AUTOSIZE);Mat img;//逐帧显示while (true){if (!cap.read(img)){std::cout << "捕获失败" << std::endl;break;}cv::imshow("CSI Camera", img);int exit_value2 = GPIO::input(csi_to_usb);if (exit_value2 == 0)break;int keycode = cv::waitKey(1) & 0xff;if (keycode == 27)break;}cap.release();destroyAllWindows();a = 0;   }}GPIO::cleanup();return 0;

控制台编译

1. g++ -o two_camera two_camera.cpp -lJetsonGPIO -lpthread  `pkg-config --cflags --libs opencv42. ./two_camera

Jetson Nano 按键切换摄像头相关推荐

  1. 在Jetson Nano上挂载摄像头并用OpenCV调用摄像头(较详细)

    --前言 我的摄像头为乐视的USB口摄像头,板子为Jetson Nano 4GB的板子 将摄像头接在Jetson Nano板子上,开机板子. 由于我之前已经实现Jetson Nano的内网穿透,故此处 ...

  2. jetson nano安装树莓派摄像头(v2)及调试的方法

    首先先烧写好jetson nano的镜像,这些工作已经默认完成. 需要在nano上使用树莓派相机,要注意jetson nano支持的是IMX219 sensor,所以树莓派相机中有这一款符合要求: R ...

  3. Jetson Nano使用CSI摄像头以及USB摄像头(CSI摄像头打开失败,USB摄像头打不开)

    目录 一.Jestson Nano打开CSI摄像头 二.Jestson Nano打开USB摄像头 大功告成!编写不易,大家成功后点个关注or赞谢谢~ 注意: 网上非常多的代码打不开的原因是要使用Pyt ...

  4. Jetson Nano 配合CSI摄像头和微雪云台Pan-tilt实现人脸追踪和人眼追踪

    我自认为自己做的这个东西是垃圾本科毕设,学术垃圾.本人制造专业,做这个程序的东西,唯一感触就是程序啊,就是东抄一点西抄一点,最后怎么成的也完全一头雾水(笑)当然,必须得认真感谢国内外各位大佬的源码. ...

  5. Jetson Nano 从入门到实战(案例:Opencv配置、人脸检测、二维码检测)

    目录 1. Jetson Nano简介 2. Jetson Nano环境配置 2.1 开箱配件介绍 2.2 烧录系统 2.3 开机和基本设置 2.4 开发环境配置 2.4.1 更新源和软件 2.4.2 ...

  6. Jetson Nano 从入门到实战(转载)(案例:Opencv配置、人脸检测、二维码检测)

    目录 1. Jetson Nano简介 2. Jetson Nano环境配置 2.1 开箱配件介绍 2.2 烧录系统 2.3 开机和基本设置 2.4 开发环境配置 2.4.1 更新源和软件 2.4.2 ...

  7. 【AI创造营】基于PaddleHub与Jetson Nano的智能宠物看护助手

    基于PaddleHub与Jetson Nano的智能宠物看护助手 一.效果展示 二.实现思路 1.训练一个适用于该需求的模型 2.使用PaddleHub预训练模型 三.数据采集 硬件部分 代码部分 四 ...

  8. K3s+Jetson Nano,在边缘端实现实时视频分析!

    随着越来越多功能强大的新设备开始支持GPU,边缘场景的用例不断扩展到各行各业.随着技术的发展,边缘的规模越来越大,效率越来越高.NVIDIA凭借其行业领先的GPU携手领先的处理器IP技术提供商ARM在 ...

  9. jetson nano 摄像头购买

    之前在jetson nano 上一直用csi 摄像头,因为使用比较频繁,两个插口都用坏了. 最近购买了linux免驱动摄像头,亲测可用 # 打开摄像头并灰度化显示 import cv2capture ...

最新文章

  1. HTTP协议解析之Cookie
  2. 多模态为什么比单模态好?第一份严谨证明来了!
  3. Android进阶:七、Retrofit2.0原理解析之最简流程【上】
  4. js删除字符串的最后一个字符三种方法
  5. 进程和程序:编写shell——《Unix/Linux编程实践教程》读书笔记(第8章)
  6. Jzoj4458 密钥破解——Pollard-rho
  7. 详解Shell编程之if语句实战(小结)
  8. 使用 Boost.MPI 的骨架和内容进行优化的示例
  9. Qt定时器QBasicTimer、startTimer、QTimer使用总结
  10. 习惯性朴实简单!一起学习MySQL常见单行函数,字符数学日期流程控制
  11. UIwebView缩放
  12. 小码农也有大梦想!人机猜拳java项目代码
  13. [转载] 用python语言设计计时器
  14. tp cli模式产生日志导致web环境写入不了
  15. 【面试题7】用两个栈实现队列
  16. linux下activityMQ安装
  17. NR 5G 网络功能之UPF
  18. 一级标题段前段后一行设置方法
  19. centos7.5 gnome3的主题优化
  20. UE4导入外部插件方法

热门文章

  1. python编程手机游戏_有哪些python写的游戏
  2. 波段操作是什么 波段操作技巧
  3. AIX环境:su 到实例用户下连库并执行Db2命令方法
  4. 程序员必看的那些电影
  5. 企业WiFi解决方案 安全上网很重要
  6. 【白板推导系列笔记】线性回归-最小二乘法及其几何意义最小二乘法-概率视角-高斯噪声-MLE
  7. 子网掩码,CIDR前缀法表示掩码
  8. foter 图像处理APP_摄影师必备的图像编辑APP
  9. 常用线性稳压器技术分析
  10. Android图文混排-实现EditText图文混合插入上传