|
| 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() |
0 commit comments