Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
#function part
|
| 7 |
+
|
| 8 |
+
def classify_image(image_path):
|
| 9 |
+
# Load the pre-trained image classification model
|
| 10 |
+
classifier = pipeline("image-classification", model="nateraw/vit-age-classifier")
|
| 11 |
+
image = Image.open(image_path)
|
| 12 |
+
predictions = classifier(image)
|
| 13 |
+
return predictions
|
| 14 |
+
|
| 15 |
+
st.set_page_config(page_title="Age Classifier", page_icon="📷")
|
| 16 |
+
st.header("Image Age Classification")
|
| 17 |
+
|
| 18 |
+
uploaded_file = st.file_uploader("Upload an Image...", type=["png", "jpg", "jpeg"])
|
| 19 |
+
|
| 20 |
+
if uploaded_file is not None:
|
| 21 |
+
image = Image.open(uploaded_file)
|
| 22 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 23 |
+
|
| 24 |
+
# Perform image classification
|
| 25 |
+
st.text('Classifying image...')
|
| 26 |
+
predictions = classify_image(uploaded_file)
|
| 27 |
+
|
| 28 |
+
# Display the top prediction
|
| 29 |
+
if predictions:
|
| 30 |
+
top_prediction = predictions[0]
|
| 31 |
+
st.write(f"**Predicted Age Group:** {top_prediction['label']}")
|
| 32 |
+
st.write(f"**Confidence:** {top_prediction['score']:.2f}")
|