xtlyxt commited on
Commit
d7dd53f
·
verified ·
1 Parent(s): b800ba6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -2
app.py CHANGED
@@ -17,10 +17,88 @@ uploaded_images = st.file_uploader("Upload images", type=["jpg", "png"], accept_
17
 
18
  # Display thumbnail images alongside file names and sizes in the sidebar
19
  if uploaded_images:
 
20
  for img in uploaded_images:
21
  image = Image.open(img)
22
- st.sidebar.image(image, caption=f"{img.name} {img.size / 1024.0:.1f} KB", width=40)
23
- st.sidebar.checkbox(f"Select {img.name}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  # Collect selected images based on checkbox input
26
  selected_images = []
 
17
 
18
  # Display thumbnail images alongside file names and sizes in the sidebar
19
  if uploaded_images:
20
+ uploaded_images_dict = {}
21
  for img in uploaded_images:
22
  image = Image.open(img)
23
+ # Check if checkbox for the current image has already been created
24
+ if img.name not in uploaded_images_dict:
25
+ st.sidebar.image(image, caption=f"{img.name} {img.size / 1024.0:.1f} KB", width=40)
26
+ uploaded_images_dict[img.name] = True
27
+ st.sidebar.checkbox(f"Select {img.name}", key=img.name)
28
+
29
+ # Collect selected images based on checkbox input
30
+ selected_images = []
31
+ for img in uploaded_images:
32
+ selected = st.sidebar.checkbox(f"Select {img.name}", value=False, key=img.name)
33
+ if selected:
34
+ selected_images.append(Image.open(img))
35
+
36
+ if st.button("Predict Emotions") and selected_images:
37
+ if len(selected_images) == 2:
38
+ # Predict emotion for each selected image using the pipeline
39
+ results = [pipe(image) for image in selected_images]
40
+
41
+ # Display images and predicted emotions side by side
42
+ col1, col2 = st.columns(2)
43
+ for i in range(2):
44
+ predicted_class = results[i][0]["label"]
45
+ predicted_emotion = predicted_class.split("_")[-1].capitalize()
46
+ col = col1 if i == 0 else col2
47
+ col.image(selected_images[i], caption=f"Predicted emotion: {predicted_emotion}", use_column_width=True)
48
+ col.write(f"Emotion Scores: {predicted_emotion}: {results[i][0]['score']:.4f}")
49
+ col.write(f"Original File Name: {uploaded_images[i].name}") # Display original file name
50
+
51
+ # Display the keys and values of all results
52
+ st.write("Keys and Values of all results:")
53
+ col1, col2 = st.columns(2)
54
+ for i, result in enumerate(results):
55
+ col = col1 if i == 0 else col2
56
+ col.write(f"Keys and Values of results[{i}]:")
57
+ for res in result:
58
+ label = res["label"]
59
+ score = res["score"]
60
+ col.write(f"{label}: {score:.4f}")
61
+
62
+ else:
63
+ # Predict emotion for each selected image using the pipeline
64
+ results = [pipe(image) for image in selected_images]
65
+
66
+ # Display images and predicted emotions
67
+ for i, result in enumerate(results):
68
+ predicted_class = result[0]["label"]
69
+ predicted_emotion = predicted_class.split("_")[-1].capitalize()
70
+ st.image(selected_images[i], caption=f"Predicted emotion: {predicted_emotion}", use_column_width=True)
71
+ st.write(f"Emotion Scores for #{i+1} Image")
72
+ st.write(f"{predicted_emotion}: {result[0]['score']:.4f}")
73
+ st.write(f"Original File Name: {uploaded_images[i].name}") # Display original file name
74
+
75
+ import streamlit as st
76
+ from PIL import Image
77
+ from transformers import pipeline
78
+
79
+ # Create an image classification pipeline with scores
80
+ pipe = pipeline("image-classification", model="trpakov/vit-face-expression", top_k=None)
81
+
82
+ # Streamlit app
83
+ st.title("Emotion Recognition with vit-face-expression")
84
+
85
+ # Slider example
86
+ x = st.slider('Select a value')
87
+ st.write(f"{x} squared is {x * x}")
88
+
89
+ # Upload images
90
+ uploaded_images = st.file_uploader("Upload images", type=["jpg", "png"], accept_multiple_files=True)
91
+
92
+ # Display thumbnail images alongside file names and sizes in the sidebar
93
+ if uploaded_images:
94
+ uploaded_images_dict = {}
95
+ for img in uploaded_images:
96
+ image = Image.open(img)
97
+ # Check if checkbox for the current image has already been created
98
+ if img.name not in uploaded_images_dict:
99
+ st.sidebar.image(image, caption=f"{img.name} {img.size / 1024.0:.1f} KB", width=40)
100
+ uploaded_images_dict[img.name] = True
101
+ st.sidebar.checkbox(f"Select {img.name}", key=img.name)
102
 
103
  # Collect selected images based on checkbox input
104
  selected_images = []