vidcapdlg.cpp
来自「一个外国人写的人脸检测程序」· C++ 代码 · 共 443 行 · 第 1/2 页
CPP
443 行
//Close Timer
KillTimer(m_nTimer);
m_nTimer = 0;
m_RunButton.SetWindowTextW(L"Run");
m_VideoFormat.SetWindowTextW(L"Video output");
//Close Capture
vcStopCaptureVideo();
}
}
void CVidCapDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
SYSTEMTIME SystemTime;
GetLocalTime(&SystemTime);
TRACE(L" %d:%d:%d\n", SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond);
unsigned char* pData = sgGrabData();
if (pData != 0)
DrawData(pData);
CDialog::OnTimer(nIDEvent);
}
void CVidCapDlg::OnWindowPosChanged(WINDOWPOS* lpwndpos)
{
CDialog::OnWindowPosChanged(lpwndpos);
// TODO: Add your message handler code here
vcChangePreviewState(!IsIconic());
}
void CVidCapDlg::DrawData(unsigned char *pData)
{
Gdiplus::Bitmap* pBitmap = sgGetBitmap();
if (pBitmap == 0)
return;
if (m_TakeSnapshot == true) {
m_TakeSnapshot = false;
sndPlaySound(L"snap.wav", SND_ASYNC);
if (pBmpEncoder != GUID_NULL) {
wchar_t FileName[_MAX_PATH] = L"";
for (unsigned int i = 1; i < 0xFFFFFFFF; i++) {
swprintf_s(FileName, _MAX_PATH, L"Snapshot %04d.jpg", i);
FILE* fp = _wfopen(FileName, L"rb");
if (fp == 0) {
pBitmap->Save(FileName, &pBmpEncoder);
break;
}
else
fclose(fp);
}
}
}
RECT r;
m_CapImgStatic.GetClientRect(&r);
Gdiplus::Graphics g(m_CapImgStatic.GetDC()->m_hDC);
//detect faces
tic();
int nFaces = cvDetect(pData);
m_Ms += (unsigned int)toc();
m_FramesProcessed++;
TRACE(L" nFaces: %d\n", nFaces);
if (nFaces < 0)
nFaces = 0;
Gdiplus::Graphics mem_g(pBitmap);
Gdiplus::Pen redPen(Gdiplus::Color(255, 255, 0, 0), 2.5f);
for (int i = 0; i < nFaces; i++) {
RECT rect;
cvFaceRect(i, rect);
mem_g.DrawRectangle(&redPen, rect.left, rect.top, rect.right, rect.bottom);
TRACE(L" face coords: %d %d %d %d", rect.left, rect.top, rect.right, rect.bottom);
}
if (nFaces > 0) {
SetCapturedImageData(m_CapturedFace, *cvGetFace(0));
mem_g.DrawImage(m_CapturedFace,
Gdiplus::Rect(10, 10, (int)((double)m_Width / m_FaceRectRatio), (int)((double)m_Width / m_FaceRectRatio)),
0, 0, m_CapturedFace->GetWidth(), m_CapturedFace->GetHeight(),
Gdiplus::UnitPixel);
}
g.DrawImage(pBitmap, Gdiplus::Rect(0, 0, r.right, r.bottom));
if (m_FramesProcessed >= m_TotalFrames) {
m_MsPerFrame = m_Ms / m_TotalFrames;
m_FpsRate = 1000.0 / double(m_MsPerFrame);
m_FramesProcessed = 0;
m_Ms = 0;
}
m_fDetectionTime.Format(L"detection time: %dms (%.2lffps) motion: %.2f, skin: %.2f", m_MsPerFrame, m_FpsRate,
cvMotionAmount(),
cvSkinAmount());
UpdateData(FALSE);
}
int CVidCapDlg::SetCapturedImageData(Gdiplus::Bitmap* pBitmap, const vec2D& pData)
{
Gdiplus::BitmapData bitmapData;
Gdiplus::Status s = pBitmap->LockBits(&Gdiplus::Rect(0, 0, pBitmap->GetWidth(), pBitmap->GetHeight()), Gdiplus::ImageLockModeWrite,
PixelFormat24bppRGB, &bitmapData);
if (s == Gdiplus::Ok) {
unsigned int Width = (pData.width() < pBitmap->GetWidth()) ? pData.width() : pBitmap->GetWidth();
unsigned int Height = (pData.height() < pBitmap->GetHeight()) ? pData.width() : pBitmap->GetHeight();
unsigned char* Pixels = (unsigned char *)bitmapData.Scan0;
for(unsigned int y = 0; y < Height; y++) {
for(unsigned int x = 0; x < Width; x++) {
unsigned char c = (unsigned char)(255.0f * pData(y, x));
Pixels[3*x] = c;
Pixels[3*x+1] = c;
Pixels[3*x+2] = c;
}
Pixels += bitmapData.Stride;
}
pBitmap->UnlockBits(&bitmapData);
}
return s;
}
void CVidCapDlg::OnBnClickedFdetectCheck()
{
// TODO: Add your control notification handler code here
UpdateData();
if (m_fDetectBox == TRUE) {
double scales[3] = {0.86, 0.73, 0.6};
cvSetScales(scales, 3);
cvInit(m_Width, m_Height, 19, 19, m_ResizeRatio);
int res = cvInitAI(L"face.nn", L"pca.nn", L"skin.nn", L"preflt.nn");
TRACE(L" cvInitNN = %d\n", res);
if (res < 0) {
MessageBox(L"Failed to load classifiers.", L"error");
m_fDetectBox = false;
UpdateData(false);
}
m_cvInfoStatic = CString(cvInfo());
} else
m_cvInfoStatic = CString(L"cvInfo");
UpdateData(false);
}
void CVidCapDlg::OnStnDblclickCapimgStatic()
{
m_TakeSnapshot = true;
}
void CVidCapDlg::OnStnDblclickPrvStatic()
{
CFileDialog dlg(true);
dlg.GetOFN().lpstrFilter = L"Image Files\0*.bmp;*.jpg;*.jpeg\0\0";
if (dlg.DoModal() == IDOK) {
Gdiplus::Bitmap* pBitmap = ::new Gdiplus::Bitmap(dlg.GetPathName());
if (pBitmap->GetLastStatus() == Gdiplus::Ok) {
if (pBitmap->GetWidth() == m_Width &&
pBitmap->GetHeight() == m_Height &&
pBitmap->GetPixelFormat() == PixelFormat24bppRGB) {
RECT r;
m_CapImgStatic.GetClientRect(&r);
Gdiplus::Graphics g(m_CapImgStatic.GetDC()->m_hDC);
int nFaces = 0;
Gdiplus::BitmapData bitmapData;
Gdiplus::Status s = pBitmap->LockBits(&Gdiplus::Rect(0, 0, m_Width, m_Height), Gdiplus::ImageLockModeRead,
PixelFormat24bppRGB, &bitmapData);
if (s == Gdiplus::Ok) {
//detect faces
tic();
nFaces = cvDetect((unsigned char *)bitmapData.Scan0);
__int64 ms = toc();
TRACE(L" nFaces: %d\n", nFaces);
if (nFaces < 0)
nFaces = 0;
pBitmap->UnlockBits(&bitmapData);
m_fDetectionTime.Format(L"detection time: %dms (%.2ffps)", (int)ms, 1000.0f / float(ms));
UpdateData(false);
}
Gdiplus::Graphics mem_g(pBitmap);
Gdiplus::Pen redPen(Gdiplus::Color(255, 255, 0, 0), 2.5f);
for (int i = 0; i < nFaces; i++) {
RECT rect;
cvFaceRect(i, rect);
mem_g.DrawRectangle(&redPen, rect.left, rect.top, rect.right, rect.bottom);
TRACE(L" face coords: %d %d %d %d", rect.left, rect.top, rect.right, rect.bottom);
}
if (nFaces > 0) {
SetCapturedImageData(m_CapturedFace, *cvGetFace(0));
mem_g.DrawImage(m_CapturedFace,
Gdiplus::Rect(10, 10, (int)((double)m_Width / m_FaceRectRatio), (int)((double)m_Width / m_FaceRectRatio)),
0, 0, m_CapturedFace->GetWidth(), m_CapturedFace->GetHeight(),
Gdiplus::UnitPixel);
}
g.DrawImage(pBitmap, Gdiplus::Rect(0, 0, r.right, r.bottom));
} else
MessageBox(L"Loaded bitmap not in the right format", L"wrong format.");
::delete pBitmap;
} else
MessageBox(L"Failed to load bitmap", L"error.");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?