TX1上H.264编解码验证方案:
TX1上使用GStreamer获取板载摄像头视频内容并压缩为H.264流服务器,再使用OpenCV+GStreamer接受H.264视频流。(TX1上H.264编解码验证方案:OpenCV+GStreamer+H.264编解码)
视频流服务器:
1.安装TX1的板载摄像头驱动:TX1没有提供默认v4l2的驱动的,所以首先需要自己安装驱动,但是这个驱动是基于gstreamer的。
2.整合了一套gstreamer的管道命令,如下:
export CLIENT_IP=127.0.0.1
gst-launch-1.0 nvcamerasrc fpsRange="30 30" intent=3 ! nvvidconv flip-method=6 ! 'video/x-raw(memory:NVMM), width=(int)960, height=(int)540, format=(string)I420, framerate=(fraction)30/1' ! omxh264enc control-rate=2 bitrate=4000000 ! 'video/x-h264, stream-format=(string)byte-stream' ! h264parse ! queue ! omxh264dec ! nvvidconv ! 'video/x-raw, format=(string)UYVY' ! videoconvert ! jpegenc quality=30 ! rtpjpegpay ! udpsink host=$CLIENT_IP port=5000 sync=false async=false
以上代码把摄像头上的内容抓取并压缩为960p的30帧的H.264格式的视频流,并通过udp协议发送到板卡的网络端口【5000】上。
以上是GStreamer的服务器端,主要提供H.264视频流服务。
视频流客户端:
1.编写客户端代码
客户端直接上代码,(参考youtube上的JETSON的OpenCV教程结合OpenCV处理GStreamer的API组装出来的程序,跑上就可以用啦)
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/calib3d/calib3d.hpp>
using namespace cv;
int main(int, char**)
{
VideoCapture input("./stream.sdp");
if(!input.isOpened()){ // check if we succeeded
std::cout<< "open failed" << std::endl;
return -1;
}
Mat img, img_gray;
OrbFeatureDetector detector(7000);
vector<KeyPoint> img_keypoints, car_keypoints;
Mat img_descriptors, car_descriptors;
input.read(img);
Mat car;
img(Rect(400, 320, 150, 100)).copyTo(car);
detector(car, Mat(), car_keypoints, car_descriptors);
drawKeypoints(car, car_keypoints, car);
for(;;)
{
if(!input.read(img))
break;
detector(img, Mat(), img_keypoints, img_descriptors);
drawKeypoints(img, img_keypoints, img);
BFMatcher matcher;
vector<DMatch> matches;
matcher.match(car_descriptors, img_descriptors, matches);
vector<Point2f> car_points, img_points;
for(int i=0; i < matches.size(); ++i){
car_points.push_back(car_keypoints[matches[i].queryIdx].pt);
img_points.push_back(img_keypoints[matches[i].queryIdx].pt);
}
std::cout<<"car points count = " << car_points.size() << std::endl;
if(car_points.size() >= 4){
Matx33f H = findHomography(car_points, img_points, CV_RANSAC);
vector<Point> car_border, img_border;
car_border.push_back(Point(0, 0));
car_border.push_back(Point(0, car.rows));
car_border.push_back(Point(car.cols, car.rows));
car_border.push_back(Point(car.cols, 0));
for (size_t i = 0; i < car_border.size(); ++i){
Vec3f p = H * Vec3f(car_border[i].x, car_border[i].y, 1);
img_border.push_back(Point(p[0]/p[2], p[1]/p[2]));
}
polylines(img, img_border, true, CV_RGB(255, 255, 0));
Mat img_matches;
drawMatches(car, car_keypoints, img, img_keypoints, matches, img_matches);
imshow("img_matches", img_matches);
}
// imshow("car", car);
// imshow("img", img);
if(waitKey(27) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
2.编写CMake
配置文件CMakeLists.txt如下:
cmake_minimum_required(VERSION 2.8)
project(hello)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(cv_hello hello.cpp)
target_link_libraries(cv_hello ${OpenCV_LIBS}
3.执行CMake
执行命令:cmake ./ && make
4.编写stream.sdp配置文件
[stream.sdp]
c=IN IP4 127.0.0.1
m=video 5000 RTP/AVP 96
a=rtpmap:96 JPEG/4000000
以上步骤完成后,就可以看到摄像头的视频了。