Reputation: 55
I'm trying to write a Python (2.7.12) and OpenCV (3.2.0) script that will open a left and right side camera Ximea xiQ camera, set some parameters, start acquisition, snap a photo from each camera once per second, write them to a file, and then repeat until KeyboardInterrupt. Everything seems to be working, but when I look at the .png images, they're blank (or show up as black in the preview, yet they're all ~2.5 MB so not empty).
I'm new to OpenCV and these cameras/the API, and am not yet fluent in Python (but I can get around). Would you all look at my script and let me know what you think the problem might be? Here's the code I am trying (which is just a more elaborate version of the included Python example:
from ximea import xiapi
import cv2
increment_val = 0
##which_camera = 'Left\\'
save_location = 'C:\\Users\\mah_usernames\\Desktop\\camera_test\\'
image_file_name = 'image'
image_file_type = '.png'
##saved_image_full_path = save_location + which_camera + image_file_name + image_file_type
saved_image_full_path = save_location + image_file_name + image_file_type
#create instance for first connected camera
left_cam = xiapi.Camera(dev_id=0)
right_cam = xiapi.Camera(dev_id=1)
#start communication
#to open specific device, use:
#cam.open_device_by_SN('41305651')
#(open by serial number)
print('Opening left side camera...')
left_cam.open_device()
print('Opening right side camera...')
right_cam.open_device()
#settings
##left_cam.enable_aeag() #enable auto exposure, auto gain
##right_cam.enable_aeag()
##left_cam.set_acq_timing_mode('XI_ACQ_TIMING_MODE_FREE_RUN')
##right_cam.set_acq_timing_mode('XI_ACQ_TIMING_MODE_FREE_RUN')
left_cam.set_led_selector('XI_LED_SEL3') #choose bottom LED
left_cam.set_led_mode('XI_LED_ON') #always on, power indicator
left_cam.set_led_selector('XI_LED_SEL2') #choose middle LED
left_cam.set_led_mode('XI_LED_FRAME_ACTIVE') #blinks when frame is active
left_cam.set_led_selector('XI_LED_SEL1') #choose top LED
left_cam.set_led_mode('XI_LED_ACQUISITION') #blinks when data is transferred
##
right_cam.set_led_selector('XI_LED_SEL3') #choose bottom LED
right_cam.set_led_mode('XI_LED_ON') #always on, power indicator
right_cam.set_led_selector('XI_LED_SEL2') #choose middle LED
right_cam.set_led_mode('XI_LED_FRAME_ACTIVE') #blinks when frame is active
right_cam.set_led_selector('XI_LED_SEL1') #choose top LED
right_cam.set_led_mode('XI_LED_ACQUISITION') #blinks when data is transferred
left_cam.set_trigger_source('XI_TRG_EDGE_RISING')
right_cam.set_trigger_source('XI_TRG_EDGE_RISING')
left_cam.set_trigger_selector('XI_TRG_SEL_EXPOSURE_ACTIVE')
right_cam.set_trigger_selector('XI_TRG_SEL_EXPOSURE_ACTIVE')
left_cam.set_gpi_mode('XI_GPI_TRIGGER')
right_cam.set_gpi_mode('XI_GPI_TRIGGER')
left_cam.set_exposure(5000)
right_cam.set_exposure(5000)
print('Left Camera exposure was set to %i us' %left_cam.get_exposure())
print('Right Camera exposure was set to %i us' %right_cam.get_exposure())
left_cam.set_imgdataformat('XI_RGB32')
right_cam.set_imgdataformat('XI_RGB32')
#create instance of Image to store image data and metadata
##img = xiapi.Image()
img_left = xiapi.Image()
img_right = xiapi.Image()
#start data acquisition
print('Starting data acquisition...')
left_cam.start_acquisition()
right_cam.start_acquisition()
try:
while True:
print str(increment_val)
#get data and pass them from camera to img
## left_cam.get_image(img_left)
## right_cam.get_image(img_right)
left_cam.get_image(img_left)
right_cam.get_image(img_right)
#get raw data from camera
#for Python2.x function returns string
#for Python3.x function returns bytes
left_cam_data = img_left.get_image_data_numpy()
right_cam_data = img_right.get_image_data_numpy()
## #transform data to list
## data = list(data_raw)
##
## #print image data and metadata
## print('Image number: ' + str(i))
## print('Image width (pixels): ' + str(img_left.width))
## print('Image height (pixels): ' + str(img_left.height))
## print('First 10 pixels: ' + str(left_cam_data[:10]))
## print('\n')
## #show acquired image
## print('Drawing image...')
## cv2.imshow('Left side camera', left_cam_data)
## cv2.imshow('Right side camera', right_cam_data)
## cv2.waitKey(0)
## cv2.destroyAllWindows()
#save acquired image
print('Saving image...')
cv2.imwrite((save_location + 'Left\\' + image_file_name + str(increment_val) + image_file_type), left_cam_data)
cv2.imwrite((save_location + 'Right\\' + image_file_name + str(increment_val) + image_file_type), right_cam_data)
increment_val += 1
print str(increment_val)
except:
KeyboardInterrupt
#stop data acquisition
print ('Stopping acquisition...')
left_cam.stop_acquisition()
right_cam.stop_acquisition()
#stop communication
left_cam.close_device()
right_cam.close_device()
print ('All finished!')
Prior to this, I had been using a slightly less modified version of the canned example...
from ximea import xiapi
import cv2
##increment_val = 0
##which_camera = 'Left\\'
save_location = 'C:\\Users\\mah_usernames\\Desktop\\camera_test\\'
image_file_name = 'image'
image_file_type = '.png'
##saved_image_full_path = save_location + which_camera + image_file_name + image_file_type
saved_image_full_path = save_location + image_file_name + image_file_type
#create instance for first connected camera
left_cam = xiapi.Camera(dev_id=0)
right_cam = xiapi.Camera(dev_id=1)
#start communication
#to open specific device, use:
#cam.open_device_by_SN('41305651')
#(open by serial number)
print('Opening left side camera...')
left_cam.open_device()
print('Opening right side camera...')
right_cam.open_device()
#settings
left_cam.set_exposure(10000)
right_cam.set_exposure(8000)
print('Left Camera exposure was set to %i us' %left_cam.get_exposure())
print('Right Camera exposure was set to %i us' %right_cam.get_exposure())
left_cam.set_imgdataformat('XI_RGB24')
right_cam.set_imgdataformat('XI_RGB24')
#create instance of Image to store image data and metadata
##img = xiapi.Image()
img_left = xiapi.Image()
img_right = xiapi.Image()
#start data acquisition
print('Starting data acquisition...')
left_cam.start_acquisition()
right_cam.start_acquisition()
for i in range(10):
increment_val = i
print str(increment_val)
#get data and pass them from camera to img
## left_cam.get_image(img_left)
## right_cam.get_image(img_right)
left_cam.get_image(img_left)
right_cam.get_image(img_right)
#get raw data from camera
#for Python2.x function returns string
#for Python3.x function returns bytes
left_cam_data = img_left.get_image_data_numpy()
right_cam_data = img_right.get_image_data_numpy()
## #transform data to list
## data = list(data_raw)
##
## #print image data and metadata
## print('Image number: ' + str(i))
## print('Image width (pixels): ' + str(img_left.width))
## print('Image height (pixels): ' + str(img_left.height))
## print('First 10 pixels: ' + str(left_cam_data[:10]))
## print('\n')
## #show acquired image
## print('Drawing image...')
## cv2.imshow('Left side camera', left_cam_data)
## cv2.imshow('Right side camera', right_cam_data)
## cv2.waitKey(0)
## cv2.destroyAllWindows()
#save acquired image
print('Saving image...')
cv2.imwrite((save_location + 'Left\\' + image_file_name + str(increment_val) + image_file_type), left_cam_data)
## which_camera = 'Right\\'
cv2.imwrite((save_location + 'Right\\' + image_file_name + str(increment_val) + image_file_type), right_cam_data)
#stop data acquisition
print('Stopping acquisition...')
left_cam.stop_acquisition()
right_cam.stop_acquisition()
#stop communication
left_cam.close_device()
right_cam.close_device()
print('Done.')
...and that one worked fine, i.e. an image was taken from each camera and all of the images displayed correctly. So, I think it's something to do with the way I have to trigger set up. FYI, I'm using a 1 PPS signal from a GPS receiver as the trigger input to the GPI pin.
Anyway, please let me know if you have any advice on this problem or refactoring the script so it makes better use of the OpenCV API.
Thanks!
Upvotes: 3
Views: 1409
Reputation: 55
The solution I posed above in my first comment is a workable answer and thus enough to close this out. I'll keep looking at why I couldn't see the RGBA files, but I think it's more to do with the way Windows deals with that file type rather than a problem with the Python/Open CV script.
I'll be leaving this up for people that need a triggered, two Ximea camera-solution in Python with OpenCV as the documentation from Ximea is lacking.
Good luck!
Upvotes: 1