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(setup_script_path=varlist['testPath']) 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 ""