0
$\begingroup$

Video player gives "could not demultiplex stream"

import cv2
import math
import numpy as np
import pyrealsense2 as rs
from ultralytics import YOLO

# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
pipeline.start(config)

# Define video writer for detection results
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out_detection = cv2.VideoWriter('detection_5.avi', fourcc, 30, (608, 480))

# Define video writer for normal video stream
out_normal = cv2.VideoWriter('normal_video_5.avi', fourcc, 30, (608,480))

# Load YOLO model
model = YOLO("/home/rushabh/Railway_bot/weights/best.pt")

# Object classes
classNames = ["car","bus", "auto"]

try:
    while True:
        # Wait for a coherent pair of frames: depth and color
        frames = pipeline.wait_for_frames()
        color_frame = frames.get_color_frame()
        
        if not color_frame:
            continue

        # Convert images to numpy arrays
        color_image = np.asanyarray(color_frame.get_data())

        # Perform object detection
        results = model(color_image, stream=True)

        # Process detection results
        for r in results:
            boxes = r.boxes

            for box in boxes:
                # Extract bounding box coordinates
                x1, y1, x2, y2 = box.xyxy[0]
                x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)  # Convert to int values

                # Draw bounding box
                cv2.rectangle(color_image, (x1, y1), (x2, y2), (0, 255, 0), 3)

                # Extract confidence and class name
                confidence = math.ceil((box.conf[0] * 100)) / 100
                cls = int(box.cls[0])

                # Draw class name
                org = [x1, y1]
                font = cv2.FONT_HERSHEY_SIMPLEX
                fontScale = 1
                color = (255, 0, 0)
                thickness = 2
                cv2.putText(color_image, classNames[cls], org, font, fontScale, color, thickness)

        # Write the frame with detections to the detection video file
        # out_detection.write(color_image)

        cv2.imshow('RealSense with Detection', color_image)
        if cv2.waitKey(1) == ord('q'):
            break

finally:
    # Stop streaming
    pipeline.stop()

    # Release resources
    out_detection.release()
    cv2.destroyAllWindows()
$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.