summaryrefslogtreecommitdiff
path: root/test-cli/test/helpers/camara.py
blob: 9b2829c0df4a349715368e48904e98321838960e (plain)
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
import cv2
from test.helpers.syscmd import SysCommand

class Camara(object):
    __parent = None
    __device_name = None
    __device = None
    __w = 1280
    __h = 720
    __contrast = 0.0
    __brightness = 0.0
    __saturation = 55.0
    __hue = 0.0
    __exposure = 166

    def __init__(self, parent, device="video0", width=1280, height=720):
        self.__parent = parent
        self.__device_name = device
        self.__w = width
        self.__h = height

    def Close(self):
        if self.__device is not None:
            del self.__device
            self.__device = None

    def Open(self):
        self.Close()
        self.__device = cv2.VideoCapture("/dev/{}".format(self.__device_name))
        if self.__device.isOpened():
            self.__configure()
            return True
        return False

    def getSize(self):
        return self.__w, self.__h

    def setSize(self, w, h):
        if self.__device is not None and self.__device.isOpened():
            self.__w = self.__setCamVar(cv2.CAP_PROP_FRAME_WIDTH, w)
            self.__h = self.__setCamVar(cv2.CAP_PROP_FRAME_HEIGHT, h)
        else:
            self.__w = w
            self.__h = h

    def setContrast(self, newVal):
        if self.__device.isOpened():
            self.__contrast = self.__setCamVar(cv2.CAP_PROP_CONTRAST, newVal)
        else:
            self.__contrast = newVal

    def getContrast(self):
        return self.__contrast

    def setBrightness(self, newVal):
        if self.__device.isOpened():
            self.__brightness = self.__setCamVar(cv2.CAP_PROP_BRIGHTNESS, newVal)
        else:
            self.__brightness = newVal

    def getBrightness(self):
        return self.__brightness

    def __configure(self):
        self.__w = self.__setCamVar(cv2.CAP_PROP_FRAME_WIDTH, self.__w)
        self.__h = self.__setCamVar(cv2.CAP_PROP_FRAME_HEIGHT, self.__h)
        cam_setup = SysCommand('v4lsetup', '{}/scripts/v4l-cam.sh'.format(self.__parent.getAppPath()))
        cam_setup.execute()

        #self.__contrast = self.__setCamVar(cv2.CAP_PROP_CONTRAST, self.__contrast)
        #self.__brightness = self.__setCamVar(cv2.CAP_PROP_BRIGHTNESS, self.__brightness)
        #self.__saturation = self.__setCamVar(cv2.CAP_PROP_SATURATION, self.__saturation)
        #self.__hue = self.__setCamVar(cv2.CAP_PROP_HUE, self.__hue)
        #self.__exposure = self.__setCamVar(cv2.CAP_PROP_EXPOSURE, self.__exposure)


    def __setCamVar(self, key, val):
        valold = cv2.VideoCapture.get(self.__device, key)
        if valold != val:
            cv2.VideoCapture.set(self.__device, key, val)
            t =  cv2.VideoCapture.get(self.__device, key)
            return t
        return val

    def __getCamVar(self, key):
        return cv2.VideoCapture.get(self.__device, key);

    def getFrameCount(self):
        return cv2.VideoCapture.get(self.__device, cv2.CAP_PROP_BUFFERSIZE);

    def getFrame(self):
        if self.__device.isOpened():
            retval, image = self.__device.read()
            if retval:
                return image
            return None
        else:
            return None

    def getImageSize(self, image):
        if hasattr(image, 'shape'):
            return image.shape[1], image.shape[0]
        else:
            return (0, 0, 0)

    def showFrame(self, name, frame, w=False, maxTime=3000):
        cv2.imshow(name, frame)
        if w:
            if maxTime == -1:
                cv2.waitKey()
            else:
                cv2.waitKey(maxTime)

    def saveFrame(self, fileName, Frame):
        cv2.imwrite(fileName, Frame)

    def readImage(self, filename):
        return cv2.imread('{}'.format(filename), 0)

    def destroyWindow(self, name):
        cv2.destroyWindow(name)

    def closeWindows(self):
        cv2.destroyAllWindows()