Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save StevenPuttemans/dbde59f8efd238c496f6247ee1557db9 to your computer and use it in GitHub Desktop.
Save StevenPuttemans/dbde59f8efd238c496f6247ee1557db9 to your computer and use it in GitHub Desktop.
testcode
/// Initialize the tracking interface
cv::MultiTracker faceTrackers("KCF");
vector< cv::Rect > objectsToTrack;
while(true){
capture >> frame;
std::vector< cv::Rect > dets_OpenCV;
model.detectMultiScale(frame, dets_OpenCV, 1.2, 3);
/// Intialize trackers if non exist yet
/// The tracker wants double valued rectangles
if(dets_OpenCV.size() > 0 || dets_OpenCV.size() != objectsToTrack.size()){
std::vector< cv::Rect2d > dets_for_tracker;
for(size_t i = 0; i < dets_OpenCV.size(); i++){
cv::Rect2d temp((double)dets_OpenCV[i].x, (double)dets_OpenCV[i].y, (double)dets_OpenCV[i].width, (double)dets_OpenCV[i].height);
dets_for_tracker.push_back(temp);
}
if(faceTrackers.objects.size() == 0){
faceTrackers.add(frame, dets_for_tracker);
/// Else update and visualise the trackers
}else{
faceTrackers.update(frame, dets_for_tracker);
for(size_t i = 0; i < faceTrackers.objects.size(); i++){
cv::drawMarker(frame, cv::Point(faceTrackers.objects[i].x + faceTrackers.objects[i].width/2, faceTrackers.objects[i].y + faceTrackers.objects[i].height/2), cv::Scalar(0,0,255));
}
}
}
std::vector<dlib::full_object_detection> shapes;
std::vector<dlib::rectangle> dets_dlib;
/// Convert the image into the dlib required format
dlib::cv_image<dlib::bgr_pixel> img_dlib(frame);
visualize = img_dlib;
for(size_t i = 0; i < dets_OpenCV.size(); i ++){
/// Since OpenCV is using the inner face, the model fitting might go wrong, thats why we want to add a 25% padding in each direction
/// Convert dets_OpenCV to dets_dlib format
int x1 = dets_OpenCV[i].tl().x - (int)((double)dets_OpenCV[i].width * 0.3);
int y1 = dets_OpenCV[i].tl().y - (int)((double)dets_OpenCV[i].height * 0.3);
int x2 = dets_OpenCV[i].br().x + (int)((double)dets_OpenCV[i].width * 0.3);
int y2 = dets_OpenCV[i].br().y + (int)((double)dets_OpenCV[i].height * 0.3);
dlib::rectangle current_det(x1, y1, x2, y2);
/// Find the facial features inside the given rectangle and store them
dlib::full_object_detection shape = sp(img_dlib, current_det);
dets_dlib.push_back(current_det);
shapes.push_back(shape);
}
/// Retrieve the points of the shapes and
win.clear_overlay();
win.set_image(visualize);
win.add_overlay(dets_dlib, dlib::rgb_pixel(255,0,0));
win.add_overlay(dlib::render_face_detections(shapes));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment