Skip to content

Commit 3f97e85

Browse files
committed
basler example camera, untested
1 parent b59172a commit 3f97e85

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

dlclivegui/camera/basler.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
"""
2+
DeepLabCut Toolbox (deeplabcut.org)
3+
© A. & M. Mathis Labs
4+
5+
Licensed under GNU Lesser General Public License v3.0
6+
"""
7+
8+
import pypylon as pylon
9+
from imutils import rotate_bound
10+
import time
11+
12+
from dlclivegui.camera import Camera, CameraError
13+
14+
15+
class BaslerCam(Camera):
16+
@staticmethod
17+
def arg_restrictions():
18+
""" Returns a dictionary of arguments restrictions for DLCLiveGUI
19+
"""
20+
21+
tlFactory = pylon.TlFactory.GetInstance()
22+
devices = tlFactory.EnumerateDevices()
23+
24+
return {"device": devices, "display": [True, False]}
25+
26+
def __init__(
27+
self,
28+
device="",
29+
resolution=[640, 480],
30+
exposure=0,
31+
rotate=0,
32+
crop=None,
33+
fps=30,
34+
display=True,
35+
display_resize=1.0,
36+
):
37+
38+
super().__init__(
39+
device,
40+
resolution=resolution,
41+
exposure=exposure,
42+
rotate=rotate,
43+
crop=crop,
44+
fps=fps,
45+
use_tk_display=display,
46+
display_resize=display_resize,
47+
)
48+
49+
self.display = display
50+
51+
def set_capture_device(self):
52+
53+
self.cam = pylon.InstantCamera(
54+
pylon.TlFactory.GetInstance().CreateDevice(self.id))
55+
self.cam.Open()
56+
57+
self.cam.Gain.SetValue(self.gain)
58+
self.cam.Exposure.SetValue(self.exposure)
59+
self.cam.Width.SetValue(self.im_size[0])
60+
self.cam.Height.SetValue(self.im_size[1])
61+
62+
self.cam.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)
63+
self.converter = pylon.ImageFormatConverter()
64+
self.converter.OutputPixelFormat = pylon.PixelType_BGR8packed
65+
self.converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned
66+
67+
return True
68+
69+
def get_image(self):
70+
71+
grabResult = self.cam.RetrieveResult(
72+
1, pylon.TimeoutHandling_ThrowException)
73+
74+
frame = None
75+
76+
if grabResult.GrabSucceeded():
77+
78+
image = self.converter.Convert(grabResult)
79+
frame = image.GetArray()
80+
81+
if self.rotate:
82+
frame = rotate_bound(frame, self.rotate)
83+
if self.crop:
84+
frame = frame[self.crop[2]: self.crop[3],
85+
self.crop[0]: self.crop[1]]
86+
87+
else:
88+
89+
raise CameraError("Basler Camera did not return an image!")
90+
91+
grabResult.Release()
92+
93+
return frame
94+
95+
def close_capture_device(self):
96+
97+
self.cam.StopGrabbing()

0 commit comments

Comments
 (0)