-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera_on.cpp
50 lines (39 loc) · 1.43 KB
/
camera_on.cpp
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
#include <opencv2/opencv.hpp>
//include<opencv.hpp>
#include <cstdlib>
int cam_on() {
cv::VideoCapture capture(0); // 0 represents the default camera (you can change this index to use a different camera)
if (!capture.isOpened()) {
std::cout << "Failed to open the camera!" << std::endl;
return -1;
}
cv::Mat frame;
while (true) {
capture >> frame; // Capture a frame from the camera
if (frame.empty()) {
std::cout << "Failed to capture an image!" << std::endl;
break;
}
cv::imshow("Camera", frame);
char key = cv::waitKey(1);
if (key == 'c' || key == 'C') {
// Save the captured frame to an image file
cv::imwrite("sample.jpg", frame);
std::cout << "Image captured!" << std::endl;
}
else if (key == 27) { // '27' represents the escape key (ESC)
break; // Exit the loop if the user presses the ESC key
}
}
// Release the camera
capture.release();
cv::destroyAllWindows();
const char* command = "python rep_py.py sample.jpg"; // For Python 3
// const char* command = "python myscript.py"; // For Python 2
// Use the system() function to run the Python script
int returnCode = system(command);
// Release the camera
//capture.release();
//cv::destroyAllWindows();
return 0;
}