-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdearpygui-pypylon.py
113 lines (88 loc) · 3.21 KB
/
dearpygui-pypylon.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import dearpygui.dearpygui as dpg
from pypylon import pylon
from pypylon import genicam
import numpy as np
# Number of images to be grabbed.
countOfImagesToGrab = 100
dpg.create_context()
texture_data = []
for i in range(0, 1024 * 1024):
# [RGBa] * size
texture_data.append(1.0)
texture_data.append(0)
texture_data.append(0)
texture_data.append(1.0)
with dpg.texture_registry(show=False):
dpg.add_dynamic_texture(
width=1024, height=1024, default_value=texture_data, tag="camera_texture"
)
with dpg.value_registry():
dpg.add_string_value(default_value="size: unknown", tag="grab_size")
dpg.add_string_value(default_value="first pixcel: 0", tag="img00str")
try:
# init pylon
camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
camera.Open()
cam_name = camera.GetDeviceInfo().GetModelName()
# get size
cam_width = camera.Width.GetValue()
cam_height = camera.Height.GetValue()
# max buffer size (def.10)
camera.MaxNumBuffer = 5
# start grabbing
# free running
# camera.StartGrabbingMax(countOfImagesToGrab)
camera.StartGrabbing()
with dpg.window(label="dearpygui basler pypylon Window"):
dpg.add_text("pypylon")
dpg.add_text("device: " + cam_name)
dpg.add_text("size: " + str(cam_width) + " x " + str(cam_height))
dpg.add_text(source="grab_size")
dpg.add_text(source="img00str")
dpg.add_image(texture_tag="camera_texture")
dpg.create_viewport(title="Custom Title", width=1040, height=1040)
dpg.setup_dearpygui()
dpg.show_viewport()
# below replaces, start_dearpygui()
while dpg.is_dearpygui_running():
# insert here any code you would like to run in the render loop
# you can manually stop by using stop_dearpygui()
# grabbing
if camera.IsGrabbing():
grabResult = camera.RetrieveResult(
5000, pylon.TimeoutHandling_ThrowException
)
if grabResult.GrabSucceeded():
grab_flag = True
grab_w = grabResult.Width
grab_h = grabResult.Height
dpg.set_value("grab_size", "size: " + str(grab_w) + " x " + str(grab_w))
img = grabResult.Array
img00 = img[0, 0]
dpg.set_value("img00str", "value: " + str(img00))
img_flat = np.ravel(img) / 255.0
next_tex = (
np.repeat([img_flat, np.ones(grab_w * grab_h)], [3, 1], axis=0)
.ravel("F")
.tolist()
)
"""
next_tex = (
np.array([img_flat, img_flat, img_flat, np.ones(grab_w * grab_h)])
.ravel("F")
.tolist()
)
"""
dpg.set_value("camera_texture", next_tex)
else:
print("grab err")
grabResult.Release()
dpg.render_dearpygui_frame()
dpg.start_dearpygui()
camera.Close()
dpg.destroy_context()
except genicam.GenericException as e:
# Error handling.
print("An exception occurred.")
print(e.GetDescription())
exitCode = 1