Project

General

Profile

Using Python With Critical Link GenTL Producer » snap.py

Update for GenTL Viewer path change. Also use harvesters new add_file()/update() to avoid deprecation warnings. - Jonathan Cormier, 12/14/2021 10:09 PM

 
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
print(os.getenv('HARVESTERS_XML_FILE_DIR'))
10

    
11
# set up a logger for the harvester library.
12
# this is not needed but can be useful for debugging your script
13
logger = logging.getLogger('harvesters');
14
ch = logging.StreamHandler()
15
logger.setLevel(logging.DEBUG)
16
ch.setLevel(logging.DEBUG)
17
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
18
ch.setFormatter(formatter)
19
logger.addHandler(ch)
20

    
21
# Create the harvester
22
h = Harvester(logger=logger)
23
# the harvester can load dlls as well as cti files.
24
if "add_file" in dir(h): # Use updated harvesters api if exists
25
	h.add_file('c:\\Program Files\\Critical Link LLC\\GenTL Viewer\\bin\\GenTL.dll')
26
else:
27
	h.add_cti_file('c:\\Program Files\\Critical Link LLC\\GenTL Viewer\\bin\\GenTL.dll')
28
if "update" in dir(h): # Use updated harvesters api if exists
29
	h.update()
30
else:
31
	h.update_device_info_list()
32

    
33
print("Found {} devices".format(len(h.device_info_list)))
34
print(h.device_info_list)
35

    
36
if len(h.device_info_list) == 0:
37
	print("No devices found")
38
	exit()
39

    
40
# create an image acquirer
41
ia = h.create_image_acquirer(list_index=0)
42
# this is required for larger images (> 16 MiB) with Critical Link's producer.
43
ia.num_buffers = 4
44
ia.remote_device.node_map.PixelFormat.value = 'Mono8'
45
# Uncomment to set the image ROI width, height. Otherwise will get full frame
46
#ia.remote_device.node_map.Width.value, ia.remote_device.node_map.Height.value = 800, 600
47

    
48
print("Starting Acquistion")
49

    
50
ia.start_image_acquisition()
51

    
52
# just capture 1 frame
53
for i in range(1):
54
	with ia.fetch_buffer(timeout=4) as buffer:
55
		payload = buffer.payload
56
		component = payload.components[0]
57
		width = component.width
58
		height = component.height
59
		data_format = component.data_format
60
		print("Image details: {}w {}h {}".format(width, height, data_format))
61
		# for monochrome 8 bit images
62
		if int(component.num_components_per_pixel) == 1:
63
			content = component.data.reshape(height, width)
64
		else:
65
			content = component.data.reshape(height, width, int(component.num_components_per_pixel))
66
		if int(component.num_components_per_pixel) == 1:
67
			plt.imshow(content, cmap='gray')
68
		else:
69
			plt.imshow(content)
70
		plt.show()
71

    
72
#
73
ia.stop_image_acquisition()
74
ia.destroy()
(3-3/5) Go to top
Add picture from clipboard (Maximum size: 1 GB)