Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,159 +1,54 @@
|
|
1 |
-
|
2 |
-
from PIL import Image
|
3 |
-
from transformers import pipeline
|
4 |
-
import pandas as pd
|
5 |
-
import matplotlib.pyplot as plt
|
6 |
-
import seaborn as sns
|
7 |
-
import xlsxwriter
|
8 |
-
import io
|
9 |
-
|
10 |
-
# Initialize session state for results, image names, and image sizes if not already present
|
11 |
-
if 'results' not in st.session_state:
|
12 |
-
st.session_state['results'] = []
|
13 |
-
if 'image_names' not in st.session_state:
|
14 |
-
st.session_state['image_names'] = []
|
15 |
-
if 'image_sizes' not in st.session_state:
|
16 |
-
st.session_state['image_sizes'] = []
|
17 |
-
|
18 |
-
# Disable PyplotGlobalUseWarning
|
19 |
-
st.set_option('deprecation.showPyplotGlobalUse', False)
|
20 |
-
|
21 |
-
# Create an image classification pipeline with scores
|
22 |
-
pipe = pipeline("image-classification", model="trpakov/vit-face-expression", top_k=None)
|
23 |
-
|
24 |
-
# Streamlit app
|
25 |
-
st.title("Emotion Recognition with vit-face-expression")
|
26 |
-
|
27 |
-
# Upload images
|
28 |
-
uploaded_images = st.file_uploader("Upload images", type=["jpg", "png"], accept_multiple_files=True)
|
29 |
-
|
30 |
-
# Display thumbnail images alongside file names and sizes in the sidebar
|
31 |
-
selected_images = []
|
32 |
-
if uploaded_images:
|
33 |
-
# Reset the image names and sizes lists each time new images are uploaded
|
34 |
-
st.session_state['image_names'] = [img.name for img in uploaded_images]
|
35 |
-
st.session_state['image_sizes'] = [round(img.size / 1024.0, 1) for img in uploaded_images]
|
36 |
-
|
37 |
-
# Add a "Select All" checkbox in the sidebar
|
38 |
-
select_all = st.sidebar.checkbox("Select All", False)
|
39 |
-
|
40 |
-
for idx, img in enumerate(uploaded_images):
|
41 |
-
image = Image.open(img)
|
42 |
-
checkbox_key = f"{img.name}_checkbox_{idx}" # Unique key for each checkbox
|
43 |
-
# Display thumbnail image and checkbox in sidebar
|
44 |
-
st.sidebar.image(image, caption=f"{img.name} {img.size / 1024.0:.1f} KB", width=40)
|
45 |
-
selected = st.sidebar.checkbox(f"Select {img.name}", value=select_all, key=checkbox_key)
|
46 |
-
|
47 |
-
if selected:
|
48 |
-
selected_images.append(image)
|
49 |
-
|
50 |
-
if st.button("Predict Emotions") and selected_images:
|
51 |
-
# Predict emotion for each selected image using the pipeline
|
52 |
-
st.session_state['results'] = [pipe(image) for image in selected_images]
|
53 |
-
|
54 |
-
# Initialize an empty DataFrame outside of the button press condition
|
55 |
-
df_emotions = pd.DataFrame()
|
56 |
|
57 |
# Generate DataFrame from results
|
58 |
if st.button("Generate HeatMap & DataFrame"):
|
59 |
-
#
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
df_emotions.drop(columns=['Image View']).to_csv('emotion_scores.csv', index=False)
|
110 |
-
st.success('DataFrame generated and saved as emotion_scores.csv')
|
111 |
-
|
112 |
-
with open('emotion_scores.csv', 'r') as f:
|
113 |
-
csv_file = f.read()
|
114 |
-
|
115 |
-
st.download_button(
|
116 |
-
label='Download Emotion Scores as CSV',
|
117 |
-
data=csv_file,
|
118 |
-
file_name='emotion_scores.csv',
|
119 |
-
mime='text/csv',
|
120 |
-
)
|
121 |
-
|
122 |
-
# Create a BytesIO buffer for the Excel file
|
123 |
-
output = io.BytesIO()
|
124 |
-
|
125 |
-
# Create a new Excel writer object using the buffer as the file
|
126 |
-
writer = pd.ExcelWriter(output, engine='xlsxwriter')
|
127 |
-
df_emotions.to_excel(writer, index=False, header=True)
|
128 |
-
|
129 |
-
# Access the xlsxwriter workbook and worksheet objects
|
130 |
-
workbook = writer.book
|
131 |
-
worksheet = writer.sheets['Sheet1']
|
132 |
-
|
133 |
-
# Set the column width and row height
|
134 |
-
worksheet.set_column('A:I', 20) # Set width for columns A-I
|
135 |
-
worksheet.set_column('J:J', 30) # Set width for column J (Image View)
|
136 |
-
for row_num in range(len(df_emotions) + 1): # +1 to include the header row
|
137 |
-
worksheet.set_row(row_num, 38) # Set the row height to 38
|
138 |
-
|
139 |
-
# Iterate over the images and insert them into the 'Image View' column
|
140 |
-
for idx, image in enumerate(selected_images):
|
141 |
-
# Convert the image to a format that can be inserted into Excel
|
142 |
-
image_stream = io.BytesIO()
|
143 |
-
image.save(image_stream, format='PNG')
|
144 |
-
image_stream.seek(0)
|
145 |
-
worksheet.insert_image(f'J{idx + 2}', 'image.png', {'image_data': image_stream})
|
146 |
-
|
147 |
-
# Close the writer object
|
148 |
-
writer.close()
|
149 |
-
|
150 |
-
# Rewind the buffer
|
151 |
-
output.seek(0)
|
152 |
-
|
153 |
-
# Use Streamlit's download button to offer the Excel file for download
|
154 |
-
st.download_button(
|
155 |
-
label='Download Emotion Scores as Excel',
|
156 |
-
data=output,
|
157 |
-
file_name='emotion_scores.xlsx',
|
158 |
-
mime='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
159 |
-
)
|
|
|
1 |
+
# ... [rest of your code] ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
# Generate DataFrame from results
|
4 |
if st.button("Generate HeatMap & DataFrame"):
|
5 |
+
# ... [rest of your code for generating df_emotions] ...
|
6 |
+
|
7 |
+
# Create a BytesIO buffer for the Excel file
|
8 |
+
output = io.BytesIO()
|
9 |
+
|
10 |
+
# Create a new Excel writer object using the buffer as the file
|
11 |
+
writer = pd.ExcelWriter(output, engine='xlsxwriter')
|
12 |
+
df_emotions.to_excel(writer, index=False, header=True)
|
13 |
+
|
14 |
+
# Access the xlsxwriter workbook and worksheet objects
|
15 |
+
workbook = writer.book
|
16 |
+
worksheet = writer.sheets['Sheet1']
|
17 |
+
|
18 |
+
# Set the column width and row height
|
19 |
+
worksheet.set_column('A:G', 8) # Set width for columns A-G
|
20 |
+
worksheet.set_column('H:H', 22) # Set width for column H (Image Name)
|
21 |
+
worksheet.set_column('I:I', 14) # Set width for column I (Image Size)
|
22 |
+
worksheet.set_column('J:J', 12) # Set width for column J (Image View)
|
23 |
+
worksheet.set_column('K:K', 12) # Set width for column K (Image Num)
|
24 |
+
for row_num in range(len(df_emotions) + 1): # +1 to include the header row
|
25 |
+
worksheet.set_row(row_num, 38) # Set the row height to 38
|
26 |
+
|
27 |
+
# Add a new column for the images and image numbers
|
28 |
+
df_emotions['Image View'] = '' # This will create a placeholder column for the images
|
29 |
+
df_emotions['Image Num'] = range(len(df_emotions)) # This will create a sequence of numbers
|
30 |
+
|
31 |
+
# Write the DataFrame to the Excel writer object
|
32 |
+
df_emotions.to_excel(writer, sheet_name='Sheet1', startrow=0, startcol=0, index=False, header=True)
|
33 |
+
|
34 |
+
# Iterate over the images and insert them into the 'Image View' column
|
35 |
+
for idx, image in enumerate(selected_images):
|
36 |
+
# Convert the image to a format that can be inserted into Excel
|
37 |
+
image_stream = io.BytesIO()
|
38 |
+
image.save(image_stream, format='PNG')
|
39 |
+
image_stream.seek(0)
|
40 |
+
worksheet.insert_image(f'J{idx + 2}', 'image.png', {'image_data': image_stream})
|
41 |
+
|
42 |
+
# Close the writer object
|
43 |
+
writer.close()
|
44 |
+
|
45 |
+
# Rewind the buffer
|
46 |
+
output.seek(0)
|
47 |
+
|
48 |
+
# Use Streamlit's download button to offer the Excel file for download
|
49 |
+
st.download_button(
|
50 |
+
label='Download Emotion Scores as Excel',
|
51 |
+
data=output,
|
52 |
+
file_name='emotion_scores.xlsx',
|
53 |
+
mime='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
54 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|