Measuring the distance to a detected face

xgarbxgarb
Posts: 21
Joined: Fri Jan 25, 2019 3:36 pm

Measuring the distance to a detected face

Postby xgarbxgarb » Tue Feb 18, 2020 11:53 am

I'm trying to calculate the distance to a detected face using the measurements from the net_boxes array.

I found this calculation from https://photo.stackexchange.com/questio ... in-a-photo

Image

Using the measurements:
Focal length: 45
Real face height: 200mm
Image height : 240px (QVGA)
h (the pixel height of the face from the net_boxes array)
Sensor height: 2.7mm (1/4 inch sensor)

and using code similar to below

Code: Select all

static void draw_face_boxes(dl_matrix3du_t *image_matrix, box_array_t *boxes)
{
  int h, i;
  for (i = 0; i < boxes->len; i++) {

    h = (int)boxes->box[i].box_p[3] - y + 1;

    int eq_top = 45 * 200 * 240; //f(mm) x real height(mm) x image height(px)
    int eq_bottom = h * 2.7; //object height(px) x sensor height(mm)

    int face_distance = eq_top/eq_bottom;

    Serial.println(face_distance);
}
The results aren't accurate.

Is one of the measurements incorrect or is this an optics issue?

xgarbxgarb
Posts: 21
Joined: Fri Jan 25, 2019 3:36 pm

Re: Measuring the distance to a detected face

Postby xgarbxgarb » Fri Apr 10, 2020 6:27 pm

OK... got confused between focal length and view angle on the lens.

Focal length appears to be 3.6 on the camera so calculation code is:

Code: Select all

int eq_top = 3.6 * 200 * 240; // f(mm) x real height(mm) x image height(px)
int eq_bottom = smoothed_face_height * 2.7; //object height(px) x sensor height(mm)
int face_distance = eq_top / eq_bottom;
I used this code for an ESP32-CAM in my face tracking robot project here: https://robotzero.one/face-tracking-robot/

Who is online

Users browsing this forum: No registered users and 12 guests