class Camera(): def __init__(self, capture=cv2.VideoCapture, width=800, height=600, label=None, label_name=None): # Get the capture object from the MainWindow init. self.capture = capture self.capture.set(3, int(width)) self.capture.set(4, int(height)) self.width = width self.height = height self.label = label self.label_name = label_name self.currentFrame = np.array([]) self.logger = MyLogging(logger_name='user').logger self.logger.info('Create [Capture]: %s [Label_name]: %s' % (self.capture, self.label_name)) def getFrame(self): try: # Get frame and convert it to PixMap ret, img = self.capture.read() img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Add the hisself Cap object's label name in pic top. if self.label_name: cv2.putText(img, self.label_name, (18, 56), 0, 1, (129, 216, 207), 3) # Get the height, width, byserPer from this img. height, width, bytesPer = img.shape # bytesPerLine = bytesPer*3 # Convert the img to QPixmap type, because the QtLabel just # accept this type. img = QImage(img, width, height, QImage.Format_RGB888) img = QPixmap.fromImage(img) # Set label show img. And update it. self.label.setPixmap(img) except: self.logger.error('Some error happened in <cameraModule.py | ' 'getFrame>.') sys.exit(0) def refresh(self): # 1. Create a new Thread Class and Init it. self.cap_timer = Camera_Timer() self.logger.info("Camera_Timer: [%s] has created." % self.cap_timer) # 2. Connect the thing let signal to do it. self.cap_timer.update.connect(self.getFrame) # 3. Start Thread. try: self.cap_timer.start() except BaseException: self.cap_timer.stop() self.cap_timer.quit() def quit(self): self.cap_timer.stop() self.cap_timer.quit()
class SelfCheck(BaseCheck): # This config is reading by Config.out_config Module. <config dict> def __init__(self, cofig): super(SelfCheck, self).__init__(config) self._config = config self._necessary_check = config['necessary_check'] self.mylogger = MyLogging(logger_name='user').logger self._show_cam_item = config['operating_mode']['show_cam_item'] self.check_status = False if int(self.necessary_check['database_check']): self.databaseCheck() if int(self.necessary_check['internet_check']): self._InternetCheck() if int(self.necessary_check['gpu_check']): self.gpuCheck() def cameraCheck(self, cam_object=BaseCamera()): if not cam_object.isOpen(): self.mylogger.error('%s fails inspection [FAILED].' % cam_object.cam_name) def weigherCheck(self, weigher_object=Weigher()): weigher = weigher_object self.weigher = weigher.weigher if self.weigher.open(QIODevice.ReadOnly): # The QSerial.waitForReadyRead function blocks until new data is available for reading and the readyRead() signal has been emitted. # This function returns true if the readyRead() signal is emitted and there is new data available for reading; # Otherwise this function returns false (if an error occurred or the operation timed out). if self.weigher.waitForReadyRead(8000): # the unit is millisecond; self.weigher.readAll() status_data_convert = True convert_times = 0 can_readline_times = 0 while status_data_convert: if self.weigher.waitForReadyRead(500): if self.weigher.canReadLine(): weigher.read() try: # This will raise error if the data can not be converted successfully. status_data_convert = False except BaseException: convert_times = convert_times + 1 self.mylogger.warning( '[Weigher] is trying to ' 'convert the data ' 'fails with total %s times.' % convert_times) if convert_times > 29: self.mylogger.error( '[Weigher]: the ' 'string data given by weigher can not be converted to the float data successfully.' ) sys.exit() else: can_readline_times = can_readline_times + 1 self.mylogger.warning( 'canReadLine fails with total %s ' 'times.' % can_readline_times) if can_readline_times > 29: self.mylogger.error( 'Weigher problem: there is no readLine signal too many times.' ) sys.exit() else: convert_times = convert_times + 1 self.mylogger.warning( 'waitForReadyRead fails with total %s ' 'times.' % convert_times) if convert_times > 29: self.mylogger.error( 'Weigher problem: the string data given by weigher can not be converted to the float data successfully.' ) sys.exit() else: self.mylogger.error( 'Weigher problem: no data received from the weigher.') sys.exit() self.weigher.close() else: self.mylogger.error( 'Weigher problem: the connection to the specific weigher fails. Please check the port name.' ) sys.exit() def databaseCheck(self): pass def _InternetCheck(self): pass def gpuCheck(self): pass # Private parameters. @property def config(self): return self._config @property def necessary_check(self): return self._necessary_check @property def show_cam_item(self): return self._show_cam_item