Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import streamlit as st
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
pipe_yolos = pipeline("object-detection", model="hustvl/yolos-tiny")
|
10 |
+
pipe_emotions = pipeline("image-classification", model="dima806/facial_emotions_image_detection")
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
st.title("Online Teaching Effect Monitor")
|
15 |
+
|
16 |
+
file_name = st.file_uploader("Upload a image or a video")
|
17 |
+
|
18 |
+
if file_name is not None:
|
19 |
+
image = Image.open(file_name)
|
20 |
+
output = pipe_yolos(face_image)
|
21 |
+
|
22 |
+
data = output
|
23 |
+
# 过滤出所有标签为 "person" 的项
|
24 |
+
persons = [item for item in data if item['label'] == 'person']
|
25 |
+
|
26 |
+
# 打印结果
|
27 |
+
print(persons)
|
28 |
+
|
29 |
+
# 假设有一张原始图片,加载图片并截取出每个 "person" 的部分
|
30 |
+
# original_image_path = 'input_image.jpg' # 替换为实际图片路径
|
31 |
+
# original_image = Image.open(original_image_path)
|
32 |
+
original_image = Image.open(face_image)
|
33 |
+
persons_image_list = []
|
34 |
+
|
35 |
+
# 截取每个 "person" 的部分并保存
|
36 |
+
for idx, person in enumerate(persons):
|
37 |
+
box = person['box']
|
38 |
+
cropped_image = original_image.crop((box['xmin'], box['ymin'], box['xmax'], box['ymax']))
|
39 |
+
cropped_image.save(f'person_{idx}.jpg')
|
40 |
+
cropped_image.show()
|
41 |
+
persons_image_list.append(cropped_image)
|
42 |
+
|
43 |
+
|
44 |
+
# 创建一个新的画布来拼接所有的person图像
|
45 |
+
fig, axes = plt.subplots(1, len(persons), figsize=(15, 5))
|
46 |
+
|
47 |
+
# 截取每个 "person" 的部分并绘制到一张图上
|
48 |
+
for idx, person in enumerate(persons):
|
49 |
+
box = person['box']
|
50 |
+
cropped_image = original_image.crop((box['xmin'], box['ymin'], box['xmax'], box['ymax']))
|
51 |
+
axes[idx].imshow(cropped_image)
|
52 |
+
axes[idx].axis('off')
|
53 |
+
axes[idx].set_title(f'Person {idx}')
|
54 |
+
|
55 |
+
# 显示拼接的图像
|
56 |
+
plt.tight_layout()
|
57 |
+
plt.show()
|
58 |
+
|
59 |
+
# 识别每个人的表情
|
60 |
+
output_list = []
|
61 |
+
|
62 |
+
for idx, face in enumerate(persons_image_list):
|
63 |
+
print(f"processing {idx}")
|
64 |
+
output = pipe_emotions(face)
|
65 |
+
output_list.append(output[0])
|
66 |
+
|
67 |
+
print(output_list)
|
68 |
+
|
69 |
+
|
70 |
+
# 统计各种标签的数量
|
71 |
+
label_counts = {}
|
72 |
+
for item in output_list:
|
73 |
+
label = item['label']
|
74 |
+
if label in label_counts:
|
75 |
+
label_counts[label] += 1
|
76 |
+
else:
|
77 |
+
label_counts[label] = 1
|
78 |
+
|
79 |
+
# 绘制饼状图
|
80 |
+
labels = list(label_counts.keys())
|
81 |
+
sizes = list(label_counts.values())
|
82 |
+
|
83 |
+
plt.figure(figsize=(8, 8))
|
84 |
+
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140)
|
85 |
+
plt.title('Distribution of Labels')
|
86 |
+
plt.axis('equal') # 确保饼状图为圆形
|
87 |
+
plt.show()
|