text: # -*- coding: utf-8 -*-
  
  # Form implementation generated from reading ui file 'second_2.ui'
  #
  # Created by: PyQt5 UI code generator 5.15.4
  #
  # WARNING: Any manual changes made to this file will be lost when pyuic5 is
  # run again.  Do not edit this file unless you know what you are doing.
  
  import cv2
  
  import sys
  from PyQt5 import QtWidgets, QtCore, QtGui
  from PyQt5.QtGui import *
  from PyQt5.QtWidgets import *
  from PyQt5.QtCore import *
  
  import cv2
  from PyQt5.QtCore import QTimer
  import tensorflow as tf
  from yolo import YOLO
  physical_devices = tf.config.experimental.list_physical_devices('GPU')
  tf.config.experimental.set_memory_growth(physical_devices[0], True)
  
  yolo = YOLO()
  
  
  import time
  import numpy as np
  from PIL import Image
  import tensorflow as tf
  
  from PyQt5 import QtCore, QtGui, QtWidgets
  
  from PyQt5.QtCore import QTimer
  from PyQt5 import QtCore, QtGui, QtWidgets
  
  
  class Ui_Second(object):
      def setupUi(self, Ui_Second):
          Ui_Second.setObjectName("MainWindow")
          Ui_Second.resize(1600, 1000)
  
          self.frame = []  # 存图片
          self.cap = []
          self.detectFlag = False  # 检测flag
          self.timer_camera = QTimer()  # 定义定时器
  
          # self.centralwidget = QtWidgets.QWidget(MainWindow)
          # self.centralwidget.setObjectName("centralwidget")
          self.verticalLayout = QtWidgets.QVBoxLayout(Ui_Second)
          self.verticalLayout.setObjectName("verticalLayout")
          self.frame = QtWidgets.QFrame(Ui_Second)
          self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
          self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
          self.frame.setObjectName("frame")
          self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.frame)
          self.horizontalLayout_2.setObjectName("horizontalLayout_2")
          #这一个模块显示图片
          self.label_photo = QtWidgets.QLabel(self.frame)
          self.label_photo.setText("")
          self.label_photo.setPixmap(QtGui.QPixmap("resource/test/2.jpg"))
          self.label_photo.setObjectName("label_photo")
          self.horizontalLayout_2.addWidget(self.label_photo)
          #这部分显示输出信息。
          self.label_output = QtWidgets.QLabel(self.frame)
          self.label_output.setObjectName("label_output")
          self.horizontalLayout_2.addWidget(self.label_output)
          self.verticalLayout.addWidget(self.frame)
  
  
          self.frame_2 = QtWidgets.QFrame(Ui_Second)
          self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
          self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
          self.frame_2.setObjectName("frame_2")
          self.horizontalLayout = QtWidgets.QHBoxLayout(self.frame_2)
          self.horizontalLayout.setObjectName("horizontalLayout")
  
          #摄像头打开的按钮
          self.pushButton_camera = QtWidgets.QPushButton(self.frame_2)
          self.pushButton_camera.setObjectName("pushButton_camera")
          self.horizontalLayout.addWidget(self.pushButton_camera)
          #给摄像头按钮绑定函数
          self.pushButton_camera.clicked.connect(self.slotStart_camera)
  
  
          #选择视频
          self.label = QtWidgets.QLabel(self.frame_2)
          self.label.setObjectName("label")
          self.horizontalLayout.addWidget(self.label)
          self.pushButton = QtWidgets.QPushButton(self.frame_2)
          self.pushButton.setObjectName("pushButton")
          self.horizontalLayout.addWidget(self.pushButton)
          #给选择视频检测绑定函数
          self.pushButton.clicked.connect(self.slotStart_movie)
  
          # 检测按键
          self.pushButton_detect = QtWidgets.QPushButton(self.frame_2)
          self.pushButton_detect.setObjectName("pushButton_detect")
          self.pushButton_detect.setStyleSheet("QPushButton{background:red;}")  # 没检测红色，检测绿色
          self.pushButton_detect.clicked.connect(self.detection)
          self.horizontalLayout.addWidget(self.pushButton_detect)
  
  
          # 关闭视频按钮
          self.pushButton_stop = QtWidgets.QPushButton(self.frame_2)
          self.pushButton_stop.setObjectName("pushButton_stop")
          self.pushButton_stop.clicked.connect(self.slotStop)
          self.horizontalLayout.addWidget(self.pushButton_stop)
          self.verticalLayout.addWidget(self.frame_2)
  
          #无用部分
          # MainWindow.setCentralWidget(self.centralwidget)
          # self.menubar = QtWidgets.QMenuBar(MainWindow)
          # self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
          # self.menubar.setObjectName("menubar")
          # MainWindow.setMenuBar(self.menubar)
          # self.statusbar = QtWidgets.QStatusBar(MainWindow)
          # self.statusbar.setObjectName("statusbar")
          # MainWindow.setStatusBar(self.statusbar)
  
          self.retranslateUi(Ui_Second)
          QtCore.QMetaObject.connectSlotsByName(Ui_Second)
  
      def retranslateUi(self, MainWindow):
          _translate = QtCore.QCoreApplication.translate
          MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
          self.label_output.setText(_translate("MainWindow", "暂未检测到图像。"))
          # self.label_2.setText(_translate("MainWindow", "TextLabel"))
          self.pushButton_camera.setText(_translate("MainWindow", "摄像头检测"))
          self.label.setText(_translate("MainWindow", "检测视频路径"))
          self.pushButton.setText(_translate("MainWindow", "选择检测视频"))
          self.pushButton_detect.setText(_translate("MainWindow", "检测"))
          self.pushButton_stop.setText(_translate("MainWindow", "关闭"))
  
      def slotStart_camera(self):
          """ Slot function to start the progamme
              """
          # videoName, _ = QFileDialog.getOpenFileName(self, "Open", "", "*.avi;;*.mp4;;All Files(*)")
          # if videoName != "":  # “”为用户取消
          # self.cap = cv2.VideoCapture(videoName)
          self.cap = cv2.VideoCapture(0)
          self.timer_camera.start(100)
          self.timer_camera.timeout.connect(self.openFrame)
  
  
      def slotStart_movie(self):
          """ Slot function to start the progamme
              """
          # 打开视频的时候，要对视频路径进行赋值
          videoName, _ = QFileDialog.getOpenFileName(self, "Open", "", "*.avi;;*.mp4;;All Files(*)")
          # Qfil
          # videoURL = QFileDialog.getExistingDirectory(self, "Select File Directory to Save File", "")
          if videoName != "":  # “”为用户取消
              self.cap = cv2.VideoCapture(videoName)
              # self.cap = cv2.VideoCapture(0)
              self.timer_camera.start(100)
              self.timer_camera.timeout.connect(self.openFrame)
  
              self.label.setText(str(videoName))
  
      def slotStop(self):
          """ Slot function to stop the programme
              """
          if self.cap != []:
              self.cap.release()
              self.timer_camera.stop()  # 停止计时器
              # self.label_2.setText("This video has been stopped.")
              # self.label_2.setStyleSheet("QLabel{background:pink;}"
              #                          "QLabel{color:rgb(100,100,100);font-size:15px;font-weight:bold;font-family:宋体;}"
              #                          )
              self.label_photo.setPixmap(QtGui.QPixmap("resource/test/2.jpg"))
              self.label_output.setText("暂未检测到图像")
          else:
              # self.label_num.setText("Push the left upper corner button to Quit.")
              Warming = QMessageBox.warning(self, "Warming", "你还没有打开过图片，无法关闭。",
                                            QMessageBox.Yes)
  
  
      def openFrame(self):
          """ Slot function to capture frame and process it
              """
  
  
          if (self.cap.isOpened()):
              ret, self.frame = self.cap.read()
              if ret:
                  frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
                  fps = 0.0
                  if self.detectFlag == True:
                      print("开始检测")
                      # 检测代码self.frame
                      t1 = time.time()
                      # 转变成Image
                      frame = Image.fromarray(np.uint8(frame))
                      # 进行检测
                      frame, text= yolo.detect_image(frame)
                      frame = np.array(frame)
  
  
                      fps = (fps + (1. / (time.time() - t1))) / 2
                      print("fps= %.2f" % (fps))
                      frame = cv2.putText(frame, "fps= %.2f" % (fps), (0, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0),
                                          2)
  
  
  
                      self.label_output.setText(text)
                  height, width, bytesPerComponent = frame.shape
                  bytesPerLine = bytesPerComponent * width
                  q_image = QImage(frame.data, width, height, bytesPerLine,
                                   QImage.Format_RGB888).scaled(self.label_photo.width(), self.label_photo.height())
                  self.label_photo.setPixmap(QPixmap.fromImage(q_image))
  
  
  
              else:
                  self.cap.release()
                  self.timer_camera.stop()  # 停止计时器
  
      # def openFrame_camera(self):
      #     """ Slot function to capture frame and process it
      #         """
      #
      #     if (self.cap.isOpened()):
      #         ret, self.frame = self.cap.read()
      #         if ret:
      #             frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
      #             fps = 0.0
      #             if self.detectFlag == True:
      #                 print("开始检测")
      #                 # 检测代码self.frame
      #                 t1 = time.time()
      #                 # 转变成Image
      #                 frame = Image.fromarray(np.uint8(frame))
      #                 # 进行检测
      #                 frame, text = yolo.detect_image(frame)
      #                 frame = np.array(frame)
      #
      #                 fps = (fps + (1. / (time.time() - t1))) / 2
      #                 print("fps= %.2f" % (fps))
      #                 frame = cv2.putText(frame, "fps= %.2f" % (fps), (0, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0),
      #                                     2)
      #
      #                 self.label_output.setText(text)
      #             height, width, bytesPerComponent = frame.shape
      #             bytesPerLine = bytesPerComponent * width
      #             q_image = QImage(frame.data, width, height, bytesPerLine,
      #                              QImage.Format_RGB888).scaled(self.label_photo.width(), self.label_photo.height())
      #             self.label_photo.setPixmap(QPixmap.fromImage(q_image))
      #
      #         else:
      #             self.cap.release()
      #             self.timer_camera.stop()  # 停止计时器
  
      def detection(self):
          self.detectFlag = not self.detectFlag  # 取反
          if self.detectFlag == True:
              self.pushButton_detect.setStyleSheet("QPushButton{background:green;}")
          else:
              self.pushButton_detect.setStyleSheet("QPushButton{background:red;}")
  # import 123_rc
  