main_0626.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #include <opencv2/opencv.hpp>
  2. #include <opencv2/core/core.hpp>
  3. #include <opencv2/highgui/highgui.hpp>
  4. #include <string>
  5. #include <iostream>
  6. #include <zconf.h>
  7. #include <csignal>
  8. #include <thread>
  9. #include "MvGmslCamera.h"
  10. #include <fstream>
  11. #include <chrono>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include <sys/ioctl.h>
  15. #include <linux/videodev2.h>
  16. #include <thread>
  17. #define VIDEO_OUT "/dev/video8"
  18. using namespace std::chrono;
  19. using std::string;
  20. sig_atomic_t exitRequested = 0;
  21. uint camera_num = 6;
  22. struct sync_out_a_cfg_client_t stCameraCfgSend = {};
  23. char dev_node[32] = "/dev/video0";
  24. std::string camera_fmt_str = "UYVY";
  25. std::string output_fmt_str = "BGRA32";
  26. uint cam_w = 1920;
  27. uint cam_h = 1080;
  28. bool MODEL = true;
  29. uint64_t timestampbefore[8] = {0};
  30. uint64_t LinuxGetFrameTimeBefore[8] = {0};
  31. void handler(int)
  32. {
  33. std::cout << "will exit..." << std::endl;
  34. exitRequested = true;
  35. }
  36. std::string GetCurrentTimeStamp(int time_stamp_type = 0)
  37. {
  38. std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
  39. std::time_t now_time_t = std::chrono::system_clock::to_time_t(now);
  40. std::tm *now_tm = std::localtime(&now_time_t);
  41. char buffer[128];
  42. strftime(buffer, sizeof(buffer), "%F %T", now_tm);
  43. std::ostringstream ss;
  44. ss.fill('0');
  45. std::chrono::milliseconds ms;
  46. std::chrono::microseconds cs;
  47. std::chrono::nanoseconds ns;
  48. switch (time_stamp_type)
  49. {
  50. case 0:
  51. ss << buffer;
  52. break;
  53. case 1:
  54. ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
  55. ss << buffer << ":" << ms.count();
  56. break;
  57. case 2:
  58. ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
  59. cs = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()) % 1000000;
  60. ss << buffer << ":" << ms.count() << ":" << cs.count() % 1000;
  61. break;
  62. case 3:
  63. ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
  64. cs = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()) % 1000000;
  65. ns = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()) % 1000000000;
  66. ss << buffer << ":" << ms.count() << ":" << cs.count() % 1000 << ":" << ns.count() % 1000;
  67. break;
  68. default:
  69. ss << buffer;
  70. break;
  71. }
  72. return ss.str();
  73. }
  74. cv::Mat meger;
  75. uint meger_width = 2020;
  76. uint meger_height = 970;
  77. int f_frame_size[] = {1280, 640, 1280, 640, 640, 360, 640, 360, 640, 320, 640, 320};
  78. cv::Rect f_rectCenter = cv::Rect(370, 330, f_frame_size[0], f_frame_size[1]);
  79. cv::Rect f_rectCenterUp = cv::Rect(650, 0, f_frame_size[2] - 560, f_frame_size[3] - 320);
  80. cv::Rect f_rectLeftDown = cv::Rect(0, 330, f_frame_size[5], f_frame_size[4]);
  81. cv::Rect f_rectRightDown = cv::Rect(1660, 330, f_frame_size[7], f_frame_size[6]);
  82. cv::Rect f_rectLeftUp = cv::Rect(1380, 0, f_frame_size[8], f_frame_size[9]);
  83. cv::Rect f_rectRightUp = cv::Rect(0, 0, f_frame_size[10], f_frame_size[11]);
  84. cv::Mat frame_0, frame_1, frame_2, frame_3, frame_4, frame_5;
  85. int is_save = false;
  86. cv::Mat fCmeraMeger(cv::Mat outMat[])
  87. {
  88. for (uint32_t i = 0; i < camera_num; i++)
  89. {
  90. switch (i)
  91. {
  92. case 0:
  93. cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
  94. frame_0 = outMat[i];
  95. break;
  96. case 1:
  97. cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
  98. frame_1 = outMat[i];
  99. resize(frame_1, frame_1, cv::Size(720, 320));
  100. break;
  101. case 2:
  102. cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
  103. frame_2 = outMat[i];
  104. cv::rotate(frame_2, frame_2, cv::ROTATE_90_CLOCKWISE);
  105. break;
  106. case 3:
  107. cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
  108. frame_3 = outMat[i];
  109. cv::rotate(frame_3, frame_3, cv::ROTATE_90_COUNTERCLOCKWISE);
  110. break;
  111. case 4:
  112. cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
  113. frame_4 = outMat[i];
  114. break;
  115. case 5:
  116. cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
  117. frame_5 = outMat[i];
  118. break;
  119. default:
  120. break;
  121. }
  122. }
  123. frame_0.copyTo(meger(f_rectCenter));
  124. frame_1.copyTo(meger(f_rectCenterUp));
  125. frame_2.copyTo(meger(f_rectLeftDown));
  126. frame_3.copyTo(meger(f_rectRightDown));
  127. frame_4.copyTo(meger(f_rectLeftUp));
  128. frame_5.copyTo(meger(f_rectRightUp));
  129. return meger;
  130. }
  131. int r_frame_size[] = {1280, 640, 1280, 640, 640, 360, 640, 360, 640, 320, 640, 320};
  132. cv::Rect r_rectCenterUp = cv::Rect(650, 0, r_frame_size[0] - 560, r_frame_size[1] - 320);
  133. cv::Rect r_rectCenter = cv::Rect(370, 330, r_frame_size[2], r_frame_size[3]);
  134. cv::Rect r_rectRightDown = cv::Rect(1660, 330, r_frame_size[5], r_frame_size[4]);
  135. cv::Rect r_rectLeftDown = cv::Rect(0, 330, r_frame_size[7], r_frame_size[6]);
  136. cv::Rect r_rectRightUp = cv::Rect(1380, 0, r_frame_size[8], r_frame_size[9]);
  137. cv::Rect r_rectLeftUp = cv::Rect(0, 0, r_frame_size[10], r_frame_size[11]);
  138. cv::Mat r_frame_0, r_frame_1, r_frame_2, r_frame_3, r_frame_4, r_frame_5;
  139. cv::Mat rCmeraMeger(cv::Mat outMat[])
  140. {
  141. for (uint32_t i = 0; i < camera_num; i++)
  142. {
  143. switch (i)
  144. {
  145. case 0:
  146. cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
  147. r_frame_0 = outMat[i];
  148. resize(r_frame_0, r_frame_0, cv::Size(720, 320));
  149. break;
  150. case 1:
  151. cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
  152. r_frame_1 = outMat[i];
  153. break;
  154. case 2:
  155. cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
  156. r_frame_2 = outMat[i];
  157. cv::rotate(r_frame_2, r_frame_2, cv::ROTATE_90_CLOCKWISE);
  158. break;
  159. case 3:
  160. cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
  161. r_frame_3 = outMat[i];
  162. cv::rotate(r_frame_3, r_frame_3, cv::ROTATE_90_COUNTERCLOCKWISE);
  163. break;
  164. case 4:
  165. cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
  166. r_frame_4 = outMat[i];
  167. break;
  168. case 5:
  169. cv::cvtColor(outMat[i], outMat[i], cv::COLOR_BGRA2RGB);
  170. r_frame_5 = outMat[i];
  171. break;
  172. default:
  173. break;
  174. }
  175. }
  176. r_frame_0.copyTo(meger(r_rectCenterUp));
  177. r_frame_1.copyTo(meger(r_rectCenter));
  178. r_frame_2.copyTo(meger(r_rectRightDown));
  179. r_frame_3.copyTo(meger(r_rectLeftDown));
  180. r_frame_4.copyTo(meger(r_rectRightUp));
  181. r_frame_5.copyTo(meger(r_rectLeftUp));
  182. return meger;
  183. }
  184. // 线程
  185. std::ifstream inFile;
  186. char c;
  187. bool current_model = true;
  188. void thread_1()
  189. {
  190. while (1)
  191. {
  192. sleep(0.1);
  193. try
  194. {
  195. std::ifstream inFile("/tmp/vedioFront", std::ios::in | std::ios::binary);
  196. if (!inFile)
  197. {
  198. }
  199. else
  200. {
  201. inFile.get(c);
  202. inFile.close();
  203. current_model = c - '0';
  204. }
  205. }
  206. catch (const std::exception &e)
  207. {
  208. std::cerr << e.what() << '\n';
  209. }
  210. }
  211. }
  212. void processFrames(cv::Mat& mege_frame, cv::Mat* outMat, size_t framesize, int& output) {
  213. while (!exitRequested) {
  214. if (MODEL) {
  215. mege_frame = fCmeraMeger(outMat);
  216. } else {
  217. mege_frame = rCmeraMeger(outMat);
  218. }
  219. cv::putText(mege_frame, GetCurrentTimeStamp(0), cv::Point(870, 25), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(255, 255, 255), 2);
  220. size_t written = write(output, mege_frame.data, framesize);
  221. if (written < 0) {
  222. std::cerr << "ERROR: could not write to output device!\n";
  223. close(output);
  224. exitRequested = true; // Set exit flag to true to terminate main loop
  225. }
  226. std::this_thread::sleep_for(std::chrono::milliseconds(2));
  227. }
  228. }
  229. int main(int argc, char *argv[])
  230. {
  231. // open output device
  232. int output = open(VIDEO_OUT, O_RDWR);
  233. if (output < 0)
  234. {
  235. std::cerr << "ERROR: could not open output device!\n"
  236. << strerror(errno);
  237. return -2;
  238. }
  239. struct v4l2_format vid_format;
  240. memset(&vid_format, 0, sizeof(vid_format));
  241. vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  242. if (ioctl(output, VIDIOC_G_FMT, &vid_format) < 0)
  243. {
  244. std::cerr << "ERROR: unable to get video format!\n"
  245. << strerror(errno);
  246. return -1;
  247. }
  248. size_t framesize = meger_width * meger_height * 3;
  249. vid_format.fmt.pix.width = meger_width;
  250. vid_format.fmt.pix.height = meger_height;
  251. vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
  252. vid_format.fmt.pix.sizeimage = framesize;
  253. vid_format.fmt.pix.field = V4L2_FIELD_NONE;
  254. if (ioctl(output, VIDIOC_S_FMT, &vid_format) < 0)
  255. {
  256. std::cerr << "ERROR: unable to set video format!\n"
  257. << strerror(errno);
  258. return -1;
  259. }
  260. camera_context_t ctx[8] = {};
  261. stCameraCfgSend.async_camera_num = 0;
  262. stCameraCfgSend.async_freq = 0;
  263. stCameraCfgSend.async_camera_bit_draw = 0;
  264. stCameraCfgSend.sync_camera_num = 8;
  265. stCameraCfgSend.sync_freq = 30;
  266. stCameraCfgSend.sync_camera_bit_draw = 0xff;
  267. char dev_node_tmp = dev_node[10];
  268. for (int i = 0; i < camera_num; i++)
  269. {
  270. dev_node[10] = dev_node_tmp + i;
  271. ctx[i].dev_node = dev_node;
  272. ctx[i].camera_fmt_str = camera_fmt_str;
  273. ctx[i].output_fmt_str = output_fmt_str;
  274. ctx[i].cam_w = cam_w;
  275. ctx[i].cam_h = cam_h;
  276. ctx[i].out_w = f_frame_size[i * 2];
  277. ctx[i].out_h = f_frame_size[i * 2 + 1];
  278. }
  279. miivii::MvGmslCamera mvcam(ctx, camera_num, stCameraCfgSend);
  280. cv::Mat outMat[camera_num];
  281. uint8_t *outbuf[camera_num];
  282. cv::Mat imgbuf[camera_num];
  283. signal(SIGINT, &handler);
  284. bool quit = false;
  285. uint64_t timestamp;
  286. meger = cv::imread("./base.jpg");
  287. cv::cvtColor(meger, meger, cv::COLOR_BGRA2RGB);
  288. resize(meger, meger, cv::Size(meger_width, meger_height));
  289. uint8_t camera_no = dev_node[10] - 0x30;
  290. cv::Mat mege_frame;
  291. int count = 0;
  292. bool is_open_file = false;
  293. // 读取文件
  294. std::thread file_thread(thread_1);
  295. file_thread.detach();
  296. std::vector<cv::Mat> outMatVector;
  297. std::thread processingThread(processFrames, std::ref(mege_frame), outMatVector.data(), framesize, std::ref(output));
  298. while (!quit)
  299. {
  300. if (mvcam.GetImageCvMat(outMat, timestamp, camera_no))
  301. {
  302. for (size_t i = 0; i < camera_num; ++i) {
  303. outMatVector.clear();
  304. outMatVector.push_back(outMat[i]);
  305. }
  306. if (current_model != MODEL)
  307. {
  308. meger = cv::imread("./base.jpg");
  309. cv::cvtColor(meger, meger, cv::COLOR_BGRA2RGB);
  310. resize(meger, meger, cv::Size(meger_width, meger_height));
  311. MODEL = current_model;
  312. }
  313. }
  314. else
  315. {
  316. std::cerr << "Can't get image form camera." << std::endl;
  317. }
  318. sleep(0.002);
  319. }
  320. processingThread.join();
  321. close(output);
  322. return 0;
  323. }