tjy/demo/core/face_detection.py

99 lines
3.0 KiB
Python
Raw Normal View History

2024-07-22 18:18:12 +08:00
from ultralytics import YOLO
2024-07-16 16:28:35 +08:00
import cv2
2024-07-22 18:18:12 +08:00
import torch
2024-07-16 16:28:35 +08:00
class FaceDetector:
2024-07-22 18:18:12 +08:00
def __init__(self, model_path='../weights/yolov10s_face.pt'):
try:
self.model = YOLO(model_path)
except FileNotFoundError:
print("ERROR: Could not load the YOLO model")
exit()
self.class_names_dict = self.model.model.names
def find_faces(self, img):
original_img = img.copy()
2024-07-16 16:28:35 +08:00
2024-07-22 18:18:12 +08:00
results = self.model(img, verbose=False)[0]
detections = results.boxes.data
2024-07-16 16:28:35 +08:00
2024-07-22 18:18:12 +08:00
face_detections = []
other_detections = []
2024-07-16 16:28:35 +08:00
bboxs = []
2024-07-22 18:18:12 +08:00
for detection in detections:
x1, y1, x2, y2, confidence, class_id = detection
x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])
class_id = int(class_id)
# Convert to xywh format
w = x2 - x1
h = y2 - y1
bboxs.append([class_id, x1, y1, w, h, confidence])
if self.class_names_dict[class_id] == 'face':
face_detections.append((x1, y1, x2, y2, confidence))
else:
other_detections.append((x1, y1, x2, y2, class_id))
bboxs = []
# Find the largest face
if face_detections:
largest_face = max(face_detections, key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))
bboxs.append(largest_face)
original_img = self.draw_face(original_img, largest_face)
# Modify pixel values for other facial features
for x1, y1, x2, y2, _ in other_detections:
img[y1:y2, x1:x2] = 125
return original_img, img, bboxs
def draw_face(self, img, face, l=30, t=2, rt=1):
x1, y1, x2, y2, confidence = face
# Draw rectangle
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 255), rt)
# Draw label
# label = f"Face {confidence:.2f}"
# cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), 2)
# Draw corners
# Top left
cv2.line(img, (x1, y1), (x1 + l, y1), (255, 0, 255), t)
cv2.line(img, (x1, y1), (x1, y1 + l), (255, 0, 255), t)
# Top right
cv2.line(img, (x2, y1), (x2 - l, y1), (255, 0, 255), t)
cv2.line(img, (x2, y1), (x2, y1 + l), (255, 0, 255), t)
# Bottom left
cv2.line(img, (x1, y2), (x1 + l, y2), (255, 0, 255), t)
cv2.line(img, (x1, y2), (x1, y2 - l), (255, 0, 255), t)
# Bottom right
cv2.line(img, (x2, y2), (x2 - l, y2), (255, 0, 255), t)
cv2.line(img, (x2, y2), (x2, y2 - l), (255, 0, 255), t)
2024-07-16 16:28:35 +08:00
return img
2024-07-22 18:18:12 +08:00
2024-07-16 16:28:35 +08:00
# 使用示例
if __name__ == "__main__":
2024-07-22 18:18:12 +08:00
print(torch.cuda.is_available())
2024-07-16 16:28:35 +08:00
detector = FaceDetector()
cap = cv2.VideoCapture(0) # 使用默认摄像头
while True:
2024-07-22 18:18:12 +08:00
success, frame = cap.read()
iii, img, bboxs = detector.find_faces(frame)
2024-07-16 16:28:35 +08:00
2024-07-22 18:18:12 +08:00
cv2.imshow("Image", iii)
2024-07-16 16:28:35 +08:00
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
2024-07-22 18:18:12 +08:00
cv2.destroyAllWindows()