Trying to implement the MiDas model for Monocular Depth estimation in tensorflow and values for distance are off the charts

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?

New contributor

Anonymous Coder is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật