static void ProcessShapes( const ShapeFile& sh, // in FILE* fitfile) // in { Pacifier pacifier(sh.nshapes_); // only used if quiet_g for (int ishape = 0; ishape < sh.nshapes_; ishape++) { if (quiet_g) pacifier.Print_(ishape); else { if (sh.nshapes_ > 1) lprintf("%*d ", NumDigits(sh.nshapes_), ishape); lprintf("%*.*s:%s", sh.nchar_, sh.nchar_, sh.bases_[ishape].c_str(), trace_g? "\n": " "); } if (ignore_multiface_imgs_g && (sh.bits_[ishape] & AT_MultiFace)) { printf_g("multiple face, skipping\n"); continue; // note continue } const char* imgpath = PathGivenDirs(sh.bases_[ishape], sh.dirs_, sh.shapepath_); Image img(cv::imread(imgpath, CV_LOAD_IMAGE_GRAYSCALE)); if (!img.data) Err("Cannot load %s", imgpath); const clock_t start_time = clock(); const Shape refshape(sh.shapes_[ishape]); const Shape refshape17(Shape17OrEmpty(refshape)); if (refshape17.rows) // converted to a Shape17? SanityCheckShape17(refshape17); if (!stasm_open_image((const char*)img.data, img.cols, img.rows, imgpath, 0 /*multi*/, minwidth_g)) Err("stasm_open_image failed: %s", stasm_lasterr()); clock_t start_time1 = clock(); const double facedet_time = double(start_time1 - start_time) / CLOCKS_PER_SEC; int foundface = false; float estyaw = 0; // estimated yaw float landmarks[2 * stasm_NLANDMARKS]; // x,y coords if (!stasm_search_auto_ext(&foundface, landmarks, &estyaw)) Err("stasm_search_auto failed: %s", stasm_lasterr()); const Shape shape(LandmarksAsShape(landmarks)); const double asmsearch_time = double(clock() - start_time) / CLOCKS_PER_SEC; ProcessFace(img, imgpath, foundface, shape, estyaw, facedet_time, asmsearch_time, refshape, fitfile, sh, ishape); } if (quiet_g) { pacifier.End_(); printf("\n"); } }
bool CFaceDetect::detectByAsm( const Mat &faceImage,vector<CFaceRect> &rclist,bool onlybig ) { int foundface = 0; float landmarks[2 * stasm_NLANDMARKS]; // x,y coords (note the 2) //if(!stasm_init("../model",0)) //{ // return ; //} Mat_<unsigned char> matimage = faceImage; //detect face if (!stasm_open_image((const char*)matimage.data, faceImage.cols, faceImage.rows, "image", onlybig?0:1 /*multiface*/, 10 /*minwidth*/)) { //cout<<"detect face failed !!"<<stasm_lasterr()<<endl; return false; } Mat_<unsigned char> outimg(faceImage.clone()); for(;;) { CFaceRect facerect; //find all the face if(!stasm_search_auto(&foundface, landmarks)) { return false; } if(foundface == 0) { break; } facerect.noalign = false; //landmarks inner the image stasm_force_points_into_image(landmarks, faceImage.cols, faceImage.rows); getAsmPointer(landmarks,facerect.rcface); //printLandmarks(landmarks); //drawLandmarks(outimg, landmarks); //markLandmarks(outimg, landmarks); facerect.rcfullface = facerect.rcface; facerect.rclefteye = Rect((int)landmarks[L_LPupil * 2],(int)landmarks[L_LPupil * 2 + 1],1,1); facerect.rcrighteye = Rect((int)landmarks[L_RPupil * 2],(int)landmarks[L_RPupil * 2 + 1],1,1); facerect.rcmouth = Rect((int)landmarks[L_CTopOfTopLip * 2],(int)landmarks[L_CTopOfTopLip * 2 + 1],1,1); //cout<<"eye "<<facerect.rclefteye.x<<" "<<facerect.rclefteye.y<<" "<<facerect.rcrighteye.x<<" "<<facerect.rcrighteye.y<<" "<<facerect.rcmouth.x<<" "<<facerect.rcmouth.y<<endl; outimg(cvRound(landmarks[L_LPupil*2+1]),cvRound(landmarks[2*L_LPupil]))=255; outimg(cvRound(landmarks[L_RPupil*2+1]),cvRound(landmarks[2*L_RPupil]))=255; outimg(cvRound(landmarks[L_CTopOfTopLip*2+1]),cvRound(landmarks[2*L_CTopOfTopLip]))=255; rclist.push_back(facerect); } return true; //imwrite("test_stasm_lib_auto.bmp", outimg); }
int main() { if (!stasm_init("../data", 0 /*trace*/)) error("stasm_init failed: ", stasm_lasterr()); static const char* path = "../data/testface.jpg"; cv::Mat_<unsigned char> img(cv::imread(path, CV_LOAD_IMAGE_GRAYSCALE)); if (!img.data) error("Cannot load", path); if (!stasm_open_image((const char*)img.data, img.cols, img.rows, path, 1 /*multiface*/, 10 /*minwidth*/)) error("stasm_open_image failed: ", stasm_lasterr()); int foundface; float landmarks[2 * stasm_NLANDMARKS]; // x,y coords (note the 2) int nfaces = 0; while (1) { if (!stasm_search_auto(&foundface, landmarks)) error("stasm_search_auto failed: ", stasm_lasterr()); if (!foundface) break; // note break // for demonstration, convert from Stasm 77 points to XM2VTS 68 points stasm_convert_shape(landmarks, 68); // draw the landmarks on the image as white dots stasm_force_points_into_image(landmarks, img.cols, img.rows); for (int i = 0; i < stasm_NLANDMARKS; i++) img(cvRound(landmarks[i*2+1]), cvRound(landmarks[i*2])) = 255; nfaces++; } printf("%s: %d face(s)\n", path, nfaces); fflush(stdout); cv::imwrite("minimal2.bmp", img); cv::imshow("stasm minimal2", img); cv::waitKey(); return 0; }
static PyObject* Py_open_image( PyObject* self, PyObject* args, PyObject* kwargs) { PyObject* img_obj; const char* debugpath = ""; int multiface = 0; int minwidth = 10; static const char* kwlist[] = { "image", "debugpath", "multiface", "minwidth", NULL }; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|sii:open_image", const_cast<char**>(kwlist), &img_obj, &debugpath, &multiface, &minwidth)) return NULL; int width, height; const char* img_data = PyArray_to_image(img_obj, &width, &height); if (img_data == NULL) { PyErr_SetString(PyExc_TypeError, imarray_error); return NULL; } if (multiface != 0 && multiface != 1) { PyErr_SetString(PyExc_TypeError, multiface_error); return NULL; } if (minwidth < 1 || minwidth > 100) { PyErr_SetString(PyExc_ValueError, minwidth_error); return NULL; } if (!stasm_open_image(img_data, width, height, debugpath, multiface, minwidth)) { PyErr_SetString(StasmException, stasm_lasterr()); return NULL; } Py_RETURN_NONE; }
bool FaceAlignment::detectLandmarks(cv::Mat& img, std::vector<std::vector<cv::Point2d> >& points, std::vector<cv::Point2d>& centers) { float landmarks[2 * stasm_NLANDMARKS]; mutex->lock(); int allowMultiFace = 0; int minFaceWidthInpercentOfWidth = 5; commonTool.log(QString("###%1###").arg(QString::fromStdString(dataPath.c_str()))); commonTool.log(QString("##$%1$##").arg(strnlen(dataPath.c_str(),50))); int success = stasm_open_image((const char*)img.data, img.cols, img.rows, dataPath.c_str(), allowMultiFace, minFaceWidthInpercentOfWidth); if (!success) commonTool.log("Error!!!"); int foundface = 1; bool aFaceWasFound = false; // int success = stasm_search_single(&foundface, landmarks, (char*)img.data, img.cols, img.rows, dataPath.c_str(), dataPath.c_str()); // if (!success) commonTool.log("Error!!!"); while (foundface) { success = stasm_search_auto(&foundface, landmarks); if (!success) commonTool.log("Error when calling stasm_search_auto"); //commonTool.log(QString("FoundFace --> %1").arg(foundface)); //if (!success || foundface == 0) { // return false; //} if (foundface) { aFaceWasFound = true; int numberOfPoint = stasm_NLANDMARKS; std::vector<cv::Point2d> landmarkList; cv::Point2d center = cv::Point2d(0.0, 0.0); for (int i = 0; i < numberOfPoint; i++) { cv::Point2d p = cv::Point2d(landmarks[i*2], landmarks[i*2+1]); landmarkList.push_back(p); center += p; } points.push_back(landmarkList); center = cv::Point2d(center.x/numberOfPoint, center.y/numberOfPoint); centers.push_back(center); } } mutex->unlock(); return aFaceWasFound; }
static void ProcessImg( const char* imgpath) // in { Image img(cv::imread(imgpath, CV_LOAD_IMAGE_GRAYSCALE)); if (!img.data) Err("Cannot load %s", imgpath); if (!stasm_open_image((const char*)img.data, img.cols, img.rows, imgpath, multiface_g, minwidth_g)) Err("stasm_open_image failed: %s", stasm_lasterr()); CImage cimg; // color version of image if (writeimgs_g) // actually need the color image? cvtColor(img, cimg, CV_GRAY2BGR); int nfaces = 0; while (1) { if (trace_g && nfaces > 0 && multiface_g) stasm_printf("\n%d: ", nfaces); int foundface; float landmarks[2 * stasm_NLANDMARKS]; // x,y coords if (!stasm_search_auto(&foundface, landmarks)) Err("stasm_search_auto failed: %s", stasm_lasterr()); if (!foundface) break; // note break ProcessFace(cimg, landmarks, nfaces, Base(imgpath)); nfaces++; } if (trace_g) lprintf("\n"); if (writeimgs_g && nfaces) { // write as a bmp not as a jpg because don't want blurred shape lines char newpath[SLEN]; sprintf(newpath, "%s_stasm.bmp", Base(imgpath)); lprintf("%s ", newpath); if (!cv::imwrite(newpath, cimg)) Err("Could not write %s", newpath); } lprintf("%d face%s\n", nfaces, plural(nfaces)); }
bool CFaceDetect::detectByAsmMarks( const Mat &faceImage,vector<float *> &vlandmarks,bool onlybig ) { int foundface = 0; Mat_<unsigned char> matimage = faceImage; //detect face if (!stasm_open_image((const char*)matimage.data, faceImage.cols, faceImage.rows, "image", onlybig?0:1 /*multiface*/, 10 /*minwidth*/)) { return false; } Mat_<unsigned char> outimg(faceImage.clone()); for(;;) { CFaceRect facerect; float *landmarks = new float[2 * stasm_NLANDMARKS]; // x,y coords (note the 2) //find all the face if(!stasm_search_auto(&foundface, landmarks)) { return false; } if(foundface == 0) { break; } facerect.noalign = false; //landmarks inner the image stasm_force_points_into_image(landmarks, faceImage.cols, faceImage.rows); //printLandmarks(landmarks); //drawLandmarks(outimg, landmarks); //markLandmarks(outimg, landmarks); //imshow("tmp", outimg); vlandmarks.push_back(landmarks); } return true; }
int main(int argc, const char** argv) { if (argc != 5) Exit("Usage: test_stasm_lib MULTI MINWIDTH TRACE IMAGE"); const int multi = argv[1][0] - '0'; if (multi != 0 && multi != 1) Exit("Usage: test_stasm_lib MULTI MINWIDTH TRACE IMAGE, " "with MULTI 0 or 1, you have MULTI %s", argv[1]); int minwidth = -1; if (sscanf(argv[2], "%d", &minwidth) != 1 || minwidth < 1 || minwidth > 100) { Exit("Usage: test_stasm_lib MULTI MINWIDTH TRACE IMAGE with " "MINWIDTH 1 to 100, you have MINWIDTH %s", argv[2]); } const int trace = argv[3][0] - '0'; if (trace < 0 || trace > 1) Exit("Usage: test_stasm_lib MULTI MINWIDTH TRACE IMAGE, with TRACE 0 or 1"); if (!stasm_init("../data", trace)) Exit("stasm_init failed: %s", stasm_lasterr()); const char* path = argv[4]; // image name stasm_printf("Reading %s\n", path); const cv::Mat_<unsigned char> img(cv::imread(path, CV_LOAD_IMAGE_GRAYSCALE)); if (!img.data) // could not load image? Exit("Cannot load %s", path); cv::Mat_<unsigned char> outimg(img.clone()); if (!stasm_open_image((const char*)img.data, img.cols, img.rows, path, multi != 0, minwidth)) Exit("stasm_open_image failed: %s", stasm_lasterr()); // Test stasm_search_auto. // The min face size was set in the above stasm_open_image call. float landmarks[2 * stasm_NLANDMARKS]; // x,y coords int iface = 0; while (1) { stasm_printf("--- Auto Face %d ---\n", iface); int foundface; float estyaw; if (!stasm_search_auto_ext(&foundface, landmarks, &estyaw)) Exit("stasm_search_auto failed: %s", stasm_lasterr()); if (!foundface) { stasm_printf("No more faces\n"); break; // note break } char s[100]; sprintf(s, "\nFinal with auto init (estyaw %.0f)", estyaw); PrintLandmarks(landmarks, s); DrawLandmarks(outimg, landmarks); iface++; if (trace) stasm_printf("\n"); } imwrite("test_stasm_lib_auto.bmp", outimg); if (stasm_NLANDMARKS != 77) { stasm_printf( "Skipping pinned test because stasm_NLANDMARKS is %d not 77\n", stasm_NLANDMARKS); } else if (multi == 0 && minwidth == 25 && iface) { // Test stasm_search_pinned. A human user is not at hand, so gyp by using // points from the last face found above for our 5 start points stasm_printf("--- Pinned Face %d ---\n", iface); float pinned[2 * stasm_NLANDMARKS]; // x,y coords memset(pinned, 0, sizeof(pinned)); pinned[L_LEyeOuter*2] = landmarks[L_LEyeOuter*2] + 2; pinned[L_LEyeOuter*2+1] = landmarks[L_LEyeOuter*2+1]; pinned[L_REyeOuter*2] = landmarks[L_REyeOuter*2] - 2; pinned[L_REyeOuter*2+1] = landmarks[L_REyeOuter*2+1]; pinned[L_CNoseTip*2] = landmarks[L_CNoseTip*2]; pinned[L_CNoseTip*2+1] = landmarks[L_CNoseTip*2+1]; pinned[L_LMouthCorner*2] = landmarks[L_LMouthCorner*2]; pinned[L_LMouthCorner*2+1] = landmarks[L_LMouthCorner*2+1]; pinned[L_RMouthCorner*2] = landmarks[L_RMouthCorner*2]; pinned[L_RMouthCorner*2+1] = landmarks[L_RMouthCorner*2+1]; memset(landmarks, 0, sizeof(landmarks)); if (!stasm_search_pinned(landmarks, pinned, (const char*)img.data, img.cols, img.rows, path)) Exit("stasm_search_pinned failed: %s", stasm_lasterr()); PrintLandmarks(landmarks, "Final with pinned init"); outimg = img.clone(); DrawLandmarks(outimg, landmarks); imwrite("test_stasm_lib_pinned.bmp", outimg); // test stasm_convert_shape float newlandmarks[2 * stasm_NLANDMARKS]; // x,y coords memcpy(newlandmarks, landmarks, 2 * stasm_NLANDMARKS * sizeof(float)); stasm_convert_shape(newlandmarks, 68); PrintLandmarks(newlandmarks, "stasm77 to xm2vts"); #if 0 outimg = img.clone(); DrawLandmarks(outimg, newlandmarks, 68); imwrite("test_stasm_lib_68.bmp", outimg); #endif memcpy(newlandmarks, landmarks, 2 * stasm_NLANDMARKS * sizeof(float)); stasm_convert_shape(newlandmarks, 76); PrintLandmarks(newlandmarks, "stasm77 to stasm76"); #if 0 outimg = img.clone(); DrawLandmarks(outimg, newlandmarks, 76); imwrite("test_stasm_lib_76.bmp", outimg); #endif #if 0 memcpy(newlandmarks, landmarks, 2 * stasm_NLANDMARKS * sizeof(float)); stasm_convert_shape(newlandmarks, 22); PrintLandmarks(newlandmarks, "stasm77 to stasm22"); outimg = img.clone(); DrawLandmarks(outimg, newlandmarks, 22); imwrite("test_stasm_lib_22.bmp", outimg); memcpy(newlandmarks, landmarks, 2 * stasm_NLANDMARKS * sizeof(float)); stasm_convert_shape(newlandmarks, 20); PrintLandmarks(newlandmarks, "stasm77 to stasm20"); outimg = img.clone(); DrawLandmarks(outimg, newlandmarks, 20); imwrite("test_stasm_lib_20.bmp", outimg); #endif } return 0; // success }