View my account

Realsense D405: Frame didn't arrive within 10000

Comments

13 comments

  • MartyG

    Hi Matsyarelease  Does the error still occur if you unplug the camera from the USB port, wait a couple of seconds and then re-insert it before you run your script, please?

    0
    Comment actions Permalink
  • Matsyarelease

    yes 

     

    0
    Comment actions Permalink
  • MartyG

    Are you accessing the Pi over an SSH terminal connection?

    0
    Comment actions Permalink
  • Matsyarelease

    No, normally used pi terminal 

    0
    Comment actions Permalink
  • Matsyarelease

    MartyG When I connect camera and run wrappers/python/examples/python-rs400-advanced-mode-example.py it detects properly like D405 is connected but I am not getting frames

     

    0
    Comment actions Permalink
  • MartyG

    The python-rs400-advanced-mode-example.py Python example program does not stream frames.  It is a demonstration of getting and setting the values of Advanced Mode options.

    0
    Comment actions Permalink
  • Matsyarelease

    Yes, and I installed RealSense viewer it works properly in 2D and 3D, but when I run python code it didn't  

    0
    Comment actions Permalink
  • Matsyarelease
    This is my code. it run properly on window10

    import cv2
    import pyrealsense2 as rs
    import pandas as pd
    import numpy as np
    import os

    # Initialize RealSense pipeline and configuration
    pipeline = rs.pipeline()
    config = rs.config()
    config.enable_stream(rs.stream.depth, 1280, 720, rs.format.z16, 30)
    config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)

    # Start the pipeline
    pipeline.start(config)

    # Function to handle mouse movement and display cursor coordinates
    def show_cursor_coords(event, x, y, flags, param):
        if event == cv2.EVENT_MOUSEMOVE:
            cv2.putText(color_image, f"({x}, {y})", (x+5, y+5), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0), 1)

    # Function to calculate Euclidean distance between two points
    def euclidean_distance(point1, point2):
        return int(round(np.sqrt((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2)))

    # Function to handle mouse clicks
    def on_mouse_click(event, x, y, flags, param):
        if event == cv2.EVENT_LBUTTONDOWN:
            depth_value = depth_frame.get_distance(x, y)
            print(f"Clicked at pixel coordinates (x, y): ({x}, {y}), Depth value: {depth_value} meters")
            clicked_points.append(((x, y), depth_value))
            cv2.circle(color_image, (x, y), 10, (148, 0, 211), -1)

    # Function to create a folder if it doesn't exist
    def create_folder_if_not_exists(folder_name):
        if not os.path.exists(folder_name):
            os.makedirs(folder_name)

    # Function to zoom the image
    def zoom_image(image, factor):
        height, width = image.shape[:2]
        center = (width // 2, height // 2)
        scaled_image = cv2.resize(image, None, fx=factor, fy=factor)
        new_height, new_width = scaled_image.shape[:2]
        y_offset = (new_height - height) // 2
        x_offset = (new_width - width) // 2
        return scaled_image[y_offset:y_offset + height, x_offset:x_offset + width]

    # Initialize variables
    clicked_points = []
    zoom_factor = 1.0
    df = pd.DataFrame(columns=['Point1', 'Point1_Depth', 'Point2', 'Point2_Depth', 'Number_of_Pixels'])
    current_recording = 1
    current_folder = f"SR_{current_recording}"

    try:
        while True:
            # Wait for frames with a timeout
            frames = pipeline.wait_for_frames(timeout_ms=10000)
            depth_frame = frames.get_depth_frame()
            color_frame = frames.get_color_frame()

            if not depth_frame or not color_frame:
                continue

            depth_image = np.asanyarray(depth_frame.get_data())
            color_image = np.asanyarray(color_frame.get_data())

            zoomed_color_image = zoom_image(color_image, zoom_factor)
            cv2.imshow("Color Image", cv2.cvtColor(zoomed_color_image, cv2.COLOR_BGR2RGB))
            cv2.setMouseCallback("Color Image", show_cursor_coords)

            depth_matrix = depth_image * depth_frame.get_units()
            depth_matrix = depth_matrix.astype(np.uint16)

            cv2.setMouseCallback("Color Image", on_mouse_click)

            key = cv2.waitKey(1) & 0xFF
            if key == ord('C') or key == ord('c'):
                if len(clicked_points) == 2:
                    point1 = clicked_points[0]
                    point2 = clicked_points[1]
                    distance_pixels = euclidean_distance(clicked_points[0][0], clicked_points[1][0])
                    print(f"Number of pixels in a straight line between two clicked points: {distance_pixels}")

                    df.loc[len(df.index)] = {
                        'Point1': point1[0],
                        'Point1_Depth': point1[1] * 100,
                        'Point2': point2[0],
                        'Point2_Depth': point2[1] * 100,
                        'Number_of_Pixels': distance_pixels
                    }

                    # Create folder for the current recording if it doesn't exist
                    create_folder_if_not_exists(current_folder)

                    # Save color image with serial number
                    color_image_path = os.path.join(current_folder, f"{current_recording}.png")
                    cv2.imwrite(color_image_path, color_image)

                    # Save depth matrix with serial number
                    depth_matrix_path = os.path.join(current_folder, f"{current_recording}.npy")
                    np.save(depth_matrix_path, depth_image)

                    # Save depth values in text file
                    depth_file = os.path.join(current_folder, f"{current_recording}.txt")
                    depth_value_in_meters = depth_image * depth_frame.get_units()
                    with open(depth_file, 'w+') as f:
                        np.savetxt(f, depth_value_in_meters, fmt='%.6f')

                    clicked_points.clear()

                    # Increment current recording and folder name
                    current_recording += 1
                    current_folder = f"SR_{current_recording}"

            elif key == ord('='):
                # Zoom in (increase the zoom factor)
                zoom_factor *= 1.1
            elif key == ord('-'):
                # Zoom out (decrease the zoom factor)
                zoom_factor /= 1.1
            elif key == ord('Q') or key == ord('q'):
                print("Program ended.")
                df.to_excel('data-1.xlsx', index=False)
                pipeline.stop()
                cv2.destroyAllWindows()
                break

    except KeyboardInterrupt:
        pass
    0
    Comment actions Permalink
  • MartyG

    Have you tried 640x480 resolution instead of 1280x720, or using 1280x720 at 15 FPS instead of 30?  

    0
    Comment actions Permalink
  • Matsyarelease

    yes 

     

    0
    Comment actions Permalink
  • MartyG

    Does the simple test script at the link below work?  If it does then it could suggest that the additional elements of your script other than basic depth and color streaming (such as zooming and clicking points) may be too much for the Pi board to process.

    https://github.com/IntelRealSense/librealsense/tree/master/wrappers/python#streaming-using-rspipeline

    0
    Comment actions Permalink
  • MartyG

    Is the RealSense Viewer closed when you run a Python script?

     

    What method did you use to install the pyrealsense2 wrapper on Pi?

    0
    Comment actions Permalink

Please sign in to leave a comment.