Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,8 +9,7 @@ import matplotlib.pyplot as plt
|
|
| 9 |
import numpy as np
|
| 10 |
from sklearn.preprocessing import LabelEncoder
|
| 11 |
from huggingface_hub import hf_hub_download
|
| 12 |
-
import
|
| 13 |
-
from transformers import ViTImageProcessor, ViTForImageClassification
|
| 14 |
|
| 15 |
# Dataset loading function with caching
|
| 16 |
@st.cache_data
|
|
@@ -29,28 +28,14 @@ def load_image(image_file):
|
|
| 29 |
|
| 30 |
def classify_image(image):
|
| 31 |
try:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
logits = outputs.logits
|
| 41 |
-
probabilities = torch.softmax(logits, dim=-1)
|
| 42 |
-
|
| 43 |
-
top_predictions = torch.topk(probabilities, k=3)
|
| 44 |
-
|
| 45 |
-
predicted_classes = [
|
| 46 |
-
{
|
| 47 |
-
'label': model.config.id2label[idx.item()],
|
| 48 |
-
'probability': prob.item()
|
| 49 |
-
}
|
| 50 |
-
for idx, prob in zip(top_predictions.indices[0], top_predictions.values[0])
|
| 51 |
-
]
|
| 52 |
-
|
| 53 |
-
return predicted_classes
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
st.error(f"Classification error: {e}")
|
|
@@ -126,7 +111,7 @@ if camera_image is not None:
|
|
| 126 |
st.subheader("Car Classification Results:")
|
| 127 |
for classification in car_classifications:
|
| 128 |
st.write(f"Model: {classification['label']}")
|
| 129 |
-
st.write(f"Confidence: {classification['
|
| 130 |
|
| 131 |
# Use the top prediction for further processing
|
| 132 |
top_prediction = car_classifications[0]['label']
|
|
|
|
| 9 |
import numpy as np
|
| 10 |
from sklearn.preprocessing import LabelEncoder
|
| 11 |
from huggingface_hub import hf_hub_download
|
| 12 |
+
from transformers import pipeline
|
|
|
|
| 13 |
|
| 14 |
# Dataset loading function with caching
|
| 15 |
@st.cache_data
|
|
|
|
| 28 |
|
| 29 |
def classify_image(image):
|
| 30 |
try:
|
| 31 |
+
# Create a pipeline for image classification
|
| 32 |
+
classifier = pipeline('image-classification', model="dima806/car_models_image_detection", device=-1) # Use -1 for CPU, or 0 for GPU if available
|
| 33 |
|
| 34 |
+
# Classify the image
|
| 35 |
+
results = classifier(image)
|
| 36 |
|
| 37 |
+
# Return top 5 predictions
|
| 38 |
+
return results[:5]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
except Exception as e:
|
| 41 |
st.error(f"Classification error: {e}")
|
|
|
|
| 111 |
st.subheader("Car Classification Results:")
|
| 112 |
for classification in car_classifications:
|
| 113 |
st.write(f"Model: {classification['label']}")
|
| 114 |
+
st.write(f"Confidence: {classification['score']*100:.2f}%")
|
| 115 |
|
| 116 |
# Use the top prediction for further processing
|
| 117 |
top_prediction = car_classifications[0]['label']
|