-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlib_camera.py
99 lines (81 loc) · 2.71 KB
/
lib_camera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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