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 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.__Camara = Camara()
self.__SDL2_Test = SDL2_Test()
self.__SDL2_Test.Clear()
def define_capture(self):
self.__Camara.setSize(
int(self.params.get('capture_size_w', self.__xmlObj.getKeyVal(self.__QVideoName, "capture_size_w", 1280))),
int(self.params.get('capture_size_h', self.__xmlObj.getKeyVal(self.__QVideoName, "capture_size_h", 720))))
self.__discard_frames_Count = int(self.params.get('capture_discardframes', self.__xmlObj.getKeyVal(self.__QVideoName, "capture_discardframes", 3)))
self.__frame_mean = int(self.params.get('capture_framemean', self.__xmlObj.getKeyVal(self.__QVideoName, "capture_framemean", 3)))
self.__max_failed = int(self.params.get('capture_maxfailed', self.__xmlObj.getKeyVal(self.__QVideoName, "capture_maxfailed", 1)))
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 = []
# set image size
self.define_capture()
# 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 ""
|