How to identify the correct coordinates for cropping image

I am using a face detection library(retina-face) to detect face on the following image

The library being used is retina-face which gives bounding box coordinates in the format (x1,y1,x2,y2).
The Bounding box for the face is as follows
face_area=[242, 92, 269, 129]

Now,when I run the following part of the code,I am able to draw the bounding box on the image to represent the face

img=cv2.imread('model_2_0.jpg')
img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
cv2.rectangle(img,(face_area[2],face_area[3]),(face_area[0],face_area[1]),(255,0,0),1)

The result of this is as follows
img_bbox

But when I use slicing to just obtain the face part of the image either I run into zero-sized array error or I get an incorrect result.
I am trying to crop as follows

face=img[face_area[1]:face_area[0],face_area[3]:face_area[2])

The incorrect crop is shown below
crop_incorrect

Any idea what I am doing wrong here,and is there a better way to crop rather than using slicing

you just got the order of the coords wrong,
if you get (x1, y1, x2, y2) from the detection, the correct numpy slice is::

img[y1:y2, x1:x2] 

also your cv2.rectangle() coords should better be:

(x1,y1), (x2,y2)
1 Like

Thanks for the tip,I was able to get the result.On a side note the cropped image tends to be extremely coarse as shown below
cropped_face_mediapipe_Nupsample
I am currently upsampling it using cv2.pyrUp and then resize it to (256,256) to get this image
cropped_face_mediapipe_upsample
Would you recommend that’s the correct approach. I am looking for a better image where the eyes can be clearly seen as shown in the original image