LCNada commited on
Commit
6ee7e4d
·
verified ·
1 Parent(s): b26a6be

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 导入所需库
2
+ import os
3
+ from PIL import Image
4
+ import pandas as pd
5
+ from transformers import pipeline
6
+
7
+ # 任务 2: 加载预训练模型
8
+ # 加载年龄分类模型
9
+ age_classifier = pipeline("image-classification", model="nateraw/vit-age-classifier")
10
+ # 加载性别分类模型
11
+ gender_classifier = pipeline("image-classification", model="rizvandwiki/gender-classification")
12
+ # 加载幸福度分类模型
13
+ emotion_classifier = pipeline("image-classification", model="Rajaram1996/Happiness-Classifier")
14
+
15
+ # 任务 1: 定义图像文件夹路径
16
+ image_folder = "images/"
17
+ image_files = [f for f in os.listdir(image_folder) if f.endswith(('.jpg', '.png'))]
18
+
19
+ # 初始化结果列表
20
+ results = []
21
+
22
+ # 任务 3: 处理每张图像并分类属性
23
+ for image_file in image_files:
24
+ image_path = os.path.join(image_folder, image_file)
25
+ image = Image.open(image_path)
26
+
27
+ # 预测年龄
28
+ age_prediction = age_classifier(image)
29
+ predicted_age = age_prediction[0]['label']
30
+
31
+ # 预测性别
32
+ gender_prediction = gender_classifier(image)
33
+ predicted_gender = gender_prediction[0]['label']
34
+
35
+ # 预测幸福度
36
+ emotion_prediction = emotion_classifier(image)
37
+ predicted_happiness = emotion_prediction[0]['label']
38
+
39
+ # 将结果添加到列表
40
+ results.append({
41
+ "Image Name": image_file,
42
+ "Predicted Age": predicted_age,
43
+ "Predicted Gender": predicted_gender,
44
+ "Predicted Happiness": predicted_happiness
45
+ })
46
+
47
+ # 任务 4: 使用 pandas 生成 CSV 报告
48
+ df = pd.DataFrame(results)
49
+ df.to_csv("participant_classification_report.csv", index=False)
50
+
51
+ print("分类完成,结果已保存到 participant_classification_report.csv")