main_copy.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. int main(int argc, char *argv[])
  213. {
  214. // open output device
  215. int output = open(VIDEO_OUT, O_RDWR);
  216. if (output < 0)
  217. {
  218. std::cerr << "ERROR: could not open output device!\n"
  219. << strerror(errno);
  220. return -2;
  221. }
  222. struct v4l2_format vid_format;
  223. memset(&vid_format, 0, sizeof(vid_format));
  224. vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  225. if (ioctl(output, VIDIOC_G_FMT, &vid_format) < 0)
  226. {
  227. std::cerr << "ERROR: unable to get video format!\n"
  228. << strerror(errno);
  229. return -1;
  230. }
  231. size_t framesize = meger_width * meger_height * 3;
  232. vid_format.fmt.pix.width = meger_width;
  233. vid_format.fmt.pix.height = meger_height;
  234. vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
  235. vid_format.fmt.pix.sizeimage = framesize;
  236. vid_format.fmt.pix.field = V4L2_FIELD_NONE;
  237. if (ioctl(output, VIDIOC_S_FMT, &vid_format) < 0)
  238. {
  239. std::cerr << "ERROR: unable to set video format!\n"
  240. << strerror(errno);
  241. return -1;
  242. }
  243. camera_context_t ctx[8] = {};
  244. stCameraCfgSend.async_camera_num = 0;
  245. stCameraCfgSend.async_freq = 0;
  246. stCameraCfgSend.async_camera_bit_draw = 0;
  247. stCameraCfgSend.sync_camera_num = 8;
  248. stCameraCfgSend.sync_freq = 30;
  249. stCameraCfgSend.sync_camera_bit_draw = 0xff;
  250. char dev_node_tmp = dev_node[10];
  251. for (int i = 0; i < camera_num; i++)
  252. {
  253. dev_node[10] = dev_node_tmp + i;
  254. ctx[i].dev_node = dev_node;
  255. ctx[i].camera_fmt_str = camera_fmt_str;
  256. ctx[i].output_fmt_str = output_fmt_str;
  257. ctx[i].cam_w = cam_w;
  258. ctx[i].cam_h = cam_h;
  259. ctx[i].out_w = f_frame_size[i * 2];
  260. ctx[i].out_h = f_frame_size[i * 2 + 1];
  261. }
  262. miivii::MvGmslCamera mvcam(ctx, camera_num, stCameraCfgSend);
  263. cv::Mat outMat[camera_num];
  264. uint8_t *outbuf[camera_num];
  265. cv::Mat imgbuf[camera_num];
  266. signal(SIGINT, &handler);
  267. bool quit = false;
  268. uint64_t timestamp;
  269. meger = cv::imread("./base.jpg");
  270. cv::cvtColor(meger, meger, cv::COLOR_BGRA2RGB);
  271. resize(meger, meger, cv::Size(meger_width, meger_height));
  272. uint8_t camera_no = dev_node[10] - 0x30;
  273. cv::Mat mege_frame;
  274. int count = 0;
  275. bool is_open_file = false;
  276. // 读取文件
  277. std::thread file_thread(thread_1);
  278. file_thread.detach();
  279. while (!quit)
  280. {
  281. if (mvcam.GetImageCvMat(outMat, timestamp, camera_no))
  282. {
  283. if (current_model != MODEL)
  284. {
  285. meger = cv::imread("./base.jpg");
  286. cv::cvtColor(meger, meger, cv::COLOR_BGRA2RGB);
  287. resize(meger, meger, cv::Size(meger_width, meger_height));
  288. MODEL = current_model;
  289. }
  290. if (MODEL)
  291. {
  292. mege_frame = fCmeraMeger(outMat);
  293. }
  294. else
  295. {
  296. mege_frame = rCmeraMeger(outMat);
  297. }
  298. cv::putText(mege_frame, GetCurrentTimeStamp(0), cv::Point(870, 25), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(255, 255, 255), 2);
  299. size_t written = write(output, mege_frame.data, framesize);
  300. if (written < 0)
  301. {
  302. std::cerr << "ERROR: could not write to output device!\n";
  303. close(output);
  304. break;
  305. }
  306. }
  307. else
  308. {
  309. std::cerr << "Can't get image form camera." << std::endl;
  310. }
  311. sleep(0.002);
  312. }
  313. close(output);
  314. return 0;
  315. }