IntelRealSense / librealsense

Intel® RealSense™ SDK
https://www.intelrealsense.com/
Apache License 2.0
7.63k stars 4.83k forks source link

Is there a Python version of this code available? #5323

Closed RajkiranVeldur closed 4 years ago

RajkiranVeldur commented 5 years ago

Required Info
Camera Model D435i
Firmware Version NA
Operating System & Version Ubuntu 18.04
Kernel Version (Linux Only) 5.0.0-36-generic
Platform PC
SDK Version NA
Language python3.6
Segment others

Face Antispoofing in Python

Can someone help me to conver the below chunk of code to Python. Appreciate your help.

bool find_depth_from(
    rs2::depth_frame const & frame,
    float const depth_scale,
    dlib::full_object_detection const & face,
    markup_68 markup_from, markup_68 markup_to,
    float * p_average_depth
)
{
    uint16_t const * data = reinterpret_cast<uint16_t const *>(frame.get_data());
    float average_depth = 0;
    size_t n_points = 0;
    for( int i = markup_from; i <= markup_to; ++i )
    {
        auto pt = face.part( i );
        auto depth_in_pixels = *(data + pt.y() * frame.get_width() + pt.x());
        if( !depth_in_pixels )
            continue;
        average_depth += depth_in_pixels * depth_scale;
        ++n_points;
    }
    if( !n_points )
        return false;
    if( p_average_depth )
        *p_average_depth = average_depth / n_points;
    return true;
}
RajkiranVeldur commented 4 years ago

Can someone tell me which pixel this line refers to?

auto depth_in_pixels = (data + pt.y() frame.get_width() + pt.x());

maloel commented 4 years ago

@RajkiranVeldur the depth frame data is stored in units that are called "pixels" here -- there's no standard convention that I know of, so that's the name that was used. Each "pixel" needs to then be scaled to an actual depth (in meters) by using the depth_scale.

RajkiranVeldur commented 4 years ago

@maloel thanks for the response. Does this mean that the variable will return the depth value of the landmark associated with it? For e.g., will it return x-depth value of nose_landmark?

maloel commented 4 years ago

@RajkiranVeldur The variable 'depth_in_pixels' is the depth (not in meters) of that specific landmark, yes. The function iterates thru the landmark indices you give it and averages their depth.

RajkiranVeldur commented 4 years ago

@maloel thanks for replying. We were trying to implement this algorithm using MTCNN instead of Dlib, yet it wasn't accurate. May I know, if you people have tried with any other face detection algorithm?

maloel commented 4 years ago

@RajkiranVeldur There's a pull request waiting right now that implements OpenVINO face detection (though without landmarks, which can be fairly easily added). I find the OpenVINO implementation (model) more robust (finds faces in a wider range of positions).

Check out their model zoo here.

RajkiranVeldur commented 4 years ago

@maloel OpenVINO is great, however we are more aligned towards MTCNN implementation for facial landmark detection but the accuracy is not even close to Dlib. Our main concern is we are planning to implement it at the edge. So, we are looking for a lightweight model and MTCNN is already lightweight.

Can you please guide us, how we can improve our accuracy with MTCNN based spoof-detection model?

maloel commented 4 years ago

Hi @RajkiranVeldur,

Can you please clarify what you mean by the accuracy issues? Are you referring to the accuracy of the depth you get from realsense, or the accuracy of MTCNN itself (such that the 5 points are off)?

Assuming the latter, it sounds like you need something more along the lines of DLIB's 68-point model which would provide many more points of reference. I did find one project with lightweight 68-point landmark extraction for javascript. Maybe the models it uses can be of use to you?

RajkiranVeldur commented 4 years ago

Hi @maloel yes, I was referring to the accuracy of using MTCNN with Realsense. Most of the time it was showing even a real person as Spoof. Might be, as you said it is because MTCNN gives us very fewer landmarks.

Also, may I get your's or anyone's email from the Realsense team? Actually, we would like to use Realsense with our Facial recognition product. If it is not possible to share here, can you please email me on rajkiran.veldur@techolution.com

Thank you.

dorodnic commented 4 years ago

Hi @RajkiranVeldur You can send me an email to sergey.dorodnicov@intel.com Any general questions about using the technology are better discussed here, so more people could benefit from it

RajkiranVeldur commented 4 years ago

Hi @dorodnic thank you for sharing the contact details. Can you please help me with the C++ code to display some text on the frame window, depending on the condition?

maloel commented 4 years ago

@RajkiranVeldur Putting text on a DLIB frame is a bit unintuitive. Check here. Let me know if it works :).

RajkiranVeldur commented 4 years ago

@dorodnic @maloel can you tell me what are the landmarks you people are considering for the facial features?

@RajkiranVeldur Putting text on a DLIB frame is a bit unintuitive. Check here. Let me know if it works :).

Hey, @maloel it worked. Thank you :)

RajkiranVeldur commented 4 years ago

@dorodnic @maloel can you please tell me what are the landmarks you are considering for the facial features? Ear, Nose, Eye, Chin and Mouth with respect to the image below.

facial_landmarks_68markup

RealSenseCustomerSupport commented 4 years ago

Hi @RajkiranVeldur

Is there anything further necessary on this thread?

*If we don’t hear from you in 7 days, this issue will be closed.