|
|
|
import os |
|
from PIL import Image |
|
import pandas as pd |
|
from transformers import pipeline |
|
|
|
|
|
|
|
age_classifier = pipeline("image-classification", model="nateraw/vit-age-classifier") |
|
|
|
gender_classifier = pipeline("image-classification", model="rizvandwiki/gender-classification") |
|
|
|
emotion_classifier = pipeline("image-classification", model="Rajaram1996/Happiness-Classifier") |
|
|
|
|
|
image_folder = "images/" |
|
image_files = [f for f in os.listdir(image_folder) if f.endswith(('.jpg', '.png'))] |
|
|
|
|
|
results = [] |
|
|
|
|
|
for image_file in image_files: |
|
image_path = os.path.join(image_folder, image_file) |
|
image = Image.open(image_path) |
|
|
|
|
|
age_prediction = age_classifier(image) |
|
predicted_age = age_prediction[0]['label'] |
|
|
|
|
|
gender_prediction = gender_classifier(image) |
|
predicted_gender = gender_prediction[0]['label'] |
|
|
|
|
|
emotion_prediction = emotion_classifier(image) |
|
predicted_happiness = emotion_prediction[0]['label'] |
|
|
|
|
|
results.append({ |
|
"Image Name": image_file, |
|
"Predicted Age": predicted_age, |
|
"Predicted Gender": predicted_gender, |
|
"Predicted Happiness": predicted_happiness |
|
}) |
|
|
|
|
|
df = pd.DataFrame(results) |
|
df.to_csv("participant_classification_report.csv", index=False) |
|
|
|
print("分类完成,结果已保存到 participant_classification_report.csv") |
|
|