QLabelCV——專門給OpenCV庫做的PyQt QLabel控制元件
很多小伙伴在用PyQt和OpenCV時,不知道如何將視頻幀顯示到QLabel控制元件上,筆者這里寫了一個控制元件,可以讓大家一步做到,同時也增加了其他功能,具體請看下文:
github地址:https://github.com/la-vie-est-belle/QLabelCV
簡單示例
from PyQt5.QtWidgets import QApplication
from QLabelCV import QLabelCV
import sys
if __name__ == '__main__':
app = QApplication(sys.argv)
label_cv = QLabelCV()
label_cv.show()
label_cv.set_camera(0)
sys.exit(app.exec_())
只需要使用set_camera() 就能將視頻幀顯示到QLabel控制元件上,可傳入不同的整數來使用不同的攝像頭設備,內部就是通過cv2.VideoCapture()來實作的,

可以直接在螢屏上畫出ROI區域:

可以使用 get_rect()獲取ROI矩形的坐標:
>>> print(label_cv.get_rect())
(161, 72, 316, 228)
或者這樣:
>>> print(label_cv.roi_top_left_x)
161
>>> print(label_cv.roi_top_left_y)
72
>>> print(label_cv.roi_bottom_right_x)
316
>>> print(label_cv.roi_bottom_right_y)
228
可以使用 save_frame()將幀保存到本地:
>>> label_cv.save_frame(path='frame.jpg')
如果想要在圖上留有ROI區域,可以傳入True:
>>> label_cv.save_frame(path='frame.jpg', with_roi_rect=True)
除此之外, 你還可以直接優化圖片,點擊螢屏上的小箭頭,呼出影像調節區域:


你可以在調節區域中調整影像的對比度、亮度、銳化程度和模糊度,
程式示例:
from PyQt5.QtWidgets import QApplication, QWidget,QLabel, QPushButton, QSpinBox, QCheckBox, QHBoxLayout, QVBoxLayout
from QLabelCV import QLabelCV
import sys
class Demo(QWidget):
def __init__(self):
super(Demo, self).__init__()
self.resize(640, 430)
self.label_cv = None
self.device_spin = None
self.screenshot_btn = None
self.print_coords_btn = None
self.with_roi_rect_checkbox = None
self.main()
def main(self):
self.init_ui()
def init_ui(self):
self.generate_widgets()
self.connect_signals()
self.set_layouts()
def generate_widgets(self):
self.label_cv = QLabelCV()
self.label_cv.set_camera(0)
self.device_spin = QSpinBox()
self.screenshot_btn = QPushButton('ScreenShot')
self.print_coords_btn = QPushButton('Print Coordinates')
self.with_roi_rect_checkbox = QCheckBox('Save ROI Rect')
def connect_signals(self):
self.device_spin.valueChanged.connect(self.set_camera)
self.screenshot_btn.clicked.connect(self.screenshot)
self.print_coords_btn.clicked.connect(self.print_coordinates)
def set_layouts(self):
v_layout = QVBoxLayout()
h_layout = QHBoxLayout()
h_layout.addWidget(QLabel('Device:'))
h_layout.addWidget(self.device_spin)
h_layout.addStretch(1)
h_layout.addWidget(self.screenshot_btn)
h_layout.addWidget(self.with_roi_rect_checkbox)
h_layout.addStretch(1)
h_layout.addWidget(self.print_coords_btn)
v_layout.addWidget(self.label_cv)
v_layout.addLayout(h_layout)
self.setLayout(v_layout)
def set_camera(self):
self.label_cv.set_camera(self.device_spin.value())
def screenshot(self):
if self.with_roi_rect_checkbox.isChecked():
self.label_cv.save_frame('frame.jpg', True)
else:
self.label_cv.save_frame('frame.jpg')
def print_coordinates(self):
print('roi rect: ', self.label_cv.get_rect())
print('roi_top_left_x: ', self.label_cv.roi_top_left_x)
print('roi_top_left_y: ', self.label_cv.roi_top_left_y)
print('roi_bottom_left_x: ', self.label_cv.roi_bottom_right_x)
print('roi_bottom_left_y: ', self.label_cv.roi_bottom_right_y)
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = Demo()
demo.show()
sys.exit(app.exec_())

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/291776.html
標籤:AI
