void compareWithOneSample(char* sample) { HImage imgOrg, imgCmp; TFacePosition fpOrg, fpCmp; FSDK_FaceTemplate ftOrg, ftCmp; int result; float similarity = 0.0f, maxSim = 0.0f; char *pair = NULL; char *temp; int nFile; char **pics; std::ofstream fileOut; fileOut.open("BadPicsPair.txt"); pics = getAllFiles(nFile); initFaceSDK(); /* set detection parameters first param : set comparing rotation ratio true == -30 ~ 30 degree, false == -15 ~ 15 second param : get twisted ratio true == get , false == not get third param : set internal resize ratio */ FSDK_SetFaceDetectionParameters(false, false, 500); for(int i = 0 ; i < nFile ; ++i) { if(!strcmp(pics[i] + strlen("./Pics/"), sample)) { temp = pics[0]; pics[0] = pics[i]; pics[i] = temp; break; } } result = FSDK_LoadImageFromFile(&imgOrg, pics[0]); if(result != FSDKE_OK) { std::cout<<"============= Input Sample is Bad ============"<<std::endl<<std::endl; return; } result = FSDK_DetectFace(imgOrg, &fpOrg); if(result != FSDKE_OK) { std::cout<<"============= Input Sample is Bad ============"<<std::endl<<std::endl; return; } result = FSDK_GetFaceTemplateInRegion(imgOrg, &fpOrg, &ftOrg); if(result != FSDKE_OK) { std::cout<<"============= Input Sample is Bad ============"<<std::endl<<std::endl; return; } for(int i = 1 ; i < nFile ; ++i) { //std::cout<<"=================== This pair ===================="<<std::endl; //std::cout<<pics[i]<<" "<<pics[j]<<std::endl<<std::endl; // load 1 imgs result = FSDK_LoadImageFromFile(&imgCmp, pics[i]); //std::cout<<"=================== Load Image ==================="<<std::endl; //std::cout<<ErrorCode[-result1]<<std::endl<<std::endl; // find faces result = FSDK_DetectFace(imgCmp, &fpCmp); if(result != FSDKE_OK) { std::cout<<"============= break this comparison ============="<<std::endl<<std::endl; if(result != FSDKE_OK) fileOut<<pics[i]<<std::endl; FSDK_FreeImage(imgCmp); break; } //std::cout<<"=================== Find Faces ==================="<<std::endl; //std::cout<<ErrorCode[-result1]<<std::endl<<std::endl; FSDK_GetFaceTemplateInRegion(imgCmp, &fpCmp, &ftCmp); if(result != FSDKE_OK) { std::cout<<"============= break this comparison ============="<<std::endl; std::cout<<pics[i]<<std::endl<<std::endl; FSDK_FreeImage(imgCmp); break; } FSDK_MatchFaces(&ftOrg, &ftCmp, &similarity); std::cout<<"=================== Similarity =================="<<std::endl; std::cout<<"Similarity is "<<similarity * 100<<"%"<<std::endl<<std::endl; if(similarity >= maxSim) { maxSim = similarity; if(pair != NULL) { delete[] pair; pair = NULL; } pair = new char[strlen(pics[i]) + 1]; strcpy(pair, pics[i]); } FSDK_FreeImage(imgCmp); } FSDK_FreeImage(imgOrg); std::cout<<"===================== Result ===================="<<std::endl; std::cout<<"Total "<<nFile<<" files"<< std::endl; std::cout<<"Max Similarity: "<<maxSim* 100<<"%"<<std::endl; std::cout<<pics[0]<<std::endl; std::cout<<pair<<std::endl; for(int i = 0 ; i < nFile ; ++i) delete[] pics[i]; delete[] pics; delete[] pair; fileOut.close(); getchar(); }
int main(int argc, char* argv[]) { if (FSDKE_OK != FSDK_ActivateLibrary("Jl3R1DBC1qVQonaiBAq8gK7KzetXbFb4r+OF1DLzInT3KyXHvgHNLyk2Tymk5G6GBv58/Oqn+SQeOWCQfQASTV1Mcd7RQAsrmW02oOa9lhZsMockPLoEnpsH4W1I0+zmxmUwecWKEep9j4BrYhQWuiA3QcNeQO+tfyLOHASk3+M=")){ MessageBox(0, L"Please run the License Key Wizard (Start - Luxand - FaceSDK - License Key Wizard)\n", L"Error activating FaceSDK", MB_ICONERROR | MB_OK); exit(-1); } FSDK_Initialize(""); // creating a window HWND hwnd = CreateWindowEx(WS_EX_TOOLWINDOW, L"LISTBOX", L"IP Camera Face Tracking", 0, 0, 0, 0, 0, 0, 0, 0, 0); HDC dc = GetDC(hwnd); SetWindowPos(hwnd, 0, 0, 0, 660, 540, SWP_NOZORDER|SWP_NOMOVE); ShowWindow(hwnd, SW_SHOW); RECT ClientRect; GetClientRect(hwnd, &ClientRect); HPEN hNPen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0)); HPEN hOPen = (HPEN)SelectObject(dc, hNPen); HBRUSH hOldBrush; HBRUSH hNewBrush; hNewBrush = (HBRUSH)GetStockObject(NULL_BRUSH); hOldBrush = (HBRUSH)SelectObject(dc, hNewBrush); HFONT myFont = CreateFont(14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, L"Microsoft Sans Serif"); // creating input boxes HWND Address = CreateWindow(L"STATIC", L"address:", WS_CHILD, 0, 0, 0, 0, hwnd, 0, 0, 0); SendMessage(Address, WM_SETFONT, (WPARAM)myFont, TRUE); SetWindowPos(Address, 0, 5, ClientRect.bottom - 20, 40, 18, SWP_NOZORDER); ShowWindow(Address, SW_SHOW); HWND AddressBox = CreateWindow(L"EDIT", L"", WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 0, 0, 0, 0, hwnd, 0, 0, 0); SendMessage(AddressBox, WM_SETFONT, (WPARAM)myFont, TRUE); SetWindowPos(AddressBox, 0, 45, ClientRect.bottom - 20, 360, 18, SWP_NOZORDER); ShowWindow(AddressBox, SW_SHOW); HWND User = CreateWindow(L"STATIC", L"username:"******"EDIT", L"admin", WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 0, 0, 0, 0, hwnd, 0, 0, 0); SendMessage(UserBox, WM_SETFONT, (WPARAM)myFont, TRUE); SetWindowPos(UserBox, 0, 460, ClientRect.bottom - 20, 65, 18, SWP_NOZORDER); ShowWindow(UserBox, SW_SHOW); HWND Password = CreateWindow(L"STATIC", L"password:"******"EDIT", L"", WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 0, 0, 0, 0, hwnd, 0, 0, 0); SendMessage(PasswordBox, WM_SETFONT, (WPARAM)myFont, TRUE); SetWindowPos(PasswordBox, 0, 580, ClientRect.bottom - 20, 65, 18, SWP_NOZORDER); ShowWindow(PasswordBox, SW_SHOW); SendMessage(hwnd, LB_ADDSTRING, 0, (LPARAM)(L"Press Esc to exit ...")); // creating a Tracker HTracker tracker = 0; FSDK_CreateTracker(&tracker); int err = 0; // set realtime face detection parameters FSDK_SetTrackerMultipleParameters(tracker, "RecognizeFaces=false; DetectFacialFeatures=true; HandleArbitraryRotations=false; DetermineFaceRotationAngle=false; InternalResizeWidth=256; FaceDetectionThreshold=5;", &err); int cameraHandle = 0; bool CameraOpened = false; MSG msg = {0}; while (msg.message != WM_QUIT) { if (CameraOpened) { HImage imageHandle; int res = FSDK_GrabFrame(cameraHandle, &imageHandle); if (res != FSDKE_OK) {// grab the current frame from the camera printf("error in FSDK_GrabFrame %d\n", res); if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); if (msg.message == WM_KEYDOWN && msg.wParam == VK_ESCAPE) break; } continue; } long long IDs[256]; long long faceCount = 0; FSDK_FeedFrame(tracker, 0, imageHandle, &faceCount, IDs, sizeof(IDs)); HImage resizedImageHandle; FSDK_CreateEmptyImage(&resizedImageHandle); int width; int height; FSDK_GetImageWidth(imageHandle, &width); FSDK_GetImageHeight(imageHandle, &height); float ratio = min(ClientRect.right/(float)width, (ClientRect.bottom - 40)/(float)height); FSDK_ResizeImage(imageHandle, ratio, resizedImageHandle); FSDK_FreeImage(imageHandle); FSDK_GetImageWidth(resizedImageHandle, &width); FSDK_GetImageWidth(resizedImageHandle, &height); HBITMAP hbitmapHandle; // to store the HBITMAP handle FSDK_SaveImageToHBitmap(resizedImageHandle, &hbitmapHandle); DrawState(dc, NULL, NULL, (LPARAM)hbitmapHandle, NULL, 0, 16, width, height, DST_BITMAP | DSS_NORMAL); for (int i = 0; i < faceCount; ++i) { TFacePosition facePosition; FSDK_GetTrackerFacePosition(tracker, 0, IDs[i], &facePosition); int x1 = (int)(ratio*(facePosition.xc - facePosition.w*0.6)); int y1 = (int)(ratio*(facePosition.yc - facePosition.w*0.5)); int x2 = (int)(ratio*(facePosition.xc + facePosition.w*0.6)); int y2 = (int)(ratio*(facePosition.yc + facePosition.w*0.7)); Rectangle(dc, x1, 16 + y1, x2, 16 + y2); } Sleep(20); DeleteObject(hbitmapHandle); // delete the HBITMAP object FSDK_FreeImage(resizedImageHandle);// delete the FSDK image handle } if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); if (msg.message == WM_KEYDOWN) { if (msg.wParam == VK_ESCAPE) break; if (msg.wParam == VK_RETURN) { if (CameraOpened && FSDKE_OK != FSDK_CloseVideoCamera(cameraHandle)) { MessageBox(0, L"Error closing camera", L"Error", MB_ICONERROR | MB_OK); return -5; } char Camera_MJPEG_URL[1024]; SendMessageA(AddressBox, WM_GETTEXT, (WPARAM) 1024, (LPARAM) Camera_MJPEG_URL); char username[1024]; SendMessageA(UserBox, WM_GETTEXT, (WPARAM) 1024, (LPARAM) username); char password[1024]; SendMessageA(PasswordBox, WM_GETTEXT, (WPARAM) 1024, (LPARAM) password); int timeout_seconds = 30; printf("Trying to open the camera...\n"); if (FSDKE_OK != FSDK_OpenIPVideoCamera(FSDK_MJPEG, Camera_MJPEG_URL, username, password, timeout_seconds, &cameraHandle)) { MessageBox(0, L"Error opening IP camera", L"Error", MB_ICONERROR | MB_OK); return -2; } CameraOpened = true; } } } } ReleaseDC(hwnd, dc); if (CameraOpened && FSDKE_OK != FSDK_CloseVideoCamera(cameraHandle)) { MessageBox(0, L"Error closing camera", L"Error", MB_ICONERROR | MB_OK); return -5; } FSDK_Finalize(); return 0; }
void compareAllSample() { HImage imgOrg, imgCmp; TFacePosition fpOrg, fpCmp; FSDK_FaceTemplate ftOrg, ftCmp; int result1, result2; float similarity = 0.0f, maxSim = 0.0f; char *pair1 = NULL, *pair2 = NULL; int nFile; char **pics; std::ofstream fileOut; fileOut.open("BadPicsPair.txt"); pics = getAllFiles(nFile); initFaceSDK(); /* set detection parameters first param : set comparing rotation ratio true == -30 ~ 30 degree, false == -15 ~ 15 second param : get twisted ratio true == get , false == not get third param : set internal resize ratio */ FSDK_SetFaceDetectionParameters(false, false, 500); for(int i = 0 ; i < nFile - 1; ++i) { for(int j = i + 1 ; j < nFile ; ++j) { //std::cout<<"=================== This pair ===================="<<std::endl; //std::cout<<pics[i]<<" "<<pics[j]<<std::endl<<std::endl; // load 2 imgs result1 = FSDK_LoadImageFromFile(&imgOrg, pics[i]); result2 = FSDK_LoadImageFromFile(&imgCmp, pics[j]); //std::cout<<"=================== Load Image ==================="<<std::endl; //std::cout<<ErrorCode[-result1]<<std::endl; //std::cout<<ErrorCode[-result2]<<std::endl<<std::endl; // find faces result1 = FSDK_DetectFace(imgOrg, &fpOrg); result2 = FSDK_DetectFace(imgCmp, &fpCmp); if(result1 != FSDKE_OK || result2 != FSDKE_OK) { std::cout<<"============= break this comparation ============="<<std::endl<<std::endl; if(result1 != FSDKE_OK) fileOut<<pics[i]<<std::endl; if(result2 != FSDKE_OK) fileOut<<pics[j]<<std::endl; FSDK_FreeImage(imgOrg); FSDK_FreeImage(imgCmp); break; } //std::cout<<"=================== Find Faces ==================="<<std::endl; //std::cout<<ErrorCode[-result1]<<std::endl; //std::cout<<ErrorCode[-result2]<<std::endl<<std::endl; FSDK_GetFaceTemplateInRegion(imgOrg, &fpOrg, &ftOrg); FSDK_GetFaceTemplateInRegion(imgCmp, &fpCmp, &ftCmp); if(result1 != FSDKE_OK || result2 != FSDKE_OK) { std::cout<<"============= break this comparation ============="<<std::endl; std::cout<<pics[i]<<" "<<pics[j]<<std::endl<<std::endl; FSDK_FreeImage(imgOrg); FSDK_FreeImage(imgCmp); break; } FSDK_MatchFaces(&ftOrg, &ftCmp, &similarity); std::cout<<"=================== Similarity ==================="<<std::endl; std::cout<<"Similarity is "<<similarity * 100<<"%"<<std::endl<<std::endl; if(similarity >= maxSim) { maxSim = similarity; if(pair1 != NULL && pair2 != NULL) { delete[] pair1; delete[] pair2; pair1 = NULL; pair2 = NULL; } pair1 = new char[strlen(pics[i]) + 1]; pair2 = new char[strlen(pics[j]) + 1]; strcpy(pair1, pics[i]); strcpy(pair2, pics[j]); } FSDK_FreeImage(imgOrg); FSDK_FreeImage(imgCmp); } } std::cout<<"===================== Result ====================="<<std::endl; std::cout<<"Total "<<nFile<<" files"<< std::endl; std::cout<<"Max Similarity: "<<maxSim* 100<<"%"<<std::endl; std::cout<<pair1<<std::endl; std::cout<<pair2<<std::endl; for(int i = 0 ; i < nFile ; ++i) delete[] pics[i]; delete[] pics; delete[] pair1; delete[] pair2; fileOut.close(); getchar(); }
void __fastcall TForm1::Button1Click(TObject *Sender) { OpenDialog1->Filter = "JPEG (*.jpg)|*.jpg|Windows bitmap (*.bmp)|*.bmp|All files|*.*"; if( OpenDialog1->Execute() == True ){ HImage imageHandle; if (FSDK_LoadImageFromFile(&imageHandle, OpenDialog1->FileName.c_str()) != FSDKE_OK) Application->MessageBox("Error loading image", "Error", 0); else { // resize image to fit the window width int imageWidth; int imageHeight; FSDK_GetImageWidth(imageHandle, &imageWidth); FSDK_GetImageHeight(imageHandle, &imageHeight); double HorRatio = (double)Image1->Width/imageWidth; double VerRatio = (double)Image1->Height/imageHeight; double ratio = (HorRatio < VerRatio) ? HorRatio : VerRatio; HImage image2Handle; FSDK_CreateEmptyImage(&image2Handle); FSDK_ResizeImage(imageHandle, ratio, image2Handle); FSDK_CopyImage(image2Handle, imageHandle); FSDK_FreeImage(image2Handle); HBITMAP hbitmapHandle; // to store the HBITMAP handle // save image into HBITMAP handle FSDK_SaveImageToHBitmap(imageHandle, &hbitmapHandle); Graphics::TBitmap *tpic = new Graphics::TBitmap; tpic->Handle = hbitmapHandle; // display current frame Image1->Picture->Assign(tpic); delete tpic; Application->ProcessMessages(); TFacePosition facePosition; if (FSDK_DetectFace(imageHandle, &facePosition) != FSDKE_OK) MessageBox(this->Handle, "No faces detected", "Face Detection", 0); else { int left = facePosition.xc - facePosition.w /2; int top = facePosition.yc - facePosition.w /2; int right = facePosition.xc + facePosition.w /2; int bottom = facePosition.yc + facePosition.w /2; Image1->Canvas->Brush->Style = bsClear; Image1->Canvas->Pen->Color = clLime; Image1->Canvas->Pen->Width = 1; Image1->Canvas->Rectangle(left, top, right, bottom); Application->ProcessMessages(); FSDK_Features facialFeatures; FSDK_DetectFacialFeatures(imageHandle, &facialFeatures); for (int i = 2; i < FSDK_FACIAL_FEATURE_COUNT; i++) Image1->Canvas->Ellipse(facialFeatures[i].x - 2, facialFeatures[i].y - 2, facialFeatures[i].x + 2, facialFeatures[i].y + 2); Image1->Canvas->Pen->Color = clBlue; for (int i = 0; i < 2; i++) Image1->Canvas->Ellipse(facialFeatures[i].x - 2, facialFeatures[i].y - 2, facialFeatures[i].x + 2, facialFeatures[i].y + 2); } FSDK_FreeImage(imageHandle); // delete the FSDK image handle } } }