mandali8686 commited on
Commit
35f8363
1 Parent(s): 16d9687

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -29
app.py CHANGED
@@ -13,39 +13,32 @@ def load_model():
13
  model = load_model()
14
 
15
  # Function to apply transforms to the image (update as per your model's requirement)
16
- def get_top_predictions(prediction, class_indices, top_k=5):
17
- probabilities = torch.nn.functional.softmax(prediction, dim=1)
18
- top_probs, top_catids = torch.topk(probabilities, top_k)
19
- top_classes = [class_indices[str(catid)] for catid in top_catids[0].tolist()]
20
- top_probs = top_probs[0].tolist()
21
- return top_classes, top_probs
 
22
 
23
- # Function to display the prediction results
24
- def display_prediction(top_classes, top_probs):
25
- for i, (cls, prob) in enumerate(zip(top_classes, top_probs)):
26
- st.write(f"{i+1}: {cls} - {prob:.2%}")
27
 
28
- # Load the class indices (you will need to have a class_indices.json file)
29
- class_indices = load_class_indices()
30
 
31
- # Streamlit interface
32
- st.title("Petn Emotion Recognition")
33
- st.write("Upload a pet facial image.")
34
 
35
  # File uploader
36
- uploaded_file = st.file_uploader("", type=["jpg", "png", "jpeg"])
37
  if uploaded_file is not None:
38
- # Display the uploaded image
39
  image = Image.open(uploaded_file).convert('RGB')
40
- st.image(image, use_column_width=True)
41
-
42
- # Classify button
43
- if st.button('Classify Image'):
44
- st.write("Classifying...")
45
- # Transform the image and make prediction
46
- input_tensor = transform_image(image)
47
- with torch.no_grad():
48
- prediction = model(input_tensor)
49
- # Get and display top predictions
50
- top_classes, top_probs = get_top_predictions(prediction, class_indices)
51
- display_prediction(top_classes, top_probs)
 
 
13
  model = load_model()
14
 
15
  # Function to apply transforms to the image (update as per your model's requirement)
16
+ def transform_image(image):
17
+ transform = transforms.Compose([
18
+ transforms.Resize((224, 224)), # Resize to the input size that your model expects
19
+ transforms.ToTensor(),
20
+ # Add other transformations as needed
21
+ ])
22
+ return transform(image).unsqueeze(0) # Add batch dimension
23
 
24
+ st.title("Animal Facial Expression Recognition")
 
 
 
25
 
 
 
26
 
 
 
 
27
 
28
  # File uploader
29
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
30
  if uploaded_file is not None:
 
31
  image = Image.open(uploaded_file).convert('RGB')
32
+ st.image(image, caption='Uploaded Image.', use_column_width=True)
33
+ st.write("")
34
+ st.write("Classifying...")
35
+
36
+ # Transform the image
37
+ input_tensor = transform_image(image)
38
+
39
+ # Make prediction
40
+ with torch.no_grad():
41
+ prediction = model(input_tensor)
42
+
43
+ # Display the prediction (modify as per your output)
44
+ st.write('Predicted class:', prediction.argmax().item())