In this tutorial, we will talk about how to do Distance detection with Depth Camera (Intel Realsense d435i).
Our goal is to detect the depth of the frame.
Intel Realsense d435i can be used for various needs, such as detecting 3D objects. for all the technical details I refer you to the intel site.

In this tutorial we are going to focus on distance detection on two steps:

• Get Frames from Depth camera
• Integration with OpenCV to detect distance on mouse hover

Before starting with the tutorial:

Libraries installation:

Before proceeding with the development of our application, we must take care of the various installations. Obviously, we will need OpenCV (if you haven’t already read How to install Python 3 and Opencv 4 on Windows) and the real sense libraries.

The installation of the libraries for distance detection with a Depth Camera is very simple and can only be done from the terminal. Copy and paste the following commanders, one at a time:

`pip install opencv-python`
`pip install pyrealsense2`

1. Get frames from Depth Camera

You can check if everything went well by importing the libraries and running the code:

```import cv2
import pyrealsense2```

Well, now everything is ready to start. To make everything simpler I have already written a class that processes the communication with realsense, (you can find everything in the file to download) you just need to download the realsense_depth.py file and import it into the project.

`from realsense_depth import *`

Initializing the camera to collect the data stream is the first step. In the code below here is the portion of code that represents it:

```# Initialize Camera Intel Realsense
dc = DepthCamera()

ret, depth_frame, color_frame = dc.get_frame()```

To show the distance and see a point on the image it will be sufficient to use some function of OpenCV in combination with the extracted data.

``` # Show distance for a specific point
point = (400,300)
cv2.circle(color_frame, point, 4, (0, 0, 255))
distance = depth_frame[point[1], point[0]]

cv2.putText(color_frame, "{}mm".format(distance), (point[0], point[1] - 20), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 0), 2)```

With only these lines we have reached the result, we can see the distance between a specific point of the frame and the intel realsense camera.

2. Get distance on Mouseover (with Opencv)

As we saw in the previous part, we managed to detect the distance of a point for a single frame. Now the first thing to do is to write a function that allows us to obtain the position of a point on the image with just the mouse passage. Nothing complicated, here is the code:

```def show_distance(event, x, y, args, params):
global point
point = (x, y)

# Create mouse event
cv2.namedWindow("Color frame")
cv2.setMouseCallback("Color frame", show_distance)```

By putting together the collected information and this piece of code in a While loop, to make sure that it is always active, we are able to obtain our distance in real-time.

As you can see in the example image below, the mouse cursor indicates a point and the text indicates 485 mm which is exactly the distance between the camera and the indicated point.