Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make examples more easy to use. #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion FaceAlignment/include/face_alignment.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class FaceAlignment{
* @param model_path Path of the model file, either absolute or relative to
* the working directory.
*/
SEETA_API FaceAlignment(const char* model_path = NULL);
SEETA_API FaceAlignment(const char* model_path = nullptr);

/** A Destructor which should never be called explicitly.
* Release all dynamically allocated resources.
Expand Down
17 changes: 13 additions & 4 deletions FaceAlignment/src/cfan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#include "cfan.h"
#include <string.h>

#include <algorithm>

using namespace std;

/** A constructor.
* Initialize basic parameters.
*/
Expand Down Expand Up @@ -106,7 +110,12 @@ CCFAN::~CCFAN(void)
void CCFAN::InitModel(const char *model_path)
{
/*Open the model file*/
#if defined(_MSC_VER)
FILE *fp = nullptr;
fopen_s(&fp, model_path, "rb+");
#else
FILE *fp = fopen(model_path, "rb+");
#endif
mean_shape_ = new float[pts_num_ * 2];
fread(mean_shape_, sizeof(float), pts_num_ * 2, fp);

Expand Down Expand Up @@ -167,10 +176,10 @@ void CCFAN::FacialPointLocate(const unsigned char *gray_im, int im_width, int im
float extend_revised_y = 0.05;

/*Compute the extended region of the detected face*/
int extend_lx = std::max(floor(left_x - extend_factor*bbox_w), double(0));
int extend_rx = std::min(floor(right_x + extend_factor*bbox_w), double(im_width - 1));
int extend_ly = std::max(floor(left_y - (extend_factor - extend_revised_y)*bbox_h), double(0));
int extend_ry = std::min(floor(right_y + (extend_factor + extend_revised_y)*bbox_h), double(im_height - 1));
int extend_lx = max((int)floor(left_x - extend_factor*bbox_w), 0);
int extend_rx = min((int)floor(right_x + extend_factor*bbox_w), im_width - 1);
int extend_ly = max((int)floor(left_y - (extend_factor - extend_revised_y)*bbox_h), 0);
int extend_ry = min((int)floor(right_y + (extend_factor + extend_revised_y)*bbox_h), im_height - 1);

int face_w = extend_rx - extend_lx + 1;
int face_h = extend_ry - extend_ly + 1;
Expand Down
123 changes: 76 additions & 47 deletions FaceAlignment/src/test/face_alignment_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,79 +34,108 @@
#include <iostream>
#include <string>

#include "cv.h"
#include "highgui.h"
#ifdef _WIN32
#pragma once
#include <opencv2/core/version.hpp>

#define CV_VERSION_ID CVAUX_STR(CV_MAJOR_VERSION) CVAUX_STR(CV_MINOR_VERSION) \
CVAUX_STR(CV_SUBMINOR_VERSION)

#ifdef _DEBUG
#define cvLIB(name) "opencv_" name CV_VERSION_ID "d"
#else
#define cvLIB(name) "opencv_" name CV_VERSION_ID
#endif //_DEBUG

#pragma comment( lib, cvLIB("core") )
#pragma comment( lib, cvLIB("imgproc") )
#pragma comment( lib, cvLIB("highgui") )

#endif //_WIN32

#include <opencv/cv.h>
#include <opencv/highgui.h>

#include "face_detection.h"
#include "face_alignment.h"

#ifdef _WIN32
std::string MODEL_DIR = "../../FaceDetection/model/";
std::string ALIENMENT_MODEL_DIR = "../../FaceAlignment/model/";
#else
std::string MODEL_DIR = "../../../FaceDetection/model/";
std::string ALIENMENT_MODEL_DIR = "../../../FaceAlignment/model/";
#endif

#define PI 3.14159265

int main(int argc, char** argv)
{
const char* img_path = "test_image.jpg";
if (argc >= 2) {
img_path = argv[1];
}

// Initialize face detection model
seeta::FaceDetection detector("seeta_fd_frontal_v1.0.bin");
seeta::FaceDetection detector((MODEL_DIR + "seeta_fd_frontal_v1.0.bin").c_str());
detector.SetMinFaceSize(40);
detector.SetScoreThresh(2.f);
detector.SetImagePyramidScaleFactor(0.8f);
detector.SetWindowStep(4, 4);

// Initialize face alignment model
seeta::FaceAlignment point_detector("../model/seeta_fa_v1.1.bin");
seeta::FaceAlignment point_detector((ALIENMENT_MODEL_DIR + "seeta_fa_v1.1.bin").c_str());

//load image
IplImage *img_grayscale = NULL;
img_grayscale = cvLoadImage("../data/image_0001.png", 0);
if (img_grayscale == NULL)
cv::Mat img = cv::imread(img_path, cv::IMREAD_UNCHANGED);
if (!img.data)
{
return 0;
std::cerr << "Could not open or find the image" << std::endl;
return -1;
}

IplImage *img_color = cvLoadImage("../data/image_0001.png", 1);
cv::Mat img_gray;
if (img.channels() != 1)
cv::cvtColor(img, img_gray, cv::COLOR_BGR2GRAY);
else
img_gray = img;

int pts_num = 5;
int im_width = img_grayscale->width;
int im_height = img_grayscale->height;
unsigned char* data = new unsigned char[im_width * im_height];
unsigned char* data_ptr = data;
unsigned char* image_data_ptr = (unsigned char*)img_grayscale->imageData;
int h = 0;
for (h = 0; h < im_height; h++) {
memcpy(data_ptr, image_data_ptr, im_width);
data_ptr += im_width;
image_data_ptr += img_grayscale->widthStep;
}

seeta::ImageData image_data;
image_data.data = data;
image_data.width = im_width;
image_data.height = im_height;
image_data.num_channels = 1;
seeta::ImageData img_data;
img_data.data = img_gray.data;
img_data.width = img_gray.cols;
img_data.height = img_gray.rows;
img_data.num_channels = 1;

// Detect faces
std::vector<seeta::FaceInfo> faces = detector.Detect(image_data);
int32_t face_num = static_cast<int32_t>(faces.size());

if (face_num == 0)
{
delete[]data;
cvReleaseImage(&img_grayscale);
cvReleaseImage(&img_color);
return 0;
std::vector<seeta::FaceInfo> faces = detector.Detect(img_data);
if (faces.empty()) {
return -2;
}

// Detect 5 facial landmarks
seeta::FacialLandmark points[5];
point_detector.PointDetectLandmarks(image_data, faces[0], points);
for (auto face : faces) {
// Detect 5 facial landmarks
seeta::FacialLandmark points[5];
point_detector.PointDetectLandmarks(img_data, face, points);

// Visualize the results
cvRectangle(img_color, cvPoint(faces[0].bbox.x, faces[0].bbox.y), cvPoint(faces[0].bbox.x + faces[0].bbox.width - 1, faces[0].bbox.y + faces[0].bbox.height - 1), CV_RGB(255, 0, 0));
for (int i = 0; i<pts_num; i++)
{
cvCircle(img_color, cvPoint(points[i].x, points[i].y), 2, CV_RGB(0, 255, 0), CV_FILLED);
// Visualize the results
cv::RotatedRect rrect = cv::RotatedRect(cv::Point(face.bbox.x + face.bbox.width / 2, face.bbox.y + face.bbox.height / 2), cv::Size(face.bbox.width, face.bbox.height), atan((points[1].y - points[0].y) / (points[1].x - points[0].x)) * 180 / PI);
cv::Point2f vertices[4];
rrect.points(vertices);
for (int i = 0; i < 4; i++)
cv::line(img, vertices[i], vertices[(i + 1) % 4], CV_RGB(255, 0, 0), 2, CV_AA);

for (int i = 0; i < pts_num; i++)
{
cv::circle(img, cvPoint(points[i].x, points[i].y), 2, CV_RGB(0, 255, 0), CV_FILLED);
}
}
cvSaveImage("result.jpg", img_color);

// Show crop face
cv::imshow("Alignment Face", img);
cv::waitKey(0);
cv::destroyWindow("Source Face");

// Release memory
cvReleaseImage(&img_color);
cvReleaseImage(&img_grayscale);
delete[]data;
return 0;
}
45 changes: 34 additions & 11 deletions FaceDetection/src/test/facedetection_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,53 @@
#include <iostream>
#include <string>

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#ifdef _WIN32
#pragma once
#include <opencv2/core/version.hpp>

#define CV_VERSION_ID CVAUX_STR(CV_MAJOR_VERSION) CVAUX_STR(CV_MINOR_VERSION) \
CVAUX_STR(CV_SUBMINOR_VERSION)

#ifdef _DEBUG
#define cvLIB(name) "opencv_" name CV_VERSION_ID "d"
#else
#define cvLIB(name) "opencv_" name CV_VERSION_ID
#endif //_DEBUG

#pragma comment( lib, cvLIB("core") )
#pragma comment( lib, cvLIB("imgproc") )
#pragma comment( lib, cvLIB("highgui") )

#endif //_WIN32

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include "face_detection.h"

using namespace std;
#ifdef _WIN32
std::string MODEL_DIR = "../../FaceDetection/model/";
#else
std::string DATA_DIR = "../../../FaceDetection/data/";
#endif

int main(int argc, char** argv) {
if (argc < 3) {
cout << "Usage: " << argv[0]
<< " image_path model_path"
<< endl;
return -1;
const char* img_path = "test_image.jpg";
if (argc >= 2) {
img_path = argv[1];
}

const char* img_path = argv[1];
seeta::FaceDetection detector(argv[2]);
seeta::FaceDetection detector((MODEL_DIR + "seeta_fd_frontal_v1.0.bin").c_str());

detector.SetMinFaceSize(40);
detector.SetScoreThresh(2.f);
detector.SetImagePyramidScaleFactor(0.8f);
detector.SetWindowStep(4, 4);

cv::Mat img = cv::imread(img_path, cv::IMREAD_UNCHANGED);
if (!img.data) {
std::cerr << "Could not open or find the image" << std::endl;
return -1;
}
cv::Mat img_gray;

if (img.channels() != 1)
Expand Down
47 changes: 0 additions & 47 deletions FaceIdentification/examples/examples.sln

This file was deleted.

Binary file removed FaceIdentification/examples/examples.v12.suo
Binary file not shown.
10 changes: 6 additions & 4 deletions FaceIdentification/src/test/test_face_recognizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ using namespace seeta;
#define EXPECT_EQ(a, b) if ((a) != (b)) std::cout << "ERROR: "

#ifdef _WIN32
std::string DATA_DIR = "../../data/";
std::string MODEL_DIR = "../../model/";
std::string DATA_DIR = "../../FaceIdentification/data/";
std::string MODEL_DIR = "../../FaceIdentification/model/";
#else
std::string DATA_DIR = "./data/";
std::string MODEL_DIR = "./model/";
std::string DATA_DIR = "./FaceIdentification/data/";
std::string MODEL_DIR = "./FaceIdentification/model/";
#endif
void TEST(FaceRecognizerTest, CropFace) {
FaceIdentification face_recognizer((MODEL_DIR + "seeta_fr_v1.0.bin").c_str());
Expand Down Expand Up @@ -130,8 +130,10 @@ void TEST(FaceRecognizerTest, CropFace) {
face_recognizer.CropFace(src_img_data, pt5, dst_img_data);
count += clock() - start;
// Show crop face
// cv::imshow("Source Face", src_img);
// cv::imshow("Crop Face", dst_img);
// cv::waitKey(0);
// cv::destroyWindow("Source Face");
// cv::destroyWindow("Crop Face");
}
ifs.close();
Expand Down
Loading