害嗨嗨!功能做出来辣!
每隔一秒截一次视频画面,而且视频一直播放,直到截取5张为止,把截取到的图片存在指定文件夹中,以选定的项目命名,芜湖!单方面宣布我PyQt5基础部分毕业!
打包!打包打包!
from PyQt5 import QtWidgets, uic, QtCore, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import QPalette, QBrush, QPixmap
import time
import cv2
import sys
import os
paths=[]
class Stats:
def __init__(self):
self.ui = uic.loadUi("a.ui")
self.timer_camera = QtCore.QTimer() #定义定时器,用于控制显示视频的帧率
self.timer_camera1 = QtCore.QTimer()
self.ui.start.clicked.connect(self.button_start)
self.ui.location.clicked.connect(self.Output_File)
self.ui.checkButton.clicked.connect(self.button_open_camera_clicked)
self.timer_camera.timeout.connect(self.show_camera) #若定时器结束,则调用show_camera()
self.timer_camera1.timeout.connect(self.Input_File)
self.cap = cv2.VideoCapture() #视频流
self.CAM_NUM = "C:\\Users\\223072088\\Desktop\\CTP.mp4" #为0时表示视频流来自笔记本内置摄像头
def button_start(self):
if self.timer_camera1.isActive()==False :
self.timer_camera1.start(1000)
def button_open_camera_clicked(self):
if self.timer_camera.isActive() == False: #若定时器未启动
flag = self.cap.open(self.CAM_NUM) #参数是0,表示打开笔记本的内置摄像头,参数是视频文件路径则打开视频
self.timer_camera.start(30) #定时器开始计时30ms,结果是每过30ms从摄像头中取一帧显示
self.ui.signal_graph.setStyleSheet('''background-color:rgb(0, 255, 0)''')
self.ui.checkButton.setText('关闭相机')
else:
self.timer_camera.stop() #关闭定时器
self.cap.release() #释放视频流
self.ui.cameraView.clear() #清空视频显示区域
self.ui.signal_graph.setStyleSheet('''background-color:rgb(255, 255, 0)''')
self.ui.checkButton.setText('打开相机')
def show_camera(self):
flag,self.image = self.cap.read() #从视频流中读取
show = cv2.resize(self.image,(400,300)) #把读到的帧的大小重新设置为 400x300
show = cv2.cvtColor(show,cv2.COLOR_BGR2RGB) #视频色彩转换回RGB,这样才是现实的颜色
self.showImage = QtGui.QImage(show.data,show.shape[1],show.shape[0],QtGui.QImage.Format_RGB888) #把读取到的视频数据变成QImage形式
self.ui.cameraView.setPixmap(QtGui.QPixmap.fromImage(self.showImage)) #往显示视频的Label里 显示QImage
def Output_File(self):
directory=QtWidgets.QFileDialog.getExistingDirectory(None, "请选择文件夹路径","D:/")
paths.append(directory)
self.ui.textEdit.setText(directory)
def Input_File(self):
#这里就是把每个选择了的都加到字符串里,
#就是把每个组框里的单选框都遍历直到发现点选了的单选框,
#就看着有点愚笨,暂时没想到别的好方法
addstr=''
for checkbox in self.ui.groupBox_1.findChildren(QtWidgets.QRadioButton):
if checkbox.isChecked():
addstr+=checkbox.text()
break
for checkbox in self.ui.groupBox_2.findChildren(QtWidgets.QRadioButton):
if checkbox.isChecked():
addstr+=checkbox.text()
break
for checkbox in self.ui.groupBox_3.findChildren(QtWidgets.QRadioButton):
if checkbox.isChecked():
addstr+=checkbox.text()
break
for checkbox in self.ui.groupBox_4.findChildren(QtWidgets.QRadioButton):
if checkbox.isChecked():
addstr+=checkbox.text()
break
for checkbox in self.ui.groupBox_5.findChildren(QtWidgets.QRadioButton):
if checkbox.isChecked():
addstr+=checkbox.text()
break
for checkbox in self.ui.groupBox_6.findChildren(QtWidgets.QRadioButton):
if checkbox.isChecked():
addstr+=checkbox.text()
break
self.showImage.save(str(self.ui.textEdit.toPlainText()) + "/" + addstr + str(self.ui.spinBox.value())+".jpg", "jpg",100);
self.ui.textBrowser.insertPlainText(str(self.ui.textEdit.toPlainText()) + "/" + addstr + str(self.ui.spinBox.value())+".jpg")
a = int(self.ui.spinBox.value())
a=a+1
self.ui.spinBox.setValue(a)
if a>5:
self.timer_camera1.stop() #这个stop我真是想破了脑汁才想出来,一些愚蠢=_=
app = QApplication([])
stats = Stats()
stats.ui.show()
app.exec_()
界面:
诶?我就不给你看,想不到吧!
害嗨嗨!
每天学一点点,就能少菜一点点,加油( •̀ ω •́ )y