xtlyxt commited on
Commit
a24e27f
·
verified ·
1 Parent(s): 8819427

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -29
app.py CHANGED
@@ -16,35 +16,46 @@ st.write(f"{x} squared is {x * x}")
16
  uploaded_images = st.file_uploader("Upload images", type=["jpg", "png"], accept_multiple_files=True)
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
-
24
- # Prediction button
25
- if st.button("Predict Emotions") and uploaded_images:
26
- # If only two images uploaded, display them side by side
27
- if len(uploaded_images) == 2:
28
- images_to_display = uploaded_images
29
- # If more than two images uploaded, allow user to select two images
30
- elif len(uploaded_images) > 2:
31
- selected_images = st.multiselect("Select two images to display side by side", uploaded_images)
32
- if len(selected_images) == 2:
33
- images_to_display = selected_images
34
- else:
35
- st.warning("Please select exactly two images.")
36
- st.stop()
37
- # Predict emotion for each selected image using the pipeline
38
- for img in images_to_display:
39
- image = Image.open(img)
40
- result = pipe(image)
41
- predicted_class = result[0]["label"]
42
- predicted_emotion = predicted_class.split("_")[-1].capitalize()
43
- st.image(image, caption=f"Predicted emotion: {predicted_emotion}", use_column_width=True)
44
- st.write(f"Emotion Scores: {predicted_emotion}: {result[0]['score']:.4f}")
45
- st.write(f"Original File Name: {img.name}") # Display original file name
46
- st.write("Keys and Values of results:")
47
- for res in result:
48
- label = res["label"]
49
- score = res["score"]
50
- st.write(f"{label}: {score:.4f}")
 
 
 
 
 
 
 
 
 
 
 
16
  uploaded_images = st.file_uploader("Upload images", type=["jpg", "png"], accept_multiple_files=True)
17
 
18
  # Display thumbnail images alongside file names and sizes in the sidebar
19
+ selected_images = []
20
  if uploaded_images:
21
  for img in uploaded_images:
22
  image = Image.open(img)
23
+ selected = st.sidebar.checkbox(f"{img.name} {img.size / 1024.0:.1f} KB", value=False, key=img.name)
24
+ if selected:
25
+ selected_images.append(image)
26
+
27
+ if st.button("Predict Emotions") and selected_images:
28
+ if len(selected_images) == 2:
29
+ # Predict emotion for each selected image using the pipeline
30
+ results = [pipe(image) for image in selected_images]
31
+
32
+ # Display images and predicted emotions side by side
33
+ col1, col2 = st.columns(2)
34
+ for i in range(2):
35
+ predicted_class = results[i][0]["label"]
36
+ predicted_emotion = predicted_class.split("_")[-1].capitalize()
37
+ col = col1 if i == 0 else col2
38
+ col.image(selected_images[i], caption=f"Predicted emotion: {predicted_emotion}", use_column_width=True)
39
+ col.write(f"Emotion Scores: {predicted_emotion}: {results[i][0]['score']:.4f}")
40
+ col.write(f"Original File Name: {uploaded_images[i].name}") # Display original file name
41
+
42
+ # Display the keys and values of all results
43
+ st.write("Keys and Values of all results:")
44
+ col1, col2 = st.columns(2)
45
+ for i, result in enumerate(results):
46
+ col = col1 if i == 0 else col2
47
+ col.write(f"Keys and Values of results[{i}]:")
48
+ for res in result:
49
+ label = res["label"]
50
+ score = res["score"]
51
+ col.write(f"{label}: {score:.4f}")
52
+
53
+ else:
54
+ # Predict emotion for each selected image using the pipeline
55
+ results = [pipe(image) for image in selected_images]
56
+
57
+ # Display images and predicted emotions
58
+ for i, result in enumerate(results):
59
+ predicted_class = result[0]["label"]
60
+ predicted_emotion = predicted_class.split("_")[-1].capitalize()
61
+ st.image(selected_images[i], caption=f"Predicted emotion: {predicted_emotion}", use_