-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrab.py
80 lines (67 loc) · 3.09 KB
/
grab.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
# ===============================================================================
# This sample illustrates how to grab and process images using the CInstantCamera class.
# The images are grabbed and processed asynchronously, i.e.,
# while the application is processing a buffer, the acquisition of the next buffer is done
# in parallel.
#
# The CInstantCamera class uses a pool of buffers to retrieve image data
# from the camera device. Once a buffer is filled and ready,
# the buffer can be retrieved from the camera object for processing. The buffer
# and additional image data are collected in a grab result. The grab result is
# held by a smart pointer after retrieval. The buffer is automatically reused
# when explicitly released or when the smart pointer object is destroyed.
# ===============================================================================
from pypylon import pylon
from pypylon import genicam
import sys
import time
# Number of images to be grabbed.
countOfImagesToGrab = 150
# The exit code of the sample application.
exitCode = 0
try:
# Create an instant camera object with the camera device found first.
camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
camera.Open()
# Print the model name of the camera.
print("Using device ", camera.GetDeviceInfo().GetModelName())
# demonstrate some feature access
new_width = camera.Width.Value - camera.Width.Inc
if new_width >= camera.Width.Min:
camera.Width.Value = new_width
# The parameter MaxNumBuffer can be used to control the count of buffers
# allocated for grabbing. The default value of this parameter is 10.
camera.MaxNumBuffer.Value = 5
# Start the grabbing of c_countOfImagesToGrab images.
# The camera device is parameterized with a default configuration which
# sets up free-running continuous acquisition.
camera.StartGrabbingMax(countOfImagesToGrab)
# Camera.StopGrabbing() is called automatically by the RetrieveResult() method
# when c_countOfImagesToGrab images have been retrieved.
n = 0
while camera.IsGrabbing():
if n == 0:
start_t = time.perf_counter()
# Wait for an image and then retrieve it. A timeout of 5000 ms is used.
grabResult = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
# Image grabbed successfully?
if grabResult.GrabSucceeded():
# Access the image data.
print(f"\nFrame: {n+1}")
print("SizeX: ", grabResult.Width)
print("SizeY: ", grabResult.Height)
print(f'FPS: {camera.ResultingFrameRate.Value}')
img = grabResult.Array
print("Gray value of first pixel: ", img[0, 0])
else:
print("Error: ", grabResult.ErrorCode, grabResult.ErrorDescription)
grabResult.Release()
n += 1
camera.Close()
print(f'elapsed time: {time.perf_counter() - start_t}')
except genicam.GenericException as e:
# Error handling.
print("An exception occurred.")
print(e)
exitCode = 1
sys.exit(exitCode)