代码拉取完成,页面将自动刷新
import numpy as np
import mvsdk
import platform
class Camera(object):
"""
Class for the industrial camera
"""
def __init__(self, DevInfo):
"""
Init
"""
super(Camera, self).__init__()
self.DevInfo = DevInfo
self.hCamera = 0
self.cap = None
self.pFrameBuffer = 0
def open(self):
"""
Open the camera
"""
if self.hCamera > 0:
return True
# Open the camera
hCamera = 0
try:
hCamera = mvsdk.CameraInit(self.DevInfo, -1, -1)
except mvsdk.CameraException as e:
print("CameraInit Failed({}): {}".format(e.error_code, e.message) )
return False
# Get the capability of the camera
cap = mvsdk.CameraGetCapability(hCamera)
# Get the type of the camera
monoCamera = (cap.sIspCapacity.bMonoSensor != 0)
if monoCamera:
mvsdk.CameraSetIspOutFormat(hCamera, mvsdk.CAMERA_MEDIA_TYPE_MONO8)
else:
mvsdk.CameraSetIspOutFormat(hCamera, mvsdk.CAMERA_MEDIA_TYPE_BGR8)
# Calculate the size of the RGB buffer according to the maximum resolution
FrameBufferSize = cap.sResolutionRange.iWidthMax * cap.sResolutionRange.iHeightMax * (1 if monoCamera else 3)
# Allocate the RGB buffer for storing the ISP image
pFrameBuffer = mvsdk.CameraAlignMalloc(FrameBufferSize, 16)
# Set the trigger mode for the camera: continurous mode
mvsdk.CameraSetTriggerMode(hCamera, 0)
# Set thr exposure time: 30ms
mvsdk.CameraSetAeState(hCamera, 0)
mvsdk.CameraSetExposureTime(hCamera, 30 * 1000)
# Start to get the image
mvsdk.CameraPlay(hCamera)
self.hCamera = hCamera
self.pFrameBuffer = pFrameBuffer
self.cap = cap
return True
def close(self):
"""
Close the camera
"""
if self.hCamera > 0:
mvsdk.CameraUnInit(self.hCamera)
self.hCamera = 0
mvsdk.CameraAlignFree(self.pFrameBuffer)
self.pFrameBuffer = 0
def grab(self):
"""
Grab an image from the camera
"""
hCamera = self.hCamera
pFrameBuffer = self.pFrameBuffer
try:
pRawData, FrameHead = mvsdk.CameraGetImageBuffer(hCamera, 200)
mvsdk.CameraImageProcess(hCamera, pRawData, pFrameBuffer, FrameHead)
mvsdk.CameraReleaseImageBuffer(hCamera, pRawData)
if platform.system() == "Windows":
mvsdk.CameraFlipFrameBuffer(pFrameBuffer, FrameHead, 1)
frame_data = (mvsdk.c_ubyte * FrameHead.uBytes).from_address(pFrameBuffer)
frame = np.frombuffer(frame_data, dtype=np.uint8)
frame = frame.reshape((FrameHead.iHeight, FrameHead.iWidth, 1 if FrameHead.uiMediaType == mvsdk.CAMERA_MEDIA_TYPE_MONO8 else 3) )
return frame
except mvsdk.CameraException as e:
if e.error_code != mvsdk.CAMERA_STATUS_TIME_OUT:
print("CameraGetImageBuffer failed({}): {}".format(e.error_code, e.message) )
return None
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。