如何使用OpenCV和PyQt打印亮度

我一直在尝试单击按钮并打印亮度。当我点击按钮时,我看到这个错误消息

选择 | 换行 | 行号
  1. Process finished with exit code 1

这是我的尝试主.py

选择 | 换行 | 行号
  1. from PyQt5.QtWidgets import QApplication
  2. from views import UI_Window
  3. from models import Camera
  4.  
  5. if __name__ == '__main__': 
  6.  
  7.     camera = Camera(0) #0, webcam numarasi
  8.  
  9.     app = QApplication([])
  10.     start_window = UI_Window(camera)
  11.     start_window.show()
  12.     app.exit(app.exec_())
  13.  
  14.  

视图.py

选择 | 换行 | 行号
  1. from PyQt5.QtCore import QThread, QTimer
  2. from PyQt5.QtWidgets import QLabel, QWidget, QPushButton, QVBoxLayout, QApplication, QHBoxLayout, QMessageBox,  QMainWindow
  3. from PyQt5.QtGui import QPixmap, QImage
  4. from models import Camera
  5.  
  6.  
  7. class UI_Window(QWidget):
  8.  
  9.     def __init__(self, cam_num):
  10.         super().__init__()
  11.         self.cam_num = cam_num
  12.         self.cam_num.open()
  13.  
  14.         # Create a timer.
  15.         self.timer = QTimer()
  16.         self.timer.timeout.connect(self.nextFrameSlot)
  17.         self.timer.start(1000. / 24)
  18.  
  19.         layout = QVBoxLayout()
  20.         button_layout = QHBoxLayout()
  21.         btnCamera = QPushButton("Print Brightness")
  22.         btnCamera.clicked.connect(self.print_brightness)
  23.         button_layout.addWidget(btnCamera)
  24.         layout.addLayout(button_layout)
  25.  
  26.         # Add a label
  27.  
  28.         self.setLayout(layout)
  29.  
  30.     def nextFrameSlot(self):
  31.  
  32.         frame = self.cam_num.read()
  33.  
  34.         if frame is not None:
  35.             image = QImage(frame, frame.shape[1], frame.shape[0], QImage.Format_RGB888) #    The image is stored using a 24-bit RGB format (8-8-8).
  36.             self.pixmap = QPixmap.fromImage(image)
  37.  
  38.     def print_brightness(self):
  39.         print(Camera.get_brightness())
  40.  
  41.  

模型.py

选择 | 换行 | 行号
  1. import cv2
  2.  
  3. class Camera:
  4.  
  5.     def __init__(self, cam_num):
  6.  
  7.         self.cap = cv2.VideoCapture(cam_num)
  8.         self.cam_num = cam_num
  9.  
  10.     def open(self, width=640, height=480, fps=30):
  11.         # vc.set(5, fps)  #set FPS
  12.         self.cap.set(3, width)  
  13.         self.cap.set(4, height)  # set height
  14.  
  15.         return self.cap.isOpened()
  16.  
  17.     def read(self):
  18.         rval, frame = self.cap.read()
  19.         frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  20.         return frame
  21.  
  22.     def get_brightness(self):
  23.         return self.cap.get(cv2.CAP_PROP_BRIGHTNESS)
  24.  

标签: python

添加新评论