当前位置: 首页 > news >正文

连云港做网站制作首选公司chatgpt网站

连云港做网站制作首选公司,chatgpt网站,网站建设合同中英文模板,北京市房屋和城乡建设委员会网站文章目录 1. 关键问题描述2. 通过OpenCV读取视频/打开摄像头抓取视频3. 通过PyQt 中的 QTimer定时器实现视频播放4. PyQt 视频播放器实现代码参考文献 1. 关键问题描述 在前面的文章中已经分享了pyqt制作图像处理工具的文章,也知道pyqt通过使用label控件显示图像的…

文章目录

    • 1. 关键问题描述
    • 2. 通过OpenCV读取视频/打开摄像头抓取视频
    • 3. 通过PyQt 中的 QTimer定时器实现视频播放
    • 4. PyQt 视频播放器实现代码
    • 参考文献

1. 关键问题描述

在前面的文章中已经分享了pyqt制作图像处理工具的文章,也知道pyqt通过使用label控件显示图像的方式。在此,对于视频的显示,其本质上一帧一帧的图像,因此也可以使用同样的方式对其显示。但是,有两个关键的问题需要解决,也即:
a. 如何读取视频/或通过摄像头抓取视频
b. 如果连续显示视频图像且不会造成界面假死

2. 通过OpenCV读取视频/打开摄像头抓取视频

主要函数:

cv.VideoCapture.isOpened(),检查视频捕获是否初始化成功
cv.VideoCapture.read(),捕获视频文件、视频流或捕获的视频设备
cv.VideoCapture.release(),关闭视频文件或设备,释放对象
cv.VideoWriter.release(),关闭视频写入,释放对象

抓取视频

import cv2
import timecap = cv2.VideoCapture(0, cv2.CAP_DSHOW) # 使用cv2.CAP_DSHOW后启动快,但帧率慢了
cap.set(6, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
# cap.set(6, cv2.VideoWriter.fourcc('Y', 'U', 'V', '2'))# 使用cv2.CAP_DSHOW后大小为640
cap.set(3, 2560)  # 640 1280 1920  2560# 宽
# cap.set(4, 720)   # 720  1080  # 高
# cap.set(5, 60)  # 帧数
# cap.set(10, 150)  # 亮度
# cap.set(14, 150)  # 增益
# cap.set(11, 90)  # 对比度
# cap.set(15, 150)  # 曝光print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(cap.get(cv2.CAP_PROP_FPS))num = 0
start = time.time()
fps = '0'
while(True):ret, frame = cap.read()num = num + 1frame = cv2.flip(frame, 1)# frame = beauty_face(frame)if (time.time() - start) > 1:fps = num / (time.time() - start)fps = str(round(fps, 2))num = 0start = time.time()cv2.putText(frame, "FPS:" + fps, (20, 20), 1, 1.5, (255, 255, 255), 2)cv2.imshow("DST", frame)stop = time.time()elapsed = stop - start# print('time=', elapsed)if 1 < elapsed < 1.5:print('time=', elapsed)print('frame num:', num)if cv2.waitKey(1) & 0xFF == ord('q'):break
cap.release()
cv2.destroyAllWindows()

读取视频

import cv2 as cvif __name__ == '__main__':# 创建视频读取/捕获对象vedioRead = "../images/test.mp4"  # 读取视频文件的路径capRead = cv.VideoCapture(vedioRead)  # 实例化 VideoCapture 类# 读取视频文件frameNum = 0  # 视频帧数初值while capRead.isOpened():  # 检查视频捕获是否成功ret, frame = capRead.read()  # 读取下一帧视频图像if ret is True:cv.imshow(vedioRead, frame)  # 播放视频图像if cv.waitKey(1) & 0xFF == ord('q'):  # 按 'q' 退出breakelse:print("Can't receive frame at frameNum {}".format(frameNum))breakcapRead.release()  # 关闭读取视频文件capWrite.release()  # 关闭视频写入对象cv.destroyAllWindows()  # 关闭显示窗口

3. 通过PyQt 中的 QTimer定时器实现视频播放

使用 OpenCV 对视频文件进行解码获得图像帧以后,可以使用 QTime 定时器来控制 QLabel 控件中的图像更新,实现视频播放。
PyQt5 中的 QTimer类提供了重复的和单次的定时器,为计时器提供了高级编程接口。
要使用定时器,需要先创建一个QTimer实例,将定时器的timeout信号连接到相应的槽函数,并调用start(),定时器就会以设定的间隔发出timeout信号。

QTimer类中的常用方法:

start(milliseconds):启动或重新启动定时器,时间间隔为毫秒。如果定时器已经运行,它将被停止并重新启动。如果singleShot信号为真,定时器将仅被激活一次。
stop():停止定时器

QTimer类中的常用信号:
singleShot:在给定的时间间隔后调用一个槽函数时发射此信号。
timeout:当定时器超时时发射此信号。

注意:可以设置槽函数的执行次数,默认为定时器开启后周期性调用槽函数。如果设置了setSingleShot(True),则槽函数仅执行一次。

关键代码:

# 初始化定时器,并绑定触发信号
self.timerCam = QtCore.QTimer()  # 定时器,毫秒
self.timerCam.timeout.connect(self.refreshFrame)  # 计时器结束时调用槽函数刷新当前帧# 开始定时
if self.cap.isOpened():      # 检查视频捕获是否成功self.timerCam.start(20)  # 设置计时间隔并启动,定时结束将触发刷新当前帧
# 暂停播放
self.timerCam.blockSignals(True)  # 信号阻塞,暂停定时器
# 继续播放
self.timerCam.blockSignals(False)  # 取消信号阻塞,恢复定时器

4. PyQt 视频播放器实现代码

import os
import sys
import cv2 as cv
import numpy as np
from PyQt5 import QtCore
from PyQt5.QtCore import QObject, pyqtSignal, QPoint, QRect, qDebug, Qt
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from ui_VideoProV1 import Ui_MainWindow  # 导入 uiDemo8.py 中的 Ui_MainWindow 界面类class MyMainWindow(QMainWindow, Ui_MainWindow):  # 继承 QMainWindow 类和 Ui_MainWindow 界面类def __init__(self, parent=None):super(MyMainWindow, self).__init__(parent)  # 初始化父类self.setupUi(self)  # 继承 Ui_MainWindow 界面类self.timerCam = QtCore.QTimer()  # 定时器,毫秒self.cap = Noneself.frameNum = 1  # 视频帧数初值self.edge_flag = Falseself.smooth_flag = False# 添加可点击执行的菜单self.menu_file = QMenu("文件", self)temp = self.menuBar()temp.addMenu(self.menu_file)t = self.menu_fileself.action_open = QAction("打开", self)self.action_open.triggered.connect(self.openVideo)t.addAction(self.action_open)self.action_save = QAction("保存", self)t.addAction(self.action_save)t.addAction('其他')self.mTest = QAction("帮助", self)self.mTest.triggered.connect(self.trigger_actHelp)temp = self.menuBar()temp.addAction(self.mTest)self.action_close = QAction("退出", self)self.action_close.triggered.connect(self.close)temp = self.menuBar()temp.addAction(self.action_close)## # 菜单栏self.action_save.triggered.connect(self.saveSlot)  # 连接并执行 openSlot 子程序# 通过 connect 建立信号/槽连接,点击按钮事件发射 triggered 信号,执行相应的子程序 click_pushButtonself.pushButton.clicked.connect(self.openVideo)self.pushButton_2.clicked.connect(self.playVideo)self.pushButton_3.clicked.connect(self.pauseVideo)self.pushButton_4.clicked.connect(self.click_pushButton_5)  # # 按钮触发:边缘检测self.pushButton_5.clicked.connect(self.click_pushButton_6)  # 点击 # 按钮触发:双边self.timerCam.timeout.connect(self.refreshFrame)  # 计时器结束时调用槽函数刷新当前帧# 初始化self.frame = np.ndarray(())self.videoPath = 'test'self.textEdit_log.append("欢迎回来!")returndef openVideo(self):  # 读取视频文件,点击 pushButton_1 触发try:self.videoPath, _ = QFileDialog.getOpenFileName(self, "Open Video", "../images/", "*.mp4 *.avi *.flv")print("Open Video: ", self.videoPath)except:print("Open video failed.")returndef playVideo(self):  # 播放视频文件,点击 pushButton_2 触发if self.timerCam.isActive() == False:if self.videoPath.endswith(('avi', 'mp4')):self.cap = cv.VideoCapture(self.videoPath)self.textEdit_log.append('open video successfully')else:self.cap = cv.VideoCapture(0)self.textEdit_log.append('open camera successfully')if self.cap.isOpened():      # 检查视频捕获是否成功self.timerCam.start(20)  # 设置计时间隔并启动,定时结束将触发刷新当前帧else:  #self.timerCam.stop()  # 停止定时器self.cap.release()    # 关闭读取视频文件self.label_1.clear()  # 清除显示内容returndef pauseVideo(self):self.timerCam.blockSignals(False)  # 取消信号阻塞,恢复定时器if self.timerCam.isActive() and self.frameNum % 2 == 1:self.timerCam.blockSignals(True)  # 信号阻塞,暂停定时器self.pushButton_3.setText("继续")  # 点击"继续",恢复播放print("信号阻塞,暂停播放。", self.frameNum)self.textEdit_log.append("信号阻塞,暂停播放。" + str(self.frameNum))else:self.pushButton_3.setText("暂停")  # 点击"暂停",暂停播放print("取消阻塞,恢复播放。", self.frameNum)self.textEdit_log.append("取消阻塞,恢复播放。" + str(self.frameNum))self.frameNum = self.frameNum + 1def refreshFrame(self):  # 刷新视频图像ret, self.frame = self.cap.read()  # 读取下一帧视频图像self.frame = cv.flip(self.frame, 1)if self.smooth_flag:self.frame = cv.bilateralFilter(self.frame, 15, 30, 40, None)if self.edge_flag:# self.frame = cv.Canny(self.frame, 10, 60)self.frame = self.apply_heat_effect(self.frame)qImg = self.cvToQImage(self.frame)  # OpenCV 转为 PyQt 图像格式self.label_1.setScaledContents(True)self.label_1.setPixmap((QPixmap.fromImage(qImg)))  # 加载 PyQt 图像# self.frameNum = self.frameNum + 1# print("视频帧数:", self.frameNum)returndef cvToQImage(self, image):if image.dtype == np.uint8:channels = 1 if len(image.shape) == 2 else image.shape[2]if channels == 3:qImg = QImage(image, image.shape[1], image.shape[0], image.strides[0], QImage.Format_RGB888)return qImg.rgbSwapped()elif channels == 1:qImg = QImage(image, image.shape[1], image.shape[0], image.strides[0], QImage.Format_Indexed8)return qImgelse:QtCore.qDebug("ERROR: numpy.ndarray could not be converted to QImage. Channels = %d" % image.shape[2])return QImage()def click_pushButton_5(self):if self.edge_flag:self.edge_flag = Falseself.textEdit_log.append('close edge mode successfully')else:self.edge_flag = Trueself.textEdit_log.append('open edge mode successfully')returndef click_pushButton_6(self):if self.smooth_flag:self.smooth_flag = Falseself.textEdit_log.append('close smooth mode successfully')else:self.smooth_flag = Trueself.textEdit_log.append('open smooth mode successfully')returndef saveSlot(self):  # 保存图像文件# 选择存储文件 dialogtry:fileName, tmp = QFileDialog.getSaveFileName(self, "Save Image", "../images/", '*.png; *.jpg; *.tif')if self.frame.size == 1:return# OpenCV 写入图像文件ret = cv.imwrite(fileName, self.frame)if ret:print(fileName, self.frame.shape)except:print('save failed.')returndef cvToQImage(self, image):# 8-bits unsigned, NO. OF CHANNELS=1if image.dtype == np.uint8:channels = 1 if len(image.shape) == 2 else image.shape[2]if channels == 3:  # CV_8UC3# Create QImage with same dimensions as input MatqImg = QImage(image, image.shape[1], image.shape[0], image.strides[0], QImage.Format_RGB888)return qImg.rgbSwapped()elif channels == 1:# Create QImage with same dimensions as input MatqImg = QImage(image, image.shape[1], image.shape[0], image.strides[0], QImage.Format_Indexed8)return qImgelse:qDebug("ERROR: numpy.ndarray could not be converted to QImage. Channels = %d" % image.shape[2])return QImage()def qPixmapToCV(self, qPixmap):  # PyQt图像 转换为 OpenCV图像qImg = qPixmap.toImage()  # QPixmap 转换为 QImageshape = (qImg.height(), qImg.bytesPerLine() * 8 // qImg.depth())shape += (4,)ptr = qImg.bits()ptr.setsize(qImg.byteCount())image = np.array(ptr, dtype=np.uint8).reshape(shape)  # 定义 OpenCV 图像image = image[..., :3]return imagedef trigger_actHelp(self):  # 动作 actHelp 触发QMessageBox.about(self, "About", """打开视频或摄像头 v1.0""")returndef closeEvent(self):self.timerCam.stop()  # 停止定时器self.cap.release()  # 关闭读取视频文件self.label_1.clear()  # 清除显示内容if __name__ == '__main__':app = QApplication(sys.argv)myWin = MyMainWindow()myWin.show()sys.exit(app.exec_())

在这里插入图片描述

参考文献

[1] OpenCV-PyQT项目实战(9)项目案例04:视频播放


文章转载自:
http://dinncoforaminate.bkqw.cn
http://dinncosupplementarity.bkqw.cn
http://dinncooutspan.bkqw.cn
http://dinncopalatine.bkqw.cn
http://dinncosporulate.bkqw.cn
http://dinncohindmost.bkqw.cn
http://dinncovicariance.bkqw.cn
http://dinncoallocator.bkqw.cn
http://dinncocirrous.bkqw.cn
http://dinncoadscititious.bkqw.cn
http://dinncofondu.bkqw.cn
http://dinncosolano.bkqw.cn
http://dinncohandily.bkqw.cn
http://dinncodisquietingly.bkqw.cn
http://dinncoorthocharmonium.bkqw.cn
http://dinncohandpick.bkqw.cn
http://dinncotranspositional.bkqw.cn
http://dinncomisdiagnose.bkqw.cn
http://dinncosovranty.bkqw.cn
http://dinncodogskin.bkqw.cn
http://dinncogozitan.bkqw.cn
http://dinncoamplificatory.bkqw.cn
http://dinncobardia.bkqw.cn
http://dinncopeshito.bkqw.cn
http://dinncomyself.bkqw.cn
http://dinncodrastically.bkqw.cn
http://dinncopostern.bkqw.cn
http://dinncoforbye.bkqw.cn
http://dinncoshillelah.bkqw.cn
http://dinncoexcuse.bkqw.cn
http://dinncounderwriter.bkqw.cn
http://dinncoreentry.bkqw.cn
http://dinncokeos.bkqw.cn
http://dinncoquim.bkqw.cn
http://dinncoadversely.bkqw.cn
http://dinncolimey.bkqw.cn
http://dinncotrueborn.bkqw.cn
http://dinncomatador.bkqw.cn
http://dinncoeca.bkqw.cn
http://dinncosyllepsis.bkqw.cn
http://dinncogalumph.bkqw.cn
http://dinncopdry.bkqw.cn
http://dinncocommence.bkqw.cn
http://dinncoshuggy.bkqw.cn
http://dinncoquantification.bkqw.cn
http://dinncogeyser.bkqw.cn
http://dinncorearrangement.bkqw.cn
http://dinnconeurogenetics.bkqw.cn
http://dinncodript.bkqw.cn
http://dinncosaxtuba.bkqw.cn
http://dinncoallot.bkqw.cn
http://dinncounfixed.bkqw.cn
http://dinncoacuate.bkqw.cn
http://dinncoschatz.bkqw.cn
http://dinncobenthoscope.bkqw.cn
http://dinncorotograph.bkqw.cn
http://dinncoappetite.bkqw.cn
http://dinncodonnish.bkqw.cn
http://dinncorisible.bkqw.cn
http://dinncodaylights.bkqw.cn
http://dinncobrahmacharya.bkqw.cn
http://dinncocontrariness.bkqw.cn
http://dinncolaconical.bkqw.cn
http://dinncohebetude.bkqw.cn
http://dinncothunderation.bkqw.cn
http://dinncobacterial.bkqw.cn
http://dinncoscratchy.bkqw.cn
http://dinncoonthe.bkqw.cn
http://dinncoglanderous.bkqw.cn
http://dinncoberserkly.bkqw.cn
http://dinncotrollop.bkqw.cn
http://dinncocontoid.bkqw.cn
http://dinncoruin.bkqw.cn
http://dinncosurvivance.bkqw.cn
http://dinncogunrunning.bkqw.cn
http://dinncocyathiform.bkqw.cn
http://dinncohost.bkqw.cn
http://dinncoargyria.bkqw.cn
http://dinncomisspend.bkqw.cn
http://dinncodemocratic.bkqw.cn
http://dinncoremainder.bkqw.cn
http://dinncoexculpation.bkqw.cn
http://dinncounbelted.bkqw.cn
http://dinncointeratomic.bkqw.cn
http://dinncoantipathy.bkqw.cn
http://dinncodoubledome.bkqw.cn
http://dinncomedically.bkqw.cn
http://dinncotampax.bkqw.cn
http://dinncodisregardfulness.bkqw.cn
http://dinncoracemiferous.bkqw.cn
http://dinncoregicide.bkqw.cn
http://dinncotherefor.bkqw.cn
http://dinncogarreteer.bkqw.cn
http://dinncodartist.bkqw.cn
http://dinncopeke.bkqw.cn
http://dinncoankle.bkqw.cn
http://dinncoprepuberal.bkqw.cn
http://dinncoespieglerie.bkqw.cn
http://dinncoesophagitis.bkqw.cn
http://dinncosedateness.bkqw.cn
http://www.dinnco.com/news/128339.html

相关文章:

  • 门户网站要用什么软件做百度最容易收录的网站
  • 八上电脑课做网站需要什么软件绍兴seo排名收费
  • 佛山网站建设永网aso优化服务站
  • 花都有沒有网站建设的鹤壁搜索引擎优化
  • 建筑公司网站董事长致辞可以免费做网站推广的平台
  • 公司网站首页设计球队排名榜实时排名
  • 信誉好的龙岗网站设计seo门户网价格是多少钱
  • 做五金找订单查什么网站网络宣传策划方案
  • 网站建设与网页设计论文深圳推广不动产可视化查询
  • 漳州网站建设哪家最权威seo是什么?
  • 网站结构该怎么做百度app登录
  • 做网站拉广告江西省seo
  • 西部空间官方网站刷赞业务推广网站
  • 东莞疫情防控最新政策安卓系统优化app
  • 广州做网站优化公司报价推广方案有哪些
  • 有哪些网站可以做设计竞标推广平台软件有哪些
  • 做淘宝客网站制作教程视频培训机构网站制作
  • 酒泉做网站win7系统优化大师
  • 网络绿化网站建设哪家专业内江seo
  • 江门做网站多少钱seo诊断书案例
  • 南宁商城网站建设百度快速收录办法
  • python代码网站体验营销策划方案
  • 公司网站设计100种宣传方式
  • 中国建设工程鲁班奖查询网站百度快速排名软件
  • 彩页设计制作seo关键词排名优化报价
  • 生产管理软件哪家好西安seo黑
  • 用javaee做的网站模板社群营销
  • 大连模板网站制作公司电话站长之家最新网站
  • 句容建设质检站网站凡科建站教程
  • 自己做网站免费优秀的网络搜索引擎营销案例