123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- #include <opencv2/opencv.hpp>
- #include <opencv2/core/core.hpp>
- #include <opencv2/highgui/highgui.hpp>
- #include <string>
- #include <iostream>
- #include <zconf.h>
- #include <csignal>
- #include <thread>
- #include "MvGmslCamera.h"
- #include <fstream>
- #include <chrono>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/ioctl.h>
- #include <linux/videodev2.h>
- #include <thread>
- #define VIDEO_OUT "/dev/video8"
- using namespace std::chrono;
- using std::string;
- sig_atomic_t exitRequested = 0;
- uint camera_num = 6;
- struct sync_out_a_cfg_client_t stCameraCfgSend = {};
- char dev_node[32] = "/dev/video0";
- std::string camera_fmt_str = "UYVY";
- std::string output_fmt_str = "BGRA32";
- uint cam_w = 1920;
- uint cam_h = 1080;
- bool MODEL = true;
- uint64_t timestampbefore[8] = {0};
- uint64_t LinuxGetFrameTimeBefore[8] = {0};
- void handler(int)
- {
- std::cout << "will exit..." << std::endl;
- exitRequested = true;
- }
- std::string GetCurrentTimeStamp(int time_stamp_type = 0)
- {
- std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
- std::time_t now_time_t = std::chrono::system_clock::to_time_t(now);
- std::tm *now_tm = std::localtime(&now_time_t);
- char buffer[128];
- strftime(buffer, sizeof(buffer), "%F %T", now_tm);
- std::ostringstream ss;
- ss.fill('0');
- std::chrono::milliseconds ms;
- std::chrono::microseconds cs;
- std::chrono::nanoseconds ns;
- switch (time_stamp_type)
- {
- case 0:
- ss << buffer;
- break;
- case 1:
- ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
- ss << buffer << ":" << ms.count();
- break;
- case 2:
- ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
- cs = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()) % 1000000;
- ss << buffer << ":" << ms.count() << ":" << cs.count() % 1000;
- break;
- case 3:
- ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
- cs = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()) % 1000000;
- ns = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()) % 1000000000;
- ss << buffer << ":" << ms.count() << ":" << cs.count() % 1000 << ":" << ns.count() % 1000;
- break;
- default:
- ss << buffer;
- break;
- }
- return ss.str();
- }
- cv::Mat meger;
- uint meger_width = 2020;
- uint meger_height = 970;
- int f_frame_size[] = {1280, 640, 1280, 640, 640, 360, 640, 360, 640, 320, 640, 320};
- cv::Rect f_rectCenter = cv::Rect(370, 330, f_frame_size[0], f_frame_size[1]);
- cv::Rect f_rectCenterUp = cv::Rect(650, 0, f_frame_size[2] - 560, f_frame_size[3] - 320);
- cv::Rect f_rectLeftDown = cv::Rect(0, 330, f_frame_size[5], f_frame_size[4]);
- cv::Rect f_rectRightDown = cv::Rect(1660, 330, f_frame_size[7], f_frame_size[6]);
- cv::Rect f_rectLeftUp = cv::Rect(1380, 0, f_frame_size[8], f_frame_size[9]);
- cv::Rect f_rectRightUp = cv::Rect(0, 0, f_frame_size[10], f_frame_size[11]);
- cv::Mat frame_0, frame_1, frame_2, frame_3, frame_4, frame_5;
- int is_save = false;
- cv::Mat fCmeraMeger(cv::Mat outMat[])
- {
- for (uint32_t i = 0; i < camera_num; i++)
- {
- switch (i)
- {
- case 0:
- cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
- frame_0 = outMat[i];
- break;
- case 1:
- cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
- frame_1 = outMat[i];
- resize(frame_1, frame_1, cv::Size(720, 320));
- break;
- case 2:
- cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
- frame_2 = outMat[i];
- cv::rotate(frame_2, frame_2, cv::ROTATE_90_CLOCKWISE);
- break;
- case 3:
- cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
- frame_3 = outMat[i];
- cv::rotate(frame_3, frame_3, cv::ROTATE_90_COUNTERCLOCKWISE);
- break;
- case 4:
- cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
- frame_4 = outMat[i];
- break;
- case 5:
- cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
- frame_5 = outMat[i];
- break;
- default:
- break;
- }
- }
- frame_0.copyTo(meger(f_rectCenter));
- frame_1.copyTo(meger(f_rectCenterUp));
- frame_2.copyTo(meger(f_rectLeftDown));
- frame_3.copyTo(meger(f_rectRightDown));
- frame_4.copyTo(meger(f_rectLeftUp));
- frame_5.copyTo(meger(f_rectRightUp));
- return meger;
- }
- int r_frame_size[] = {1280, 640, 1280, 640, 640, 360, 640, 360, 640, 320, 640, 320};
- cv::Rect r_rectCenterUp = cv::Rect(650, 0, r_frame_size[0] - 560, r_frame_size[1] - 320);
- cv::Rect r_rectCenter = cv::Rect(370, 330, r_frame_size[2], r_frame_size[3]);
- cv::Rect r_rectRightDown = cv::Rect(1660, 330, r_frame_size[5], r_frame_size[4]);
- cv::Rect r_rectLeftDown = cv::Rect(0, 330, r_frame_size[7], r_frame_size[6]);
- cv::Rect r_rectRightUp = cv::Rect(1380, 0, r_frame_size[8], r_frame_size[9]);
- cv::Rect r_rectLeftUp = cv::Rect(0, 0, r_frame_size[10], r_frame_size[11]);
- cv::Mat r_frame_0, r_frame_1, r_frame_2, r_frame_3, r_frame_4, r_frame_5;
- cv::Mat rCmeraMeger(cv::Mat outMat[])
- {
- for (uint32_t i = 0; i < camera_num; i++)
- {
- switch (i)
- {
- case 0:
- cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
- r_frame_0 = outMat[i];
- resize(r_frame_0, r_frame_0, cv::Size(720, 320));
- break;
- case 1:
- cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
- r_frame_1 = outMat[i];
- break;
- case 2:
- cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
- r_frame_2 = outMat[i];
- cv::rotate(r_frame_2, r_frame_2, cv::ROTATE_90_CLOCKWISE);
- break;
- case 3:
- cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
- r_frame_3 = outMat[i];
- cv::rotate(r_frame_3, r_frame_3, cv::ROTATE_90_COUNTERCLOCKWISE);
- break;
- case 4:
- cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
- r_frame_4 = outMat[i];
- break;
- case 5:
- cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
- r_frame_5 = outMat[i];
- break;
- default:
- break;
- }
- }
- r_frame_0.copyTo(meger(r_rectCenterUp));
- r_frame_1.copyTo(meger(r_rectCenter));
- r_frame_2.copyTo(meger(r_rectRightDown));
- r_frame_3.copyTo(meger(r_rectLeftDown));
- r_frame_4.copyTo(meger(r_rectRightUp));
- r_frame_5.copyTo(meger(r_rectLeftUp));
- return meger;
- }
- // 线程
- std::ifstream inFile;
- char c;
- bool current_model = true;
- void thread_1()
- {
- while (1)
- {
- sleep(0.1);
- try
- {
- std::ifstream inFile("/tmp/vedioFront", std::ios::in | std::ios::binary);
- if (!inFile)
- {
- }
- else
- {
- inFile.get(c);
- inFile.close();
- current_model = c - '0';
- }
- }
- catch (const std::exception &e)
- {
- std::cerr << e.what() << '\n';
- }
- }
- }
- void processFrames(cv::Mat& mege_frame, cv::Mat* outMat, size_t framesize, int& output) {
- while (!exitRequested) {
- if (MODEL) {
- mege_frame = fCmeraMeger(outMat);
- } else {
- mege_frame = rCmeraMeger(outMat);
- }
- cv::putText(mege_frame, GetCurrentTimeStamp(0), cv::Point(870, 25), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(255, 255, 255), 2);
-
- size_t written = write(output, mege_frame.data, framesize);
- if (written < 0) {
- std::cerr << "ERROR: could not write to output device!\n";
- close(output);
- exitRequested = true; // Set exit flag to true to terminate main loop
- }
- std::this_thread::sleep_for(std::chrono::milliseconds(2));
- }
- }
- int main(int argc, char *argv[])
- {
- // open output device
- int output = open(VIDEO_OUT, O_RDWR);
- if (output < 0)
- {
- std::cerr << "ERROR: could not open output device!\n"
- << strerror(errno);
- return -2;
- }
- struct v4l2_format vid_format;
- memset(&vid_format, 0, sizeof(vid_format));
- vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
- if (ioctl(output, VIDIOC_G_FMT, &vid_format) < 0)
- {
- std::cerr << "ERROR: unable to get video format!\n"
- << strerror(errno);
- return -1;
- }
- size_t framesize = meger_width * meger_height * 3;
- vid_format.fmt.pix.width = meger_width;
- vid_format.fmt.pix.height = meger_height;
- vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
- vid_format.fmt.pix.sizeimage = framesize;
- vid_format.fmt.pix.field = V4L2_FIELD_NONE;
- if (ioctl(output, VIDIOC_S_FMT, &vid_format) < 0)
- {
- std::cerr << "ERROR: unable to set video format!\n"
- << strerror(errno);
- return -1;
- }
- camera_context_t ctx[8] = {};
- stCameraCfgSend.async_camera_num = 0;
- stCameraCfgSend.async_freq = 0;
- stCameraCfgSend.async_camera_bit_draw = 0;
- stCameraCfgSend.sync_camera_num = 8;
- stCameraCfgSend.sync_freq = 30;
- stCameraCfgSend.sync_camera_bit_draw = 0xff;
- char dev_node_tmp = dev_node[10];
- for (int i = 0; i < camera_num; i++)
- {
- dev_node[10] = dev_node_tmp + i;
- ctx[i].dev_node = dev_node;
- ctx[i].camera_fmt_str = camera_fmt_str;
- ctx[i].output_fmt_str = output_fmt_str;
- ctx[i].cam_w = cam_w;
- ctx[i].cam_h = cam_h;
- ctx[i].out_w = f_frame_size[i * 2];
- ctx[i].out_h = f_frame_size[i * 2 + 1];
- }
- miivii::MvGmslCamera mvcam(ctx, camera_num, stCameraCfgSend);
- cv::Mat outMat[camera_num];
- uint8_t *outbuf[camera_num];
- cv::Mat imgbuf[camera_num];
- signal(SIGINT, &handler);
- bool quit = false;
- uint64_t timestamp;
- meger = cv::imread("./base.jpg");
- cv::cvtColor(meger, meger, cv::COLOR_BGRA2RGB);
- resize(meger, meger, cv::Size(meger_width, meger_height));
- uint8_t camera_no = dev_node[10] - 0x30;
- cv::Mat mege_frame;
- int count = 0;
- bool is_open_file = false;
- // 读取文件
- std::thread file_thread(thread_1);
- file_thread.detach();
- std::vector<cv::Mat> outMatVector;
- std::thread processingThread(processFrames, std::ref(mege_frame), outMatVector.data(), framesize, std::ref(output));
- while (!quit)
- {
- if (mvcam.GetImageCvMat(outMat, timestamp, camera_no))
- {
- for (size_t i = 0; i < camera_num; ++i) {
- outMatVector.clear();
- outMatVector.push_back(outMat[i]);
- }
- if (current_model != MODEL)
- {
- meger = cv::imread("./base.jpg");
- cv::cvtColor(meger, meger, cv::COLOR_BGRA2RGB);
- resize(meger, meger, cv::Size(meger_width, meger_height));
- MODEL = current_model;
- }
-
- }
- else
- {
- std::cerr << "Can't get image form camera." << std::endl;
- }
- sleep(0.002);
- }
- processingThread.join();
- close(output);
- return 0;
- }
|