-
Notifications
You must be signed in to change notification settings - Fork 208
/
dataprocessing_barcode.py
94 lines (78 loc) · 3.18 KB
/
dataprocessing_barcode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# ===============================================================================
# pylon data processing
# Sample using the Barcode vTool (license required)
# ===============================================================================
import os
num = 2
os.environ["PYLON_CAMEMU"] = "%d" % num
from pypylon import pylondataprocessing
from pypylon import pylon
from pypylon import genicam
import sys
# Number of images to be grabbed.
countOfImagesToGrab = 100
# The exit code of the sample application.
exitCode = 0
try:
# This object is used for collecting the output data.
resultCollector = pylondataprocessing.GenericOutputObserver()
# Create a recipe representing a design created using
# the pylon Viewer Workbench.
recipe = pylondataprocessing.Recipe()
# Load the recipe file.
thisdir = os.path.dirname(__file__)
recipefilename = os.path.join(thisdir, 'dataprocessing_barcode.precipe')
recipe.Load(recipefilename)
# Register the helper object for receiving all output data.
recipe.RegisterAllOutputsObserver(resultCollector, pylon.RegistrationMode_Append);
# Allocate the required resources. This includes the camera device.
recipe.PreAllocateResources()
# Tell the camemu camera device where the barcode images are located
imagespath = os.path.join(thisdir, 'images', 'barcode')
recipe.GetParameter("MyCamera/@CameraDevice/ImageFilename").SetValue(imagespath);
# Start the processing. The recipe is triggered internally
# by the camera vTool for each image.
recipe.Start()
for i in range(0, countOfImagesToGrab):
if resultCollector.GetWaitObject().Wait(5000):
# Get the recipe dependend dictionary; key is the pin name and the value is a variant object
result = resultCollector.RetrieveResult()
# Show some image information
variant = result["Image"]
if not variant.HasError():
# Print result data
pylonimage = variant.ToImage()
print("SizeX: ", pylonimage.Width)
print("SizeY: ", pylonimage.Height)
img = pylonimage.Array
print("Gray value of first pixel: ", img[0, 0])
pylonimage.Release()
else:
print("Error: " + variant.GetErrorDescription())
# Print the barcodes
variant = result["Barcodes"]
if not variant.HasError():
# Print result data
for barcodeIndex in range(0, variant.NumArrayValues):
print(variant.GetArrayValue(barcodeIndex).ToString())
else:
print("Error: " + variant.GetErrorDescription())
else:
print("Result timeout")
break
# Stop the processing.
recipe.Stop()
# Optionally, deallocate resources.
recipe.DeallocateResources()
# Unload the recipe and free all created vTools
recipe.Unload()
except genicam.GenericException as e:
# Error handling.
print("An exception occurred.")
print(e)
exitCode = 1
except Exception as e:
print("An unexpected exception occurred.")
print(e)
exitCode = 1
sys.exit(exitCode)