Finding largest detected rectangle

Is there a way of detecting and finding the width of only the largest rectangle in the cameras field of vision?

The attached image shows two rectangles being detected, however, I require only the windscreen (largest width rectangle) of the vehicle to be detected at various distances from the camera.



img = sensor.snapshot()
img.draw_rectangle((90, 70, 130, 90), color = (255, 0, 0), thickness = 2)

for r in img.find_rects(roi= (90, 70, 130, 90), threshold=35000):

img.draw_rectangle(r.rect(), color = (0, 255, 0), thickness = 2)

Save the output of find rects in a list.

Then r_max = max(rect_list, key = lambda x: x[2] * x[3])

Because the system is programmable in Python everything you know about Python applies.