OpenCV drawing with depth

Solution 1:

This seems to be what you are asking, but it might not behave as you intend.

Create a list above of the while-loop. Also add a boolean variable (optional - see below)

rectangles = []
detected = False

while True:

When the distance is small enough, add the coordinates of the rectangle to the list - use a tuple. Use the detected variable to prevent adding a rectangle on the same location every frame. A rectangle is only added on the first frame something is in range. Remove that code if you intend to track something across the screen.

if hight_over_table > 0:
    if not detected:
        detected = True
        rectangles.append(((x1_bounding_box,y1_bounding_box),(x2_bounding_box,y2_bounding_box)))
else: 
    detected = False
    print("test")

Each frame, draw all rectangles before displaying the image

for p1, p2 in rectangles:
    cv2.rectangle(rgb_color_image,p1,p2,(0,255,0),3)

cv2.imshow("RGB Stream", rgb_color_image)

Note that the rectangles are drawn to the screen - not to a surface. If you point the camera to the sky, the rectangles will still be there. It is technically possible to stick the rects to a surface, but that is much more complicated.