diff --git a/AITracker/src/model.cpp b/AITracker/src/model.cpp index 22b6fb8..3572796 100644 --- a/AITracker/src/model.cpp +++ b/AITracker/src/model.cpp @@ -31,8 +31,8 @@ Tracker::Tracker(std::unique_ptr&& solver, std::wstring& detecti // Face detector float score_threshold = .8f; - float nms_threshold = .3f; - int topK = 50; + float nms_threshold = .5f; + int topK = 7; face_detector = cv::FaceDetectorYN::create( std::string(detection_model_path.begin(), detection_model_path.end()), "", @@ -82,29 +82,29 @@ float inline logit(float p) return log(p) / 16.0f; } -float Tracker::get_distance_squared(float x0, float y0, float x1, float y1) +float StandardTracker::get_distance_squared(float x0, float y0, float x1, float y1) { // calculate distance squared. // no need to for sqrt to obtain the smallest distance for optimization - float x_distance = (x1 - x0); - float y_distance = (y1 - y0); + float x_distance = (x1 - x0); + float y_distance = (y1 - y0); float distance_squared = (x_distance * x_distance) + (y_distance * y_distance); return distance_squared; } -int Tracker::get_center_weighted_faces_row(const cv::Mat& image, const cv::Mat& faces) +int StandardTracker::get_center_weighted_faces_row(const cv::Mat& image, const cv::Mat& faces) { // get center coordinates for image float image_center_x = (float)(image.rows / 2); float image_center_y = (float)(image.cols / 2); float smallest_distance_squared = 0.0f; - int center_weighted_face_row = -1; + int center_weighted_face_row = -1; for (int row = 0; row < faces.rows; row++) { // get center coordinates for faces at row - float x0 = faces.at(row, 0); - float y0 = faces.at(row, 1); + float x0 = faces.at(row, 0); + float y0 = faces.at(row, 1); float face_w = faces.at(row, 2); float face_h = faces.at(row, 3); float face_center_x = x0 + (face_w / 2); @@ -113,14 +113,14 @@ int Tracker::get_center_weighted_faces_row(const cv::Mat& image, const cv::Mat& float distance_squared = get_distance_squared(image_center_x, image_center_y, face_center_x, face_center_y); if ((center_weighted_face_row == -1) || (distance_squared < smallest_distance_squared)) { - center_weighted_face_row = row; + center_weighted_face_row = row; smallest_distance_squared = distance_squared; } } return center_weighted_face_row; } -void Tracker::detect_face(const cv::Mat& image, FaceData& face_data) +void StandardTracker::detect_face(const cv::Mat& image, FaceData& face_data) { cv::Mat resized, faces; cv::resize(image, resized, cv::Size(224, 224), NULL, NULL, cv::INTER_LINEAR); @@ -147,8 +147,7 @@ void Tracker::detect_face(const cv::Mat& image, FaceData& face_data) float y0 = faces.at(faces_row, 1); float face_w = faces.at(faces_row, 2); float face_h = faces.at(faces_row, 3); - - + float w_ratio = (width / im_width); float h_ratio = (height / im_height); @@ -199,10 +198,21 @@ void Tracker::proc_face_detect(float* face, float width, float height) float w = face[2]; float h = face[3]; - int crop_x1 = (int)(x); - int crop_y1 = (int)(y); - int crop_x2 = (int)(x + w); - int crop_y2 = (int)(y + h + h * 0.1f); // force a little taller BB so the chin tends to be covered + /* Need to increase the boundary for face detection by 10% to compensate for increased size due to yaw, pitch and roll */ + /* But we also need a minimum amount of pixels for detection at farther distances where the face is smaller */ + int additional_width_margin = (int)(w * 0.1f); + int minimum_width_margin = (int)width / (4 * 10); + if (additional_width_margin < minimum_width_margin) + additional_width_margin = minimum_width_margin; + int additional_height_margin = (int)(h * 0.1f); + int minimum_height_margin = (int)height / (4 * 10); + if (additional_height_margin < minimum_height_margin) + additional_height_margin = minimum_height_margin; + + int crop_x1 = (int)(x - additional_width_margin); + int crop_y1 = (int)(y - additional_height_margin); + int crop_x2 = (int)(x + w + additional_width_margin); + int crop_y2 = (int)(y + h + additional_height_margin); // force a little taller BB so the chin tends to be covered face[0] = (float)std::max(0, crop_x1); face[1] = (float)std::max(0, crop_y1); diff --git a/AITracker/src/model.h b/AITracker/src/model.h index 739a834..a7a2804 100644 --- a/AITracker/src/model.h +++ b/AITracker/src/model.h @@ -48,7 +48,4 @@ class Tracker void proc_face_detect(float* face, float width = 1080, float height = 720); void detect_landmarks(const cv::Mat& image, int x0, int y0, float scale_x, float scale_y, FaceData& face_data); void proc_heatmaps(float* heatmaps, int x0, int y0, float scale_x, float scale_y, FaceData& face_data); - - float get_distance_squared(float x0, float y0, float x1, float y1); - int get_center_weighted_faces_row(const cv::Mat& image, const cv::Mat& faces); }; diff --git a/Client/src/camera/CameraFactory.cpp b/Client/src/camera/CameraFactory.cpp index d5bc034..3766e53 100644 --- a/Client/src/camera/CameraFactory.cpp +++ b/Client/src/camera/CameraFactory.cpp @@ -42,32 +42,41 @@ std::vector> CameraFactory::getCameras(CameraSettings& s { std::vector> cams; - // Search first for any PS3 camera. - try - { - cams.push_back(std::make_shared(640, 480, 60)); - cams[0]->set_settings(settings); - } - catch (...) - { - std::cout << "No PS3 camera available." << std::endl; - cams.clear(); - } - - for (int i = 0; i < 5; i++) { + bool bFoundPs3Camera = false; + // Search first for any PS3 camera. try { - std::shared_ptr c = std::make_shared(settings.width, settings.height, settings.fps, i); + std::shared_ptr c = std::make_shared(640, 480, 60); c->set_settings(settings); // Brightness / Exposure cams.push_back(std::move(c)); - std::cout << "Found ID: " << i << std::endl; + bFoundPs3Camera = true; + std::cout << "Found PS3 camera ID: " << i << std::endl; } - catch (const std::exception&) + catch (...) { - std::cout << "Not found device" << i << std::endl; } + + if (!bFoundPs3Camera) + { + // Then search or OCV Camera. + try + { + std::shared_ptr c = std::make_shared(settings.width, settings.height, settings.fps, i); + c->set_settings(settings); // Brightness / Exposure + cams.push_back(std::move(c)); + std::cout << "Found OCV camera ID: " << i << std::endl; + } + catch (const std::exception&) + { + } + } + + } + if (cams.size() == 0) + { + std::cout << "No cameras found" << std::endl; } return cams;