opencv(오픈cv) 질문 코딩초보에요(C언어)
-
게시물 수정 , 삭제는 로그인 필요
opencv(오픈cv) 질문 코딩초보에요(C언어)
눈 깜빡임을 인식하고, 깜빡임 횟수와 시간이 2.5초 이상이 되면 알림을 울리게 하는 프로그램을 작성하려면 몇 가지 단계를 거쳐야 합니다. 여기서는 눈 깜빡임을 인식하는 기본적인 흐름을 설명하겠습니다.
1. **눈 영역 인식**:
눈 영역을 인식하기 위해 Dlib의 얼굴 랜드마크 모델을 사용합니다. 36번부터 41번까지의 점이 눈의 위치를 나타냅니다.
2. **눈 깜빡임 감지**:
눈이 깜빡임을 감지하기 위해 눈의 상태를 지속적으로 모니터링합니다. 이를 위해 눈이 열려있는 상태와 감긴 상태를 구분하는 함수가 필요합니다.
3. **깜빡임 횟수 및 시간 계산**:
눈이 감긴 상태를 일정 시간 이상 지속하면 이를 깜빡임으로 간주하고, 깜빡임 시간을 측정합니다. 2.5초 이상 감긴 상태가 유지되면 알림과 알람을 울리도록 합니다.
아래는 위 과정을 구현한 코드 예제입니다:
```cpp
#include <dlib/opencv.h>
#include <opencv2/highgui/highgui.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <chrono>
using namespace dlib;
using namespace std;
double eye_aspect_ratio(const full_object_detection& shape, const int* eye_points) {
double A = length(shape.part(eye_points[1]) - shape.part(eye_points[5]));
double B = length(shape.part(eye_points[2]) - shape.part(eye_points[4]));
double C = length(shape.part(eye_points[0]) - shape.part(eye_points[3]));
return (A + B) / (2.0 * C);
}
int main()
{
try
{
cv::VideoCapture cap(0);
if (!cap.isOpened())
{
cerr << "Unable to connect to camera" << endl;
return 1;
}
image_window win;
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor pose_model;
deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;
const int LEFT_EYE[] = {36, 37, 38, 39, 40, 41};
const int RIGHT_EYE[] = {42, 43, 44, 45, 46, 47};
const double EYE_AR_THRESH = 0.25;
const int EYE_AR_CONSEC_FRAMES = 3;
int blink_count = 0;
int frame_count = 0;
auto start_time = chrono::high_resolution_clock::now();
while (!win.is_closed())
{
cv::Mat temp;
cap >> temp;
cv_image<bgr_pixel> cimg(temp);
std::vector<rectangle> faces = detector(cimg);
std::vector<full_object_detection> shapes;
for (unsigned long i = 0; i < faces.size(); ++i)
shapes.push_back(pose_model(cimg, faces[i]));
win.clear_overlay();
win.set_image(cimg);
win.add_overlay(render_face_detections(shapes));
for (auto& shape : shapes)
{
double left_eye_ratio = eye_aspect_ratio(shape, LEFT_EYE);
double right_eye_ratio = eye_aspect_ratio(shape, RIGHT_EYE);
double ear = (left_eye_ratio + right_eye_ratio) / 2.0;
if (ear < EYE_AR_THRESH)
{
frame_count++;
if (frame_count >= EYE_AR_CONSEC_FRAMES)
{
blink_count++;
frame_count = 0;
auto current_time = chrono::high_resolution_clock::now();
chrono::duration<double> elapsed = current_time - start_time;
if (elapsed.count() > 2.5)
{
cout << "Blink detected for more than 2.5 seconds!" << endl;
// 알람 코드 추가 (비프음 등)
}
}
}
else
{
frame_count = 0;
start_time = chrono::high_resolution_clock::now();
}
}
}
}
catch (serialization_error& e)
{
cout << "You need dlib's default face landmarking model file to run this example." << endl;
cout << "You can get it from the following URL: " << endl;
cout << " http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
cout << endl << e.what() << endl;
}
catch (exception& e)
{
cout << e.what() << endl;
}
return 0;
}
```
### 설명:
1. **눈 비율 계산**:
`eye_aspect_ratio` 함수를 사용하여 눈의 비율을 계산합니다. 특정 점 사이의 거리 비율로 눈이 감긴 상태인지 판단합니다.
2. **깜빡임 감지 및 시간 측정**:
`EYE_AR_THRESH`는 눈이 감긴 상태를 판단하는 임계값입니다. 눈의 비율이 이 값보다 작으면 눈이 감긴 상태로 간주합니다. `EYE_AR_CONSEC_FRAMES`는 연속된 프레임 수를 통해 깜빡임을 감지합니다.
3. **시간 체크 및 알람**:
2.5초 이상 눈이 감긴 상태가 유지되면 콘솔에 메시지를 출력하고, 실제 알람을 추가할 수 있습니다.
이 코드가 눈 깜빡임 감지의 기본적인 구조를 제공하니, 이를 바탕으로 더 복잡한 기능을 추가하거나 조정할 수 있습니다.
... int main() { try { cv::VideoCapture cap(0); if (!cap.isOpened()) { cerr << "Unable... ㅜㅜ 내공팍팍드립니다 감사합니다 꼭 파이썬말고 c언어로...
... 효율적인 C언어 코딩이 될 거 같아요~! 저는 게임프로그래밍쪽 C언어를 공부하고... 아래의 오픈채팅 링크 첨부해드릴게요~! https://open.kakao.com/o/shzBGWPe
... 안녕하세요 코딩의 필요성과 오픈소스 질문하셔서 답변남겨드립니다. 최근 코딩... 센텀초보입문 센텀C언어 , 센텀C++ 센텀JAVA 센텀자바 센텀자바스크립트배우기...
... 현재 파이썬으로 코딩을 해보고있는데 찾아보면 Visual studio(c 언어)로 가능하다 하시는분도 있어서 질문 드려요 제가 전에 씨언어쪽은 공부를 해봐서 따로 불편함 없이...
... 모르겠네요ㅠㅠ C언어는 독학이 가능한지 IT,코딩은... 뭘공부해야하는지,앞에 질문한거 아시는분은 상세한... 상담신청 http://naver.me/53W9f1WM 오픈카톡 http://open.kakao.com/o...
... 파이썬과 오픈cv를 통해 만들고싶습니다. 만들 수 있을까요/ 지문인식센서를 이용해서요 초보 프로그래머 상담사... 궁금하신점이 있으시다면 추가질문을 부탁드리겠습니다.
c언어를 배운지 6개월도 안된 풋내기입니다.... 몰라 질문을 드리네요 ^^; 도와주십시요!... out(); } // 오픈 void open() { FILE *fp; int i; if((fp = fopen...
... CSS, C언어 등 이런걸 가지고 공부하라고 하는데 책이나 뭔가가... 혹시 문의사항 있으시면 오픈카톡으로 문의주시면 실시간 1:1 상담이 가능합니다^^ https://open.kakao.com/o/sugUPOcb
... 기본적인 질문 몇 가지만 할게요 1. 코딩의 종류? 가... 초보가 하기 가장 쉬운 것 4. 독학? 강의? 학원? 5. 코딩... https://open.kakao.com/o/svu6JQZb< 부담갖지말고 대화...
... 또 c++에서 쓰이는 inline이나 auto형들을 c언어 환경에서 연동되나요? return Point(rc.x + rc.width / 2, rc.y + rc.height / 2); 이렇게 하거나...