OpenCV 和 Python 连接相机与 GigE 时出错

问题描述 投票:0回答:1

我使用的是 DALSA linea C4096-7。我正在尝试通过 opencv 连接到相机,但没有成功。我使用的是该品牌的 SDK,sapera LT v8.31。 我得到的错误如下。

[ WARN:[email protected]] global cap_ffmpeg_impl.hpp:453 _opencv_ffmpeg_interrupt_callback Stream timeout triggered after 30076.866000 ms

有人遇到过这样的问题吗?

python opencv computer-vision
1个回答
0
投票

它只是向我发送此消息,并且不会给出任何类型的错误。

import cv2
import numpy as np
import os
import time
import sys

# Set environment variables for OpenCV
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;udp"

# Enable verbose logging for debugging
os.environ["OPENCV_LOG_LEVEL"] = "VERBOSE"
os.environ["OPENCV_VIDEOIO_DEBUG"] = "1"
os.environ["OPENCV_VIDEOCAPTURE_DEBUG"] = "1"

def connect_to_camera(ip_address, max_retries=5, retry_delay=5):
    """Attempt to connect to the camera with retries."""
    url = f"rtsp://{ip_address}/stream"
    for attempt in range(max_retries):
        print(f"Attempting to connect to camera (Attempt {attempt + 1}/{max_retries})...")
        cap = cv2.VideoCapture(url, cv2.CAP_FFMPEG)
        
        # Set timeout for connection (5 seconds)
        cap.set(cv2.CAP_PROP_OPEN_TIMEOUT_MSEC, 5000)
        
        if cap.isOpened():
            print("Successfully connected to camera.")
            return cap
        else:
            print(f"Failed to connect. Retrying in {retry_delay} seconds...")
            time.sleep(retry_delay)
    
    print("Failed to connect to camera after multiple attempts.")
    return None

def process_frame(frame):
    """Process the frame as needed. This is a placeholder function."""
    # Add your image processing code here
    return frame

def main():
    # Camera IP address
    camera_ip = "169.254.1.1"  # Replace with your camera's actual IP
    
    # Connect to the camera
    cap = connect_to_camera(camera_ip)
    if cap is None:
        sys.exit(1)
    
    try:
        while True:
            ret, frame = cap.read()
            if not ret:
                print("Failed to receive frame. Attempting to reconnect...")
                cap.release()
                cap = connect_to_camera(camera_ip)
                if cap is None:
                    break
                continue
            
            # Process the frame
            processed_frame = process_frame(frame)
            
            # Display the frame
            cv2.imshow('Frame', processed_frame)
            
            # Break the loop if 'q' is pressed
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
    
    except KeyboardInterrupt:
        print("Interrupted by user. Shutting down...")
    
    finally:
        # Release the camera and close windows
        if cap is not None:
            cap.release()
        cv2.destroyAllWindows()
        print("Camera released and windows closed.")

if __name__ == "__main__":
    main()
© www.soinside.com 2019 - 2024. All rights reserved.