Hello.
Camera image recognition is an ever-expanding industry.
With A.I. today human faces, animals and vehicles are easily recognized.
To calculate the distance of an object or an obstacle today there are several techniques. Indeed, there are special cameras with a processor dedicated to A.I.
One technique is to discriminate a certain object and then calculate its surface (even a circumscribed surface). If this surface increases, it means that the object is approaching.
For small spaces, ultrasonic capsules can be used. Together with appropriate ICs and MCUs, they are able to establish the distance of the obstacle and, consequently, change the color of the image.