By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,276 Members | 1,951 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,276 IT Pros & Developers. It's quick & easy.

How to print brightness using OpenCV and PyQt

P: 1
I have been trying to click a button and print brightness. When i click button i see this error message
Expand|Select|Wrap|Line Numbers
  1. Process finished with exit code 1
Here is my try

main.py
Expand|Select|Wrap|Line Numbers
  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.  
views.py


Expand|Select|Wrap|Line Numbers
  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.  
models.py
Expand|Select|Wrap|Line Numbers
  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.  
4 Weeks Ago #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.