更新使用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 requirements one by one...
echo Installing OpenCV... echo Installing OpenCV...
python -m pip install opencv-python==4.8.1.78 python -m pip install opencv-python==4.8.1.78
echo Installing MediaPipe...
python -m pip install mediapipe==0.10.18
echo Installing PyQt6... echo Installing PyQt6...
python -m pip install PyQt6==6.6.1 PyQt6-Qt6==6.6.1 PyQt6-sip==13.6.0 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 traceback
import logging import logging
import os import os
import mediapipe as mp
from datetime import datetime from datetime import datetime
from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QPushButton, QLabel, QSpinBox, QHBoxLayout, QPushButton, QLabel, QSpinBox,
@ -38,7 +37,7 @@ try:
try: try:
super().__init__() super().__init__()
logger.info("Initializing WorkspaceMonitor...") logger.info("Initializing WorkspaceMonitor...")
self.setWindowTitle("自动锁屏") self.setWindowTitle("工位监控系统")
self.setMinimumSize(1000, 700) self.setMinimumSize(1000, 700)
# 系统托盘 # 系统托盘
@ -50,13 +49,11 @@ try:
# 初始化变量 # 初始化变量
self.running = False self.running = False
self.timeout = 10 self.timeout = 10
logger.info("Initializing MediaPipe...") logger.info("Loading face detection model...")
self.mp_face_detection = mp.solutions.face_detection # 使用OpenCV的人脸检测器
self.mp_drawing = mp.solutions.drawing_utils self.face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
self.face_detection = self.mp_face_detection.FaceDetection( if self.face_cascade.empty():
model_selection=1, raise Exception("无法加载人脸检测模型")
min_detection_confidence=0.5
)
logger.info("Opening camera...") logger.info("Opening camera...")
self.cap = cv2.VideoCapture(0) self.cap = cv2.VideoCapture(0)
@ -81,7 +78,7 @@ try:
main_layout = QVBoxLayout(central_widget) main_layout = QVBoxLayout(central_widget)
# 标题 # 标题
title_label = QLabel("自动锁屏") title_label = QLabel("工位监控系统")
title_label.setFont(QFont('Arial', 16, QFont.Weight.Bold)) title_label.setFont(QFont('Arial', 16, QFont.Weight.Bold))
title_label.setAlignment(Qt.AlignmentFlag.AlignCenter) title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
main_layout.addWidget(title_label) main_layout.addWidget(title_label)
@ -223,26 +220,27 @@ try:
self.handle_camera_disconnected() self.handle_camera_disconnected()
return 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() current_time = time.time()
max_confidence = 0
if results.detections: if len(faces) > 0:
for detection in results.detections: # 简单地使用检测到的人脸数量作为置信度的基础
confidence = detection.score[0] max_confidence = min(len(faces) * 0.5, 1.0) # 限制最大值为1.0
max_confidence = max(max_confidence, confidence)
bbox = detection.location_data.relative_bounding_box for (x, y, w, h) in faces:
h, w, _ = frame.shape # 绘制人脸框
x = int(bbox.xmin * w) cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
y = int(bbox.ymin * h) # 显示置信度
width = int(bbox.width * w) cv2.putText(frame, f"置信度: {max_confidence:.2f}",
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, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 255, 0), 2) 0.5, (0, 255, 0), 2)
@ -283,10 +281,11 @@ try:
self.set_progress_bar_style("#4CAF50") # 重置为绿色 self.set_progress_bar_style("#4CAF50") # 重置为绿色
time.sleep(1) 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 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( scaled_pixmap = QPixmap.fromImage(qt_image).scaled(
self.camera_label.size(), self.camera_label.size(),
Qt.AspectRatioMode.KeepAspectRatio Qt.AspectRatioMode.KeepAspectRatio
@ -331,7 +330,7 @@ try:
def closeEvent(self, event): def closeEvent(self, event):
if self.running: if self.running:
self.tray_icon.showMessage("工位监控", "程序已最小化系统托盘", self.tray_icon.showMessage("工位监控", "程序已最小化系统托盘",
QSystemTrayIcon.MessageIcon.Information) QSystemTrayIcon.MessageIcon.Information)
self.hide() self.hide()
event.ignore() event.ignore()