Skip to content

Commit 3fab928

Browse files
authored
Merge pull request #12 from DeepLabCut/basler
Basler camera support
2 parents cdef1be + f8ec1d1 commit 3fab928

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

dlclivegui/camera/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@
3636
from dlclivegui.camera.pseye import PSEyeCam
3737
except Exception as e:
3838
pass
39+
40+
try:
41+
from dlclivegui.camera.basler import BaslerCam
42+
except Exception as e:
43+
pass

dlclivegui/camera/basler.py

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

docs/camera_support.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,73 @@ If you're camera has built in methods to ensure the correct frame rate (e.g. whe
6363
The `get_image` method has no input arguments, but must return an image as a numpy array. We also recommend converting images to 8-bit integers (data type `uint8`).
6464

6565
The `get_image_on_time` method has no input arguments, but must return an image as a numpy array (as in `get_image`) and the timestamp at which the image is returned (using python's `time.time()` function).
66+
67+
### Camera Specific Tips for Installation & Use:
68+
69+
#### Basler cameras
70+
71+
Basler USB3 cameras are compatible with Aravis. However, integration with DeepLabCut-live-GUI can also be obtained with `pypylon`, the python module to drive Basler cameras, and supported by the company. Please note using `pypylon` requires you to install Pylon viewer, a free of cost GUI also developed and supported by Basler and available on several platforms.
72+
73+
* **Pylon viewer**: https://www.baslerweb.com/en/sales-support/downloads/software-downloads/#type=pylonsoftware;language=all;version=all
74+
* `pypylon`: https://github.com/basler/pypylon/releases
75+
76+
If you want to use DeepLabCut-live-GUI with a Basler USB3 camera via pypylon, see the folllowing instructions. Please note this is tested on Ubuntu 20.04. It may (or may not) work similarly in other platforms (contributed by [@antortjim](https://github.com/antortjim)). This procedure should take around 10 minutes:
77+
78+
**Install Pylon viewer**
79+
80+
1. Download .deb file
81+
Download the .deb file in the downloads center of Basler. Last version as of writing this was **pylon 6.2.0 Camera Software Suite Linux x86 (64 Bit) - Debian Installer Package**.
82+
83+
84+
2. Install .deb file
85+
86+
```
87+
sudo dpkg -i pylon_6.2.0.21487-deb0_amd64.deb
88+
```
89+
90+
**Install swig**
91+
92+
Required for compilation of non python code within pypylon
93+
94+
1. Install swig dependencies
95+
96+
You may have to install these in a fresh Ubuntu 20.04 install
97+
98+
```
99+
sudo apt install gcc g++
100+
sudo apt install libpcre3-dev
101+
sudo apt install make
102+
```
103+
104+
2. Download swig
105+
106+
Go to http://prdownloads.sourceforge.net/swig/swig-4.0.2.tar.gz and download the tar gz
107+
108+
3. Install swig
109+
```
110+
tar -zxvf swig-4.0.2.tar.gz
111+
cd swig-4.0.2
112+
./configure
113+
make
114+
sudo make install
115+
```
116+
117+
**Install pypylon**
118+
119+
1. Download pypylon
120+
121+
```
122+
wget https://github.com/basler/pypylon/archive/refs/tags/1.7.2.tar.gz
123+
```
124+
125+
or go to https://github.com/basler/pypylon/releases and get the version you want!
126+
127+
2. Install pypylon
128+
129+
```
130+
tar -zxvf 1.7.2.tar.gz
131+
cd pypylon-1.7.2
132+
python setup.py install
133+
```
134+
135+
Once you have completed these steps, you should be able to call your Basler camera from DeepLabCut-live-GUI using the BaslerCam camera type that appears after clicking "Add camera")

0 commit comments

Comments
 (0)