Search code examples
pythonopencvcameraphoto

Python Accessing EO Edmund Optics Camera


I have a camera (EO Edmund Optics Camera, Model UI-154xSE-M) connected to a Windows computer via USB. When I open IDS Camera Manager, The camera is "configured correctly and can be opened".

I am trying to write a code in Python to have the camera capture an image every x minutes for a period of y total minutes using the cv2 package. Unfortunately, I am getting an error while doing this.

Please find my code below and the result:

import cv2
import os
import time
from datetime import datetime, timedelta


output_directory = 

camera = cv2.VideoCapture(0)

if not camera.isOpened():
    print("Error: Could not access the camera.")
else:
    print("Camera found and initialized. Starting photo capture...")

    
    end_time = datetime.now() + timedelta(hours=24)
    capture_interval = 10 * 60  # Capture every 10 minutes (600 seconds)

    try:
        while datetime.now() < end_time:
            
            ret, frame = camera.read()
            
            
            print(f"Capture status: {ret}")  # Print whether the capture was successful
            if not ret:
                print("Error: Failed to capture image.")
                continue  # Skip this iteration and try again

            
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            file_path = os.path.join(output_directory, f"photo_{timestamp}.jpg")

           
            cv2.imwrite(file_path, frame)
            print(f"Photo captured and saved at {file_path}")

            
            time.sleep(capture_interval)
    except KeyboardInterrupt:
        print("Photo capture interrupted by user.")
    finally:
        
        camera.release()
        print("Camera released. Photo capture ended.")

"Error: Failed to capture image." prints for the time frame I specify. Although "Camera found and initialized. Starting photo capture..." prints as well.

I am very unfamiliar with accessing/controlling external devices with Python code. I am open to alternate ways to do this besides Python.

I am new to coding and devices.


Solution

  • So isOpened() says True, but the read() call returns False?

    It might be worth trying the read call several times, instead of failing at the first sign of trouble. Some cameras are weird like that.

    OpenCV also has this quirk where it tries to set 640 x 480 resolution. If a camera doesn't support that, the interaction will fail. Try setting a supported resolution, e.g. the sensor's preferred video mode resolution (1280 x 1024 was it?)


    OpenCV struggles because that camera doesn't appear to support normal camera APIs, such as USB Unified Video Class (UVC). That is the interface supported by all webcams and most modern frame grabbers and capture cards.

    Your camera, being an industrial camera, probably supports either "GigE Vision" or "USB3 Vision", which are standards for industrial cameras.

    OpenCV might support that, if you compile it to do that. To my knowledge, no (official or third-party) binary build of OpenCV for Python has such support.

    The manufacturer's own site links to a Python library you can use to access your camera:

    https://pypi.org/project/pyueye


    I just checked the spec sheet for the part number you gave. The model has been discontinued. You might be out of luck in finding any support for it.

    The spec sheet doesn't speak about USB 3, so it's got to be USB 2, and that never, to my knowledge, carried any data conforming to an industrial vision API.