主頁 >  其他 > 人工智能專案:AI監督電腦使用行為

人工智能專案:AI監督電腦使用行為

2022-01-03 07:22:45 其他

專案簡介:

電子科技大學中山學院2019級人工智能課程設計

語言:Python

開發工具:Pycharm、Python3.9

庫:PyQt、OpenCv、dlib

功能:

通過人工智能監督用戶使用電腦時的姿態與距離,

原始碼:

Qt界面:

"""
計算機視覺課程設計
ComputerVisual curriculum design

開發時間:2021年12月8日 下午 19:14
Development time: 2021.12.8 pm 19:14

著作權所有?:電子科技大學中山學院-陶凌杰
Property in copyright: University of Electronic Science and Technology of China_ZhongShan college-TonnyJack

僅供參考,禁止商用
For reference only, no commercial use
"""
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'CourseDesign.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# 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 sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

from PyQt5 import QtCore, QtGui, QtWidgets

import demo


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(662, 565)
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(151, 150, 171, 51))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(Dialog)
        self.pushButton_2.setGeometry(QtCore.QRect(341, 150, 171, 51))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(Dialog)
        self.pushButton_3.setGeometry(QtCore.QRect(151, 210, 171, 51))
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_4 = QtWidgets.QPushButton(Dialog)
        self.pushButton_4.setGeometry(QtCore.QRect(341, 210, 171, 51))
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton_5 = QtWidgets.QPushButton(Dialog)
        self.pushButton_5.setGeometry(QtCore.QRect(151, 270, 171, 51))
        self.pushButton_5.setObjectName("pushButton_5")
        self.pushButton_6 = QtWidgets.QPushButton(Dialog)
        self.pushButton_6.setGeometry(QtCore.QRect(341, 270, 171, 51))
        self.pushButton_6.setObjectName("pushButton_6")
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(230, 40, 241, 61))
        self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.label.setObjectName("label")
        self.textBrowser = QtWidgets.QTextBrowser(Dialog)
        self.textBrowser.setGeometry(QtCore.QRect(90, 350, 501, 191))
        self.textBrowser.setObjectName("textBrowser")

        self.retranslateUi(Dialog)
        self.pushButton.clicked.connect(self.push_buttom1)
        self.pushButton_2.clicked.connect(self.push_buttom2)
        self.pushButton_3.clicked.connect(self.push_buttom3)
        self.pushButton_4.clicked.connect(self.push_buttom4)
        self.pushButton_5.clicked.connect(self.push_buttom5)
        self.pushButton_6.clicked.connect(self.push_buttom6)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
        Dialog.show()

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "AiProgramByZSC_TonnyJack"))
        self.pushButton.setText(_translate("Dialog", "資料采集"))
        self.pushButton_2.setText(_translate("Dialog", "訓練模型"))
        self.pushButton_3.setText(_translate("Dialog", "人臉識別"))
        self.pushButton_4.setText(_translate("Dialog", "面部標點"))
        self.pushButton_5.setText(_translate("Dialog", "梯形方向"))
        self.pushButton_6.setText(_translate("Dialog", "三維坐標"))
        self.label.setText(_translate("Dialog",
                                      "<html><head/><body><p><span style=\" "
                                      "font-size:14pt;\">歡迎使用TonnyJack的程式</span></p></body></html>"))
        self.textBrowser.setHtml(_translate("Dialog",
                                            "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" "
                                            "\"http://www.w3.org/TR/REC-html40/strict.dtd\">\n "
                                            "<html><head><meta name=\"qrichtext\" content=\"1\" /><style "
                                            "type=\"text/css\">\n "
                                            "p, li { white-space: pre-wrap; }\n"
                                            "</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; "
                                            "font-weight:400; font-style:normal;\">\n "
                                            "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; "
                                            "margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" "
                                            "font-size:11pt;\">這是一個由Opencv和Python開發的人工智能程式</span></p>\n "
                                            "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; "
                                            "margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; "
                                            "font-size:11pt;\"><br /></p>\n "
                                            "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; "
                                            "margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" "
                                            "font-size:11pt;\">部分代碼來自:抖音-恩培</span></p>\n "
                                            "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; "
                                            "margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; "
                                            "font-size:11pt;\"><br /></p>\n "
                                            "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; "
                                            "margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" "
                                            "font-size:11pt;\">代碼已開源并發布在個人博客:</span></p>\n "
                                            "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; "
                                            "margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; "
                                            "font-size:11pt;\"><br /></p>\n "
                                            "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; "
                                            "margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" "
                                            "font-size:11pt;\">本程式僅供學習與參考,禁止商用!</span></p>\n "
                                            "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; "
                                            "margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; "
                                            "font-size:11pt;\"><br /></p>\n "
                                            "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; "
                                            "margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" "
                                            "font-size:11pt;\">著作權所有?:電子科技大學中山學院-陶凌杰</span></p>\n "
                                            "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; "
                                            "margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; "
                                            "font-size:11pt;\"><br /></p></body></html>"))

    def push_buttom1(self):
        self.m = demo.MonitorBabay()
        self.m.collectFacesFromCamera(1, 3, 3)

    def push_buttom2(self):
        self.m = demo.MonitorBabay()
        self.m.train()

    def push_buttom3(self):
        self.m = demo.MonitorBabay()
        self.m.run(960, 720, 1)

    def push_buttom4(self):
        self.m = demo.MonitorBabay()
        self.m.run(960, 720, 2)

    def push_buttom5(self):
        self.m = demo.MonitorBabay()
        self.m.run(960, 720, 3)

    def push_buttom6(self):
        self.m = demo.MonitorBabay()
        self.m.run(960, 720, 5)



if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = QWidget()
    ui = Ui_Dialog()
    ui.setupUi(form)
    form.show()
    sys.exit(app.exec_())

主程式:

"""
主要功能:檢測孩子是否在看電視,看了多久,距離多遠
使用技術點:人臉檢測、人臉識別(采集照片、訓練、識別)、姿態估計
"""
import os
from argparse import ArgumentParser

import cv2
import dlib
import time

from pose_estimator import PoseEstimator
from utils import Utils


class MonitorBabay:
    def __init__(self):
        # 人臉檢測
        self.face_detector = dlib.get_frontal_face_detector()
        # 人臉識別模型:pip uninstall opencv-python,pip install opencv-contrib-python
        self.face_model = cv2.face.LBPHFaceRecognizer_create()

        # 人臉68個關鍵點
        self.landmark_predictor = dlib.shape_predictor("./assets/shape_predictor_68_face_landmarks.dat")

        # 站在1.5M遠處,左眼最左邊距離右眼最右邊的像素距離(請使用getEyePixelDist方法校準,然后修改這里的值)
        self.eyeBaseDistance = 65

        # pose_estimator.show_3d_model()

        self.utils = Utils()

    # 采集照片用于訓練
    # 引數
    # label_index: label的索引
    # save_interval:隔幾秒存盤照片
    # save_num:存盤總量
    def collectFacesFromCamera(self, label_index, save_interval, save_num):
        cap = cv2.VideoCapture(0)

        width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
        height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)

        fpsTime = time.time()
        last_save_time = fpsTime
        saved_num = 0
        while True:
            _, frame = cap.read()
            frame = cv2.flip(frame, 1)
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            faces = self.face_detector(gray)

            for face in faces:

                if saved_num < save_num:
                    if (time.time() - last_save_time) > save_interval:
                        self.utils.save_face(face, frame, label_index)
                        saved_num += 1
                        last_save_time = time.time()

                        print('label_index:{index},成功采集第{num}張照片'.format(index=label_index, num=saved_num))
                else:
                    print('照片采集完畢!')
                    exit()

                self.utils.draw_face_box(face, frame, '', '', '')

            cTime = time.time()
            fps_text = 1 / (cTime - fpsTime)
            fpsTime = cTime

            frame = self.utils.cv2AddChineseText(frame, "幀率: " + str(int(fps_text)), (10, 30), textColor=(0, 255, 0),
                                                 textSize=50)
            frame = cv2.resize(frame, (int(width) // 2, int(height) // 2))
            cv2.imshow('Collect data', frame)
            if cv2.waitKey(5) & 0xFF == 27:
                break
        cap.release()

    # 訓練人臉模型
    def train(self):
        print('訓練開始!')
        label_list, img_list = self.utils.getFacesLabels()
        self.face_model.train(img_list, label_list)
        self.face_model.save("./face_model/model.yml")
        print('訓練完畢!')

    # 獲取兩個眼角像素距離
    def getEyePixelDist(self):

        cap = cv2.VideoCapture(0)

        width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
        height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)

        # 姿態估計
        self.pose_estimator = PoseEstimator(img_size=(height, width))

        fpsTime = time.time()

        while True:
            _, frame = cap.read()
            frame = cv2.flip(frame, 1)
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            faces = self.face_detector(gray)

            pixel_dist = 0

            for face in faces:
                # 關鍵點
                landmarks = self.landmark_predictor(gray, face)
                image_points = self.pose_estimator.get_image_points(landmarks)

                left_x = int(image_points[36][0])
                left_y = int(image_points[36][1])
                right_x = int(image_points[45][0])
                right_y = int(image_points[45][1])

                pixel_dist = abs(right_x - left_x)

                cv2.circle(frame, (left_x, left_y), 8, (255, 0, 255), -1)
                cv2.circle(frame, (right_x, right_y), 8, (255, 0, 255), -1)

                # 人臉框
                frame = self.utils.draw_face_box(face, frame, '', '', '')

            cTime = time.time()
            fps_text = 1 / (cTime - fpsTime)
            fpsTime = cTime

            frame = self.utils.cv2AddChineseText(frame, "幀率: " + str(int(fps_text)), (20, 30), textColor=(0, 255, 0),
                                                 textSize=50)
            frame = self.utils.cv2AddChineseText(frame, "像素距離: " + str(int(pixel_dist)), (20, 100),
                                                 textColor=(0, 255, 0), textSize=50)

            # frame = cv2.resize(frame, (int(width)//2, int(height)//2) )
            cv2.imshow('Baby wathching TV', frame)
            if cv2.waitKey(5) & 0xFF == 27:
                break
        cap.release()

    # 運行主程式
    def run(self, w, h, display):
        model_path = "./face_model/model.yml"
        if not os.path.exists(model_path):
            print('人臉識別模型檔案不存在,請先采集訓練')
            exit()

        label_zh = self.utils.loadLablZh()

        self.face_model.read(model_path)

        cap = cv2.VideoCapture(0)

        width = w
        height = h

        print(width, height)

        # 姿態估計
        self.pose_estimator = PoseEstimator(img_size=(height, width))

        fpsTime = time.time()

        zh_name = ''
        x_label = ''
        z_label = ''
        is_watch = ''
        angles = [0, 0, 0]
        person_distance = 0
        watch_start_time = fpsTime
        watch_duration = 0

        # fps = 12 videoWriter = cv2.VideoWriter('./record_video/out'+str(time.time())+'.mp4',
        # cv2.VideoWriter_fourcc(*'H264'), fps, (width,height))

        while True:
            _, frame = cap.read()
            frame = cv2.resize(frame, (width, height))
            frame = cv2.flip(frame, 1)
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            faces = self.face_detector(gray)

            for face in faces:

                x1, y1, x2, y2 = self.utils.getFaceXY(face)
                face_img = gray[y1:y2, x1:x2]

                try:
                    # 人臉識別
                    idx, confidence = self.face_model.predict(face_img)
                    zh_name = label_zh[str(idx)]
                except cv2.error:
                    print('cv2.error')

                # 關鍵點
                landmarks = self.landmark_predictor(gray, face)
                # 計算旋轉矢量
                rotation_vector, translation_vector = self.pose_estimator.solve_pose_by_68_points(landmarks)

                # 計算距離
                person_distance = round(self.pose_estimator.get_distance(self.eyeBaseDistance), 2)

                # 計算角度
                rmat, jac = cv2.Rodrigues(rotation_vector)
                angles, mtxR, mtxQ, Qx, Qy, Qz = cv2.RQDecomp3x3(rmat)

                if angles[1] < -15:
                    x_label = '左'
                elif angles[1] > 15:
                    x_label = '右'
                else:
                    x_label = '前'

                if angles[0] < -15:
                    z_label = "下"
                elif angles[0] > 15:
                    z_label = "上"
                else:
                    z_label = "中"

                is_watch = '是' if (x_label == '前' and z_label == '中') else '否'

                if is_watch == '是':
                    now = time.time()
                    watch_duration += (now - watch_start_time)

                watch_start_time = time.time()

                if display == 1:
                    # 人臉框
                    frame = self.utils.draw_face_box(face, frame, zh_name, is_watch, person_distance)
                if display == 2:
                    # 68個關鍵點
                    self.utils.draw_face_points(landmarks, frame)
                if display == 3:
                    # 梯形方向
                    self.pose_estimator.draw_annotation_box(
                        frame, rotation_vector, translation_vector, is_watch)
                if display == 4:
                    # 指標
                    self.pose_estimator.draw_pointer(frame, rotation_vector, translation_vector)
                if display == 5:
                    # 三維坐標系
                    self.pose_estimator.draw_axes(frame, rotation_vector, translation_vector)

                # 僅測驗單人
                break

            cTime = time.time()
            fps_text = 1 / (cTime - fpsTime)
            fpsTime = cTime

            frame = self.utils.cv2AddChineseText(frame, "幀率: " + str(int(fps_text)), (20, 30), textColor=(0, 255, 0),
                                                 textSize=50)

            color = (255, 0, 255) if person_distance <= 1 else (0, 255, 0)

            frame = self.utils.cv2AddChineseText(frame, "距離: " + str(person_distance) + "m", (20, 100), textColor=color,
                                                 textSize=50)

            color = (255, 0, 255) if is_watch == '是' else (0, 255, 0)

            frame = self.utils.cv2AddChineseText(frame, "觀看: " + str(is_watch), (20, 170), textColor=color, textSize=50)

            #
            duration_str = str(round((watch_duration / 60), 2)) + "min"

            frame = self.utils.cv2AddChineseText(frame, "時長: " + duration_str, (20, 240), textColor=(0, 255, 0),
                                                 textSize=50)

            color = (255, 0, 255) if x_label == '前' else (0, 255, 0)

            frame = self.utils.cv2AddChineseText(frame, "X軸: {degree}° {x_label} ".format(x_label=str(x_label),
                                                                                          degree=str(int(angles[1]))),
                                                 (20, height - 220), textColor=color, textSize=40)

            color = (255, 0, 255) if z_label == '中' else (0, 255, 0)

            frame = self.utils.cv2AddChineseText(frame, "Z軸: {degree}° {z_label}".format(z_label=str(z_label),
                                                                                         degree=str(int(angles[0]))),
                                                 (20, height - 160), textColor=color, textSize=40)

            frame = self.utils.cv2AddChineseText(frame, "Y軸: {degree}°".format(degree=str(int(angles[2]))),
                                                 (20, height - 100), textColor=(0, 255, 0), textSize=40)

            # videoWriter.write(frame)
            # frame = cv2.resize(frame, (int(width)//2, int(height)//2) )
            cv2.imshow('Baby wathching TV', frame)
            if cv2.waitKey(5) & 0xFF == 27:
                break
        cap.release()


if __name__ == "__main__":
    m = MonitorBabay()
    m.run(960, 720, 5)

# 引數
# parser = ArgumentParser()
# parser.add_argument("--mode", type=str, default='run',
#                     help="運行模式:collect,train,distance,run對應:采集、訓練、評估距離、主程式")
# parser.add_argument("--label_id", type=int, default=1,
#                     help="采集照片標簽id.")
# parser.add_argument("--img_count", type=int, default=3,
#                     help="采集照片數量")
# parser.add_argument("--img_interval", type=int, default=3,
#                     help="采集照片間隔時間")
#
# parser.add_argument("--display", type=int, default=1,
#                     help="顯示模式,取值1-5")
#
# parser.add_argument("--w", type=int, default=960,
#                     help="畫面寬度")
# parser.add_argument("--h", type=int, default=720,
#                     help="畫面高度")
# args = parser.parse_args()
#
# mode = args.mode
#
# if mode == 'collect':
#     print("即將采集照片.")
#     if args.label_id and args.img_count and args.img_interval:
#         m.collectFacesFromCamera(args.label_id, args.img_interval, args.img_count)
#
# if mode == 'train':
#     m.train()
#
# if mode == 'distance':
#     m.getEyePixelDist()
#
# if mode == 'run':
#     m.run(args.w, args.h, args.display)

封裝姿態判斷工具:

"""
封裝常用工具,降低Demo復雜度

"""
import cv2
import numpy as np


class PoseEstimator:
    """Estimate head pose according to the facial landmarks"""

    def __init__(self, img_size=(480, 640)):
        self.size = img_size

        self.model_points_68 = self._get_full_model_points()
        self.image_points = None

        # Camera internals
        self.focal_length = self.size[1]
        self.camera_center = (self.size[1] / 2, self.size[0] / 2)
        self.camera_matrix = np.array(
            [[self.focal_length, 0, self.camera_center[0]],
             [0, self.focal_length, self.camera_center[1]],
             [0, 0, 1]], dtype="double")

        # Assuming no lens distortion
        self.dist_coeefs = np.zeros((4, 1))

        # Rotation vector and translation vector
        self.r_vec = np.array([[0.01891013], [0.08560084], [-3.14392813]])
        self.t_vec = np.array(
            [[-14.97821226], [-10.62040383], [-2053.03596872]])
        # self.r_vec = None
        # self.t_vec = None

    def _get_full_model_points(self, filename='assets/model.txt'):
        """Get all 68 3D model points from file"""
        raw_value = []
        with open(filename) as file:
            for line in file:
                raw_value.append(line)
        model_points = np.array(raw_value, dtype=np.float32)
        model_points = np.reshape(model_points, (3, -1)).T

        # Transform the model into a front view.
        model_points[:, 2] *= -1

        return model_points

    def show_3d_model(self):
        import matplotlib.pyplot as plt

        x = self.model_points_68[:, 0]
        y = self.model_points_68[:, 1]
        z = self.model_points_68[:, 2]

        fig = plt.figure()
        ax = plt.axes(projection="3d")

        ax.scatter3D(x, y, z)
        plt.show()

    # 利用相似三角形估算距離

    def get_distance(self, eyeBaseDistance):
        image_points = self.image_points
        left_x = int(image_points[36][0])
        right_x = int(image_points[45][0])

        pixel_dist = abs(right_x - left_x)

        return (eyeBaseDistance / pixel_dist) * 1.5

    def get_image_points(self, landmarks):
        landmarks_list = []
        for n in range(0, 68):
            x = landmarks.part(n).x
            y = landmarks.part(n).y
            landmarks_list.append([x, y])

        image_points = np.array(landmarks_list, dtype="double")

        self.image_points = image_points

        return image_points

    def solve_pose_by_68_points(self, landmarks):

        image_points = self.get_image_points(landmarks)
        """
        Solve pose from all the 68 image points
        Return (rotation_vector, translation_vector) as pose.
        """

        if self.r_vec is None:
            (_, rotation_vector, translation_vector) = cv2.solvePnP(
                self.model_points_68, image_points, self.camera_matrix, self.dist_coeefs)
            self.r_vec = rotation_vector
            self.t_vec = translation_vector

        (_, rotation_vector, translation_vector) = cv2.solvePnP(
            self.model_points_68,
            image_points,
            self.camera_matrix,
            self.dist_coeefs,
            rvec=self.r_vec,
            tvec=self.t_vec,
            useExtrinsicGuess=True)

        return (rotation_vector, translation_vector)

    def draw_annotation_box(self, image, rotation_vector, translation_vector, is_watch, line_width=2):
        """Draw a 3D box as annotation of pose"""
        point_3d = []
        rear_size = 75
        rear_depth = 0
        point_3d.append((-rear_size, -rear_size, rear_depth))
        point_3d.append((-rear_size, rear_size, rear_depth))
        point_3d.append((rear_size, rear_size, rear_depth))
        point_3d.append((rear_size, -rear_size, rear_depth))
        point_3d.append((-rear_size, -rear_size, rear_depth))

        front_size = 100
        front_depth = 100
        point_3d.append((-front_size, -front_size, front_depth))
        point_3d.append((-front_size, front_size, front_depth))
        point_3d.append((front_size, front_size, front_depth))
        point_3d.append((front_size, -front_size, front_depth))
        point_3d.append((-front_size, -front_size, front_depth))
        point_3d = np.array(point_3d, dtype=np.float).reshape(-1, 3)

        # Map to 2d image points
        (point_2d, _) = cv2.projectPoints(point_3d,
                                          rotation_vector,
                                          translation_vector,
                                          self.camera_matrix,
                                          self.dist_coeefs)
        point_2d = np.int32(point_2d.reshape(-1, 2))

        color = (255, 0, 255) if is_watch == '是' else (0, 255, 0)

        # Draw all the lines
        cv2.polylines(image, [point_2d], True, color, line_width, cv2.LINE_AA)
        cv2.line(image, tuple(point_2d[1]), tuple(
            point_2d[6]), color, line_width, cv2.LINE_AA)
        cv2.line(image, tuple(point_2d[2]), tuple(
            point_2d[7]), color, line_width, cv2.LINE_AA)
        cv2.line(image, tuple(point_2d[3]), tuple(
            point_2d[8]), color, line_width, cv2.LINE_AA)

    # 坐標系
    def draw_axes(self, img, R, t):
        img = cv2.drawFrameAxes(img, self.camera_matrix, self.dist_coeefs, R, t, 200)

    # 方向指標
    def draw_pointer(self, img, R, t):
        point1 = (int(self.image_points[30][0]), int(self.image_points[30][1]))

        nose_end_point2D, jacobian = cv2.projectPoints(np.array([(0.0, 0.0, 300.0)]), R, t, self.camera_matrix,
                                                       self.dist_coeefs)

        point2 = (int(nose_end_point2D[0][0][0]), int(nose_end_point2D[0][0][1]))

        cv2.line(img, point1, point2, (0, 255, 0), 4)

封裝常用工具:

"""
封裝常用工具,降低Demo復雜度
"""
# 匯入PIL
from PIL import Image, ImageDraw, ImageFont
# 匯入OpenCV
import cv2
from matplotlib.pyplot import xlabel
import numpy as np
import time
import os
import glob

from sys import platform as _platform


class Utils:
    def __init__(self):
        pass

    # 添加中文
    def cv2AddChineseText(self, img, text, position, textColor=(0, 255, 0), textSize=30):
        if isinstance(img, np.ndarray):  # 判斷是否OpenCV圖片型別
            img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
        # 創建一個可以在給定影像上繪圖的物件
        draw = ImageDraw.Draw(img)
        # 字體的格式
        fontStyle = ImageFont.truetype(
            "./fonts/simsun.ttc", textSize, encoding="utf-8")
        # 繪制文本
        draw.text(position, text, textColor, font=fontStyle)
        # 轉換回OpenCV格式
        return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)

    def getFaceXY(self, face):
        x1 = face.left()
        y1 = face.top()
        x2 = face.right()
        y2 = face.bottom()

        return x1, y1, x2, y2

    # 人臉框框
    def draw_face_box(self, face, frame, zh_name, is_watch, distance):

        color = (255, 0, 255) if is_watch == '是' else (0, 255, 0)
        x1, y1, x2, y2 = self.getFaceXY(face)

        frame = cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
        m_str = '' if distance == '' else 'm'
        frame = self.cv2AddChineseText(frame,
                                       "{name} {distance}{m_str}".format(name=zh_name, distance=distance, m_str=m_str),
                                       (x1, y1 - 60), textColor=color, textSize=50)

        return frame

    # 保存人臉照片
    def save_face(self, face, frame, face_label_index):

        path = './face_imgs/' + str(face_label_index)
        if not os.path.exists(path):
            os.makedirs(path)

        x1, y1, x2, y2 = self.getFaceXY(face)
        face_img = frame[y1:y2, x1:x2]

        filename = path + '/' + str(face_label_index) + '-' + str(time.time()) + '.jpg'

        cv2.imwrite(filename, face_img)

    # 人臉點
    def draw_face_points(self, landmarks, frame):
        for n in range(0, 68):
            x = landmarks.part(n).x
            y = landmarks.part(n).y
            cv2.circle(frame, (x, y), 4, (255, 0, 255), -1)

    # 獲取人臉和label資料
    def getFacesLabels(self):
        # 遍歷所有檔案
        label_list = []
        img_list = []
        for file_path in glob.glob('./face_imgs/*/*'):

            dir_str = '/'
            if _platform == "linux" or _platform == "linux2":
                # linux
                pass
            elif _platform == "darwin":
                # MAC OS X
                pass
            elif _platform == "win32":
                # Windows
                dir_str = '\\'
            elif _platform == "win64":
                # Windows 64-bit
                dir_str = '\\'

            label_list.append(int(file_path.split(dir_str)[-1].split('-')[0]))

            img = cv2.imread(file_path)
            # img = cv2.resize(img, (224, 224))
            img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            img_list.append(img)

        return np.array(label_list), img_list

    # 加載label 對應中文
    def loadLablZh(self):
        with open('./face_model/label.txt', encoding='utf-8') as f:
            back_dict = {}
            for line in f.readlines():
                label_index = line.split(',')[0]
                label_zh = line.split(',')[1].split('\n')[0]
                back_dict[label_index] = label_zh

            return back_dict

使用方法:

一:搜索Python,進入官網,下載Python3.9解釋器

二:搜索Pycharm,進入官網,下載Pycharm community并安裝

三:在Pycharm中配置python解釋器,打開終端,輸入以下三個命令

pip install OpenCV-python -i https://mirrors.aliyun.com/pypi/simple/

pip install mediapipe -i https://mirrors.aliyun.com/pypi/simple/

pip install PyQt5==5.10.1 -i https://mirrors.aliyun.com/pypi/simple/

四:將三個代碼檔案放在同一檔案目錄下,運行QT界面的代碼

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/401498.html

標籤:其他

上一篇:原始碼解讀ReDet:A Rotation-equivariant Detector for Aerial Object Detection

下一篇:MATLAB的colorbar取對數

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more