Я использую следующий код для определения лица и рисования прямоугольника поверх лица, подобного этому.
Логический вывод.py В этом файле мы пытаемся нарисовать raw_bounding_box вокруг лица:
import cv2
import matplotlib.pyplot as plt
import numpy as np
from keras.preprocessing import image
def load_image(image_path, grayscale=False, target_size=None):
pil_image = image.load_face_coordinates(image_path, grayscale, target_size)
return image.face_coordinates_to_array(pil_image)
def load_detection_model(model_path):
detection_model = cv2.CascadeClassifier(model_path)
return detection_model
def detect_faces(detection_model, gray_image_array):
return detection_model.detectMultiScale(gray_image_array, 1.3, 5)
def draw_bounding_box(face_coordinates, image_array, color,r,d):
x1,y1,x2,y2 = face_coordinates
# cv2.rectangle(image_array, (x, y), (x + w, y + h), color, 2)
cv2.line(image_array, (x1 + r, y1), (x1 + r + d, y1), color, 2)
cv2.line(image_array, (x1, y1 + r), (x1, y1 + r + d), color, 2)
cv2.ellipse(image_array, (x1 + r, y1 + r), (r, r), 180, 0, 90, color, 2)
# Top right
cv2.line(image_array, (x2 - r, y1), (x2 - r - d, y1), color, 2)
cv2.line(image_array, (x2, y1 + r), (x2, y1 + r + d), color, 2)
cv2.ellipse(image_array, (x2 - r, y1 + r), (r, r), 270, 0, 90, color, 2)
# Bottom left
cv2.line(image_array, (x1 + r, y2), (x1 + r + d, y2), color, 2)
cv2.line(image_array, (x1, y2 - r), (x1, y2 - r - d), color, 2)
cv2.ellipse(image_array, (x1 + r, y2 - r), (r, r), 90, 0, 90, color, 2)
# Bottom right
cv2.line(image_array, (x2 - r, y2), (x2 - r - d, y2), color, 2)
cv2.line(image_array, (x2, y2 - r), (x2, y2 - r - d), color, 2)
cv2.ellipse(image_array, (x2 - r, y2 - r), (r, r), 0, 0, 90, color, 2)
image_array = np.zeros((256,256,3), dtype=np.uint8)
deteface.py В этом файле мы обнаруживаем лицо и вызываем функции из Inference.Пы, чтобы нарисовать коробки вокруг лица.
# starting video streaming
cv2.namedWindow('window_frame')
video_capture = cv2.VideoCapture(0)
while True:
bgr_image = video_capture.read()[1]
gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY)
rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
faces = detect_faces(face_detection, gray_image)
for face_coordinates in faces:
x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)
gray_face = gray_image[y1:y2, x1:x2]
try:
gray_face = cv2.resize(gray_face, (emotion_target_size))
except:
continue
gray_face = preprocess_input(gray_face, True)
gray_face = np.expand_dims(gray_face, 0)
gray_face = np.expand_dims(gray_face, -1)
emotion_prediction = emotion_classifier.predict(gray_face)
emotion_probability = np.max(emotion_prediction)
emotion_label_arg = np.argmax(emotion_prediction)
emotion_text = emotion_labels[emotion_label_arg]
emotion_window.append(emotion_text)
if len(emotion_window) > frame_window:
emotion_window.pop(0)
try:
emotion_mode = mode(emotion_window)
except:
continue
if emotion_text == 'angry':
color = emotion_probability * np.asarray((255, 0, 0))
elif emotion_text == 'sad':
color = emotion_probability * np.asarray((0, 0, 255))
elif emotion_text == 'happy':
color = emotion_probability * np.asarray((0, 128, 255))
elif emotion_text == 'surprise':
color = emotion_probability * np.asarray((0, 255, 255))
else:
color = emotion_probability * np.asarray((0, 255, 0))
color = color.astype(int)
color = color.tolist()
draw_bounding_box(face_coordinates, rgb_image, color)
Последняя строка кода в этом файле ( detectface.py ), похоже, не верна, поэтому я не знаю, как добавить два отсутствующих требуемых позиционных аргумента: 'r' и 'd' в этом файле.Пожалуйста, поделитесь, если у вас есть идеи для достижения этой цели