-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.ixx
More file actions
72 lines (51 loc) · 1.87 KB
/
Camera.ixx
File metadata and controls
72 lines (51 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
module;
#include <opencv2/opencv.hpp>
#include <thread>
#include <iostream>
export module Camera;
import VideoFrame;
[[noreturn]] void run_camera(cv::VideoCapture &&camera, VideoFrame::VideoFramesQueue &queue) {
cv::Mat frame;
std::string windowName = "Camera";
while (true) {
camera >> frame;
if (frame.empty()) {
std::cout << "Failed to capture frame" << std::endl;
continue;
}
cv::Mat yuvFrame{};
cv::cvtColor(frame, yuvFrame, cv::COLOR_BGR2YUV);
cv::imshow(windowName, yuvFrame);
cv::waitKey(25);
std::vector<cv::Mat> channels;
cv::split(yuvFrame, channels);
VideoFrame::Frame videoFrame{};
videoFrame.strideY = channels[0].step;
videoFrame.strideU = channels[1].step;
videoFrame.strideV = channels[2].step;
videoFrame.dataY.assign(channels[0].datastart, channels[0].dataend);
videoFrame.dataU.assign(channels[1].datastart, channels[1].dataend);
videoFrame.dataV.assign(channels[2].datastart, channels[2].dataend);
queue.push(videoFrame);
}
}
export namespace Camera {
struct Info {
int width;
int height;
};
std::optional<Info> run(VideoFrame::VideoFramesQueue &queue) {
cv::VideoCapture camera{};
auto result = camera.open(0, cv::CAP_ANY);
if (!result) {
std::cout << "Failed to open camera" << std::endl;
return std::nullopt;
}
auto width = camera.get(cv::CAP_PROP_FRAME_WIDTH);
auto height = camera.get(cv::CAP_PROP_FRAME_HEIGHT);
std::cout << "Camera resolution: " << width << "x" << height << std::endl;
auto cameraThread = std::thread{run_camera, std::move(camera), std::ref(queue)};
cameraThread.detach();
return Info{static_cast<int>(width), static_cast<int>(height)};
}
}