Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IntelRealsense Python error: RuntimeError: Frame didn't arrive within 5000 #12895

Open
benicio22 opened this issue May 3, 2024 · 4 comments
Open
Labels

Comments

@benicio22
Copy link

I am trying to extract depth, color and IR images from my realsense file saved in bag format. But I am having this errors:

frames = pipeline.wait_for_frames()
^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Frame didn't arrive within 5000

I checked my bag file and it is not corrupted. There is no problem with my file. So, probably the error is with the code. Could you help me, please? Here is the code:

'''import pyrealsense2 as rs
import cv2
import numpy as np
import os

Folder path containing the bag files

folder_path = r"\172.22.209.201\Data\I-DigitAL-Research\PROJECTS\DATA\LIVESTOCK\CATTLE\BEEF\EMBRAPA\EMBRAPA_2024\Depth_Camera_19_01\FACE_VIEW"
ouput=r"\172.22.209.201\Data\I-DigitAL-Research\PROJECTS\DATA\LIVESTOCK\CATTLE\BEEF\EMBRAPA\EMBRAPA_2024\Depth_Camera_19_01\FACE_VIEW"

Get a list of all .bag files in the folder

bag_files = [file for file in os.listdir(folder_path) if file.endswith(".bag")]

Main loop to process bag files

for bag_file in bag_files:

Configure pipeline settings

pipeline = rs.pipeline()
config = rs.config()
config.enable_device_from_file(os.path.join(folder_path, bag_file))

Start the pipeline

pipeline.start(config)

try:
frame_count = 0 # Variable to track the frame count
start_frame = 1 # Frame number from which you want to start exporting

Skip frames until the desired start frame is reached

while frame_count < start_frame:
frames = pipeline.wait_for_frames()
frame_count += 1

while True:

Wait for frames

frames = pipeline.wait_for_frames()

Get color and depth frames

color_frame = frames.get_color_frame()
depth_frame = frames.get_depth_frame()

if not color_frame or not depth_frame:
continue

try:

Convert frames to numpy arrays

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

Convert color image to BGR color space (or the desired color space)

color_image_bgr = cv2.cvtColor(color_image, cv2.COLOR_RGB2BGR)

Save color image as JPG

color_filename = os.path.join(ouput, f"{bag_file}20230626_103234_color_image{frame_count}.jpg")
cv2.imwrite(color_filename, color_image_bgr)

Save depth image as TXT

depth_filename = os.path.join(ouput, f"{bag_file}20230626_103234_depth_image{frame_count}.txt")
np.savetxt(depth_filename, depth_image)

print(f"Frame {frame_count} in {bag_file}: Color image and depth image saved.")
frame_count += 1

except Exception as e:

Handle the exception (e.g., log the error, skip the frame)

print(f"Error processing frame {frame_count} in {bag_file}: {e}")

except KeyboardInterrupt:
pass

finally:

Stop the pipeline

pipeline.stop()
'''
import pyrealsense2 as rs
import cv2
import numpy as np
import os

Folder path containing the bag files

folder_path = "D:/11-13-2023/DEPTH/INTEL_REALSENSE/ORIGINAL_DATA/FRONT_VIEW"
ouput="D:/11-13-2023/DEPTH/INTEL_REALSENSE/ORIGINAL_DATA/EXTRACTED/FRONT_VIEW"

Get a list of all .bag files in the folder

bag_files = [file for file in os.listdir(folder_path) if file.endswith("20231113_072901.bag")]

Main loop to process bag files

for bag_file in bag_files:

Configure pipeline settings

pipeline = rs.pipeline()
config = rs.config()
config.enable_device_from_file(os.path.join(folder_path, bag_file))

Create align object

align_to = rs.stream.color
align = rs.align(align_to)

Start the pipeline

pipeline.start(config)

try:
frame_count = 0 # Variable to track the frame count
start_frame = 1 # Frame number from which you want to start exporting

Skip frames until the desired start frame is reached

while frame_count < start_frame:
frames = pipeline.wait_for_frames()
frame_count += 1

while True:

Wait for frames

frames = pipeline.wait_for_frames()

Align frames

aligned_frames = align.process(frames)
color_frame = aligned_frames.get_color_frame()
depth_frame = aligned_frames.get_depth_frame()
ir_frame = aligned_frames.get_infrared_frame()

if not color_frame or not depth_frame or not ir_frame:
continue

try:

Convert frames to numpy arrays

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

Convert color image to BGR color space (or the desired color space)

color_image_bgr = cv2.cvtColor(color_image, cv2.COLOR_RGB2BGR)

Save color image as JPG

color_filename = os.path.join(ouput, f"{bag_file}20230626_103234_color_image{frame_count}.png")
cv2.imwrite(color_filename, color_image_bgr)

Save depth image as TXT

depth_filename = os.path.join(ouput, f"{bag_file}20230626_103234_depth_image{frame_count}.txt")
np.savetxt(depth_filename, depth_image)

Save IR image as JPG

ir_filename = os.path.join(ouput, f"{bag_file}20230626_103234_ir_image{frame_count}.png")
cv2.imwrite(ir_filename, ir_image)

print(f"Frame {frame_count} in {bag_file}: Color image, depth image, and IR image saved.")
frame_count += 1

except Exception as e:

Handle the exception (e.g., log the error, skip the frame)

print(f"Error processing frame {frame_count} in {bag_file}: {e}")

except KeyboardInterrupt:
pass

finally:

Stop the pipeline

pipeline.stop()

@MartyG-RealSense
Copy link
Collaborator

Hi @benicio22 Please first try commenting out most of your frame skip code (the lines highlighted below) but keeping the while True instruction.

while frame_count < start_frame:
frames = pipeline.wait_for_frames()
frame_count += 1

If you wish to skip to a specific frame of a bag file then #6340 demonstrates the best-practice method for how to do so by setting up a playback definition and using it to set set_real_time as false. Doing so provides greater stability when skipping through frames of a bag file.

However, if you simply want to skip a small number of frames at the start of the bag then #9800 (comment) has Python code for such a mechanism.

@benicio22
Copy link
Author

Thank you for the help, i will work on that.

@MartyG-RealSense
Copy link
Collaborator

Hi @benicio22 Do you have an update about this case that you can provide, please? Thanks!

@MartyG-RealSense
Copy link
Collaborator

Hi @benicio22 Do you require further assistance with this case, please? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants