Project

General

Profile

Using Python With Critical Link GenTL Producer » snap.py

Add setup instructions to header. Increase buffers to 5. Update to avoid deprecation warnings. - Jonathan Cormier, 06/27/2022 08:48 PM

 
1
# example python script for capturing an image from CMV50K EVK
2
#
3
# conda create -n gentl_38 matplotlib python=3.8
4
# conda activate gentl_38
5
# pip install harvesters numpy
6
#
7
# you must have c:\Program Files\Critical Link LLC\GenTL Viewer\bin in your path prior to running this script.
8
# set PATH=c:\Program Files\Critical Link LLC\GenTL Viewer\bin;%PATH%
9
#
10
# To enable simulated camera for testing:
11
# copy "c:\Program Files\Critical Link LLC\GenTL Viewer\bin\sim_camera.xml" .
12
# set SIMCAM_XML_FILE=sim_camera.xml
13
# 
14
# (gentl_38) C:\Users\jcormier\Documents>python snap.py
15
from harvesters.core import Harvester
16
import logging
17
import matplotlib.pyplot as plt
18

    
19
import os
20
print(os.getenv('HARVESTERS_XML_FILE_DIR'))
21

    
22
# set up a logger for the harvester library.
23
# this is not needed but can be useful for debugging your script
24
logger = logging.getLogger('harvesters');
25
ch = logging.StreamHandler()
26
logger.setLevel(logging.DEBUG)
27
ch.setLevel(logging.DEBUG)
28
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
29
ch.setFormatter(formatter)
30
logger.addHandler(ch)
31

    
32
# Create the harvester
33
h = Harvester(logger=logger)
34
# the harvester can load dlls as well as cti files.
35
if "add_file" in dir(h): # Use updated harvesters api if exists
36
	h.add_file('c:\\Program Files\\Critical Link LLC\\GenTL Viewer\\bin\\GenTL.dll')
37
else:
38
	h.add_cti_file('c:\\Program Files\\Critical Link LLC\\GenTL Viewer\\bin\\GenTL.dll')
39
if "update" in dir(h): # Use updated harvesters api if exists
40
	h.update()
41
else:
42
	h.update_device_info_list()
43

    
44
print("Found {} devices".format(len(h.device_info_list)))
45
print(h.device_info_list)
46

    
47
if len(h.device_info_list) == 0:
48
	print("No devices found")
49
	exit()
50

    
51
# create an image acquirer
52
if "create" in dir(h): # Use updated harvesters api if exists
53
	ia = h.create(0)
54
else:
55
	ia = h.create_image_acquirer(list_index=0)
56
# this is required for larger images (> 16 MiB) with Critical Link's producer.
57
ia.num_buffers = 5
58
ia.remote_device.node_map.PixelFormat.value = 'Mono8'
59
# Uncomment to set the image ROI width, height. Otherwise will get full frame
60
#ia.remote_device.node_map.Width.value, ia.remote_device.node_map.Height.value = 800, 600
61

    
62
print("Starting Acquistion")
63

    
64
if "start" in dir(ia): # Use updated harvesters api if exists
65
	ia.start()
66
else:
67
	ia.start_image_acquisition()
68

    
69
# just capture 1 frame
70
for i in range(1):
71
	if "fetch" in dir(ia): # Use updated harvesters api if exists
72
		fetch = ia.fetch
73
	else:
74
		fetch = ia.fetch_buffer
75
	with fetch(timeout=4) as buffer:
76
		payload = buffer.payload
77
		component = payload.components[0]
78
		width = component.width
79
		height = component.height
80
		data_format = component.data_format
81
		print("Image details: {}w {}h {}".format(width, height, data_format))
82
		# for monochrome 8 bit images
83
		if int(component.num_components_per_pixel) == 1:
84
			content = component.data.reshape(height, width)
85
		else:
86
			content = component.data.reshape(height, width, int(component.num_components_per_pixel))
87
		if int(component.num_components_per_pixel) == 1:
88
			plt.imshow(content, cmap='gray')
89
		else:
90
			plt.imshow(content)
91
		plt.show()
92

    
93
#
94
if "stop" in dir(ia): # Use updated harvesters api if exists
95
	ia.stop()
96
else:
97
	ia.stop_image_acquisition()
98

    
99
ia.destroy()
(4-4/5) Go to top
Add picture from clipboard (Maximum size: 1 GB)