import datetime
from PyQt5 import QtWidgets
from qimage2ndarray.dynqt import QtGui
from mtcnn.detector import detect_faces, show_bboxes, get_face_expression, get_head_pose, get_emotion, get_face_state
from MainWindow import Ui_MainWindow
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
from PyQt5.QtCore import QTimer, QCoreApplication, QDateTime
from PyQt5.QtGui import QPixmap, QImage, QTextCursor
import qimage2ndarray
from torch.autograd import *
from detection import *
import os
# os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
from ssd_net_vgg import *
from voc0712 import *
import torch
import torch.nn as nn
import numpy as np
import cv2
import utils
import torch.backends.cudnn as cudnn
import time
class CameraShow(QMainWindow, Ui_MainWindow):
def __del__(self):
try:
self.camera.release() # 释放资源
except:
return
def __init__(self, parent=None):
super(CameraShow, self).__init__(parent)
self.setupUi(self)
self.Timer = QTimer()
self.timer = QTimer()
self.time_first = time.time()
self.time_ing = time.time()
self.Timer.timeout.connect(self.show_img)
self.timer.timeout.connect(self.showTime)
self.PrepCamera()
self.PrepareTorch()
self.CallBackFunctions()
self.showTime()
self.case = 0
self.frag_cap = True
# self.Timer.timeout.connect(self.TimerOutFun)
self.video_flg = True
self.colors_tableau = [(214, 39, 40), (23, 190, 207), (188, 189, 34), (188, 34, 188), (205, 108, 8),
(150, 34, 188), (105, 108, 8)]
# 初始化网络
self.net = SSD()
self.net = torch.nn.DataParallel(self.net)
self.net.train(mode=False)
# net.load_state_dict(torch.load('./weights/ssd300_VOC_100000.pth',map_location=lambda storage,loc: storage))
# self.net.load_state_dict(
# torch.load('./weights/final_20200226_VOC_100000.pth', map_location=lambda storage, loc: storage))
self.net.load_state_dict(torch.load('./weights/final_20200226_VOC_100000.pth',map_location=lambda storage, loc: storage.cuda(0)))
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
self.net.to(device)
if torch.cuda.is_available():
self.net = self.net.cuda()
cudnn.benchmark = True
self.net = self.net.cuda()
self.img_mean = (104.0, 117.0, 123.0)
self.max_fps = 0
filename = ('E:\PythonEye\Dataset\\3-FemaleGlasses.mp4')
# 保存检测结果的List
# 眼睛和嘴巴都是,张开为‘1’,闭合为‘0’
self.Image_num = 0
self.RecordPath = 'E:/PythonEye/DachuangProject/test/3-FemaleGlasses.mp4'
self.VideoPath.setText(self.RecordPath)
self.isRecordImg = False
self.EMOTIONS = ["生气", "厌恶", "害怕", "喜悦", "悲伤", "惊讶", "普通"]
# prepare
def PrepCamera(self):
try:
self.camera = cv2.VideoCapture(0)
self.Image_num = 0
self.Msg.clear()
self.Msg.append('Oboard camera connected.')
self.Msg.setPlainText()
self.showTime()
except Exception as e:
self.Msg.clear()
self.Msg.append(str(e))
def CallBackFunctions(self):
self.BtnRecord.clicked.connect(self.setRecordImg)
self.btntestcamera.clicked.connect(self.testCamera)
# self.StopBt.clicked.connect(self.StopCamera)
self.btn_start.clicked.connect(self.StartDection)
self.btnexit.clicked.connect(self.ExitApp)
self.btn_testvideo.clicked.connect(self.testVideo)
self.BtnReadvideo.clicked.connect(self.setFilePath)
# 显示时间
def showTime(self):
# time = QDateTime.currentDateTime()
now_time = datetime.datetime.now()
self.timer.start()
# timeDisplay = time.toString("yyyy-MM-dd hh:mm:ss dddd")
hour = now_time.strftime('%H')
minute = now_time.strftime('%M')
second = now_time.strftime('%S')
self.TimeHourLCD.display(hour)
self.TimeMinuteLCD.display(minute)
self.TimeSecondLCD.display(second)
def ColorAdjust(self, img):
try:
B = img[:, :, 0]
G = img[:, :, 1]
R = img[:, :, 2]
img1 = img
img1[:, :, 0] = B
img1[:, :, 1] = G
img1[:, :, 2] = R
return img1
except Exception as e:
self.Msg.setPlainText(str(e))
# 打开相机
def testCamera(self):
# self.camera = cv2.VideoCapture(0)
if self.Timer.isActive() == False:
flag = self.camera.open(0)
if flag == False:
msg = QtWidgets.QMessageBox.warning(self, u"Warning", u"请检测相机与电脑是否连接正确",
buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
else:
self.case = 1
self.timelb = time.clock()
self.btntestcamera.setText(u'关闭相机')
self.btn_start.setEnabled(False)
self.btn_testvideo.setEnabled(False)
self.Image_num = 0
self.Timer.start(30)
else:
self.Timer.stop()
self.camera.release()
# self.Camera.clear()
self.Camera_2.clear()
self.btntestcamera.setText(u'打开相机')
self.btn_start.setEnabled(True)
self.btn_testvideo.setEnabled(True)
def PrepareTorch(self):
if torch.cuda.is_available():
print('-----gpu mode-----')
torch.set_default_tensor_type('torch.cuda.FloatTensor')
else:
print('-----cpu mode-----')
def TimerOutFun(self):
success, img = self.camera.read()
if success:
self.Image = self.ColorAdjust(img)
self.showTime()
self.Image_num += 1
if self.Image_num % 10 == 9:
frame_rate = 10 / (time.clock() - self.timelb)
self.FmRateLCD.display(frame_rate)
self.timelb = time.clock()
else:
self.Msg.clear()
self.Msg.setPlainText('Image obtaining failed.')
def StartDection(self):
if self.Timer.isActive() == False:
flag = self.camera.open(0)
if flag == False:
msg = QtWidgets.QMessageBox.warning(self, u"Warning", u"请检测相机与电脑是否连接正确",
buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
else:
self.list_B = np.ones(10) # 眼睛状态List,建议根据fps修改
self.list_Y = np.zeros(50) # 嘴巴状态list,建议根据fps修改
self.list_Y1 = np.ones(8) # 如果在list_Y中存在list_Y1,则判定一次打哈欠,同上,长度建议修改
self.blink_count = 0 # 眨眼计数
self.list_blink=np.ones(60) #判断60帧的睁眼闭眼
self.danger_count=0#危险行为帧
self.yawn_count = 0#哈欠帧
self.blink_freq = 0.5
self.yawn_freq = 0
self.timelb = time.clock()
self.btn_start.setText(u'停止运行')
self.btntestcamera.setEnabled(False)
self.btn_testvideo.setEnabled(False)
self.open_t = 0 # 用于刷新眼部状态label
self.danger_t=0 #用于刷新危险行为状态
self.blink_start = time.time() # 眨眼时间
self.yawn_start = time.time() # 打哈欠时间