# How to grab pixel coordinates of detected face/eyes?

I've got face and eye detection/tracking running and I'm interested in having OpenCV provide the coordinates of the initial cv2.rectangle corner coordinates. The rectangle is built using frame:

 cv2.rectangle(frame, (rect[0], rect[1]), (rect[2], rect[3]), (0, 255, 0), 2)


Is rect[0], rect[1] the "upper left" corner of the rectangle, and rect[3], rect[4] the "lower right" corner?

My goal is to have these parameters drive servos that will keep the moving face "centered" by having the camera move based on these variable values (as a relative position to center image coordinates less rectangle dimension offset).

Dave

edit retag close merge delete

Sort by » oldest newest most voted

(rect[0], rect[1]) yes those are exactly upper left and,.. , (rect[2], rect[3]) are the lower left corners..

you can just do the following

xFaceCenter=(rect[2]-rect[0])/2
yFaceCenter=(rect[3]-rect[1])/2


and now you have the height and width of the image frame lets say h,w then you can easily use

dy=(h/2)-yFaceCenter
dx=(w/2)-xFaceCenter


So you now just have to make these dy and dx values to be zero by changing the horizontal and vertical servo angle accordingly

more

As stated by yourself, the coordinates of the bounding box of any returned face/eye detection is of the form x_upper_left_corner y_upper_left_corner width height keeping in mind that the origin of OpenCV is at the upper left image corner.

You can easily use these parameters to apply some normalization of faces, as discussed in OpenCV 3 Blueprints, chapter 6, and as seen in the code for that chapter, right here.

more

Official site

GitHub

Wiki

Documentation