Ejemplo n.º 1
0
 def openCamera(self):
     with self.mutex:
         for i in range(3):
             try:
                 self.id = nivision.IMAQdxOpenCamera(
                     self.name, nivision.IMAQdxCameraControlModeController)
             except nivision.ImaqDxError as e:
                 if i == 2:
                     raise
                 time.sleep(2.0)
                 continue
             break
Ejemplo n.º 2
0
    def __init__(self, sn=None, alias=None):

        cams = enumerate_cameras()
        for cam in cams:
            if alias and not sn:
                if bytes(alias, encoding='ascii') == cam.InterfaceName:
                    self.camera = cam
                    print(f'Connected to {alias}.')
                    break
            elif sn and not alias:
                hi = cam.SerialNumberHi
                lo = cam.SerialNumberLo
                hb = hi.to_bytes(4, 'big')
                lb = lo.to_bytes(4, 'big')
                xx = int.from_bytes(hb + lb, 'big')
                if not xx == sn:
                    xx = hex(xx).split('x')[-1].upper()
                if isinstance(sn, str):
                    sn = sn.upper()
                if xx == sn:
                    self.camera = cam
                    print(f'Connected to {sn}.')
                    break
            else:
                raise Exception(
                    'Need to define either alias or sn to connect to camera.')

        try:
            print('Opening camera in controller mode.')
            print('self.camera.InterfaceName is', self.camera.InterfaceName)

            self.imaqdx = nv.IMAQdxOpenCamera(
                self.camera.InterfaceName,
                nv.IMAQdxCameraControlModeController)
        except AttributeError:
            print('Camera not found.')

        # Keep an img attribute so we don't have to create it every time
        print('Creating image object.')
        self.img = nv.imaqCreateImage(nv.IMAQ_IMAGE_U16)

        self._abort_acquisition = False
Ejemplo n.º 3
0
    def __init__(self, serial_number, **kwargs):
        super().__init__(**kwargs)

        # Find the camera:
        print("Finding camera...")
        for cam in nv.IMAQdxEnumerateCameras(True):
            if serial_number == (
                    cam.SerialNumberHi << 32) + cam.SerialNumberLo:
                self.camera = cam
                break
        else:
            msg = f"No connected camera with serial number {serial_number:X} found"
            raise Exception(msg)
        # Connect to the camera:
        print("Connecting to camera...")
        self.imaqdx = nv.IMAQdxOpenCamera(self.camera.InterfaceName,
                                          nv.IMAQdxCameraControlModeController)

        # Keep an img attribute so we don't have to create it every time
        self._img = nv.imaqCreateImage(nv.IMAQ_IMAGE_U16)
Ejemplo n.º 4
0
    def __init__(self, serial_number):
        global nv
        import nivision as nv
        _monkeypatch_imaqdispose()

        # Find the camera:
        print("Finding camera...")
        for cam in nv.IMAQdxEnumerateCameras(True):
            if serial_number == (
                    cam.SerialNumberHi << 32) + cam.SerialNumberLo:
                self.camera = cam
                break
        else:
            msg = f"No connected camera with serial number {serial_number:X} found"
            raise Exception(msg)
        # Connect to the camera:
        print("Connecting to camera...")
        self.imaqdx = nv.IMAQdxOpenCamera(self.camera.InterfaceName,
                                          nv.IMAQdxCameraControlModeController)
        # Keep an img attribute so we don't have to create it every time
        self.img = nv.imaqCreateImage(nv.IMAQ_IMAGE_U16)
        self._abort_acquisition = False
Ejemplo n.º 5
0
import ctypes
import nivision
import numpy as np
import struct

from library import cam_x, cam_y, cam_offset_x, cam_offset_y

offsetX = cam_offset_x
offsetY = cam_offset_y
total_pixels = cam_x * cam_y
fmt = '>' + str(total_pixels) + 'B'

cam = ctypes.c_char_p(b"cam0")
id = nivision.IMAQdxOpenCamera(cam, nivision.IMAQdxCameraControlModeController)
img = nivision.imaqCreateImage(nivision.ImageType(0))
nivision.IMAQdxSetAttribute(id, nivision.IMAQdxAttributeOffsetX, offsetX)
nivision.IMAQdxSetAttribute(id, nivision.IMAQdxAttributeOffsetY, offsetY)
nivision.IMAQdxSetAttribute(id, nivision.IMAQdxAttributeWidth, cam_x)
nivision.IMAQdxSetAttribute(id, nivision.IMAQdxAttributeHeight, cam_y)
nivision.IMAQdxSetAttribute(id, b'CameraAttributes::Shutter::Mode', 0)
nivision.IMAQdxSetAttribute(id, b'CameraAttributes::Shutter::Value', 10)
nivision.IMAQdxSetAttribute(id, b'CameraAttributes::FrameRate::Mode', 0)
nivision.IMAQdxSetAttribute(id, b'CameraAttributes::FrameRate::Value', 200)
nivision.IMAQdxConfigureGrab(id)

def capture():
   nivision.IMAQdxGrab(id, img, 1)
   picture = nivision.imaqImageToArray(img)
   data = struct.unpack(fmt, picture[0])
   data = np.asarray(data)
   data = np.reshape(data, (cam_y, cam_x))