def set_attribute(self, name, value): """Set the value of the attribute of the given name to the given value""" _value = value # Keep the original for the sake of the error message if isinstance(_value, str): _value = _value.encode('utf8') try: nv.IMAQdxSetAttribute(self.imaqdx, name.encode('utf8'), _value) except Exception as e: # Add some info to the exception: msg = f"failed to set attribute {name} to {value}" raise Exception(msg) from e
def updateSettings(self): with self.mutex: wasActive = self.active # Stop acquistion, close and reopen camera if wasActive: self.stopCapture() if self.id is not None: self.closeCamera() self.openCamera() # Video Mode modes, currentMode = nivision.IMAQdxEnumerateVideoModes(self.id) foundMode = None foundFps = 1000 for mode in modes: m = self.reMode.match(mode.Name) if not m: continue if int(m.group("width")) != self.width: continue if int(m.group("height")) != self.height: continue fps = float(m.group("fps")) if fps < self.fps: continue if fps > foundFps: continue isJpeg = m.group("format") in (b"jpeg", b"JPEG") if (self.useJpeg and not isJpeg) or (not self.useJpeg and isJpeg): continue foundMode = mode foundFps = fps if foundMode is not None: logger.info("found mode %d: %s" % (foundMode.Value, foundMode.Name)) if foundMode.Value != currentMode.value: nivision.IMAQdxSetAttribute( self.id, nivision.IMAQdxAttributeVideoMode, foundMode) # White Balance if self.whiteBalance == "auto": nivision.IMAQdxSetAttribute(self.id, self.ATTR_WB_MODE, b"Auto") else: nivision.IMAQdxSetAttribute(self.id, self.ATTR_WB_MODE, b"Manual") if self.whiteBalanceValue is not None: nivision.IMAQdxSetAttribute(self.id, self.ATTR_WB_VALUE, self.whiteBalanceValue) # Exposure if self.exposure == "auto": nivision.IMAQdxSetAttribute(self.id, self.ATTR_EX_MODE, b"AutoAperaturePriority") else: nivision.IMAQdxSetAttribute(self.id, self.ATTR_EX_MODE, b"Manual") if self.exposureValue is not None: minv = nivision.IMAQdxGetAttributeMinimum( self.id, self.ATTR_EX_VALUE) maxv = nivision.IMAQdxGetAttributeMaximum( self.id, self.ATTR_EX_VALUE) val = minv + ((maxv - minv) * (self.exposureValue / 100.0)) nivision.IMAQdxSetAttribute(self.id, self.ATTR_EX_VALUE, val) # Brightness nivision.IMAQdxSetAttribute(self.id, self.ATTR_BR_MODE, b"Manual") minv = nivision.IMAQdxGetAttributeMinimum(self.id, self.ATTR_BR_VALUE) maxv = nivision.IMAQdxGetAttributeMaximum(self.id, self.ATTR_BR_VALUE) val = minv + ((maxv - minv) * (self.brightness / 100.0)) nivision.IMAQdxSetAttribute(self.id, self.ATTR_BR_VALUE, val) # Restart acquisition if wasActive: self.startCapture()
def set_attribute(self, attr, value): # attr names need to be bytes but values can be strings attr = _ensure_str(attr).encode('utf8') assert isinstance(value, (bytes, str, float)) nv.IMAQdxSetAttribute(self.imaqdx, attr, value)
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))