def __init__(self):
        QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)

        # 初始变量
        self.video_path = None
        self.video_type = None  # 0: offline  1: webcamera
        self.status = opt.STATUS_INIT  # 0: init 1:playing 2: pause
        self.video_label_size = None
        self.video_size = None
        self.video_show_size = None
        self.video_center_box = None
        self.fr_ready = False
        self.fr_input_img = None
        opt.display_mw_view = self.viewList  # 更新输出控件
        opt.display_mw_sys = self.infoList  # 更新输出控件
        # 线程设置
        self.timer = None
        self.fr_worker = None
        self.load_worker = None
        self.dot_worker = None

        # 视频初始参数设置
        self.playCapture = VideoCapture()
        # self.videoWriter = VideoWriter('*.mp4', VideoWriter_fourcc('M', 'J', 'P', 'G'), self.fps, size)
        # 欢迎信息
        opt.print_system_gui('欢迎进入系统!')
    def login_main(self):

        string = '数据加载中,请稍后...'
        print(string)
        opt.print_system_gui(string)
        # 强制处理其他事物,防止GUI卡死
        QApplication.processEvents()

        string = '正在加载人员数据'
        print(string)
        opt.print_system_gui(string)
        self.load_database()
        # 强制处理其他事物,防止GUI卡死
        QApplication.processEvents()

        string = '正在加载深度神经网络'
        print(string)
        opt.print_system_gui(string)
        self.load_model()
        # 强制处理其他事物,防止GUI卡死
        QApplication.processEvents()

        string = '正在编码人员数据'
        print(string)
        opt.print_system_gui(string)
        self.create_person_data()
        # 强制处理其他事物,防止GUI卡死
        QApplication.processEvents()

        string = '数据加载成功,可以开始监测。'
        print(string)
        opt.print_system_gui(string)

        opt.is_net_load_done = True
 def pause(self):
     if self.playCapture.isOpened():
         # 停止线程
         self.timer.stop()
         self.fr_worker.stop()
         # 改为暂停状态
         self.status = opt.STATUS_PAUSE
         self.playButton.setIcon(self.style().standardIcon(
             QStyle.SP_MediaPlay))
         print('暂停显示')
         opt.print_system_gui('暂停监测')
 def re_play(self):
     if self.playCapture.isOpened():
         # 启动线程
         self.timer.start()
         # 改为播放状态
         self.status = opt.STATUS_PLAYING
         self.playButton.setIcon(self.style().standardIcon(
             QStyle.SP_MediaPause))
         print('恢复显示')
         opt.print_system_gui('恢复监测')
         # 延后启动线程
         time.sleep(0.01)
         self.fr_worker.start()
 def stop(self):
     if self.playCapture.isOpened():
         # 注意先后暂停线程
         if self.fr_worker is not None:
             self.fr_worker.stop()
         if self.timer is not None:
             self.timer.stop()
         self.playCapture.release()
         # 改为停止状态
         self.status = opt.STATUS_STOP
         self.playButton.setIcon(self.style().standardIcon(
             QStyle.SP_MediaPlay))
         print('停止显示')
         opt.print_system_gui('停止监测')
 def chk_video_type(self):
     if self.fr_ready:
         if self.offlineButton.isChecked(
         ) and self.video_type != opt.VIDEO_TYPE_OFFLINE:
             self.switch_offline()
         if self.webcamButton.isChecked(
         ) and self.video_type != opt.VIDEO_TYPE_WEBCAMERA:
             self.switch_webcam()
         if self.onlineButton.isChecked(
         ) and self.video_type != opt.VIDEO_TYPE_ONLINE:
             self.switch_online()
     else:
         print('未加载网络数据')
         opt.print_system_gui('未加载网络数据!请先加载网络数据')
 def play(self):
     if not self.playCapture.isOpened():
         if self.video_type is opt.VIDEO_TYPE_WEBCAMERA:
             self.playCapture = VideoCapture(0)
         else:
             print('获取在线流媒体中...')
             self.playCapture.open(self.video_path)
     # 启动线程
     self.timer.start()
     # 改为播放状态
     self.status = opt.STATUS_PLAYING
     self.playButton.setIcon(self.style().standardIcon(
         QStyle.SP_MediaPause))
     print('开始显示')
     opt.print_system_gui('开始监测')
     # 延后启动线程
     time.sleep(0.01)
     self.fr_worker.start()
    def who_is_it(self, test_img):
        """
        Implements face recognition for the happy house by finding who is the person on the image_path image.

        Arguments:
        image_path -- path to an image
        database -- database containing image encodings along with the name of the person on the image
        model -- your Inception model instance in Keras

        Returns:
        min_dist -- the minimum distance between image_path encoding and the encodings from the database
        identity -- string, the name prediction for the person on image_path
        """

        # START CODE HERE #
        # 适应网络输入尺寸, 预处理变换
        test_img = self.img_preprocessing(test_img)
        # print(np.shape(test_img))
        test_img = torch.unsqueeze(test_img, 0)
        # print(np.shape(test_img))
        # Step 1: Compute the target "encoding" for the image.
        if opt.cuda:
            images = Variable(test_img).cuda()
        else:
            images = Variable(test_img)
        self.net.eval()

        # Step 1: Compute the encoding for the image.
        with torch.no_grad():
            encodings = self.net(images)
        encoding = torch.squeeze(encodings)
        # print(np.shape(encoding))
        if opt.cuda:
            encoding = encoding.data.cpu().numpy()
        else:
            encoding = encoding.data.numpy()

        # Step 2: Find the closest encoding #

        # Initialize "min_dist" to a large value, say 100 (≈1 line)
        min_dist = 100
        identity = 'None'
        # Loop over the database dictionary's names and encodings.
        for (name, db_enc) in self.database.items():

            # Compute L2 distance between the target "encoding" and the current "emb" from the database. (≈ 1 line)
            dist = np.linalg.norm(encoding - db_enc)
            print('监测与 %s 距离为: %f ' % (name, dist))
            # If this distance is less than the min_dist, then set min_dist to dist, and identity to name. (≈ 3 lines)
            if dist < min_dist:
                min_dist = dist
                identity = name

        # END CODE HERE #

        time_str = time.strftime('%m月%d日-%H时%M分%S秒')
        if min_dist > self.net_best_margin:
            string = time_str + ' : ' + '非数据库中人员'
            sys_string = '检测事件(监测到人员不在数据库当中,检测距离%0.2f)' % min_dist
        else:
            string = time_str + ' : ' + identity + '到访'
            sys_string = '检测事件(监测到人员%s,检测距离%0.2f)' % (identity, min_dist)
        print(opt.WARNING + string + opt.ENDC)
        opt.print_view_gui(string)
        opt.print_system_gui(sys_string)
 def switch_online(self):
     self.set_video(video_path='rtmp://58.200.131.2:1935/livetv/hunantv',
                    video_type=opt.VIDEO_TYPE_ONLINE)
     opt.print_system_gui('视频模式:online')
 def switch_webcam(self):
     self.set_video(video_path='', video_type=opt.VIDEO_TYPE_WEBCAMERA)
     opt.print_system_gui('视频模式:webcam')
 def switch_offline(self):
     self.set_video(video_path='./video/f40.mp4',
                    video_type=opt.VIDEO_TYPE_OFFLINE)
     opt.print_system_gui('视频模式:offline')
 def login_finish(self):
     self.main_window.show()
     opt.print_system_gui('登录系统')
     self.close()
 def login_fun(self):
     fr.login_main()
     self.main_window.show()
     opt.print_system_gui('登录系统')
     self.close()