Downloaded Tflite MiDas model from Kaggle.
Looked at a few articles for MiDas and Monocular Depth Estimation(Basically finding the distance of an object from the camera using pixels)
This is for object detection using bounding boxes.
I tried to get the centre of the bounding boxes and get the distance from there. and tried to fix a few errors but
I am still unsure whether I have done all the right steps or implemented it correctly. – Pls check If I forgot to do something.
Example output: (I was right in front of the camera)
Min depth value: 88.5720443725586, Max depth value: 911.3470458984375 Depth value at (126, 127): 248.00 meters Min depth value: 76.10022735595703, Max depth value: 920.8251342773438 Depth value at (126, 127): 246.00 meters Min depth value: 72.52698516845703, Max depth value: 916.8196411132812 Depth value at (126, 127): 246.00 meters Min depth value: 68.72769165039062, Max depth value: 908.2048950195312 Depth value at (126, 127): 247.00 meters Min depth value: 67.79693603515625, Max depth value: 898.6953125 Depth value at (126, 127): 248.00 meters Min depth value: 69.44243621826172, Max depth value: 902.6591186523438 Depth value at (126, 127): 250.00 meters Min depth value: 78.3673324584961, Max depth value: 914.4412231445312 Depth value at (126, 127): 249.00 meters
Code below:
#Loading the model
interpreter = tf.lite.Interpreter(model_path="/Users/Taparia/Desktop/ObjectDetection/1.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
#Loading video
cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,720)
cap.set(10,70)
while True:
success,img = cap.read()
if not success: break
classIds, confs, bbox = net.detect(img,confThreshold=thres)
#print(classIds,bbox)
#Preparing image for distance measurement
img_rgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img_rgb_resized = cv2.resize(img_rgb,(256,256))
img_tensor = np.expand_dims(img_rgb_resized.astype(np.float32)/255.0, axis=0)
#Estimate depth
interpreter.set_tensor(input_details[0]['index'], img_tensor)
interpreter.invoke()
depth_map = interpreter.get_tensor(output_details[0]['index'])[0]
print(f"Min depth value: {np.min(depth_map)}, Max depth value: {np.max(depth_map)}")
#Normalize depth map for visualization
depth_normalized = cv2.normalize(depth_map,None,alpha=0, beta=255,norm_type=cv2.NORM_MINMAX)
depth_normalized = np.uint8(depth_normalized)
if len(classIds) != 0:
for classId, confidence, box in zip(classIds.flatten(), confs.flatten(), bbox):
class_name = classNames[classId - 1].lower()
if class_name in useful_objects:
cv2.rectangle(img, box, (0, 0, 255), 1) #Draws box around object
cv2.putText(img, classNames[classId - 1].upper(), (box[0] + 10, box[1] + 30),
cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1)
x_start, y_start, x_end, y_end = box
# Compute the center of the bounding box in depth map coordinates
center_x = int((x_start + x_end) / 2 * 256 / img.shape[1])
center_y = int((y_start + y_end) / 2 * 256 / img.shape[0])
#Needed to resize images to (256,256)
# Ensure the coordinates are within the bounds of depth_map
if 0 <= center_x < depth_normalized.shape[1] and 0 <= center_y < depth_normalized.shape[0]:
depth_value = depth_normalized[center_y, center_x].item()
print(f"Depth value at ({center_x}, {center_y}): {depth_value:.2f} meters")
# Check if the object is close
if depth_value < 1.0: # Example threshold
print("Object is pretty close by")
cv2.putText(img, "Close Object", (box[0], box[1] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
else:
print("Depth map coordinates are out of bounds")
All I tried was that I noticed that in the line
if 0 <= center_x < depth_normalized.shape[1] and 0 <= center_y < depth_normalized.shape[0]:
depth_value = depth_normalized[center_y, center_x].item()
print(f"Depth value at ({center_x}, {center_y}): {depth_value:.2f} meters")
Originally I had done depth_value = depth_map[center_y, center_x].item() but there was no difference in the output whether I put depth_map or depth_normalized. That was pretty weird as I expected a difference but to no avail. Did something go wrong there?
Anonymous Coder is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.