class MainLogic(QWidget,Ui_Form,ParseData): def __init__(self): super().__init__() self.setWindowFlags(QtCore.Qt.FramelessWindowHint) self.setAttribute(QtCore.Qt.WA_TranslucentBackground) # 设置窗口背景透明 self.setWindowIcon(QIcon("source/logo1.png")) self.setupUi(self) self.widget_style() self.setCursor(Qt.ArrowCursor) def setVideo(self,url): self.player = QMediaPlayer() self.player.positionChanged.connect(self.playSlide) #进度条 self.play_sld.sliderMoved.connect(self.changeSlide) #进度条 self.video_widget = QVideoWidget(self.video_box) self.video_widget.setGeometry(0,0,100,100) self.video_widget.setAspectRatioMode(1) self.video_widget.show() self.verticalLayout_4.addWidget(self.video_widget) self.player.setMedia(QMediaContent(QUrl(url))) self.player.setVolume(50) #设置声音大小 self.player.setVideoOutput(self.video_widget) self.player.pause() def parse_video(self): try: self.parse_url(self.parse_le.text()) self.get_data() self.setVideo(self.play_addr) self.insertData() self.clear_btn.setEnabled(True) self.play_btn.setEnabled(True) self.download_btn.setEnabled(True) self.parse_btn.setEnabled(False) except: QMessageBox.critical(self, "解析失败", "请检查当前网络链接\n或者检查分享链接是否输入正确") def widget_style(self): self.min_btn = QPushButton(qtawesome.icon('fa.window-minimize', color='#ddd'), "",self.widget_3) self.min_btn.setMaximumSize(QtCore.QSize(50, 16777215)) self.min_btn.setStyleSheet("margin: 12px;padding: 2px 0;background:#b99910;border-radius:5px;") self.min_btn.setText("") self.min_btn.setAutoRepeatDelay(300) self.min_btn.setObjectName("min_btn") self.horizontalLayout.addWidget(self.min_btn) self.close_btn = QPushButton(qtawesome.icon('fa.window-close', color='#ddd'),'',self.widget_3) self.close_btn.setMaximumSize(QtCore.QSize(50, 16777215)) self.close_btn.setStyleSheet("margin: 12px;padding: 2px 0;background:#bf3b3b;border-radius:5px;") self.close_btn.setText("") self.close_btn.setObjectName("close_btn") self.horizontalLayout.addWidget(self.close_btn) self.min_btn.clicked.connect(self.showMinimized) self.close_btn.clicked.connect(self.close) self.parse_btn_2.setIcon(QIcon('source/parse.ico')) self.introduce_label.setAlignment(Qt.AlignTop) self.desc_label.setAlignment(Qt.AlignTop) def clear_data(self): self.clear_btn.setEnabled(False) self.play_btn.setEnabled(False) self.download_btn.setEnabled(False) self.parse_btn.setEnabled(True) self.player.deleteLater() self.video_widget.deleteLater() self.avatar_label.clear() self.author_name_label.clear() self.douyin_id_label.clear() self.introduce_label.clear() self.desc_label.clear() self.create_time_label.clear() self.video_size_label.clear() self.play_btn.setText('播放') self.play_sld.setValue(0) self.video_time.setText("00:00/00:00") def insertData(self): avatar = requests.get(self.author_avatar_addr,headers = self.headers).content img = QPixmap() img.loadFromData(avatar) self.avatar_label.setPixmap(img) self.avatar_label.setScaledContents(True) self.response = requests.get(self.play_addr, headers=self.headers) self.video_size = str(format(int(self.response.headers['Content-Length']) / 1048576, ".2f") + " Mb") self.author_name_label.setText(self.author_nickname) self.douyin_id_label.setText(self.author_unique_id) self.introduce_label.setText(self.author_signature) self.desc_label.setText(self.douyin_desc) self.video_size_label.setText(self.video_size) self.create_time_label.setText(str(self.douyin_create_time)) def playSlide(self,position): self.vidoeLength = self.player.duration() + 0.01 videoLength = divmod(round(position / 1000), 60) self.video_time.setText(f'{str("%02d:%02d" % (videoLength[0],videoLength[1]))}/{self.video_duration}') self.play_sld.setValue(round((position/self.vidoeLength)*100)) if self.vidoeLength > 0.01 and round(position) == round(self.vidoeLength): #判断是否播放结束 self.play_btn.setText('播放') self.play_sld.setValue(0) def changeSlide(self): self.play_position = round(self.play_sld.value()/100 * self.v_length) self.player.setPosition(self.play_position) def play(self): if self.play_btn.text() == '播放' and self.vidoeLength > 0.01: self.play_btn.setText('暂停') self.player.play() else: self.play_btn.setText('播放') self.player.pause() def download(self): fw = open(self.desc_label.text() + '.mp4', "wb") chunk_size = 1048576 r = requests.get(self.play_addr, headers=self.headers, stream=True) filesize = self.response.headers['Content-Length'] if filesize is None: fw.write(r.content) else: dl = 0 total_length = int(filesize) t1 = time.time() for data in r.iter_content(chunk_size): dl += len(data) show = dl / total_length fw.write(data) t2 = time.time() t = t2 - t1 speed = dl / 1024 / 1024 / t self.progressBar.setValue(show * 100) self.download_speed_label.setText(f'{str(speed)[0:4]}M/s') def mouseMoveEvent(self, e: QMouseEvent): # 重写移动事件 try: self._endPos = e.pos() - self._startPos self.move(self.pos() + self._endPos) except: pass def mousePressEvent(self, e: QMouseEvent): if e.button() == QtCore.Qt.LeftButton: self._isTracking = True self._startPos = QtCore.QPoint(e.x(), e.y()) def mouseReleaseEvent(self, e: QMouseEvent): if e.button() == QtCore.Qt.LeftButton: self._isTracking = False self._startPos = None self._endPos = None
class Ui_Dialog(QtWidgets.QDialog): def __init__(self, parent=None): super().__init__(parent) #父类的构造函数 self.model, flag = net.load_model(use_gpu=False) #加载模型 self.timer_camera = QtCore.QTimer() #定义定时器,用于控制显示视频的帧率 self.cap = cv2.VideoCapture() #视频流 self.CAM_NUM = 0 #为0时表示视频流来自笔记本内置摄像头 self.danceMatching = DanceMatching() #实例化一个姿态检测类,也是线程类 self.setupUi() self.slot_init() def setupUi(self): self.resize(1386, 747) font = QtGui.QFont() font.setPointSize(25) self.setFont(font) self.pushButton = QtWidgets.QPushButton(self) self.pushButton.setGeometry(QtCore.QRect(180, 670, 301, 41)) self.pushButton_2 = QtWidgets.QPushButton(self) self.pushButton_2.setGeometry(QtCore.QRect(890, 670, 291, 41)) self.label = QtWidgets.QLabel(self) self.label.setGeometry(QtCore.QRect(33, 21, 1301, 51)) font = QtGui.QFont() font.setPointSize(25) self.label.setFont(font) self.label.setAlignment(QtCore.Qt.AlignCenter) self.label.setObjectName("label") #摄像视频组件设定 self.label_show_camera = QtWidgets.QLabel(self) #定义显示视频的Label self.label_show_camera.setGeometry(QtCore.QRect(20, 80, 670, 480)) #视频播放组件设定 self.video_widget = QVideoWidget(self) # 1 self.video_widget.setGeometry(700, 80, 670, 480) self.player = QMediaPlayer(self) self.player.setVideoOutput(self.video_widget) # 2 self.player.setVolume(80) self.retranslateUi() QtCore.QMetaObject.connectSlotsByName(self) def retranslateUi(self): self.setWindowTitle("Dialog") self.pushButton.setText("开始") self.pushButton_2.setText("返回") self.label.setText("hello world!") '''初始化所有槽函数''' def slot_init(self): self.pushButton.clicked.connect( self.button_open_camera_clicked ) #若该按键被点击,则调用button_open_camera_clicked() self.timer_camera.timeout.connect( self.show_camera) #若定时器结束,则调用show_camera() #self.pushButton_2.clicked.connect(self.close)#若该按键被点击,则调用close(),注意这个close是父类QtWidgets.QWidget自带的,会关闭程序 self.danceMatching.update_date.connect(self.change_label) self.danceMatching.now_to_getImages.connect(self.get_camera_images) '''槽函数之一''' def change_label(self, poseScore): ''' 通过传入的匹配得分,来修改label的显示 ''' showStr = '小脑有问题' if poseScore == 0: showStr = '小脑有问题' elif poseScore == 2: showStr = "再接再厉" elif poseScore == 4: showStr = '有点意思' elif poseScore == 6: showStr = "哎呦,不错哦!" elif poseScore == 8: showStr = '完美!!' elif poseScore == 10: showStr = '此舞只应天上有,人间哪得几回闻。' showStr = showStr + ' score + ' + str(poseScore) self.label.setText(showStr) def button_open_camera_clicked(self): ''' 打开摄像头,并同时播放视频,同时进行姿态检测 ''' if self.timer_camera.isActive() == False: #若定时器未启动 flag = self.cap.open( self.CAM_NUM) #参数是0,表示打开笔记本的内置摄像头,参数是视频文件路径则打开视频 if flag == False: #flag表示open()成不成功 msg = QtWidgets.QMessageBox.warning( self, 'warning', "请检查相机于电脑是否连接正确", buttons=QtWidgets.QMessageBox.Ok) else: self.pushButton.setText('关闭') self.timer_camera.start(30) #定时器开始计时30ms,结果是每过30ms从摄像头中取一帧显示 time.sleep(0.03) self.player.setMedia( QMediaContent(QUrl.fromLocalFile(gl.GL_VPath))) #设置视频资源 self.player.play() #视频播放 self.danceMatching.start() #开始姿态检测 else: self.timer_camera.stop() #关闭定时器 self.cap.release() #释放视频流 self.label_show_camera.clear() #清空视频显示区域 self.player.pause() #视频暂停 self.player.setPosition(0) #设置播放进度为初始 self.danceMatching.isEnd = True #设置线程需要被关闭,以达到关闭姿态匹配线程的目的 self.pushButton.setText('开始') def show_camera(self): ''' 读取摄像头的一帧图片,并显示 ''' flag, self.image = self.cap.read() #从视频流中读取 show = cv2.resize(self.image, (640, 480)) #把读到的帧的大小重新设置为 640x480 show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB) #视频色彩转换回RGB,这样才是现实的颜色 showImage = QtGui.QImage( show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888) #把读取到的视频数据变成QImage形式 self.label_show_camera.setPixmap( QtGui.QPixmap.fromImage(showImage)) #往显示视频的Label里 显示QImage def get_camera_images(self): ''' 截取摄像头图片,存入全局变量中 ''' reactTimer = MyTimer() waitTimer = MyTimer() reactTime = 0.5 imageSize = (640, 480) images = [] reactTime = int(reactTime * 1000) reactTimer.start(reactTime) waitTime = 100 while reactTimer.isTimeOut() == False: waitTimer.start(waitTime) flag, image = self.cap.read() image = cv2.resize(image, imageSize) images.append(image) waitTimer.waitTimeOut() gl.GL_Images = images gl.GL_Images_Length = images.__len__() print("length={}".format(images.__len__()))