forked from emilianavt/OpenSeeFace
-
Notifications
You must be signed in to change notification settings - Fork 3
/
escapi.py
125 lines (102 loc) · 2.92 KB
/
escapi.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
A simple python wrapper around escapi
Usage:
from camera import Device
device = Deveice.connect(0, 500, 500)
image = device.get_image()
"""
import os
import platform
import sys
from ctypes import *
from PIL import Image
import numpy as np
import cv2
def resolve(name):
f = os.path.join(os.path.dirname(__file__), name)
return f
class CAPTURE_PROPETIES:
CAPTURE_BRIGHTNESS = 1,
CAPTURE_CONTRAST = 2,
CAPTURE_HUE = 3,
CAPTURE_SATURATION = 4,
CAPTURE_SHARPNESS = 5,
CAPTURE_GAMMA = 6,
CAPTURE_COLORENABLE = 7,
CAPTURE_WHITEBALANCE = 8,
CAPTURE_BACKLIGHTCOMPENSATION = 9,
CAPTURE_GAIN = 10,
CAPTURE_PAN = 11,
CAPTURE_TILT = 12,
CAPTURE_ROLL = 13,
CAPTURE_ZOOM = 14,
CAPTURE_EXPOSURE = 15,
CAPTURE_IRIS = 16,
CAPTURE_FOCUS = 17,
CAPTURE_PROP_MAX = 18,
class SimpleCapParms(Structure):
_fields_ = [
("buffer", POINTER(c_int)),
("width", c_int),
("height", c_int),
("fps", c_int),
]
lib = None
def init():
global lib
if platform.architecture()[0] == '32bit':
dll_path = resolve(os.path.join("escapi", "escapi_x86.dll"))
lib = cdll.LoadLibrary(dll_path)
else:
dll_path = resolve(os.path.join("escapi", "escapi_x64.dll"))
lib = cdll.LoadLibrary(dll_path)
if lib is None or lib.ESCAPIVersion() != 0xfff001:
print("Invalid ESCAPI DLL found.")
sys.exit(1)
lib.initCapture.argtypes = [c_int, POINTER(SimpleCapParms)]
lib.initCapture.restype = c_int
lib.initCOM()
def count_capture_devices():
return lib.countCaptureDevices()
def device_name(device):
"""
Get the device name for the given device
:param device: The number of the device
:return: The name of the device
"""
namearry = (c_char * 256)()
lib.getCaptureDeviceName(device, namearry, 256)
camearaname = namearry.value
return camearaname
def init_camera(device, width, height, fps):
global devices
array = (width * height * c_int)()
options = SimpleCapParms()
options.width = width
options.height = height
options.fps = fps
options.buffer = array
lib.initCapture(device, byref(options))
return array
def do_capture(device):
lib.doCapture(device)
def is_capture_done(device):
return lib.isCaptureDone(device)
def read(device, width, height, array):
if is_capture_done(device):
img = Image.frombuffer('RGBA', (width, height), array, 'raw', 'BGRA', 0, 1)
img = np.array(img)
img = cv2.cvtColor(img, cv2.COLOR_RGBA2BGR);
return img
else:
return None
def get_image(device, width, height, array):
lib.doCapture(device)
while lib.isCaptureDone(device) == 0:
pass
img = Image.frombuffer('RGBA', (width, height), array, 'raw', 'BGRA', 0, 0)
img = np.array(img)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR);
return img
def deinit_camera(device):
lib.deinitCapture(device)