How can I get the nearest entity in python
My code:
from mss import mss
import math
import cv2
import numpy as np
import torc

with mss() as sct:
   monitor = {&quot;top&quot;: 220, &quot;left&quot;: 640, &quot;width&quot;: 640, &quot;height&quot;:640}    

while True:
    screenshot = np.array(sct.grab(monitor))
    results = model(screenshot, size=600)
    df = results.pandas().xyxy[0]
    distances = [] 
    closest = 1000
                      
       try:

          xmin = int(df.iloc[0, 0])
          ymin = int(df.iloc[0, 1])
          xmax = int(df.iloc[0, 2])
          ymax = int(df.iloc[0, 3])
          
          centerX = (xmax + xmin) / 2 + xmin
          centerY = (ymax + ymin) / 2 + ymin
          
          distance2 = math.sqrt(((centerX - 320) ** 2) + ((centerY - 320) ** 2))
          distances.append(distance2)
          if closest &gt; distances[i]:
              closest = distances[i]
              closestEnemy = i

Only problem now is that it doesn't seem to get the closest enemy, is my math wrong? If my math should be wrong, how can I improve it? Also if my math is correct, how can I improve it in order to achieve my goal of getting the nearest entity? Any help will be very appriciated. Thanks in advance to everyone who invests his / her time in helping me :)

It's not entirely clear, what you are after... but my guess is, that there is a small mistake when calculating the center of the enemies. Either use:
centerX = (xmax + xmin) / 2  # do not add xmin here
centerY = (ymax + ymin) / 2  # do not add ymin here

or calculate the distance between the minimum and maximum values and add the minim again:
centerX = (xmax - xmin) / 2 + xmin  # subtract minimum from maximum
centerY = (ymax - ymin) / 2 + ymin # subtract minimum from maximum

Additional remark:
Performance wise it is mostly not a good idea to iterate over a pandas data frame. Another approach is to add a new column distance to the data frame and then search for the index of the minimum value:
df['distance'] = (
    (( (df['xmax']+df['xmin'])/2 - 320) ** 2) + 
    (( (df['ymax']+df['ymin'])/2 - 320) ** 2)
    ) **0.5
         
closest_enemy = df['distance'].idxmin()

