本案例介紹如何打開USB相機并每隔100ms進行刷新的代碼,效果如下:
一、主要思路:
1. 打開視頻流、讀取幀
self.cam_cap = cv2.VideoCapture(0) #打開 視頻流
cam_ret, cam_frame = self.cam_cap.read() //讀取幀。
2.使用定時器,每隔100ms讀取幀
3.顯示到Qt的QLabel控件上。
二、代碼如下:
import os
import sys
from PySide6.QtCore import Qt, QTimer
import cv2
from PySide6.QtGui import QImage, QPixmap
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QMessageBox, QLabel, QHBoxLayout
from utils.ImageUtil import show_cvimage_to_qlabeldef show_cvimage_to_qlabel(frame, qlabel):w = qlabel