face detection, eye detection,blink detection, and color tracking

MPT是一款跨平台工具箱,提供实时感知基础组件库,包括人脸识别、眼动检测、眨眼检测及颜色追踪等功能。此外,还介绍了实时人脸跟踪与识别的概念,即在视频流中定位人脸并匹配已知人脸数据库进行身份验证。
部署运行你感兴趣的模型镜像

MPT is a toolbox that supplies cross-platform librariesfor real-time perception primitives, including face detection, eye detection,blink detection, and color tracking.

 

http://sourceforge.net/projects/mptbox/

 

 

Real time face tracking and recognition

 

refers to the task of locating human faces in a video stream and identifying the faces by matching them against the database of known faces.

http://rtftr.sourceforge.net/

 

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

对以下代码在不改变功能的情况下,简化代码。import cv2 import dlib import math import numpy as np import time from scipy.spatial import distance as dist # Load detector detector = dlib.get_frontal_face_detector() # Load predictor try: predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") except Exception as e: print(f"Failed to load model file: {e}") exit() # Define facial landmarks indices MOUTH_POINTS = list(range(48, 68)) LEFT_EYE_POINTS = list(range(36, 42)) RIGHT_EYE_POINTS = list(range(42, 48)) # Euclidean distance calculation def euclidean_distance(point1, point2): return math.sqrt((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2) # Mouth Aspect Ratio calculation def mouth_aspect_ratio(mouth_points): if len(mouth_points) < 11: return 0.0 # Vertical distances A = euclidean_distance(mouth_points[2], mouth_points[10]) B = euclidean_distance(mouth_points[4], mouth_points[8]) # Horizontal distance C = euclidean_distance(mouth_points[0], mouth_points[6]) mar = (A + B) / (2.0 * C) return mar # Eye Aspect Ratio (EAR) calculation def eye_aspect_ratio(eye_points): A = dist.euclidean(eye_points[1], eye_points[5]) B = dist.euclidean(eye_points[2], eye_points[4]) C = dist.euclidean(eye_points[0], eye_points[3]) ear = (A + B) / (2.0 * C) return ear # Initialize video capture cap = cv2.VideoCapture(0) if not cap.isOpened(): print("Failed to open camera") exit() # Get screen resolution for window sizing try: # Default to 1080p screen_width = 1920 screen_height = 1080 except: screen_width = 1920 screen_height = 1080 # Scale factor for larger window SCALE_FACTOR = 1.8 # Threshold settings MOUTH_CLOSED_THRESHOLD = 0.45 MOUTH_OPEN_THRESHOLD = 0.45 EYE_CLOSED_THRESHOLD = 0.20 EYE_OPEN_THRESHOLD = 0.25 # Drowsiness detection parameters YAWN_DURATION_THRESH = 1.5 # Minimum yawn duration (seconds) MICRO_SLEEP_THRESH = 1.0 # Minimum micro-sleep duration (seconds) CONSECUTIVE_YAWN_THRESH = 3 # Consecutive yawn warning threshold STATE_RESET_TIME = 60.0 # Reset state every 30 seconds BLINK_DURATION_THRESH = 2.0 # Minimum blink duration for counting (seconds) # Face distance calculation parameters KNOWN_FACE_WIDTH = 16.0 # Average face width in centimeters FOCAL_LENGTH = 700 # Approximate focal length (adjust based on camera) # State tracking variables blink_total = 0 yawn_counter = 0 eye_closed_duration = 0 last_eye_open_time = time.time() last_yawn_time = 0 last_reset_time = time.time() last_blink_time = 0 drowsy_warning = "" drowsy_level = 0 face_distance = 0.0 eyes_closed = False blink_detected = False # Performance counters start_time = time.time() # Function to calculate face distance def calculate_face_distance(face_width_pixels): """ Calculate distance to face using triangle similarity :param face_width_pixels: width of face in pixels :return: distance in centimeters """ if face_width_pixels == 0: return 0.0 return (KNOWN_FACE_WIDTH * FOCAL_LENGTH) / face_width_pixels while True: ret, frame = cap.read() if not ret or frame is None: print("Failed to capture frame") continue # Horizontal flip frame = cv2.flip(frame, 1) # Create a copy for transparent overlays display_frame = frame.copy() # Convert to grayscale for face detection gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Detect faces faces = detector(gray) # Reset per-frame states mouth_open = False current_time = time.time() # Reset state every 30 seconds if current_time - last_reset_time > STATE_RESET_TIME: blink_total = 0 yawn_counter = 0 drowsy_level = 0 drowsy_warning = "" last_reset_time = current_time print("State reset") # Calculate face distance if face detected face_distance = 0.0 # Reset each frame if len(faces) > 0: face = faces[0] # Use first face for distance calculation face_width = face.right() - face.left() face_distance = calculate_face_distance(face_width) # Display face distance in top-left corner (RED) distance_text = f"Distance: {face_distance:.1f} cm" cv2.putText(display_frame, distance_text, (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) for face in faces: x1, y1, x2, y2 = face.left(), face.top(), face.right(), face.bottom() cv2.rectangle(display_frame, (x1, y1), (x2, y2), (0, 255, 0), 2) # Get facial landmarks try: landmarks = predictor(gray, face) except Exception as e: print(f"Landmark prediction error: {e}") continue # Get mouth points mouth_points = [] for n in MOUTH_POINTS: try: x = landmarks.part(n).x y = landmarks.part(n).y mouth_points.append((x, y)) except: continue # Get eye points left_eye_points = [] for n in LEFT_EYE_POINTS: try: x = landmarks.part(n).x y = landmarks.part(n).y left_eye_points.append((x, y)) except: continue right_eye_points = [] for n in RIGHT_EYE_POINTS: try: x = landmarks.part(n).x y = landmarks.part(n).y right_eye_points.append((x, y)) except: continue # Calculate mouth state if len(mouth_points) >= 11: mar = mouth_aspect_ratio(mouth_points) if mar > MOUTH_OPEN_THRESHOLD: mouth_status = "OPEN" mouth_color = (0, 0, 255) # Red mouth_open = True # Record yawn start time if last_yawn_time == 0: last_yawn_time = current_time else: mouth_status = "CLOSED" if mar < MOUTH_CLOSED_THRESHOLD else "NEUTRAL" mouth_color = (0, 255, 0) if mouth_status == "CLOSED" else (0, 255, 255) # Detect yawn completion if last_yawn_time > 0: yawn_duration = current_time - last_yawn_time if yawn_duration >= YAWN_DURATION_THRESH: yawn_counter += 1 print(f"Yawn detected #{yawn_counter}, duration: {yawn_duration:.2f} seconds") last_yawn_time = 0 # Display mouth status (red text) cv2.putText(display_frame, f"Mouth: {mouth_status}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 1) # Draw mouth contour if len(mouth_points) > 2: try: mouth_hull = cv2.convexHull(np.array(mouth_points)) cv2.drawContours(display_frame, [mouth_hull], -1, mouth_color, 1) except: pass # Calculate eye states left_ear, right_ear, avg_ear = 0, 0, 0 left_eye_status = "N/A" right_eye_status = "N/A" if len(left_eye_points) == 6: left_ear = eye_aspect_ratio(left_eye_points) if left_ear < EYE_CLOSED_THRESHOLD: left_eye_status = "CLOSED" left_eye_color = (0, 0, 255) elif left_ear > EYE_OPEN_THRESHOLD: left_eye_status = "OPEN" left_eye_color = (0, 255, 0) else: left_eye_status = "PARTIAL" left_eye_color = (0, 255, 255) # Draw eye contour try: left_eye_hull = cv2.convexHull(np.array(left_eye_points)) cv2.drawContours(display_frame, [left_eye_hull], -1, left_eye_color, 1) except: pass if len(right_eye_points) == 6: right_ear = eye_aspect_ratio(right_eye_points) if right_ear < EYE_CLOSED_THRESHOLD: right_eye_status = "CLOSED" right_eye_color = (0, 0, 255) elif right_ear > EYE_OPEN_THRESHOLD: right_eye_status = "OPEN" right_eye_color = (0, 255, 0) else: right_eye_status = "PARTIAL" right_eye_color = (0, 255, 255) # Draw eye contour try: right_eye_hull = cv2.convexHull(np.array(right_eye_points)) cv2.drawContours(display_frame, [right_eye_hull], -1, right_eye_color, 1) except: pass # Calculate average EAR if len(left_eye_points) == 6 and len(right_eye_points) == 6: avg_ear = (left_ear + right_ear) / 2.0 # Detect eye state changes if avg_ear < EYE_CLOSED_THRESHOLD: if not eyes_closed: # Eyes just closed - record start time last_eye_closed_time = current_time eyes_closed = True blink_detected = False else: # Eyes still closed - check if we should count a blink if not blink_detected and (current_time - last_eye_closed_time) >= BLINK_DURATION_THRESH: blink_total += 1 blink_detected = True # Mark this closure as counted print(f"Blink detected (duration: {current_time - last_eye_closed_time:.1f}s)") else: if eyes_closed: # Eyes just opened eye_closed_duration = current_time - last_eye_closed_time # Detect micro-sleep if eye_closed_duration >= MICRO_SLEEP_THRESH: drowsy_warning = "SLEEPY!" drowsy_level = max(drowsy_level, 2) print(f"Micro-sleep detected: {eye_closed_duration:.2f} seconds") # Reset eye state eyes_closed = False blink_detected = False # Display eye status (red text, separate lines) if left_eye_status != "N/A": cv2.putText(display_frame, f"L-Eye: {left_eye_status}", (x1, y1 - 30), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 1) if right_eye_status != "N/A": cv2.putText(display_frame, f"R-Eye: {right_eye_status}", (x1, y1 - 50), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 1) # Drowsiness analysis elapsed_time = current_time - start_time # 1. Detect yawns if last_yawn_time > 0: yawn_duration = current_time - last_yawn_time if yawn_duration >= YAWN_DURATION_THRESH and yawn_counter > 0: drowsy_warning = "SLEEPY!" drowsy_level = max(drowsy_level, 1) # 2. Detect consecutive yawns if yawn_counter >= CONSECUTIVE_YAWN_THRESH: drowsy_warning = "SLEEPY!" drowsy_level = 3 # Display drowsiness status drowsy_status = "" status_color = (0, 255, 0) # Green if drowsy_level == 1: drowsy_status = "Mild Drowsiness" status_color = (0, 255, 255) # Yellow elif drowsy_level == 2: drowsy_status = "Moderate Drowsiness" status_color = (0, 165, 255) # Orange elif drowsy_level >= 3: drowsy_status = "Severe Drowsiness!" status_color = (0, 0, 255) # Red # Display "SLEEPY!" warning in large red text if drowsy_warning: text_size = cv2.getTextSize(drowsy_warning, cv2.FONT_HERSHEY_SIMPLEX, 2.0, 5)[0] text_x = (display_frame.shape[1] - text_size[0]) // 2 text_y = (display_frame.shape[0] + text_size[1]) // 2 cv2.putText(display_frame, drowsy_warning, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (0, 0, 255), 5) # Create transparent overlay for status information overlay = display_frame.copy() cv2.rectangle(overlay, (10, 50), (250, 150), (0, 0, 0), -1) # Positioned below distance text alpha = 0.5 # Transparency factor cv2.addWeighted(overlay, alpha, display_frame, 1 - alpha, 0, display_frame) # Display status information with red text cv2.putText(display_frame, f"Status: {drowsy_status}", (15, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.6, status_color, 1) cv2.putText(display_frame, f"Blinks: {blink_total}", (15, 95), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1) cv2.putText(display_frame, f"Yawns: {yawn_counter}", (15, 120), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1) # Add reset timer info reset_in = STATE_RESET_TIME - (current_time - last_reset_time) cv2.putText(display_frame, f"Reset in: {max(0, reset_in):.0f}s", (15, 145), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1) # Display instructions (red text) cv2.putText(display_frame, "Press ESC to quit", (display_frame.shape[1] - 200, display_frame.shape[0] - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1) # Resize window to be larger display_width = int(display_frame.shape[1] * SCALE_FACTOR) display_height = int(display_frame.shape[0] * SCALE_FACTOR) resized_frame = cv2.resize(display_frame, (display_width, display_height)) # Show frame cv2.imshow("Drowsiness Detection System", resized_frame) # Reset warning drowsy_warning = "" # Exit on ESC key if cv2.waitKey(1) == 27: break # Release resources cap.release() cv2.destroyAllWindows()
06-25
import cv2 from cvzone.FaceMeshModule import FaceMeshDetector from cvzone.PlotModule import LivePlot # 初始化摄像头 cap = cv2.VideoCapture(0) # 使用默认摄像头 # 创建人脸网格检测器 detector = FaceMeshDetector(maxFaces=1) # 定义左眼和右眼的关键点 RIGHT_EYE = [33, 7, 163, 144, 145, 153, 154, 155, 133, 173, 157, 158, 159, 160, 161, 246] LEFT_EYE = [362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386, 385, 384, 398] # 初始化绘图器 plotY = LivePlot(640, 360, [20, 50], invert=True) # 初始化变量 BlinkCounter = 0 ratioList = [] counter = 0 color = (255, 0, 255) def get_aspect_ratio(points, landmarks): """计算眼睛的纵横比""" # 获取垂直方向上的关键点 vertical_1 = landmarks[points[1]] vertical_2 = landmarks[points[15]] # 获取水平方向上的关键点 horizontal_1 = landmarks[points[0]] horizontal_2 = landmarks[points[8]] # 计算垂直和水平距离 vertical_distance = abs(vertical_1.y - vertical_2.y) horizontal_distance = abs(horizontal_1.x - horizontal_2.x) # 避免除以零 if horizontal_distance == 0: return 0 # 返回纵横比 return (vertical_distance / horizontal_distance) * 100 while True: success, img = cap.read() if not success: break # 检测人脸网格 img, faces = detector.findFaceMesh(img, draw=False) if faces: face = faces[0] landmarks = face # 计算右眼和左眼的纵横比 right_eye_ratio = get_aspect_ratio(RIGHT_EYE, landmarks) left_eye_ratio = get_aspect_ratio(LEFT_EYE, landmarks) # 取平均值作为最终的眼睛纵横比 avg_ratio = (right_eye_ratio + left_eye_ratio) / 2 ratioList.append(avg_ratio) if len(ratioList) > 3: ratioList.pop(0) ratioAvg = sum(ratioList) / len(ratioList) # 眨眼计数逻辑 if ratioAvg < 35 and counter == 0: # 阈值可以根据实际情况调整 BlinkCounter += 1 color = (0, 200, 0) counter = 1 if counter != 0: counter += 1 if counter > 10: counter = 0 # 显示眨眼计数 cv2.putText(img, f'Blink Count: {BlinkCounter}', (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 2, color, 3) # 更新绘图 imgPlot = plotY.update(ratioAvg) imgStack = cv2.resize(img, (640, 360)) imgStack = cv2.hconcat([imgStack, imgPlot]) else: imgStack = cv2.resize(img, (640, 360)) imgStack = cv2.hconcat([imgStack, imgStack]) # 显示结果 cv2.imshow('Blink Detection', imgStack) # 按下 'q' 键退出循环 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放资源 cap.release() cv2.destroyAllWindows()这是代码F:\文档文件\作业\无人系统仿真课设\.venv\Scripts\python.exe F:\文档文件\作业\无人系统仿真课设\1.py Traceback (most recent call last): File "F:\文档文件\作业\无人系统仿真课设\1.py", line 8, in <module> detector = FaceMeshDetector(maxFaces=1) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\文档文件\作业\无人系统仿真课设\.venv\Lib\site-packages\cvzone\FaceMeshModule.py", line 32, in __init__ self.faceMesh = self.mpFaceMesh.FaceMesh(static_image_mode=self.staticMode, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\文档文件\作业\无人系统仿真课设\.venv\Lib\site-packages\mediapipe\python\solutions\face_mesh.py", line 95, in __init__ super().__init__( File "F:\文档文件\作业\无人系统仿真课设\.venv\Lib\site-packages\mediapipe\python\solution_base.py", line 234, in __init__ validated_graph.initialize( FileNotFoundError: The path does not exist: F:\文档文件\作业\无人系统仿真课设\.venv\Lib\site-packages\mediapipe/modules/face_landmark/face_landmark_front_cpu.binarypb 进程已结束,退出代码为 1 这是报错,更正代码
06-22
### 关于眼睑检测算法的实现 对于眼睑检测技术而言,一种常见的方法是利用机器学习模型来处理图像数据并识别眨眼动作。具体来说,在计算机视觉领域内,可以通过对面部特征点进行追踪进而判断眼睛的状态。 #### 基于面部标志点的方法 该方法依赖于预先训练好的人脸检测器以及 facial landmark predictor 来定位眼部区域的关键位置。一旦获取到这些坐标信息之后就可以计算两个眼角之间的距离变化情况从而推断出是否有眨眼行为发生[^1]。 ```python import dlib from skimage import io # 加载预训练的人脸检测器和landmark预测器 detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') def calculate_eye_aspect_ratio(eye_points): """ 计算眼睛纵横比 """ A = distance.euclidean(eye_points[1], eye_points[5]) B = distance.euclidean(eye_points[2], eye_points[4]) C = distance.euclidean(eye_points[0], eye_points[3]) ear = (A + B) / (2.0 * C) return ear image_path = "path_to_image" img = io.imread(image_path) faces = detector(img, 0) for face in faces: landmarks = [(p.x, p.y) for p in predictor(img, face).parts()] left_eye = landmarks[36:42] right_eye = landmarks[42:48] # 判断是否闭眼逻辑... ``` 此代码片段展示了如何使用 `dlib` 库加载一个人脸检测器和一个用于提取面部特征点(包括双眼轮廓)的预测器。通过比较特定时间间隔内的EAR值可以有效地监测眨眼事件的发生[^2]。 另外值得注意的是,除了上述提到的传统CV方式外,还可以考虑采用深度神经网络来进行更精确的眼睛状态分类任务。例如卷积神经网络(CNNs),其能够自动从大量样本中学习有用的表征特征而无需手动设计复杂的规则体系[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值