# Detect object’s distance with LIDAR camera Intel Realsense L515

We analyze the intel real sense lidar camera L515 and write the code to take the distance of a specific object from the camera. The software used to measure distance will also work with Intel Realsense D435i

## Measure object distance, and differences between the two realsense camera

The two depth cameras have several differences, but it all depends on what information we need to obtain and their use. The LIDAR camera is very precise and works in a wider range, starting from 25 cm up to 9 meters, and has an error rate of a few millimeters. The other realsense is more sensitive to environmental conditions and can measure distances up to 3 meters. Another factor that distinguishes them is the price, the Realsense L515 costs twice as much as the d435i

## Object Distance software

I have prepared a code to use the Intel RealSense L515 camera with Python but it can also be used with the other RealSense. I created functions to simplify the less interesting steps for this project. We can divide the entire procedure into 3 steps:
1. Code to connect the camera
2. Object detection
3. Object Distance of a specific object

### 1. Code to connect the camera

As a first step we must connect the camera with the Python code and import all the libraries, in this way, it will be possible to recover the frame from the camera but not yet obtain the Object Distance.

```from realsense_camera import RealsenseCamera
from object_detection import ObjectDetection
import cv2

# Create the Camera object
camera = RealsenseCamera()

# Create the Object Detection object
object_detection = ObjectDetection()

while True:
# Get frame from realsense camera
ret, color_image, depth_image = camera.get_frame_stream()

# show image
cv2.imshow("depth Image", depth_image)
key = cv2.waitKey(1)
if key == 27:
break

# release the camera
camera.release()
cv2.destroyAllWindows()```

With OpenCV, we have to identify a precise point on the screen and pass the coordinates to the camera to get the distance to the precise point

```
...

# Get center of the bbox
cx, cy = (x + x2) // 2, (y + y2) // 2
distance = camera.get_distance_point(depth_image, cx, cy)

# Draw circle
cv2.circle(color_image, (cx, cy), 5, color, -1)
cv2.putText(color_image, f"Distance: {distance} cm", (cx, cy + 20),
cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2, cv2.LINE_AA)

...

```

this is the result, in the image I indicate the red point in which the distance measurement takes place

### 2. Object Detection

To have the distance of a specific object it is necessary to identify the objects present in the image. To do this we use an object detection algorithm and insert it into the code.

```   # Get the object detection
bboxes, class_ids, score = object_detection.detect(color_image)
for bbox, class_id, score in zip(bboxes, class_ids, score):
x, y, x2, y2 = bbox
color = object_detection.colors[class_id]
cv2.rectangle(color_image, (x, y), (x2, y2), color, 2)

# display name
class_name = object_detection.classes[class_id]
cv2.putText(color_image, f"{class_name}", (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2, cv2.LINE_AA)```

here is the result: the bounding boxes on the identified objects

### Object Distance of a specific object

As soon as the objects have been identified, it is necessary to identify a point inside the bounding box, exactly as in this photo, Cx, Cy represent the coordinates of the point.

With this line we calculate the center of the box and pass the coordinates to the camera

```...

# Get center of the bbox
cx, cy = (x + x2) // 2, (y + y2) // 2
distance = camera.get_distance_point(depth_image, cx, cy)

...```

this is the final result of the entire exercise

## Final thoughts

I would like to point out that this is just an exercise on how to use and define the distance of the object with the realsense camera L515. In real use or industrial conditions, some problems may arise that need to be resolved.

For example, if we wanted to identify the distance of an irregular object like the easel in the photo,you should use segmentation to find points only in the area I highlighted in yellow

Another problem could be black objects from which distance information cannot be obtained, so this aspect must also be taken into consideration.

In conclusion, the lidar depth camera is an excellent product if used after a careful study of its application in the environment of use

## Learn to build Computer Vision Software easily and efficiently.

This is a FREE Workshop where I'm going to break down the 4 steps that are necessary to build software to detect and track any object.