Project

General

Profile

Using Python With Critical Link GenTL Producer » snap.py

harvesters 1.4.0/genicam 1.2.0 - Jonathan Cormier, 11/14/2022 11:24 PM

 
1
# example python script for capturing an image from Critical Link Camera Platforms
2
#
3
# conda create -n gentl_38 matplotlib python=3.8
4
# conda activate gentl_38
5
# pip install numpy harvesters==1.4.0 genicam==1.2.0
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
# set HARVESTERS_XML_FILE_DIR=C:\Users\jcormier\Documents\Critical Link LLC\gentlviewer\xml
11
#
12
# To enable simulated camera for testing:
13
# copy "c:\Program Files\Critical Link LLC\GenTL Viewer\bin\sim_camera.xml" .
14
# set SIMCAM_XML_FILE=sim_camera.xml
15
# 
16
# (gentl_38) C:\Users\jcormier\Documents>python snap.py
17
import os
18
document_path = os.path.expanduser('~\\Documents')
19
os.environ["HARVESTERS_XML_FILE_DIR"] = os.path.join(document_path, "Critical Link LLC\\gentlviewer\\xml")
20
print(os.getenv('HARVESTERS_XML_FILE_DIR'))
21
print(os.getenv('PATH'))
22

    
23
from harvesters.core import Harvester
24
import logging
25
import matplotlib.pyplot as plt
26

    
27

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

    
38
# Create the harvester, don't cleanup downloaded camera xml files
39
h = Harvester(logger=logger, do_clean_up=False)
40
# the harvester can load dlls as well as cti files.
41
if "add_file" in dir(h): # Use updated harvesters api if exists
42
	h.add_file('c:\\Program Files\\Critical Link LLC\\GenTL Viewer\\bin\\GenTL.dll')
43
else:
44
	h.add_cti_file('c:\\Program Files\\Critical Link LLC\\GenTL Viewer\\bin\\GenTL.dll')
45
if "update" in dir(h): # Use updated harvesters api if exists
46
	h.update()
47
else:
48
	h.update_device_info_list()
49

    
50
print("Found {} devices".format(len(h.device_info_list)))
51
print(h.device_info_list)
52

    
53
if len(h.device_info_list) == 0:
54
	print("No devices found")
55
	exit()
56

    
57
index = 0
58
if len(h.device_info_list) > 1:
59
	num = None
60
	while num is None:
61
		print("More than 1 camera detected,")
62
		# Print camera name with index
63
		for idx, dev in enumerate(h.device_info_list):
64
			print(f"{idx}: {dev.model}")
65
		num = input("Enter index of camera:")
66
		try:
67
			num = int(num)
68
		except:
69
			num = None
70
	index = num
71

    
72
# create an image acquirer
73
if "create" in dir(h): # Use updated harvesters api if exists
74
	ia = h.create(index)
75
else:
76
	ia = h.create_image_acquirer(list_index=index)
77
# this is required for larger images (> 16 MiB) with Critical Link's producer.
78
ia.num_buffers = 5
79
# Leave default PixelFormat for now
80
#ia.remote_device.node_map.PixelFormat.value = 'Mono8'
81
# Uncomment to set the image ROI width, height. Otherwise will get full frame
82
#ia.remote_device.node_map.Width.value, ia.remote_device.node_map.Height.value = 800, 600
83

    
84
print("Starting Acquistion")
85

    
86
if "start" in dir(ia): # Use updated harvesters api if exists
87
	ia.start()
88
else:
89
	ia.start_image_acquisition()
90

    
91
# just capture 1 frame
92
for i in range(1):
93
	if "fetch" in dir(ia): # Use updated harvesters api if exists
94
		fetch = ia.fetch
95
	else:
96
		fetch = ia.fetch_buffer
97
	with fetch(timeout=4) as buffer:
98
		payload = buffer.payload
99
		component = payload.components[0]
100
		width = component.width
101
		height = component.height
102
		data_format = component.data_format
103
		print("Image details: {}w {}h {}".format(width, height, data_format))
104
		# for monochrome 8 bit images
105
		if int(component.num_components_per_pixel) == 1:
106
			content = component.data.reshape(height, width)
107
		else:
108
			content = component.data.reshape(height, width, int(component.num_components_per_pixel))
109
		if int(component.num_components_per_pixel) == 1:
110
			plt.imshow(content, cmap='gray')
111
		else:
112
			plt.imshow(content)
113
		plt.show()
114

    
115
#
116
if "stop" in dir(ia): # Use updated harvesters api if exists
117
	ia.stop()
118
else:
119
	ia.stop_image_acquisition()
120

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