xtlyxt commited on
Commit
fb60ae4
·
verified ·
1 Parent(s): 768af5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +159 -51
app.py CHANGED
@@ -1,54 +1,162 @@
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
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
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
+ # Access the results, image names, and sizes from the session state
60
+ results = st.session_state['results']
61
+ image_names = st.session_state['image_names']
62
+ image_sizes = st.session_state['image_sizes']
63
+ if results:
64
+ # Initialize an empty list to store all the data
65
+ data = []
66
+
67
+ # Iterate over the results and populate the list with dictionaries
68
+ for i, result_set in enumerate(results):
69
+ # Initialize a dictionary for the current set with zeros
70
+ current_data = {
71
+ 'Happy': 0,
72
+ 'Surprise': 0,
73
+ 'Neutral': 0,
74
+ 'Sad': 0,
75
+ 'Disgust': 0,
76
+ 'Angry': 0,
77
+ 'Fear': 0,
78
+ 'Image Name': image_names[i],
79
+ 'Image Size (KB)': f"{image_sizes[i]:.1f}" # Format the size to one decimal place
80
+ }
81
+
82
+ for result in result_set:
83
+ # Capitalize the label and update the score in the current set
84
+ emotion = result['label'].capitalize()
85
+ score = round(result['score'], 4) # Round the score to 4 decimal places
86
+ current_data[emotion] = score
87
+
88
+ # Append the current data to the data list
89
+ data.append(current_data)
90
+
91
+ # Convert the list of dictionaries into a pandas DataFrame
92
+ df_emotions = pd.DataFrame(data)
93
+
94
+ # Add a placeholder for the 'Image View' column
95
+ df_emotions['Image View'] = [''] * len(df_emotions)
96
+ # Add a sequence of numbers for the 'Image Num' column
97
+ df_emotions['Image Num'] = list(range(len(df_emotions)))
98
+
99
+ # Display the DataFrame
100
+ st.write(df_emotions)
101
+
102
+ # Plotting the heatmap for the first seven columns
103
+ plt.figure(figsize=(10, 10))
104
+ sns.heatmap(df_emotions.iloc[:, :7], annot=True, fmt=".1f", cmap='viridis')
105
+ plt.title('Heatmap of Emotion Scores')
106
+ plt.xlabel('Emotion Categories')
107
+ plt.ylabel('Data Points')
108
+ st.pyplot(plt)
109
+
110
+ # Save the DataFrame to a CSV file without the 'Image View' and 'Image Num' columns
111
+ df_emotions.drop(columns=['Image View', 'Image Num']).to_csv('emotion_scores.csv', index=False)
112
+ st.success('DataFrame generated and saved as emotion_scores.csv')
113
+
114
+ with open('emotion_scores.csv', 'r') as f:
115
+ csv_file = f.read()
116
+
117
+ st.download_button(
118
+ label='Download Emotion Scores as CSV',
119
+ data=csv_file,
120
+ file_name='emotion_scores.csv',
121
+ mime='text/csv',
122
+ )
123
+
124
+ # Create a BytesIO buffer for the Excel file
125
+ output = io.BytesIO()
126
+
127
+ # Create a new Excel writer object using the buffer as the file
128
+ writer = pd.ExcelWriter(output, engine='xlsxwriter')
129
+ df_emotions.to_excel(writer, index=False, header=True)
130
+
131
+ # Access the xlsxwriter workbook and worksheet objects
132
+ workbook = writer.book
133
+ worksheet = writer.sheets['Sheet1']
134
+
135
+ # Set the column width and row height
136
+ worksheet.set_column('A:I', 20) # Set width for columns A-I
137
+ worksheet.set_column('J:J', 30) # Set width for column J (Image View)
138
+ worksheet.set_column('K:K', 15) # Set width for column K (Image Num)
139
+ for row_num in range(len(df_emotions) + 1): # +1 to include the header row
140
+ worksheet.set_row(row_num, 38) # Set the row height to 38
141
+
142
+ # Iterate over the images and insert them into the 'Image View' column
143
+ for idx, image in enumerate(selected_images):
144
+ # Convert the image to a format that can be inserted into Excel
145
+ image_stream = io.BytesIO()
146
+ image.save(image_stream, format='PNG')
147
+ image_stream.seek(0)
148
+ worksheet.insert_image(f'J{idx + 2}', 'image.png', {'image_data': image_stream})
149
+
150
+ # Close the writer object
151
+ writer.close()
152
+
153
+ # Rewind the buffer
154
+ output.seek(0)
155
+
156
+ # Use Streamlit's download button to offer the Excel file for download
157
+ st.download_button(
158
+ label='Download Emotion Scores as Excel',
159
+ data=output,
160
+ file_name='emotion_scores.xlsx',
161
+ mime='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
162
+ )