Luxand FaceSDK: console and windows form

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello everybody!<br/>
I have a console code:<br/>
<br/>
<div style="color:black; background-color:white
<pre>#include <span style="color:#a31515 "stdafx.h"
#include <span style="color:#a31515 "LuxandFaceSDK.h"

<span style="color:blue int _tmain(<span style="color:blue int argc, _TCHAR* argv[])
{
<span style="color:blue if (FSDKE_OK != FSDK_ActivateLibrary(<span style="color:#a31515 "HERE_IS_A_KEY")) {
MessageBox(0, L<span style="color:#a31515 "Please run the License Key Wizard (Start - Luxand - FaceSDK - License Key Wizard)n", L<span style="color:#a31515 "Error activating FaceSDK", MB_ICONERROR | MB_OK);
exit(-1);
}

FSDK_Initialize(<span style="color:#a31515 "");
<span style="color:green // set realtime face detection parameters
FSDK_SetFaceDetectionParameters(<span style="color:blue false, <span style="color:blue false, 100);
FSDK_SetFaceDetectionThreshold(3);

HWND hwnd = CreateWindowEx(WS_EX_TOOLWINDOW, L<span style="color:#a31515 "LISTBOX", L<span style="color:#a31515 "Facial Features", 0, 0, 0, 0, 0, 0, 0, 0, 0);
HDC dc = GetDC(hwnd);
SetWindowPos(hwnd, 0, 0, 0, 646, 518, SWP_NOZORDER|SWP_NOMOVE);
ShowWindow(hwnd, SW_SHOW);

RECT ClientRect;
GetClientRect(hwnd, &ClientRect);

HPEN FaceRectanglePen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
HBRUSH FaceRectangleBrush = (HBRUSH)GetStockObject(NULL_BRUSH);

HPEN FeatureCirclesPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
LOGBRUSH brush;
brush.lbColor = RGB(0, 0, 255);
brush.lbStyle = BS_SOLID;
HBRUSH FeatureCirclesBrush = CreateBrushIndirect(&brush);

<span style="color:green //WARNING: this doesnt work with x64, so comment it if you compile for x64:
SendMessage(hwnd, LB_ADDSTRING, 0, (<span style="color:blue long)(L<span style="color:#a31515 "Press Enter to open image, press Esc to exit ..."));

TFacePosition facePosition;
FSDK_Features facialFeatures;

MSG msg = {0};

<span style="color:blue bool ImageOpened = <span style="color:blue false;
<span style="color:blue int FaceDetected = -1;

<span style="color:blue int width, height;

HImage ResizedImageHandle;
FSDK_CreateEmptyImage(&ResizedImageHandle);

<span style="color:blue while(msg.message != WM_QUIT)
<span style="color:blue if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
<span style="color:blue if (msg.message == WM_KEYDOWN && msg.wParam == VK_ESCAPE)
<span style="color:blue break;

<span style="color:blue if (msg.message == WM_KEYDOWN && msg.wParam == VK_RETURN)
{
HImage imageHandle;
OPENFILENAMEA oFile;
<span style="color:blue char szPath[MAX_PATH];
<span style="color:blue char szDir[MAX_PATH];
ZeroMemory(&oFile, <span style="color:blue sizeof(oFile));
GetCurrentDirectoryA(<span style="color:blue sizeof(szDir), szDir);
lstrcpyA(szPath, <span style="color:#a31515 "");
oFile.lStructSize = <span style="color:blue sizeof(oFile);
oFile.hwndOwner = hwnd;
oFile.hInstance = NULL;
oFile.lpstrFilter = <span style="color:#a31515 "Images(*.jpg;*.jpeg;*.jpe;*.jfif;*.bmp;*.png)*.jpg;*.jpeg;*.jpe;*.jfif;*.bmp;*.png";
oFile.lpstrFile = szPath;
oFile.nMaxFile = <span style="color:blue sizeof(szPath);
oFile.lpstrInitialDir = szDir;
oFile.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
oFile.lpstrDefExt = NULL;
<span style="color:blue if (GetOpenFileNameA(&oFile) && FSDKE_OK == FSDK_LoadImageFromFile(&imageHandle, oFile.lpstrFile))
{
FaceDetected = FSDK_DetectFace(imageHandle, &facePosition);
<span style="color:blue if (FaceDetected == FSDKE_OK )
FSDK_DetectFacialFeaturesInRegion(imageHandle, &facePosition, &facialFeatures);

FSDK_GetImageWidth(imageHandle, &width);
FSDK_GetImageHeight(imageHandle, &height);
<span style="color:blue double resizeCoefficient = min(ClientRect.right/(<span style="color:blue double)width, (ClientRect.bottom - 16)/(<span style="color:blue double)height);

FSDK_FreeImage(ResizedImageHandle);
FSDK_CreateEmptyImage(&ResizedImageHandle);
FSDK_ResizeImage(imageHandle, resizeCoefficient, ResizedImageHandle);

FSDK_GetImageWidth(ResizedImageHandle, &width);
FSDK_GetImageHeight(ResizedImageHandle, &height);

FSDK_FreeImage(imageHandle);<span style="color:green // delete the FSDK image handle

<span style="color:blue if ( FaceDetected == FSDKE_OK )
{
facePosition.xc = (<span style="color:blue int)(resizeCoefficient * facePosition.xc);
facePosition.yc = (<span style="color:blue int)(resizeCoefficient * facePosition.yc);
facePosition.w = (<span style="color:blue int)(resizeCoefficient * facePosition.w);
<span style="color:blue for (<span style="color:blue int i = 0; i < FSDK_FACIAL_FEATURE_COUNT; i++)
{
facialFeatures.x = (<span style="color:blue int)(resizeCoefficient * facialFeatures.x);
facialFeatures.y = (<span style="color:blue int)(resizeCoefficient * facialFeatures.y);
}
}

ImageOpened = <span style="color:blue true;
InvalidateRect(hwnd, NULL, TRUE);
}
}

<span style="color:blue if (msg.message == WM_MOVE)
InvalidateRect(hwnd, NULL, TRUE);

<span style="color:blue if (msg.message == WM_PAINT && ImageOpened )
{
HBITMAP hbitmapHandle; <span style="color:green // to store the HBITMAP handle
FSDK_SaveImageToHBitmap(ResizedImageHandle, &hbitmapHandle);
DrawState(dc, NULL, NULL, (LPARAM)hbitmapHandle, NULL, (ClientRect.right - width)/2, 8 + (ClientRect.bottom - height)/2, width, height, DST_BITMAP | DSS_NORMAL);
DeleteObject(hbitmapHandle); <span style="color:green // delete the HBITMAP object

<span style="color:blue if (FaceDetected == FSDKE_OK)
{
<span style="color:blue int left = min(width - 1, max(0, facePosition.xc - facePosition.w/2)) + (ClientRect.right - width)/2;
<span style="color:blue int right = min(width - 1, max(0, facePosition.xc + facePosition.w/2)) + (ClientRect.right - width)/2;
<span style="color:blue int top = min(height - 1, max(0, facePosition.yc - facePosition.w/2)) + 8 + (ClientRect.bottom - height)/2;
<span style="color:blue int bottom = min(height - 1, max(0, facePosition.yc + facePosition.w/2)) + 8 + (ClientRect.bottom - height)/2;
SelectObject(dc, FaceRectanglePen);
SelectObject(dc, FaceRectangleBrush);
Rectangle(dc, left, top, right, bottom);

SelectObject(dc, FeatureCirclesPen);
SelectObject(dc, FeatureCirclesBrush);
<span style="color:blue for (<span style="color:blue int i = 0; i < FSDK_FACIAL_FEATURE_COUNT; i++)
Ellipse(dc, min(width - 1, max(0, facialFeatures.x - 2)) + (ClientRect.right - width)/2,
min(height - 1, max(0, facialFeatures.y - 2)) + 8 + (ClientRect.bottom - height)/2,
min(width - 1, max(0, facialFeatures.x + 2)) + (ClientRect.right - width)/2,
min(height - 1, max(0, facialFeatures.y + 2)) + 8 + (ClientRect.bottom - height)/2);
}
}
}
FSDK_FreeImage(ResizedImageHandle);<span style="color:green // delete the FSDK resized image handle
ReleaseDC(hwnd, dc);

FSDK_Finalize();
<span style="color:blue return 0;
}
[/code]

<br/>
and Im trying to convert it into WinAPI form application (here is a part of code for Callback function):

<div style="color:black; background-color:white
<pre>LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
<span style="color:blue int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

<span style="color:green // FaceSDK part

RECT ClientRect;
GetClientRect(hWnd, &ClientRect);

HPEN FaceRectanglePen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
HBRUSH FaceRectangleBrush = (HBRUSH)GetStockObject(NULL_BRUSH);

HPEN FeatureCirclesPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
LOGBRUSH brush;
brush.lbColor = RGB(0, 0, 255);
brush.lbStyle = BS_SOLID;
HBRUSH FeatureCirclesBrush = CreateBrushIndirect(&brush);

<span style="color:green //WARNING: this doesnt work with x64, so comment it if you compile for x64:
<span style="color:green // SendMessage(hWnd, LB_ADDSTRING, 0, (long)(L"Press Enter to open image, press Esc to exit ..."));

TFacePosition facePosition;
FSDK_Features facialFeatures;

<span style="color:blue bool ImageOpened = <span style="color:blue false;
<span style="color:blue int FaceDetected = -1;

<span style="color:blue int width, height;

HImage ResizedImageHandle;
FSDK_CreateEmptyImage(&ResizedImageHandle);

<span style="color:green // end of FaceSDK part

<span style="color:blue switch (message)
{
<span style="color:blue case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
<span style="color:green // Parse the menu selections:
<span style="color:blue switch (wmId)
{
<span style="color:blue case ID_FILE_OPEN:

HImage imageHandle;
OPENFILENAMEA oFile;
<span style="color:blue char szPath[MAX_PATH];
<span style="color:blue char szDir[MAX_PATH];
ZeroMemory(&oFile, <span style="color:blue sizeof(oFile));
GetCurrentDirectoryA(<span style="color:blue sizeof(szDir), szDir);
lstrcpyA(szPath, <span style="color:#a31515 "");
oFile.lStructSize = <span style="color:blue sizeof(oFile);
oFile.hwndOwner = hWnd;
oFile.hInstance = NULL;
oFile.lpstrFilter = <span style="color:#a31515 "Images(*.jpg;*.jpeg;*.jpe;*.jfif;*.bmp;*.png)*.jpg;*.jpeg;*.jpe;*.jfif;*.bmp;*.png";
oFile.lpstrFile = szPath;
oFile.nMaxFile = <span style="color:blue sizeof(szPath);
oFile.lpstrInitialDir = szDir;
oFile.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
oFile.lpstrDefExt = NULL;
<span style="color:blue if (GetOpenFileNameA(&oFile) && FSDKE_OK == FSDK_LoadImageFromFile(&imageHandle, oFile.lpstrFile))
{
FaceDetected = FSDK_DetectFace(imageHandle, &facePosition);
<span style="color:blue if (FaceDetected == FSDKE_OK )
FSDK_DetectFacialFeaturesInRegion(imageHandle, &facePosition, &facialFeatures);

FSDK_GetImageWidth(imageHandle, &width);
FSDK_GetImageHeight(imageHandle, &height);
<span style="color:blue double resizeCoefficient = min(ClientRect.right/(<span style="color:blue double)width, (ClientRect.bottom - 16)/(<span style="color:blue double)height);

FSDK_FreeImage(ResizedImageHandle);
FSDK_CreateEmptyImage(&ResizedImageHandle);
FSDK_ResizeImage(imageHandle, resizeCoefficient, ResizedImageHandle);

FSDK_GetImageWidth(ResizedImageHandle, &width);
FSDK_GetImageHeight(ResizedImageHandle, &height);

FSDK_FreeImage(imageHandle);<span style="color:green // delete the FSDK image handle

<span style="color:blue if ( FaceDetected == FSDKE_OK )
{
facePosition.xc = (<span style="color:blue int)(resizeCoefficient * facePosition.xc);
facePosition.yc = (<span style="color:blue int)(resizeCoefficient * facePosition.yc);
facePosition.w = (<span style="color:blue int)(resizeCoefficient * facePosition.w);
<span style="color:blue for (<span style="color:blue int i = 0; i < FSDK_FACIAL_FEATURE_COUNT; i++)
{
facialFeatures.x = (<span style="color:blue int)(resizeCoefficient * facialFeatures.x);
facialFeatures.y = (<span style="color:blue int)(resizeCoefficient * facialFeatures.y);
}
}

ImageOpened = <span style="color:blue true;
InvalidateRect(hWnd, NULL, TRUE);
}

<span style="color:blue break;
<span style="color:blue case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
<span style="color:blue break;
<span style="color:blue case IDM_EXIT:
FSDK_FreeImage(ResizedImageHandle);<span style="color:green // delete the FSDK resized image handle
ReleaseDC(hWnd, hdc);

FSDK_Finalize();

DestroyWindow(hWnd);
<span style="color:blue break;
<span style="color:blue default:
<span style="color:blue return DefWindowProc(hWnd, message, wParam, lParam);
}
<span style="color:blue break;
<span style="color:blue case WM_PAINT:
GetClientRect(hWnd, &ClientRect);
hdc = BeginPaint(hWnd, &ps);

<span style="color:blue if (ImageOpened)
{
hdc = GetDC(hWnd);

<span style="color:green // TODO: Add any drawing code here...

HBITMAP hbitmapHandle; <span style="color:green // to store the HBITMAP handle
FSDK_SaveImageToHBitmap(ResizedImageHandle, &hbitmapHandle);
DrawState(hdc, NULL, NULL, (LPARAM)hbitmapHandle, NULL, (ClientRect.right - width)/2, 8 + (ClientRect.bottom - height)/2, width, height, DST_BITMAP | DSS_NORMAL);
DeleteObject(hbitmapHandle); <span style="color:green // delete the HBITMAP object

<span style="color:blue if (FaceDetected == FSDKE_OK)
{
<span style="color:blue int left = min(width - 1, max(0, facePosition.xc - facePosition.w/2)) + (ClientRect.right - width)/2;
<span style="color:blue int right = min(width - 1, max(0, facePosition.xc + facePosition.w/2)) + (ClientRect.right - width)/2;
<span style="color:blue int top = min(height - 1, max(0, facePosition.yc - facePosition.w/2)) + 8 + (ClientRect.bottom - height)/2;
<span style="color:blue int bottom = min(height - 1, max(0, facePosition.yc + facePosition.w/2)) + 8 + (ClientRect.bottom - height)/2;
SelectObject(hdc, FaceRectanglePen);
SelectObject(hdc, FaceRectangleBrush);
Rectangle(hdc, left, top, right, bottom);

SelectObject(hdc, FeatureCirclesPen);
SelectObject(hdc, FeatureCirclesBrush);
<span style="color:blue for (<span style="color:blue int i = 0; i < FSDK_FACIAL_FEATURE_COUNT; i++)
Ellipse(hdc, min(width - 1, max(0, facialFeatures.x - 2)) + (ClientRect.right - width)/2,
min(height - 1, max(0, facialFeatures.y - 2)) + 8 + (ClientRect.bottom - height)/2,
min(width - 1, max(0, facialFeatures.x + 2)) + (ClientRect.right - width)/2,
min(height - 1, max(0, facialFeatures.y + 2)) + 8 + (ClientRect.bottom - height)/2);
}


}
EndPaint(hWnd, &ps);
<span style="color:blue break;
<span style="color:blue case WM_DESTROY:
PostQuitMessage(0);
<span style="color:blue break;
<span style="color:blue default:
<span style="color:blue return DefWindowProc(hWnd, message, wParam, lParam);
}
<span style="color:blue return 0;
}
[/code]

<br/>
But nothing is output in the form (the first code works OK).

<br/>

View the full article
 
Back
Top