import random import string # from typing_extensions import ParamSpecArgs from PyQt5.QtWidgets import * from SSS_win import Ui_MainWindow from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtCore import Qt import sys, os import math import time import threading import numpy as np # 使用matplotlib绘图 import matplotlib matplotlib.use("Qt5Agg") # 声明使用QT5 from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.backends.backend_qt5 import NavigationToolbar2QT as NavigationToolbar from matplotlib.figure import Figure import matplotlib.pyplot as plt import cv2 import threading import sys global path1 import inspect import ctypes class mywindow(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self): super(mywindow, self).__init__() self.setupUi(self) self.stopEvent = threading.Event() self.stopEvent.clear() def exit(self): self.close() def press_shibie(self): try: if self.model_name == 'PPYOLO-BOT': self.model_path = './inference_model/ppyolov2_r50vd_dcn_voc' elif self.model_name == 'RCNN': self.model_path = '.\\inference_model\\faster_rcnn' except: self.model_path = './inference_model/ppyolov2_r50vd_dcn_voc' if self.Video_fname != None : self.video_image_path = './video_output/'+self.Video_fname.split('/')[-1].split('.')[0] th4 = threading.Thread(target=self.video_yuce) th4.start() th3 = threading.Thread(target=self.Images_Display) th3.start() else: th5 = threading.Thread(target=self.Image_yuce) th5.start() def video_yuce(self): print('cd {}'.format(self.path1)) print( 'python deploy/python/infer.py --model_dir={} --video_file={} --use_gpu=True'.format(self.model_path, self.Video_fname)) os.system('cd {}'.format(self.path1)) os.system( 'python deploy/python/infer.py --model_dir={} --image_dir={} --output_dir=./video_output/{} --threshold=0.3 --use_gpu=True'.format( self.model_path,'./linshicunfang' ,self.Video_fname.split('/')[-1].split('.')[0])) # print(self.path1+'video_output/'+self.Video_fname.split('/')[-1]) # self.cap = cv2.VideoCapture # self.framRate = self.cap.get(cv2.CAP_PROP_FPS) # th = threading.Thread(target=self.Display) def Images_Display(self): img_list=[] img_number=9 while 1: try: files=os.listdir(self.video_image_path) for imgFile in files: if int(imgFile.strip('.jpg').split('_')[0])==int(img_number): print(imgFile) self.label_movie.setPixmap(QtGui.QPixmap( self.video_image_path+'/'+imgFile)) img_number += 10 flv = 0 gx = 0 mbw = 0 object = 0 print([int(i) for i in imgFile.strip('.jpg').split('_')[1:]]) for i in [int(i) for i in imgFile.strip('.jpg').split('_')[1:]]: i = int(i) if i == 0: flv+=1 if i == 1: gx+=1 if i == 2: mbw+=1 if i == 3: object+=1 if flv>=1 or gx>=1 or mbw>=1 or object>=1: print(flv,gx,mbw) self.flv.setText(str(flv)) self.gx.setText(str(gx)) self.mbw.setText(str(mbw)) self.label_object.setText(str(object)) time.sleep(0.5) img_list.append(imgFile) if len(img_list) == len(os.listdir('./linshicunfang')): for i in os.listdir('./linshicunfang'): os.remove('./linshicunfang/'+i) print('1'*100) break except Exception as e: pass # print(type(files)) # files=self.Sorted(files) # print(files) # for xmlFile in files: # print(xmlFile) # self.label_movie.setPixmap(QtGui.QPixmap( # self.video_image_path+'/'+xmlFile)) def Sorted(self,files): files=[int(i.split('.')[0]) for i in files] files.sort() files=[str(i)+'.jpg' for i in files] return files def Image_yuce(self): print('cd {}'.format(self.path1)) print( 'python deploy/python/infer.py --model_dir={} --image_file={} --use_gpu=True'.format(self.model_path, self.Img_fname)) os.system('cd {}'.format(self.path1)) os.system( 'python deploy/python/infer.py --model_dir={} --image_file={} --output_dir=./output --threshold=0.3 --use_gpu=True --cpu_threads=4'.format( self.model_path, self.Img_fname)) print(self.path1+'output/'+self.Img_fname.split('/')[-1]) files=os.listdir(self.path1+'output/') for i in files: if i.strip('.jpg').split('_')[0]+'.jpg'==self.Img_fname.split('/')[-1]: self.label_movie.setPixmap(QtGui.QPixmap( self.path1+'output/'+i)) flv = 0 gx = 0 mbw = 0 object = 0 for x in [int(m) for m in i.strip('.jpg').split('_')[1:]]: x = int(x) if x == 0: flv+=1 if x == 1: gx+=1 if x == 2: mbw+=1 if x == 3: object+=1 if flv>=1 or gx>=1 or mbw>=1 or object>=1: print(flv,gx,mbw) self.flv.setText(str(flv)) self.gx.setText(str(gx)) self.mbw.setText(str(mbw)) self.label_object.setText(str(object)) break sys.exit(0) def press_xunlian(self): print('3') def Close(self): self.stopEvent.set() def Display(self): while self.cap.isOpened(): success, frame = self.cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) img = QtGui.QImage(frame.data,frame.shape[1],frame.shape[0], QtGui.QImage.Format_RGB888) self.label_movie.setPixmap(QtGui.QPixmap.fromImage(img)) self.label_movie.setScaledContents(True) cv2.waitKey(int(1000/self.framRate)) if True == self.stopEvent.is_set(): self.stopEvent.clear() self.label_movie.clear() break def Split(self): img_id = 0 while (1): success, frame = self.cap2.read() img_outpath = './linshicunfang/'+str(img_id)+'.jpg' img_id += 1 if not img_id % 10: cv2.imwrite(img_outpath, frame) if img_id == self.frame_count: break frame_count = self.cap.get(cv2.CAP_PROP_FRAME_COUNT) def press_movie(self): print('4') self.path1 = os.path.abspath(__file__) self.path1 = self.path1.strip('main.py') print(self.path1) self.Video_fname, self.fileType = QFileDialog.getOpenFileName(self,"选择文件",os.getcwd(),"All Files (*);;MP4 Files (*.mp4)") self.cap = cv2.VideoCapture(self.Video_fname) self.cap2 = cv2.VideoCapture(self.Video_fname) self.framRate = self.cap.get(cv2.CAP_PROP_FPS) self.frame_count = self.cap.get(cv2.CAP_PROP_FRAME_COUNT) th2 = threading.Thread(target=self.Split) th2.start() def import_pic(self): self.Video_fname = None self.path1 = os.path.abspath(__file__) self.path1 = self.path1.strip('main.py') print(self.path1) # 打开图片读取对话框进行选择 self.Img_fname, _ = QFileDialog.getOpenFileName(self, '打开图片', '.', '图片文件(*.jpg *.png)') self.label_movie.setPixmap(QtGui.QPixmap(self.Img_fname)) pass print('1') def gpu_use(self,index): self.gpu = self.comboBox_GPU.itemText(index) print(self.gpu) def moxingxuanze(self,index): self.model_name = self.comboBox_sbsuanfa.itemText(index) pass if __name__ == '__main__': QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) app = QApplication(sys.argv) window = mywindow() window.show() sys.exit(app.exec_())