Hi there, I am trying to use the U20CAM-9281M camera with the external trigger.
I generate the trigger using an Arduino with this code:
#define TRIGGER_PIN 5 // The pin used to send the trigger signal
void setup() {
pinMode(TRIGGER_PIN, OUTPUT); // Initialize the trigger pin as an output
digitalWrite(TRIGGER_PIN, LOW); // Ensure the trigger pin starts in a LOW state
Serial.begin(115200); // Start serial communication at 115200 baud rate
}
void loop() {
digitalWrite(TRIGGER_PIN, HIGH); // Trigger signal HIGH
delayMicroseconds(1000); // Pulse duration of HIGH signal is 200 microseconds
digitalWrite(TRIGGER_PIN, LOW); // Trigger signal LOW
Serial.println("C"); // Send trigger command
delay(1000); // Full one second delay to maintain 1 Hz frequency
}
Then I use this python script to capture the images triggered by the camera:
import cv2
import threading
import queue
import datetime
import os
# Global flag for stopping threads
stop_threads = False
# Queue for storing frames for display and save
frame_queue = queue.Queue(maxsize=10)
# Directory to save images
save_dir = "images"
os.makedirs(save_dir, exist_ok=True)
# Number of images to capture and save
image_count = 20
# Global counter for image filenames
image_counter = 1
def camera_capture_thread(camera_device):
global stop_threads
cap = cv2.VideoCapture(camera_device)
if not cap.isOpened():
print("Cannot open camera")
return
while not stop_threads and image_count > 0:
ret, frame = cap.read()
if not ret:
print("Can't receive frame. Exiting ...")
break
try:
frame_queue.put_nowait(frame)
except queue.Full:
pass
cap.release()
def display_and_save_thread():
global stop_threads, image_count, image_counter
while not stop_threads and image_count > 0:
if not frame_queue.empty():
frame = frame_queue.get()
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S-%f")
filename = os.path.join(save_dir, f"img_{image_counter}_{timestamp}.jpg")
cv2.imwrite(filename, frame)
print(f"Saved {filename}")
image_count -= 1
image_counter += 1 # Increment the counter after saving an image
cv2.putText(frame, timestamp, (10, 50), cv2.FONT_HERSHEY_SIMPLEX,
1, (255, 255, 255), 2, cv2.LINE_AA)
cv2.imshow('Captured Image', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
stop_threads = True
if __name__ == "__main__":
capture_thread = threading.Thread(target=camera_capture_thread, args=("/dev/video0",))
display_thread = threading.Thread(target=display_and_save_thread)
capture_thread.start()
display_thread.start()
capture_thread.join()
display_thread.join()
cv2.destroyAllWindows()
Here, I can see roughly 300ms delay between two images when they are triggered sometimes, and about 1 second other times.
How can I prevent the duplicate capturing by a single trigger signal?
Any help would be helpful.
Hi there, I am trying to use the U20CAM-9281M camera with the external trigger. I generate the trigger using an Arduino with this code:
Then I use this python script to capture the images triggered by the camera:
I get this output from my v4l2:
With this setup, I have noticed that I get two images after each trigger:
Here, I can see roughly 300ms delay between two images when they are triggered sometimes, and about 1 second other times. How can I prevent the duplicate capturing by a single trigger signal? Any help would be helpful.