加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
capture.py 20.41 KB
一键复制 编辑 原始数据 按行查看 历史
vv 提交于 2023-04-03 01:17 . 算法加入
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
import sys
import time
import tkinter as tk
import cv2
import datetime
from PyQt5.QtCore import *
from PyQt5.QtCore import QTimer, QThread
# from PySide2.QtCore import QThread
from PyQt5.QtGui import QPixmap, QImage
from new import Ui_MainWindow
from PySide2.QtUiTools import QUiLoader
import os
from PyQt5 import QtCore
from PyQt5.QtCore import QUrl
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent, QMediaPlaylist
from PyQt5.QtWidgets import QMainWindow, QFileDialog, QApplication, QSlider
from time import sleep
from tttt import nnnnn
from PySide2.QtWidgets import *
from PySide2.QtUiTools import *
from PySide2.QtCore import *
from PySide2 import QtGui,QtCore,QtWidgets
from PySide2.QtGui import *
import threading
# 子界面
from log import Ui_son
from time_son import Ui_time
class MyThread(QThread):
# 设置线程变量
trigger = Signal(str)
def __init__(self, parent=None):
super(MyThread, self).__init__(parent)
def run_(self, message):
'''
向信号trigger发送消息
'''
self.trigger.emit(message)
# class runThread(QObject):
# # 设置线程变量
# trigger = pyqtSignal(str)# 自定义信号, object 为信号对象承载数据的类型
#
# def __init__(self, parent=None):
# super(runThread, self).__init__(parent)
#
# def run_(self, message):
# '''
# 向信号trigger发送消息
# '''
# # sys.path.append("pc-software-algorithm-master/deploy/pipeline/pipeline.py")
# print("Thread is Running")
# os.system("python3 pipeline.py %f %f" % ("deploy/pipeline/config/infer_cfg_pphuman.yml", "E:/PYQT/comprehensive-dispatching/pc-software-algorithm-master/2.mp4"))
# class WorkThread(QThread):
# # 初始化线程
# def __int__(self):
# super(WorkThread, self).__init__()
# # 线程运行函数
class time_son(QMainWindow, Ui_time):
def __init__(self):
super(time_son, self).__init__() # 调用父类的init
self.ui2 = QUiLoader().load('time_son.ui')
def show_pic(self,s):
path="test_out\\234055234029"+str(s)+".jpg"
pix = QPixmap(path)
self.ui2.pic.setPixmap(pix)
self.ui2.pic.setScaledContents(True)
class mywindow(): # 这个窗口继承了用QtDesignner 绘制的窗口
file_path = None
flag = 1
camera = 1 # 定义变量,camera为1时表示展示视频的画面关闭,为0时开启
play_num = 2 # 为0时播放视频
def __init__(self):
self.main()
# self.save.clicked.connect(self.save_video)
# self.open_camera.clicked.connect(self.open)
# self.cap = cv2.VideoCapture(0) # 初始化摄像头
# self.camera_timer = QTimer() # 初始化定时器,定时读取视频产生的图片流
# self.camera_timer.timeout.connect(self.show_video) # 将定时器与动作(从视频中读取图片)绑定
#
# 定时器2——用于读取界面2的视频
self.pic_time= []
self.pic_time = QTimer()
# self.pic_time.timeout.connect(self.OpenFrame)
# self.ui.player = QMediaPlayer(self)# 播放视频,视频需要解码
#self.player.setVideoOutput(self.camera_widget)
# # 播放列表
# self.playlist = QMediaPlaylist()
# self.player.setPlaylist(self.playlist)
#
# self.wenjian.triggered.connect(self.open_file)
# self.jia.triggered.connect(self.open_dir)
# self.listWidget.itemClicked.connect(self.change_by_list)
# # self.play_btn.clicked.connect(self.playwidget) # 将播放按钮与play函数绑定
# # self.stop_btn.clicked.connect(self.stopwidget)
# self.main_start.triggered.connect(self.playwidget)
# self.main_stop.triggered.connect(self.stopwidget)
# self.search.triggered.connect(self.log)
# self.threads = MyThread(self) # 自定义线程类,用于展示日志栏
# self.threads.trigger.connect(self.update_text) # 当信号接收到消息时,更新数据
# self.th = MyThread(self) # 自定义线程类,用于展示人数栏
# self.th.trigger.connect(self.update_number)
# self.num.clicked.connect(self.show_number)
# self.thread_no = 0 # 定义在播放列表中视频的序号,初始为0
# self.Mytimer()
# self.th_no = 0 # 定义人数栏的线程序号
# self.cap = cv2.VideoCapture(r'video_test\demo.avi')
# self.number.itemClicked.connect(self.show_pic)
# 界面切换
# self.stack1.clicked.connect(lambda: self.on_pushbutton(self.stack1))
# self.stack2.clicked.connect(lambda: self.on_pushbutton(self.stack2))
# self.stack3.clicked.connect(lambda: self.on_pushbutton(self.stack3))
# self.stack4.clicked.connect(lambda: self.on_pushbutton(self.stack4))
# self.stack5.clicked.connect(lambda: self.on_pushbutton(self.stack5))
# self.stack6.clicked.connect(lambda: self.on_pushbutton(self.stack6))
# self.stack7.clicked.connect(lambda: self.on_pushbutton(self.stack7))
# self.stack8.clicked.connect(lambda: self.on_pushbutton(self.stack8))
self.player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.main()
# self.thrs = runThread() # 自定义线程类,用于展示日志栏
# self.thrs.trigger.connect(self.update_text) # 当信号接收到消息时,更新数据
def run(self):
'''
向信号trigger发送消息
'''
# sys.path.append("pc-software-algorithm-master/deploy/pipeline/pipeline.py")
print("Thread is Running")
# os.system("python ./pc-software-algorithm-master/deploy/pipeline/pipeline.py %s %s" % (
# "config deploy/pipeline/config/infer_cfg_pphuman.yml",
# "E:/PYQT/comprehensive-dispatching/pc-software-algorithm-master/2.mp4"))
os.system("python ./pc-software-algorithm-master/deploy/pipeline/pipeline.py --config ./pc-software-algorithm-master/deploy/pipeline/config/infer_cfg_pphuman.yml --video_file=%s" %
("E:/PYQT/comprehensive-dispatching/pc-software-algorithm-master/2.mp4 --device=gpu"))
print("运行完毕")
def show_ui(self, location):
loca = "ui/" + location
qfile_staus = QFile(loca)
qfile_staus.open(QFile.ReadOnly)
qfile_staus.close()
self.ui = QUiLoader().load(qfile_staus)
self.ui.setWindowTitle("智慧景区调度")
appIcon = QIcon("source/first/logo.jpg")
self.ui.setWindowIcon(appIcon)
def main(self):
self.show_ui("new.ui")
self.ui.show()
self.ui.stack1.clicked.connect(self.main_first) # 行人跟踪
self.ui.stack2.clicked.connect(self.main_people) # 车辆跟踪
self.ui.stack3.clicked.connect(self.main_fall) # 摔倒检测
self.ui.stack4.clicked.connect(self.main_danger) # 摔倒检测
self.ui.stack5.clicked.connect(self.main_conflict) # 摔倒检测
self.ui.stack6.clicked.connect(self.main_fire) # 摔倒检测
self.ui.stack7.clicked.connect(self.main_people_c) # 摔倒检测
# self.ui.pushButton_3.clicked.connect(self.mult_menu)
# 界面1
def main_first(self):
self.show_ui("main_first.ui")
self.ui.show()
pic = QPixmap('source/second/pre.png')
# self.ui.play_label.setPixmap(pic)
self.ui.upload.clicked.connect(self.open_vedio)
self.ui.play_button.clicked.connect(self.start_pic)
self.ui.stop_button.clicked.connect(self.stop_pic)
self.ui.come_back.clicked.connect(self.main)#返回键
self.t1 = threading.Thread(target=self.run) # 传递参数给它
self.t1.start()
def main_people(self):
self.show_ui("main_people_second.ui")
self.ui.show()
pic = QPixmap('source/second/pre.png')
self.ui.play_label.setPixmap(pic)
self.ui.upload.clicked.connect(self.open_vedio)
self.ui.play_button.clicked.connect(self.start_pic)
self.ui.stop_button.clicked.connect(self.stop_pic)
self.ui.come_back.clicked.connect(self.main) # 返回键
self.ui.come_back.clicked.connect(self.main)
def main_fall(self):
self.show_ui("main_fall_third.ui")
self.ui.show()
self.ui.come_back.clicked.connect(self.main)
def main_danger(self):
self.show_ui("main_danger_fourth.ui")
self.ui.show()
self.ui.come_back.clicked.connect(self.main)
def main_conflict(self):
self.show_ui("main_conflict_fifth.ui")
self.ui.show()
self.ui.come_back.clicked.connect(self.main)
def main_fire(self):
self.show_ui("main_fire_sixth.ui")
self.ui.show()
self.ui.come_back.clicked.connect(self.main)
def main_people_c(self):
self.show_ui("main_people_seventh.ui")
self.ui.show()
self.ui.come_back.clicked.connect(self.main)
def open_vedio(self):
'''
file_name:为选中的文件
:return:
'''
"""选取视频文件"""
# 这里以mp4和avi视频播放为例
# self.ui.label.setText("<font color=red>等待模型运行完毕.</font>")
self.ui.play_label.setPixmap(QPixmap(""))
openfile_name = QFileDialog.getOpenFileName(self.ui, 'chose files', '', 'Image files(*.mp4 *.avi)') # 打开文件选择框选择文件
self.file_name = openfile_name[0] # 获取图片名称
# 得到文件后缀名 需要根据情况进行修改
suffix = self.file_name.split("/")[-1][self.file_name.split("/")[-1].index(".") + 1:]
# print(self.file_name, suffix)
if self.file_name == '':
pass
elif suffix == "mp4" or suffix == "avi":
self.cap = cv2.VideoCapture(self.file_name)
def start_pic(self):
'''
开始读取界面2的视频图片流
用start()函数进行控制
:return:
'''
self.pic_time.start(30)
self.pic_time.timeout.connect(self.OpenFrame)
# self.thread_no += 1
# message = f"播放视频".format(self.thread_no)
# self.threads.run_(message)
def stop_pic(self):
self.pic_time.stop()
# self.thread_no += 1
# message = f"暂停视频".format(self.thread_no)
# self.threads.run_(message)
def playwidget(self):
'''
点击播放按钮,播放视频
:return:
'''
self.player.play()
self.play_num = 0
self.thread_no += 1
message = "播放视频".format(self.thread_no)
self.threads.run_(message)
def stopwidget(self):
'''
点击暂停按钮,暂停视频
:return:
'''
self.player.pause()
self.play_num = 1
self.thread_no += 1
message = "暂停视频".format(self.thread_no)
self.threads.run_(message)
def play(self, stack):
self.player.setVideoOutput(stack)
# 选取文件
def open_file(self):
urls = QFileDialog.getOpenFileUrls()[0]
for url in urls:
yu = url.toString()
drv, left = os.path.split(yu)
# self.listWidget.addItem(left)
# content = QMediaContent(url)
print(url)
# self.playlist.addMedia(content)
# num = self.playlist.mediaCount() - len(urls)
# self.playlist.setCurrentIndex(num)
# self.listWidget.setCurrentRow(num)
# if self.play_num==0:
# self.player.play()
# self.thread_no += 1
# message = f"上传文件".format(self.thread_no)
# self.threads.run_(message)
# 选取文件夹
def open_dir(self):
dir = QFileDialog.getExistingDirectory()
if dir=="":
files=[]
else:
files = os.listdir(dir)
for file in files:
self.listWidget.addItem(file)
url = os.path.join(dir, file)
Qurl = QUrl.fromLocalFile(url)
content = QMediaContent(Qurl)
self.playlist.addMedia(content)
self.playlist.setCurrentIndex(0)
self.listWidget.setCurrentRow(0)
self.player.play()
self.thread_no += 1
message = f"上传文件夹".format(self.thread_no)
self.threads.run_(message)
#摄像头展示
def open_camrea(self,ph):
dir = ph
if dir == "":
files = []
else:
self.listWidget.clear()
self.playlist.clear()
files = os.listdir(dir)
for file in files:
self.listWidget.addItem(file[:-4])
url = os.path.join(dir, file)
Qurl = QUrl.fromLocalFile(url)
content = QMediaContent(Qurl)
self.playlist.addMedia(content)
self.playlist.setCurrentIndex(0)
self.listWidget.setCurrentRow(0)
self.player.play()
self.thread_no += 1
message = f"上传文件夹".format(self.thread_no)
self.threads.run_(message)
def change_by_list(self, current):
index = self.listWidget.currentRow()
self.playlist.setCurrentIndex(index)
self.player.play()
self.thread_no += 1
message = f"切换,代号:{index}".format(self.thread_no)
self.threads.run_(message)
def set_path(self):
'''
固定前半部分的相对路径
:return:
'''
root = tk.Tk()
root.withdraw()
file_path = "pyqt_test"
print(file_path)
if file_path == "":
return
self.file_path = file_path
def show_video(self):
flag, self.image = self.cap.read() # 从视频流中读取图片
image_show = cv2.resize(self.image, (640, 480)) # 把读到的帧的大小重新设置为 600*360
image_show = cv2.cvtColor(image_show, cv2.COLOR_BGR2RGB) # opencv读的通道是BGR,要转成RGB
image_show = cv2.flip(image_show, 1) # 水平翻转,因为摄像头拍的是镜像的。
# 把读取到的视频数据变成QImage形式(图片数据、高、宽、RGB颜色空间,三个通道各有2**8=256种颜色)
# 获取摄像头的实时图片,展示出来
def open(self):
'''
:return:
'''
if self.camera == 1:
self.camera = 0
message = f"打开摄像头".format(self.thread_no)
self.threads.run_(message)
self.cap = cv2.VideoCapture(0)
self.camera_timer.start(50);
self.open_camera.setText("关闭摄像头")
while True:
ret, frame = self.cap.read()
frame = cv2.flip(frame, 1)
cv2.imshow("show", frame)
cv2.waitKey(1)
if self.camera == 1:
break
cv2.destroyAllWindows()
else:
self.camera = 1
message = f"关闭摄像头".format(self.thread_no)
self.threads.run_(message)
self.camera_timer.stop()
self.cap.release()
self.open_camera.setText("打开摄像头")
def save_video(self):
if self.flag == 1:
self.flag = 0
self.cap = cv2.VideoCapture(0)
self.camera_timer.start(50);
self.save.setText("停止录制")
self.stop_video()
fourcc = cv2.VideoWriter_fourcc(*"mp4v") # 保存的格式
real_path = "pyqt_test"
FName = fr"video{time.strftime('%Y%m%d%H%M%S', time.localtime())}"
VM = cv2.VideoWriter(real_path + "\\{}.mp4".format(FName), fourcc, 30,
(640, 480)) # 第一个参数是文件保存的名字,第二个参数是文件名称,第三个参数是帧率,第四个参数是文件的尺寸大小
self.save.setEnabled(True)
while True:
ret, frame = self.cap.read()
if not ret:
print("carmera,is not open")
frame = cv2.flip(frame, 1)
VM.write(frame) # 视频保存
cv2.imshow("Recording...", frame)
cv2.waitKey(1)
if self.flag == 1:
break
VM.release()
cv2.destroyAllWindows()
self.start_video()
self.thread_no += 1
message = f"保存视频:{real_path}".format(self.thread_no)
self.threads.run_(message)
self.camera_timer.stop()
self.cap.release()
else:
self.flag = 1
self.save.setText("录制视频")
def start_video(self):
'''
定时器,以恒定间隔触发事件:产生固定间隔读取视频生成的图片流
:return:
'''
self.camera_timer.start()
def stop_video(self):
'''
定时器停止计时,停止触发事件,不再读取图片
:return:
'''
self.camera_timer.stop()
def update_text(self, message):
'''
添加信息到日志栏中(即控件QTextBrowser中)
'''
self.textBrowser.append(message)
def show_number(self):
'''
触发线程
:return:
'''
self.th_no += 1
th_mess = f"触发线程".format(self.th_no)
self.th.run_(th_mess)
def update_number(self, mess):
current_time = str(datetime.datetime.now())[:-7]
mess1 = f"{current_time}人数为{nnnnn}人".format(self.th_no)
mess2=f"{current_time}人数为6000人".format(self.th_no)
print("thtth",mess1)
mess=[mess1,mess2]
for item in mess:
self.number.addItem(item) # 添加item
def show_pic(self):
#im = self.number.selectedItems()[0] # 获取选择的item并转为文本,然后进行判断
# 点击listWidget组建中的item响应事件
imagefile = self.number.currentItem().text()
print("showpic",imagefile)
t.show()
time_son.show_pic("demo")
def Mytimer(self):
timer = QTimer(self)
timer.timeout.connect(self.update)
timer.start(100)
# 界面2
# 显示图片
'''
def picture(self):
index=0
pix = QPixmap(r'test_out//demo'+str(index)+'.jpg')
self.pic.setPixmap(pix)
self.pic.setScaledContents(True)
self.thread_no += 1
message = f"显示人数图".format(self.thread_no)
self.threads.run_(message)
'''
# 显示视频
def OpenFrame(self):
ret, image = self.cap.read()
if ret:
if len(image.shape) == 3:
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
vedio_img = QImage(image.data, image.shape[1], image.shape[0], QImage.Format_RGB888)
elif len(image.shape) == 1:
vedio_img = QImage(image.data, image.shape[1], image.shape[0], QImage.Format_Indexed8)
else:
vedio_img = QImage(image.data, image.shape[1], image.shape[0], QImage.Format_RGB888)
self.ui.play_label.setPixmap(QPixmap(vedio_img))
self.ui.play_label.setScaledContents(True) # 自适应窗口
else:
self.cap.release()
self.pic_time.stop()
# 界面选择
def on_pushbutton(self, btn):
if btn == self.stack1:
self.play(self.camera_widget)
self.open_camrea("people_test")
self.btn_color2()
self.btn_color(btn)
self.stackedWidget.setCurrentIndex(0)
self.thread_no += 1
message = f"切换至页面1".format(self.thread_no)
self.threads.run_(message)
elif btn == self.stack2:
self.play(self.widget_2)
self.open_camrea("fail_test")
self.btn_color2()
self.btn_color(btn)
self.stackedWidget.setCurrentIndex(1)
self.thread_no += 1
message = f"切换至页面2".format(self.thread_no)
self.threads.run_(message)
elif btn == self.stack3:
self.btn_color2()
self.btn_color(btn)
self.stackedWidget.setCurrentIndex(2)
self.thread_no += 1
message = f"切换至页面3".format(self.thread_no)
self.threads.run_(message)
elif btn == self.stack4:
self.btn_color2()
self.btn_color(btn)
self.stackedWidget.setCurrentIndex(3)
self.thread_no += 1
message = f"切换至页面4".format(self.thread_no)
self.threads.run_(message)
elif btn == self.stack5:
self.btn_color2()
self.btn_color(btn)
self.stackedWidget.setCurrentIndex(4)
self.thread_no += 1
message = f"切换至页面5".format(self.thread_no)
self.threads.run_(message)
elif btn == self.stack6:
self.btn_color2()
self.btn_color(btn)
self.stackedWidget.setCurrentIndex(5)
self.thread_no += 1
message = f"切换至页面6".format(self.thread_no)
self.threads.run_(message)
elif btn == self.stack7:
self.btn_color2()
self.btn_color(btn)
self.stackedWidget.setCurrentIndex(6)
self.thread_no += 1
message = f"切换至页面7".format(self.thread_no)
self.threads.run_(message)
elif btn == self.stack8:
path = "people_test//people_num.jpeg"
pix = QPixmap(path)
self.label_4.setPixmap(pix)
self.label_4.setScaledContents(True)
self.btn_color2()
self.btn_color(btn)
self.stackedWidget.setCurrentIndex(7)
self.thread_no += 1
message = f"切换至页面8".format(self.thread_no)
self.threads.run_(message)
# 改变底色
def btn_color(self, btn_num):
btn_num.setStyleSheet('''QWidget{background-color:rgb(245, 245, 245);}''')
# 把界面按钮所有颜色覆盖
def btn_color2(self):
a = [self.stack1, self.stack2, self.stack3, self.stack4, self.stack5, self.stack6, self.stack7, self.stack8]
for i in range(8):
a[i].setStyleSheet('''QWidget{background-color:rgb(225, 225, 225);}''')
# 调用子界面
def log(self):
q.show()
self.thread_no += 1
message = f"切换至子界面".format(self.thread_no)
self.threads.run_(message)
# 子界面的类
class picture(QMainWindow, Ui_son):
def __init__(self):
super(picture, self).__init__() # 调用父类的init
self.setupUi(self)
self.player2 = QMediaPlayer(self) # 播放视频,视频需要解码
self.player2.setVideoOutput(self.widget)
self.playlist2 = QMediaPlaylist()
self.player2.setPlaylist(self.playlist2)
self.pic_list.itemClicked.connect(self.pic_by_list)
self.pushButton.clicked.connect(self.open_dir)
# 实现点击图片列表展现图片的效果
def pic_by_list(self, current):
index = self.pic_list.currentRow()
self.playlist2.setCurrentIndex(index)
self.player2.play()
def open_dir(self):
dir = 'test_out//' # 本地路径
files = os.listdir(dir)
for file in files:
self.pic_list.addItem(file)
url = os.path.join(dir, file)
Qurl = QUrl.fromLocalFile(url)
content = QMediaContent(Qurl)
self.playlist2.addMedia(content)
self.playlist2.setCurrentIndex(0)
self.pic_list.setCurrentRow(0)
self.player2.play()
#“当前时间”子界面
if __name__ == '__main__':
# workThread = WorkThread()
# workThread.start()
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) # 自适应分辨率
app = QApplication(sys.argv)
MainWindow = QMainWindow()
window = mywindow() # 主界面
window.ui.show() # 显示主界面
# q = picture() # 子界面1
# t= time_son() #子界面2
app.exec_()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化