xtlyxt commited on
Commit
b2639c4
·
verified ·
1 Parent(s): fb8f465

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -35
app.py CHANGED
@@ -1,28 +1,9 @@
1
  import streamlit as st
2
- import torch
3
-
4
- #streamlit clean
5
- #streamlit run app.py
6
-
7
- #pip install --upgrade pip
8
- #curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
9
-
10
-
11
  from PIL import Image
12
- #from transformers import ViTForImageClassification, ViTImageProcessor
13
-
14
 
15
- from transformers import AutoImageProcessor, AutoModelForImageClassification
16
-
17
- # Load the model
18
- model_name = "trpakov/vit-face-expression"
19
- image_processor = AutoImageProcessor.from_pretrained(model_name)
20
- model = AutoModelForImageClassification.from_pretrained(model_name)
21
-
22
- # Load the model
23
- model_name = "trpakov/vit-face-expression"
24
- #model = ViTForImageClassification.from_pretrained(model_name)
25
- #image_processor = ViTImageProcessor.from_pretrained(model_name)
26
 
27
  # Streamlit app
28
  st.title("Emotion Recognition with vit-face-expression")
@@ -36,21 +17,15 @@ uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png"])
36
 
37
  if uploaded_image:
38
  image = Image.open(uploaded_image)
39
- inputs = image_processor(images=image, return_tensors="pt")
40
- pixel_values = inputs.pixel_values
41
-
42
- # Predict emotion
43
- with torch.no_grad():
44
- outputs = model(pixel_values)
45
- predicted_class = torch.argmax(outputs.logits, dim=1).item()
46
-
47
- emotion_labels = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]
48
- predicted_emotion = emotion_labels[predicted_class]
49
 
50
  st.image(image, caption=f"Predicted emotion: {predicted_emotion}", use_column_width=True)
51
 
52
 
53
  # Display scores for each category
54
- st.write("Emotion Scores:")
55
- for label, score in zip(emotion_labels, outputs.logits[0]):
56
- st.write(f"{label}: {score:.4f}")
 
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
2
  from PIL import Image
3
+ from transformers import pipeline
 
4
 
5
+ # Create an image classification pipeline
6
+ pipe = pipeline("image-classification", model="trpakov/vit-face-expression")
 
 
 
 
 
 
 
 
 
7
 
8
  # Streamlit app
9
  st.title("Emotion Recognition with vit-face-expression")
 
17
 
18
  if uploaded_image:
19
  image = Image.open(uploaded_image)
20
+ # Predict emotion using the pipeline
21
+ results = pipe(image)
22
+ predicted_class = results[0]["label"]
23
+ predicted_emotion = predicted_class.split("_")[-1].capitalize()
 
 
 
 
 
 
24
 
25
  st.image(image, caption=f"Predicted emotion: {predicted_emotion}", use_column_width=True)
26
 
27
 
28
  # Display scores for each category
29
+ #st.write("Emotion Scores:")
30
+ #for label, score in zip(emotion_labels, outputs.logits[0]):
31
+ # st.write(f"{label}: {score:.4f}")