def set_mark(self, **kwargs): if 'mark' in kwargs: if str(kwargs['mark']).lower() == 'true': self._mark['mark'] = True else: self._mark['mark'] = False if 'text' in kwargs and len(str(kwargs['text'])) > 0: self._mark['text'] = kwargs['text'] else: if 'logMark' in kwargs: self._mark['text'] = kwargs['logMark'] else: self._mark['text'] = ' ' if 'x' in kwargs and 0 <= int(kwargs['x']) <= 640: self._mark['x'] = int(kwargs['x']) if 'y' in kwargs and 0 <= int(kwargs['y']) <= 480: self._mark['y'] = int(kwargs['y']) if 'fontScale' in kwargs and int(kwargs['fontScale']) > 0: self._mark['fontScale'] = int(kwargs['fontScale']) if 'R' in kwargs and 0 <= int(kwargs['R']) <= 255: self._mark['R'] = int(kwargs['R']) if 'G' in kwargs and 0 <= int(kwargs['G']) <= 255: self._mark['G'] = int(kwargs['G']) if 'B' in kwargs and 0 <= int(kwargs['B']) <= 255: self._mark['B'] = int(kwargs['B']) if 'thick' in kwargs and int(kwargs['thick']) > 0: self._mark['thick'] = int(kwargs['thick']) if 'logMark' in kwargs: self._mark['mark'] = True video_time = int(float(self.frameCnt) / self.fps) if video_time < 0: video_time = 0 logger.info( "Case: <{}> start to run at time: <{}min - {}sec>".format(str(kwargs['logMark']), video_time / 60, video_time % 60))
def start_camera(self, camera_id=0, **kwargs): """ 功能:打开摄像头 参数:camera_id:摄像头id,如果有超过两个摄像头,则依次为:0 1 2 ... kwargs : 可设置的摄像头参数 返回: 示例: """ self._mark = { 'mark': True, 'text': ' ', 'x': 10, 'y': 60, 'fontScale': 1, 'R': 0, 'G': 255, 'B': 255, 'thick': 1 } self.capture = cv2.VideoCapture(camera_id) if not self.capture.isOpened(): self.capture.open(camera_id) if kwargs: available_params = list(self.FrameID.keys()) set_params = list(kwargs.keys()) for p in set_params: if p not in available_params: logger.info("un support camera param: {}={}".format(p, kwargs[p])) continue logger.info("setting camera param: {}={}".format(p, kwargs[p])) self.set_property(p, kwargs[p])
def set_mark(self, **kwargs): """ 功能:设置视频水印 参数:kwargs:mark:[False:不设置水印, True:设置水印] text:设置需要显示在视频中的文本, 默认为logMark x, y:设置视频中显示文本的位置,起始坐标点为左上角,default=(10, 60) fontScale:设置视频中显示文本的缩放, default=1 R, G, B:设置视频中显示文本的颜色, default=(0, 255, 255) thick:设置视频中显示文本的粗细, default=1 logMark:将用例的执行时间打印到log中, 该参数建议使用用例名称 返回: 示例:set_mark(mark=True, text='test', x=50, y=50, fontScale=2, R=100, thick=2) """ if 'mark' in kwargs: if str(kwargs['mark']).lower() == 'true': self._mark['mark'] = True else: self._mark['mark'] = False if 'text' in kwargs and len(str(kwargs['text'])) > 0: self._mark['text'] = kwargs['text'] else: if 'logMark' in kwargs: self._mark['text'] = kwargs['logMark'] else: self._mark['text'] = ' ' if 'x' in kwargs and 0 <= int(kwargs['x']) <= 640: self._mark['x'] = int(kwargs['x']) if 'y' in kwargs and 0 <= int(kwargs['y']) <= 480: self._mark['y'] = int(kwargs['y']) if 'fontScale' in kwargs and int(kwargs['fontScale']) > 0: self._mark['fontScale'] = int(kwargs['fontScale']) if 'R' in kwargs and 0 <= int(kwargs['R']) <= 255: self._mark['R'] = int(kwargs['R']) if 'G' in kwargs and 0 <= int(kwargs['G']) <= 255: self._mark['G'] = int(kwargs['G']) if 'B' in kwargs and 0 <= int(kwargs['B']) <= 255: self._mark['B'] = int(kwargs['B']) if 'thick' in kwargs and int(kwargs['thick']) > 0: self._mark['thick'] = int(kwargs['thick']) if 'logMark' in kwargs: self._mark['mark'] = True video_time = int(float(self.frameCnt) / self.fps) if video_time < 0: video_time = 0 logger.info( "Case: <{}> start to run at time: <{}min - {}sec>".format(str(kwargs['logMark']), video_time / 60, video_time % 60))
def convert_signal(self, signals): """ 转换信号{"IP_EngineOffTime":0x2}为 {"0-5":0x2},如果是后者则不进行转换 :param signals: {"IP_EngineOffTime":0x2} :return: {"0-5":0x2} """ result = dict() logger_result = dict() logger.debug(f"signals is {signals}") for name in list(signals.keys()): logger.debug(f"name is {name}") signal_name = self.convert_signal_name(name) result[signal_name] = self.calc_value(signals[name], name, True) logger_result[name] = result[signal_name] logger.info(logger_result) return result
def _record(self, name='test.avi', fps=20, time_=20, width=640, height=480, code='MJPG'): try: name = str(name) time_ = int(time_) fps = float(fps) if fps < 5 or fps > 30: fps = self.fps else: self.fps = fps code = str(code) fourcc = cv2.VideoWriter_fourcc(*code) if code.lower() == 'none': fourcc = -1 out = cv2.VideoWriter(name, fourcc, fps, (width, height), True) self._start_time = time.time() logger.info("Start to record video: <{}> at time: {}".format(name, self._start_time)) self.frameCnt = 0 while self.capture.isOpened(): ret, self.frame = self.capture.read() if self._mark['mark']: self._mark['text'] = self._utils.get_time_as_string() cv2.putText(self.frame, self._mark['text'], (self._mark['x'], self._mark['y']), cv2.FONT_HERSHEY_SIMPLEX, self._mark['fontScale'], (self._mark['R'], self._mark['G'], self._mark['B']), self._mark['thick'], cv2.LINE_AA) if ret: out.write(self.frame) self.frameCnt += 1 self._check_time = time.time() if int(self._check_time - self._start_time) >= (time_ * 60): break if self.stopRecord: break out.release() logger.info("Stop record video: <{}> at time: {}, or {}sec, total frame: {}" .format(name, self._check_time, int(self._check_time - self._start_time), self.frameCnt)) except Exception as e: logger.error("_record : {}".format(e))
def _read_frame(self, filename='test.wav', record_time=30): """ 功能:采集声音 参数:record_time:录音时间,单位s filename:保存文件名称,可以是路径 返回:无 示例: """ record_time = int(record_time) pa = PyAudio() stream = pa.open(format=paInt16, channels=1, rate=self.sampling_rate, input=True, frames_per_buffer=self.num_samples) wf = wave.open(filename, 'wb') wf.setnchannels(1) wf.setsampwidth(2) wf.setframerate(self.sampling_rate) start_time = time.time() logger.info("Audio record start from time: %s", str(start_time)) while True: string_audio_data = stream.read(self.num_samples) wf.writeframes(np.array(string_audio_data).tostring()) check_time = time.time() if int(check_time - start_time) >= record_time: logger.info("Audio record end at time: %s, total record time: %s", str(check_time), str(check_time - start_time)) break if self.stopRecord: break stream.close() wf.close()
def _record(self, name='test.avi', fps=20, time_=20, width=640, height=480, code='MJPG'): """ 功能:录制 参数:name:保存视频的名称或路径:test.avi or D:/GIT/automatedtest_5X3/test.avi fps:帧率设置[5.0, 30.0], default=20 t:每段录像的时长,达到该时间后自动保存录像并开启新的录像写入另一个视频文件,单位:minutes, 如果t=0则保存为一个视频文件, 默认值:t=20 width, height : 录像视屏的分辨率 code:录像的编码格式,目前只支持MJPG 返回:无 示例:_record('name.avi') """ try: name = str(name) time_ = int(time_) fps = float(fps) if fps < 5 or fps > 30: fps = self.fps else: self.fps = fps code = str(code) # 各种编码格式每分钟压缩后大小:MJPG(80M), fourcc = cv2.VideoWriter_fourcc(*code) if code.lower() == 'none': fourcc = -1 out = cv2.VideoWriter(name, fourcc, fps, (width, height), True) self._start_time = time.time() logger.info("Start to record video: <{}> at time: {}".format(name, self._start_time)) self.frameCnt = 0 while self.capture.isOpened(): ret, self.frame = self.capture.read() if self._mark['mark']: self._mark['text'] = self._utils.get_time_as_string() cv2.putText(self.frame, self._mark['text'], (self._mark['x'], self._mark['y']), cv2.FONT_HERSHEY_SIMPLEX, self._mark['fontScale'], (self._mark['R'], self._mark['G'], self._mark['B']), self._mark['thick'], cv2.LINE_AA) if ret: out.write(self.frame) self.frameCnt += 1 # cv2.imshow('f', self.frame) # if cv2.waitKey(1) & 0xFF == ord('q'): # break self._check_time = time.time() if int(self._check_time - self._start_time) >= (time_ * 60): break if self.stopRecord: break out.release() logger.info("Stop record video: <{}> at time: {}, or {}sec, total frame: {}" .format(name, self._check_time, int(self._check_time - self._start_time), self.frameCnt)) # cv2.destroyAllWindows() except Exception as e: logger.error("_record : {}".format(e))