Jetson TX1使用usb camera采集图像 (2)

该方法只启动usb摄像头

1import cv2 2import numpy 3import matplotlib.pyplot as plot 4 5class Camera: 6 cap = cv2.VideoCapture(0) 7 8 @staticmethod 9 def getCamera(): 10 ret, frame = Camera.cap.read() 11 return ret, frame 12 13 @staticmethod 14 def getCap(): 15 return Camera.cap 16 17 18def main(): 19 camera = Camera() 20 while(1): 21 ret, frame = camera.getCamera() 22 23 cv2.imshow("capture", frame) 24 if cv2.waitKey(1) & 0xFF == ord('q'): 25 break 26 27 camera.cap.release() 28 # cv2.destroyAllWindows() 29 30if __name__ == '__main__': 31 main()

C++ start onboard camera

1#include <stdio.h> 2#include <opencv2/opencv.hpp> 3 4using namespace cv; 5using namespace std; 6 7int main(int argc, char** argv) 8{ 9 VideoCapture cap("nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720,format=(string)I420, framerate=(fraction)24/1 ! nvvidconv flip-method=2 ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink"); 10 if (!cap.isOpened()) 11 { 12 cout << "Failed to open camera." << endl; 13 return -1; 14 } 15 16 for(;;) 17 { 18 Mat frame; 19 cap >> frame; 20 imshow("original", frame); 21 //waitKey(1); 22 if(waitKey(30) >= 0) 23 break; 24 } 25 return 0; 26}

C++ start usb camera

1/* 2Author:Jack-Cui 3Blog:http://blog.csdn.net/c406495762 4Time:25 May 2017 5*/ 6#include <unistd.h> 7#include <error.h> 8#include <errno.h> 9#include <fcntl.h> 10#include <sys/ioctl.h> 11#include <sys/types.h> 12#include <pthread.h> 13#include <linux/videodev2.h> 14#include <sys/mman.h> 15#include <opencv2/core/core.hpp> 16#include <opencv2/highgui/highgui.hpp> 17#include <stdio.h> 18#include <stdlib.h> 19#include <string.h> 20 21#include <iostream> 22#include <iomanip> 23#include <string> 24 25using namespace std; 26 27#define CLEAR(x) memset(&(x), 0, sizeof(x)) 28 29#define IMAGEWIDTH 3264 30#define IMAGEHEIGHT 2448 31 32class V4L2Capture { 33public: 34 V4L2Capture(char *devName, int width, int height); 35 virtual ~V4L2Capture(); 36 37 int openDevice(); 38 int closeDevice(); 39 int initDevice(); 40 int startCapture(); 41 int stopCapture(); 42 int freeBuffers(); 43 int getFrame(void **,size_t *); 44 int backFrame(); 45 static void test(); 46 47private: 48 int initBuffers(); 49 50 struct cam_buffer 51 { 52 void* start; 53 unsigned int length; 54 }; 55 char *devName; 56 int capW; 57 int capH; 58 int fd_cam; 59 cam_buffer *buffers; 60 unsigned int n_buffers; 61 int frameIndex; 62}; 63 64V4L2Capture::V4L2Capture(char *devName, int width, int height) { 65 // TODO Auto-generated constructor stub 66 this->devName = devName; 67 this->fd_cam = -1; 68 this->buffers = NULL; 69 this->n_buffers = 0; 70 this->frameIndex = -1; 71 this->capW=width; 72 this->capH=height; 73} 74 75V4L2Capture::~V4L2Capture() { 76 // TODO Auto-generated destructor stub 77} 78 79int V4L2Capture::openDevice() { 80 /*设备的打开*/ 81 printf("video dev : %s\n", devName); 82 fd_cam = open(devName, O_RDWR); 83 if (fd_cam < 0) { 84 perror("Can't open video device"); 85 } 86 return 0; 87} 88 89int V4L2Capture::closeDevice() { 90 if (fd_cam > 0) { 91 int ret = 0; 92 if ((ret = close(fd_cam)) < 0) { 93 perror("Can't close video device"); 94 } 95 return 0; 96 } else { 97 return -1; 98 } 99} 100 101int V4L2Capture::initDevice() { 102 int ret; 103 struct v4l2_capability cam_cap; //显示设备信息 104 struct v4l2_cropcap cam_cropcap; //设置摄像头的捕捉能力 105 struct v4l2_fmtdesc cam_fmtdesc; //查询所有支持的格式:VIDIOC_ENUM_FMT 106 struct v4l2_crop cam_crop; //图像的缩放 107 struct v4l2_format cam_format; //设置摄像头的视频制式、帧格式等 108 109 /* 使用IOCTL命令VIDIOC_QUERYCAP,获取摄像头的基本信息*/ 110 ret = ioctl(fd_cam, VIDIOC_QUERYCAP, &cam_cap); 111 if (ret < 0) { 112 perror("Can't get device information: VIDIOCGCAP"); 113 } 114 printf( 115 "Driver Name:%s\nCard Name:%s\nBus info:%s\nDriver Version:%u.%u.%u\n", 116 cam_cap.driver, cam_cap.card, cam_cap.bus_info, 117 (cam_cap.version >> 16) & 0XFF, (cam_cap.version >> 8) & 0XFF, 118 cam_cap.version & 0XFF); 119 120 /* 使用IOCTL命令VIDIOC_ENUM_FMT,获取摄像头所有支持的格式*/ 121 cam_fmtdesc.index = 0; 122 cam_fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 123 printf("Support format:\n"); 124 while (ioctl(fd_cam, VIDIOC_ENUM_FMT, &cam_fmtdesc) != -1) { 125 printf("\t%d.%s\n", cam_fmtdesc.index + 1, cam_fmtdesc.description); 126 cam_fmtdesc.index++; 127 } 128 129 /* 使用IOCTL命令VIDIOC_CROPCAP,获取摄像头的捕捉能力*/ 130 cam_cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 131 if (0 == ioctl(fd_cam, VIDIOC_CROPCAP, &cam_cropcap)) { 132 printf("Default rec:\n\tleft:%d\n\ttop:%d\n\twidth:%d\n\theight:%d\n", 133 cam_cropcap.defrect.left, cam_cropcap.defrect.top, 134 cam_cropcap.defrect.width, cam_cropcap.defrect.height); 135 /* 使用IOCTL命令VIDIOC_S_CROP,获取摄像头的窗口取景参数*/ 136 cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 137 cam_crop.c = cam_cropcap.defrect; //默认取景窗口大小 138 if (-1 == ioctl(fd_cam, VIDIOC_S_CROP, &cam_crop)) { 139 //printf("Can't set crop para\n"); 140 } 141 } else { 142 printf("Can't set cropcap para\n"); 143 } 144 145 /* 使用IOCTL命令VIDIOC_S_FMT,设置摄像头帧信息*/ 146 cam_format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 147 cam_format.fmt.pix.width = capW; 148 cam_format.fmt.pix.height = capH; 149 cam_format.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG; //要和摄像头支持的类型对应 150 cam_format.fmt.pix.field = V4L2_FIELD_INTERLACED; 151 ret = ioctl(fd_cam, VIDIOC_S_FMT, &cam_format); 152 if (ret < 0) { 153 perror("Can't set frame information"); 154 } 155 /* 使用IOCTL命令VIDIOC_G_FMT,获取摄像头帧信息*/ 156 cam_format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 157 ret = ioctl(fd_cam, VIDIOC_G_FMT, &cam_format); 158 if (ret < 0) { 159 perror("Can't get frame information"); 160 } 161 printf("Current data format information:\n\twidth:%d\n\theight:%d\n", 162 cam_format.fmt.pix.width, cam_format.fmt.pix.height); 163 ret = initBuffers(); 164 if (ret < 0) { 165 perror("Buffers init error"); 166 //exit(-1); 167 } 168 return 0; 169} 170 171int V4L2Capture::initBuffers() { 172 int ret; 173 /* 使用IOCTL命令VIDIOC_REQBUFS,申请帧缓冲*/ 174 struct v4l2_requestbuffers req; 175 CLEAR(req); 176 req.count = 4; 177 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 178 req.memory = V4L2_MEMORY_MMAP; 179 ret = ioctl(fd_cam, VIDIOC_REQBUFS, &req); 180 if (ret < 0) { 181 perror("Request frame buffers failed"); 182 } 183 if (req.count < 2) { 184 perror("Request frame buffers while insufficient buffer memory"); 185 } 186 buffers = (struct cam_buffer*) calloc(req.count, sizeof(*buffers)); 187 if (!buffers) { 188 perror("Out of memory"); 189 } 190 for (n_buffers = 0; n_buffers < req.count; n_buffers++) { 191 struct v4l2_buffer buf; 192 CLEAR(buf); 193 // 查询序号为n_buffers 的缓冲区,得到其起始物理地址和大小 194 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 195 buf.memory = V4L2_MEMORY_MMAP; 196 buf.index = n_buffers; 197 ret = ioctl(fd_cam, VIDIOC_QUERYBUF, &buf); 198 if (ret < 0) { 199 printf("VIDIOC_QUERYBUF %d failed\n", n_buffers); 200 return -1; 201 } 202 buffers[n_buffers].length = buf.length; 203 //printf("buf.length= %d\n",buf.length); 204 // 映射内存 205 buffers[n_buffers].start = mmap( 206 NULL, // start anywhere 207 buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd_cam, 208 buf.m.offset); 209 if (MAP_FAILED == buffers[n_buffers].start) { 210 printf("mmap buffer%d failed\n", n_buffers); 211 return -1; 212 } 213 } 214 return 0; 215} 216 217int V4L2Capture::startCapture() { 218 unsigned int i; 219 for (i = 0; i < n_buffers; i++) { 220 struct v4l2_buffer buf; 221 CLEAR(buf); 222 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 223 buf.memory = V4L2_MEMORY_MMAP; 224 buf.index = i; 225 if (-1 == ioctl(fd_cam, VIDIOC_QBUF, &buf)) { 226 printf("VIDIOC_QBUF buffer%d failed\n", i); 227 return -1; 228 } 229 } 230 enum v4l2_buf_type type; 231 type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 232 if (-1 == ioctl(fd_cam, VIDIOC_STREAMON, &type)) { 233 printf("VIDIOC_STREAMON error"); 234 return -1; 235 } 236 return 0; 237} 238 239int V4L2Capture::stopCapture() { 240 enum v4l2_buf_type type; 241 type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 242 if (-1 == ioctl(fd_cam, VIDIOC_STREAMOFF, &type)) { 243 printf("VIDIOC_STREAMOFF error\n"); 244 return -1; 245 } 246 return 0; 247} 248 249int V4L2Capture::freeBuffers() { 250 unsigned int i; 251 for (i = 0; i < n_buffers; ++i) { 252 if (-1 == munmap(buffers[i].start, buffers[i].length)) { 253 printf("munmap buffer%d failed\n", i); 254 return -1; 255 } 256 } 257 free(buffers); 258 return 0; 259} 260 261int V4L2Capture::getFrame(void **frame_buf, size_t* len) { 262 struct v4l2_buffer queue_buf; 263 CLEAR(queue_buf); 264 queue_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 265 queue_buf.memory = V4L2_MEMORY_MMAP; 266 if (-1 == ioctl(fd_cam, VIDIOC_DQBUF, &queue_buf)) { 267 printf("VIDIOC_DQBUF error\n"); 268 return -1; 269 } 270 *frame_buf = buffers[queue_buf.index].start; 271 *len = buffers[queue_buf.index].length; 272 frameIndex = queue_buf.index; 273 return 0; 274} 275 276int V4L2Capture::backFrame() { 277 if (frameIndex != -1) { 278 struct v4l2_buffer queue_buf; 279 CLEAR(queue_buf); 280 queue_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 281 queue_buf.memory = V4L2_MEMORY_MMAP; 282 queue_buf.index = frameIndex; 283 if (-1 == ioctl(fd_cam, VIDIOC_QBUF, &queue_buf)) { 284 printf("VIDIOC_QBUF error\n"); 285 return -1; 286 } 287 return 0; 288 } 289 return -1; 290} 291 292void V4L2Capture::test() { 293 unsigned char *yuv422frame = NULL; 294 unsigned long yuvframeSize = 0; 295 296 string videoDev="/dev/video0"; 297 V4L2Capture *vcap = new V4L2Capture(const_cast<char*>(videoDev.c_str()), 298 1920, 1080); 299 vcap->openDevice(); 300 vcap->initDevice(); 301 vcap->startCapture(); 302 vcap->getFrame((void **) &yuv422frame, (size_t *)&yuvframeSize); 303 304 vcap->backFrame(); 305 vcap->freeBuffers(); 306 vcap->closeDevice(); 307} 308 309void VideoPlayer() { 310 unsigned char *yuv422frame = NULL; 311 unsigned long yuvframeSize = 0; 312 313 string videoDev = "/dev/video0"; 314 V4L2Capture *vcap = new V4L2Capture(const_cast<char*>(videoDev.c_str()), 1920, 1080); 315 vcap->openDevice(); 316 vcap->initDevice(); 317 vcap->startCapture(); 318 319 cvNamedWindow("Capture",CV_WINDOW_AUTOSIZE); 320 IplImage* img; 321 CvMat cvmat; 322 double t; 323 while(1){ 324 t = (double)cvGetTickCount(); 325 vcap->getFrame((void **) &yuv422frame, (size_t *)&yuvframeSize); 326 cvmat = cvMat(IMAGEHEIGHT,IMAGEWIDTH,CV_8UC3,(void*)yuv422frame); //CV_8UC3 327 328 //解码 329 img = cvDecodeImage(&cvmat,1); 330 if(!img){ 331 printf("DecodeImage error!\n"); 332 } 333 334 cvShowImage("Capture",img); 335 cvReleaseImage(&img); 336 337 vcap->backFrame(); 338 if((cvWaitKey(1)&255) == 27){ 339 exit(0); 340 } 341 t = (double)cvGetTickCount() - t; 342 printf("Used time is %g ms\n",( t / (cvGetTickFrequency()*1000))); 343 } 344 vcap->stopCapture(); 345 vcap->freeBuffers(); 346 vcap->closeDevice(); 347 348} 349 350int main() { 351 VideoPlayer(); 352 return 0; 353}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Jetson TX1使用usb camera采集图像 (2) - HelloWorld