title: How to crop images depending on the position of the face?
body: <p>I have such data:  x / y center of the face, height and width of the face, height and width of the original image. User uploads images where a face can be in perfectly different places and the aspect ratio of the image may be different.I determine if there is a face using openCV:</p>
  
  <pre><code>def crop_face(img, margin_x = 550, margin_y = 400):
  image = cv2.imread(img)
  height, width = image.shape[:2]
  #print('width - {0}, height - {1}'.format(width, height))
  face_cascade = cv2.CascadeClassifier(FACE_DETECTOR_PATH)
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  faces = face_cascade.detectMultiScale(gray, 1.29, 5)
  print ("Found {0} faces!".format(len(faces)))
  if len(faces) == 1:     
      for (x,y,w,h) in faces:                 
          r = max(w, h) / 2
          centerx = x + w / 2
          centery = y + h / 2
          nx = int(centerx - r)
          ny = int(centery - r)
          nr = int(r * 2)
          print ('x - {0}, y - {1}, w - {2}, h - {3}'.format(x,y,w,h))
          print ('r - {0}, centerX - {1}, centerY - {2}, nx - {3}, ny - {4},nr - {5}'.format(r,centerx,centery,nx,ny, nr))
          margin_dict = get_normal_margin(margin_x, margin_y ,centerx, centery, height,width)
          margin_dict['x_max'] -= int(w/2);
          margin_dict['x_min'] -= int(w/2);
          margin_dict['y_max'] -= int(h/2);
          margin_dict['y_min'] -= int(h/2);
          roi_color = image[y-margin_dict['y_min']:y + h+margin_dict['y_max'], x-margin_dict['x_min']:x + w +margin_dict['x_max']]#y max, y min, x max, x min
          #roi_color = image[y:y+h, x:x+w]
          return roi_color
  </code></pre>
  
  <p>so trying to get the maximum allowable indent for x and y:</p>
  
  <pre><code>def get_normal_margin(margin_x, margin_y, center_x, center_y, height, width):
  margin_to_return = {'y_max': margin_y, 'y_min': margin_y, 'x_max': margin_x, 'x_min': margin_x}
  for x in range(int(center_x),  width - int(center_x)):      
      if(x &lt; margin_x and x &gt; 0):
          margin_to_return['x_max'] = x           
  for x in reversed(range(int(center_x),  width - int(center_x))):
      if(x &lt; margin_x and x &gt; 0):     
          margin_to_return['x_min'] = x
  for x in range(int(center_y),  height - int(center_y)):     
      if(x &lt; margin_y and x &gt; 0):
              margin_to_return['y_max'] = x
  for x in reversed(range(int(center_y),  height - int(center_y))):
      if(x &lt; margin_y and x &gt; 0):
          margin_to_return['y_min'] = x           
  return margin_to_return
  </code></pre>
  
  <p>but it does not work. some images cuts off nomral, some are completely abnormal.
  here are examples:
  <a href="https://imgur.com/a/BOebwgn" rel="nofollow noreferrer">I give the entrance</a>
  <a href="https://imgur.com/a/hcSEb8R" rel="nofollow noreferrer">getting output</a></p>
  
  <p><a href="https://imgur.com/a/BYI4nmd" rel="nofollow noreferrer">here is an example of what is needed</a></p>
  
  <p>Am I on the right track or not at all?</p>