1
|
# example python script for capturing an image from CMV50K EVK
|
2
|
# you must have c:\Program Files\Critical Link LLC\GenTL Viewer\bin
|
3
|
# in your path prior to running this script.
|
4
|
from harvesters.core import Harvester
|
5
|
import logging
|
6
|
import matplotlib.pyplot as plt
|
7
|
|
8
|
import os
|
9
|
|
10
|
os.environ['PATH'] = 'c:\\Program Files\\Critical Link LLC\\GenTL Viewer\\bin'
|
11
|
|
12
|
# os.environ['SIMCAM_XML_FILE'] = 'C:\\Program Files\\Critical Link LLC\\GenTL Viewer\\bin\\sim_camera.xml'
|
13
|
os.environ['HARVESTERS_XML_FILE_DIR'] = 'C:\\Users\\salhadda\\Documents\\XMLFOLDER\\'
|
14
|
print(os.getenv('HARVESTERS_XML_FILE_DIR'))
|
15
|
|
16
|
# set up a logger for the harvester library.
|
17
|
# this is not needed but can be useful for debugging your script
|
18
|
logger = logging.getLogger('harvesters');
|
19
|
ch = logging.StreamHandler()
|
20
|
logger.setLevel(logging.DEBUG)
|
21
|
ch.setLevel(logging.DEBUG)
|
22
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
23
|
ch.setFormatter(formatter)
|
24
|
logger.addHandler(ch)
|
25
|
|
26
|
# Create the harvester
|
27
|
h = Harvester(logger=logger)
|
28
|
# the harvester can load dlls as well as cti files.
|
29
|
if "add_file" in dir(h): # Use updated harvesters api if exists
|
30
|
h.add_file('c:\\Program Files\\Critical Link LLC\\GenTL Viewer\\bin\\GenTL.dll')
|
31
|
else:
|
32
|
h.add_cti_file('c:\\Program Files\\Critical Link LLC\\GenTL Viewer\\bin\\GenTL.dll')
|
33
|
if "update" in dir(h): # Use updated harvesters api if exists
|
34
|
h.update()
|
35
|
else:
|
36
|
h.update_device_info_list()
|
37
|
|
38
|
print("Found {} devices".format(len(h.device_info_list)))
|
39
|
print(h.device_info_list)
|
40
|
|
41
|
if len(h.device_info_list) == 0:
|
42
|
print("No devices found")
|
43
|
|
44
|
|
45
|
# create an image acquirer
|
46
|
ia = h.create_image_acquirer(list_index=1)
|
47
|
# this is required for larger images (> 16 MiB) with Critical Link's producer.
|
48
|
ia.num_buffers = 4
|
49
|
ia.remote_device.node_map.PixelFormat.value = 'Mono8'
|
50
|
# Uncomment to set the image ROI width, height. Otherwise will get full frame
|
51
|
#ia.remote_device.node_map.Width.value, ia.remote_device.node_map.Height.value = 800, 600
|
52
|
|
53
|
print("Starting Acquistion")
|
54
|
|
55
|
ia.start_image_acquisition()
|
56
|
|
57
|
# just capture 1 frame
|
58
|
for i in range(1):
|
59
|
with ia.fetch_buffer(timeout=4) as buffer:
|
60
|
payload = buffer.payload
|
61
|
component = payload.components[0]
|
62
|
width = component.width
|
63
|
height = component.height
|
64
|
data_format = component.data_format
|
65
|
print("Image details: {}w {}h {}".format(width, height, data_format))
|
66
|
# for monochrome 8 bit images
|
67
|
if int(component.num_components_per_pixel) == 1:
|
68
|
content = component.data.reshape(height, width)
|
69
|
else:
|
70
|
content = component.data.reshape(height, width, int(component.num_components_per_pixel))
|
71
|
if int(component.num_components_per_pixel) == 1:
|
72
|
plt.imshow(content, cmap='gray')
|
73
|
else:
|
74
|
plt.imshow(content)
|
75
|
plt.show()
|
76
|
|
77
|
#
|
78
|
ia.stop_image_acquisition()
|
79
|
ia.destroy()
|