Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# Load the age classification pipeline
|
6 |
+
age_classifier = pipeline("image-classification", model="nateraw/vit-age-classifier")
|
7 |
+
|
8 |
+
def classify_age(image):
|
9 |
+
"""Classify the age of a person in the given image."""
|
10 |
+
results = age_classifier(image)
|
11 |
+
|
12 |
+
# Sort results by score in descending order
|
13 |
+
results = sorted(results, key=lambda x: x['score'], reverse=True)
|
14 |
+
|
15 |
+
return results
|
16 |
+
|
17 |
+
# Streamlit UI
|
18 |
+
st.title("Age Classification using ViT")
|
19 |
+
|
20 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
21 |
+
|
22 |
+
if uploaded_file is not None:
|
23 |
+
image = Image.open(uploaded_file).convert("RGB")
|
24 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
25 |
+
|
26 |
+
# Classify age
|
27 |
+
age_predictions = classify_age(image)
|
28 |
+
|
29 |
+
# Display results
|
30 |
+
st.subheader("Predicted Age Range:")
|
31 |
+
st.write(f"Age range: {age_predictions[0]['label']}")
|