Find distance of object along a line

I used morphology in my code but I didn’t use background subtraction and thresholding. What if I only had this image only and I needed to make it work with just this image? How would I get the output I want with just this image at my disposal and not having the ability to take pictures without the box and so on?

Moreover, there are stray wires and glare in the image in my SO question which the findContours function is detecting. I am not sure atm how I can get my program to ignore those.