chore(speed): avoid unnecessary color conversion (#83)

* chore(speed): avoid unnecessary color conversion

This speeds up a bit the presentation by avoiding color conversion from BGR (OpenCV) to RGB.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Jérome Eertmans
2022-12-02 18:02:54 +01:00
committed by GitHub
parent 1bca2683e1
commit 97e7bf8cb0

View File

@ -11,7 +11,7 @@ import numpy as np
from pydantic import ValidationError from pydantic import ValidationError
from PySide6 import QtGui from PySide6 import QtGui
from PySide6.QtCore import Qt, QThread, Signal, Slot from PySide6.QtCore import Qt, QThread, Signal, Slot
from PySide6.QtGui import QCloseEvent, QIcon, QKeyEvent, QPixmap, QResizeEvent from PySide6.QtGui import QCloseEvent, QIcon, QImage, QKeyEvent, QPixmap, QResizeEvent
from PySide6.QtWidgets import QApplication, QGridLayout, QLabel, QWidget from PySide6.QtWidgets import QApplication, QGridLayout, QLabel, QWidget
from tqdm import tqdm from tqdm import tqdm
@ -598,24 +598,16 @@ class App(QWidget): # type: ignore
@Slot(np.ndarray) @Slot(np.ndarray)
def update_image(self, cv_img: np.ndarray) -> None: def update_image(self, cv_img: np.ndarray) -> None:
"""Updates the (image) label with a new opencv image""" """Updates the (image) label with a new opencv image"""
self.pixmap = self.convert_cv_qt(cv_img) h, w, ch = cv_img.shape
self.label.setPixmap(self.pixmap)
def convert_cv_qt(self, cv_img: np.ndarray) -> np.ndarray:
"""Convert from an opencv image to QPixmap"""
rgb_image = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
h, w, ch = rgb_image.shape
bytes_per_line = ch * w bytes_per_line = ch * w
convert_to_Qt_format = QtGui.QImage( qt_img = QImage(cv_img.data, w, h, bytes_per_line, QImage.Format_BGR888)
rgb_image.data, w, h, bytes_per_line, QtGui.QImage.Format_RGB888
) if w != self.width() or h != self.height():
p = convert_to_Qt_format.scaled( qt_img = qt_img.scaled(
self.width(), self.width(), self.height(), self.aspect_ratio, self.resize_mode
self.height(), )
self.aspect_ratio,
self.resize_mode, self.label.setPixmap(QPixmap.fromImage(qt_img))
)
return QPixmap.fromImage(p)
@click.command() @click.command()