Skip to content

Instantly share code, notes, and snippets.

@uzl
Created April 11, 2019 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uzl/c4c606b64984ecf62e65829147394f39 to your computer and use it in GitHub Desktop.
Save uzl/c4c606b64984ecf62e65829147394f39 to your computer and use it in GitHub Desktop.
Capturing Usb Industrial Camera
from harvesters.core import Harvester
import numpy as np
import matplotlib.pyplot as plt
import cv2
import time
from harvesters.util.pfnc import mono_location_formats, \
rgb_formats, bgr_formats, bayer_location_formats, \
rgba_formats, bgra_formats
## s-----------------------------------------
def process_img(buffer):
payload = buffer.payload
component = payload.components[0]
width = component.width
height = component.height
data_format = component.data_format
print('num_components_per_pixel', component.num_components_per_pixel )
# Reshape the image so that it can be drawn on the VisPy canvas:
if data_format in mono_location_formats:
content = component.data.reshape(height, width)
else:
# The image requires you to reshape it to draw it on the
# canvas:
if data_format in rgb_formats or \
data_format in rgba_formats or \
data_format in bgr_formats or \
data_format in bayer_location_formats or \
data_format in bgra_formats:
#
content = component.data.reshape(
height, width,
int(component.num_components_per_pixel) # Set of R, G, B, and Alpha
)
#
if data_format in bgr_formats:
# Swap every R and B:
content = content[:, :, ::-1]
else:
return
return content
## ------------------------------------------
h = Harvester()
h.add_cti_file('/usr/local/lib/baumer/libbgapi2_usb.cti') # '/usr/local/lib/baumer/libbgapi2_gige.cti')
h.update_device_info_list()
print('Device Found:',len(h.device_info_list))
print(h.device_info_list)
ia = h.create_image_acquirer(0)
ia.device.node_map.Width.value, ia.device.node_map.Height.value = 2560, 2048
print(ia.device.node_map.PixelFormat.value)
ia.device.node_map.PixelFormat.value = 'BayerGR12'
ia.device.node_map.ExposureTime.value = 9000
ia.start_image_acquisition()
with ia.fetch_buffer() as buffer:
content = process_img(buffer)
# print('2D: {0}'.format(_2d))
cv2.imwrite('saved_images/jai.png', cv2.cvtColor(content, cv2.COLOR_BAYER_BG2BGR))
ia.stop_image_acquisition()
ia.destroy()
h.reset()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment