更新使用opencv自己的人脸

This commit is contained in:
scout 2024-12-04 15:40:25 +08:00
parent f8d02565d9
commit 0bb246e76f
2 changed files with 32 additions and 35 deletions

View File

@ -15,8 +15,6 @@ python -m pip install --upgrade pip
echo Installing requirements one by one...
echo Installing OpenCV...
python -m pip install opencv-python==4.8.1.78
echo Installing MediaPipe...
python -m pip install mediapipe==0.10.18
echo Installing PyQt6...
python -m pip install PyQt6==6.6.1 PyQt6-Qt6==6.6.1 PyQt6-sip==13.6.0

View File

@ -5,7 +5,6 @@ import sys
import traceback
import logging
import os
import mediapipe as mp
from datetime import datetime
from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QPushButton, QLabel, QSpinBox,
@ -38,7 +37,7 @@ try:
try:
super().__init__()
logger.info("Initializing WorkspaceMonitor...")
self.setWindowTitle("自动锁屏")
self.setWindowTitle("工位监控系统")
self.setMinimumSize(1000, 700)
# 系统托盘
@ -50,13 +49,11 @@ try:
# 初始化变量
self.running = False
self.timeout = 10
logger.info("Initializing MediaPipe...")
self.mp_face_detection = mp.solutions.face_detection
self.mp_drawing = mp.solutions.drawing_utils
self.face_detection = self.mp_face_detection.FaceDetection(
model_selection=1,
min_detection_confidence=0.5
)
logger.info("Loading face detection model...")
# 使用OpenCV的人脸检测器
self.face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
if self.face_cascade.empty():
raise Exception("无法加载人脸检测模型")
logger.info("Opening camera...")
self.cap = cv2.VideoCapture(0)
@ -81,7 +78,7 @@ try:
main_layout = QVBoxLayout(central_widget)
# 标题
title_label = QLabel("自动锁屏")
title_label = QLabel("工位监控系统")
title_label.setFont(QFont('Arial', 16, QFont.Weight.Bold))
title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
main_layout.addWidget(title_label)
@ -223,27 +220,28 @@ try:
self.handle_camera_disconnected()
return
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = self.face_detection.process(frame_rgb)
# 转换为灰度图进行人脸检测
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = self.face_cascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30)
)
max_confidence = 0
current_time = time.time()
max_confidence = 0
if results.detections:
for detection in results.detections:
confidence = detection.score[0]
max_confidence = max(max_confidence, confidence)
bbox = detection.location_data.relative_bounding_box
h, w, _ = frame.shape
x = int(bbox.xmin * w)
y = int(bbox.ymin * h)
width = int(bbox.width * w)
height = int(bbox.height * h)
cv2.rectangle(frame, (x, y), (x + width, y + height), (0, 255, 0), 2)
cv2.putText(frame, f"置信度: {confidence:.2f}",
(x, y-10), cv2.FONT_HERSHEY_SIMPLEX,
if len(faces) > 0:
# 简单地使用检测到的人脸数量作为置信度的基础
max_confidence = min(len(faces) * 0.5, 1.0) # 限制最大值为1.0
for (x, y, w, h) in faces:
# 绘制人脸框
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# 显示置信度
cv2.putText(frame, f"置信度: {max_confidence:.2f}",
(x, y-10), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 255, 0), 2)
self.last_face_time = current_time
@ -283,10 +281,11 @@ try:
self.set_progress_bar_style("#4CAF50") # 重置为绿色
time.sleep(1)
rgb_display = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
h, w, ch = rgb_display.shape
# 显示图像
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
h, w, ch = rgb_frame.shape
bytes_per_line = ch * w
qt_image = QImage(rgb_display.data, w, h, bytes_per_line, QImage.Format.Format_RGB888)
qt_image = QImage(rgb_frame.data, w, h, bytes_per_line, QImage.Format.Format_RGB888)
scaled_pixmap = QPixmap.fromImage(qt_image).scaled(
self.camera_label.size(),
Qt.AspectRatioMode.KeepAspectRatio
@ -328,10 +327,10 @@ try:
def lock_windows(self):
ctypes.windll.user32.LockWorkStation()
def closeEvent(self, event):
if self.running:
self.tray_icon.showMessage("工位监控", "程序已最小化系统托盘",
self.tray_icon.showMessage("工位监控", "程序已最小化系统托盘",
QSystemTrayIcon.MessageIcon.Information)
self.hide()
event.ignore()