У меня есть фотографии в формате людей. Я попробовал: < /p>
exiftool -orientation= -n -if ‘$Orientation ne “1”’ -auto-orient -overwrite_original *.jpg
< /code>
с менее чем удовлетворительными результатами. Я изменил сценарий Python: < /p>
import cv2
import os
# Load OpenCV face detector (Haar cascade)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# Rotation map for OpenCV
rotation_methods = {
0: lambda img: img,
90: lambda img: cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE),
180: lambda img: cv2.rotate(img, cv2.ROTATE_180),
270: lambda img: cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
}
def detect_faces(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
return len(faces)
def auto_rotate_image(file_path):
original_img = cv2.imread(file_path)
if original_img is None:
print(f"Could not read {file_path}")
return
best_angle = 0
max_faces = 0
for angle in [0, 90, 180, 270]:
rotated = rotation_methods[angle](original_img)
face_count = detect_faces(rotated)
if face_count > max_faces:
best_angle = angle
max_faces = face_count
if best_angle != 0:
rotated_img = rotation_methods[best_angle](original_img)
cv2.imwrite(file_path, rotated_img)
print(f"✓ Rotated {file_path} by {best_angle}° to align face(s)")
else:
print(f"× No face detected in {file_path}, skipped")
# Process all JPGs in current folder
for filename in os.listdir('.'):
if filename.lower().endswith('.jpg'):
auto_rotate_image(filename)
< /code>
Это работает, но результаты плохие. Можно ли использовать обнаружение лица MediaPipe? Вы знаете о лучшем решении, которое можно реализовать в Linux?
Подробнее здесь: https://stackoverflow.com/questions/797 ... rientation