summaryrefslogtreecommitdiff
path: root/test-cli/test/tests/qvideo.py
blob: 225940b71b7a75764f7275ca7286efc6691b72fc (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
125
import unittest
import sh
import time
from test.helpers.camara import Camara
from test.helpers.detect import Detect_Color
from test.helpers.sdl import SDL2_Test

class Qvideo(unittest.TestCase):
    params = None
    __resultlist = None  # resultlist is a python list of python dictionaries

    def __init__(self, testname, testfunc, varlist):
        self.params = varlist
        super(Qvideo, self).__init__(testfunc)
        self._testMethodDoc = testname
        self.__xmlObj = varlist["xml"]
        self.__QVideoName = varlist.get('name', 'qvideo')      
        self.__resultlist = []
        self.__w = int(varlist.get('capture_size_w', self.__xmlObj.getKeyVal(self.__QVideoName, "capture_size_w", 1280)))
        self.__h = int(varlist.get('capture_size_h', self.__xmlObj.getKeyVal(self.__QVideoName, "capture_size_h", 720)))
        self.__discard_frames_Count = int(varlist.get('capture_discardframes',self.__xmlObj.getKeyVal(self.__QVideoName, "capture_discardframes", 3)))
        self.__frame_mean = int(varlist.get('capture_framemean', self.__xmlObj.getKeyVal(self.__QVideoName, "capture_framemean", 3)))
        self.__max_failed = int(varlist.get('capture_maxfailed', self.__xmlObj.getKeyVal(self.__QVideoName, "capture_maxfailed", 1)))
        self.__cam_setupscript = varlist.get('cam_setupfile', self.__xmlObj.getKeyVal(self.__QVideoName, "cam_setupfile",
                                                                                      "/root/hwtest-files/board/scripts/v4l-cam.sh"))
        self.__sdl_display = varlist.get('sdl_display', self.__xmlObj.getKeyVal(self.__QVideoName, "sdl_display", ":0"))
        self.__sdl_driver = varlist.get('sdl_driver', self.__xmlObj.getKeyVal(self.__QVideoName, "sdl_driver", "x11"))
        self.__camdevice = varlist.get('camdevice', self.__xmlObj.getKeyVal(self.__QVideoName, "camdevice", "video0"))

        self.__Camara = Camara(setup_script_path=self.__cam_setupscript, device=self.__camdevice, width=self.__w, height=self.__h)
        self.__SDL2_Test = SDL2_Test(driver=self.__sdl_driver, display=self.__sdl_display, w=self.__w, h=self.__h)
        self.__SDL2_Test.Clear()

    def __drop_frames(self, frame_count):
        count = frame_count
        while count > 0:
            _ = self.__Camara.getFrame()
            count -= 1

    def Verify_Color(self, color):
        self.paintColor(color)
        count = self.__frame_mean
        res_ok = 0
        res_failed = 0
        r_count = 0
        c_mean = 0
        while count > 0:
            res, t = self.Capture(color)
            if res:
                res_ok += 1
            else:
                res_failed += 1
                if t == "count":
                    r_count += 1
                else:
                    c_mean +=1
            count -= 1
        self.unpaintColor(color)
        if res_failed > self.__max_failed:
            if r_count > 0:
                return '{}_COLOR_FAILED'.format(color)
            elif c_mean > 0:
                return 'MEAN_{}_FAILED'.format(color)
        return "ok"

    def paintColor(self, color):
        self.__SDL2_Test.Paint(color)
        time.sleep(3)
        self.__drop_frames(self.__Camara.getFrameCount())

    def unpaintColor(self, color):
        self.__SDL2_Test.Clear()
        time.sleep(1)

    def Capture(self, color):
        image = self.__Camara.getFrame()
        if image is None:
            return None
        dtObject = Detect_Color(image)
        if color == 'RED':
            _, _, mean, count = dtObject.getRed()
        elif color == 'BLUE':
            _, _, mean, count = dtObject.getBlue()
        elif color == 'GREEN':
            c_mask, croped, mean, count = dtObject.getGreen()

        return self.checkThreshold(color, mean, count)

    def checkThreshold(self, color, mean, count):
        min_count = int(
            self.params.get('{}_min_count'.format(color),
                            self.__xmlObj.getKeyVal(self.__QVideoName, '{}_min_count'.format(color), 50000)))

        str = ""
        # first verify count
        if count >= min_count:
            return True, ""
        else:
            str = "count"
        return False, str

    def execute(self):
        self.__resultlist = []

        # Open camara
        if not self.__Camara.Open():
            self.fail('Error: USB camera not found')
        # discard initial frames
        self.__drop_frames(self.__discard_frames_Count)
        # Test
        res = self.Verify_Color('RED')
        if res != "ok":
            self.fail("failed: RED verification failed")
        res = self.Verify_Color('BLUE')
        if res != "ok":
            self.fail("failed: BLUE verification failed")
        res = self.Verify_Color('GREEN')
        if res != "ok":
            self.fail("failed: GREEN verification failed")

    def getresults(self):
        return self.__resultlist

    def gettextresult(self):
        return ""