'OpenCV Video Feed Automatic Resizing with PyQt5
this is my first post on Stack Overflow so I hope I'm doing everything correctly.
I have been trying to get a video feed using OpenCV on a GUI made using PyQt5. I eventually got this to work, but it is always 640 x 480 (the size I set it to). However, I want it to automatically scale/resize (with the same aspect ratio) to fit within its parenting widget. For example, when I expand the window, the video feed should scale, too. Does anyone know a way to do this?
Here is a sample of my code for reference:
Camera Widget
class Camera(QWidget):
def __init__(self, port):
super().__init__()
self.display_width = 640
self.display_height = 480
self.camera = QLabel(self)
self.camera.resize(self.display_width, self.display_height)
self.label = QLabel(f'Port {port}')
self.label.setStyleSheet("""
QLabel {
color: white;
font: bold 30px
}
""")
# Layout
self.layout = QVBoxLayout()
self.layout.addWidget(self.label)
self.layout.addWidget(self.camera)
self.setLayout(self.layout)
self.thread = VideoThread(port)
self.thread.change_pixmap_signal.connect(self.update_image)
self.thread.start()
def close_event(self, event):
self.thread.stop()
event.accept()
@pyqtSlot(ndarray)
def update_image(self, cv_img):
qt_img = self.convert_cv_qt(cv_img)
self.camera.setPixmap(qt_img)
def convert_cv_qt(self, cv_img):
rgb_image = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
h, w, ch = rgb_image.shape
bytes_per_line = ch * w
convert_to_Qt_format = QImage(rgb_image.data, w, h, bytes_per_line, QImage.Format_RGB888)
p = convert_to_Qt_format.scaled(self.display_width, self.display_height, Qt.KeepAspectRatio)
return QPixmap.fromImage(p)
Video feed Thread
class VideoThread(QThread):
change_pixmap_signal = pyqtSignal(ndarray)
def __init__(self, port):
super().__init__()
self.running = True
self.port = port
def run(self):
self.capture = cv2.VideoCapture(self.port)
while self.running:
self.ret, self.image = self.capture.read()
if self.ret:
self.change_pixmap_signal.emit(self.image)
self.capture.release()
def stop(self):
self.running = False
self.wait()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
