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.
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.
Share Improve this question edited Jan 14 at 23:29 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked Jan 14 at 23:10 BSPBSP 353 bronze badges 4- Did you specify the type of video you are trying to read. See geeksfeeks./python-opencv-capture-video-from-camera. Search Google for more details or answers – fmw42 Commented Jan 14 at 23:49
- 1 geeksfeeks is a junk site. I would not recommend using or speaking of it. none of the authors of the articles on that site have much of a clue. it's either copy-pasted from elsewhere, taken from generative AI and maybe reworded, blatantly flawed, or trivial content. – Christoph Rackwitz Commented Jan 15 at 1:20
- 1 that camera doesn't do USB UVC. it probably does "USB3 Vision", a different standard. OpenCV doesn't normally come with support for that. I keep fetting if there is actual driver support for that in OpenCV. there is talk of such driver support. -- you could investigate how to get OpenCV with support for this. it might involve (re)compilation of OpenCV. -- you could look for another library, one that can access USB3 Vision devices. afaik there are libraries for python that do this. – Christoph Rackwitz Commented Jan 15 at 1:21
- Sorry, I should have paid more attention to the website. I understand that web site is junk. – fmw42 Commented Jan 15 at 1:47
2 Answers
Reset to default 4So 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./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.
if not camera.isOpened():
print("Error:")
else:
print("Success.")
for i in range(10):
ret, frame = camera.read()
if ret:
print("Success")
break
else:
print("Fail")
if not ret:
print("Fail")
else:
cv2.imshow("Captured Frame", frame)
cv2.waitKey(0)```