Spaces:
Sleeping
Sleeping
Commit
·
d9c4377
1
Parent(s):
8ec1acd
car identigier
Browse files
app.py
CHANGED
|
@@ -1,25 +1,163 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
if image is None:
|
| 9 |
-
return "Please upload an image"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load a model trained on car brands/makes
|
| 5 |
+
# Using a model from the Hugging Face hub that's good at vehicle classification
|
| 6 |
+
car_classifier = pipeline("image-classification",
|
| 7 |
+
model="dima806/car_brand_classification")
|
| 8 |
|
| 9 |
+
# Alternative models you can try:
|
| 10 |
+
# - "nickmuchi/vit-finetuned-cars-biased"
|
| 11 |
+
# - "saltacc/anime-ai-detect"
|
| 12 |
+
# - Or use a general object detection + custom logic
|
| 13 |
+
|
| 14 |
+
def identify_car(image):
|
| 15 |
+
if image is None:
|
| 16 |
+
return "Please upload an image of a car"
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
# Convert to RGB if necessary
|
| 20 |
+
if image.mode != 'RGB':
|
| 21 |
+
image = image.convert('RGB')
|
| 22 |
+
|
| 23 |
+
# Get predictions
|
| 24 |
+
predictions = car_classifier(image)
|
| 25 |
+
|
| 26 |
+
# Format results
|
| 27 |
+
result_text = "🚗 Car Identification Results:\n\n"
|
| 28 |
+
|
| 29 |
+
top_5 = predictions[:5] # Get top 5 predictions
|
| 30 |
+
for i, pred in enumerate(top_5, 1):
|
| 31 |
+
label = pred['label'].replace('_', ' ').title()
|
| 32 |
+
confidence = pred['score']
|
| 33 |
+
result_text += f"{i}. {label}: {confidence:.2%}\n"
|
| 34 |
+
|
| 35 |
+
# Try to extract additional information
|
| 36 |
+
result_text += f"\n🔍 Most likely: **{top_5[0]['label'].replace('_', ' ').title()}** " \
|
| 37 |
+
f"(confidence: {top_5[0]['score']:.2%})"
|
| 38 |
+
|
| 39 |
+
return result_text
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return f"Error processing image: {str(e)}"
|
| 43 |
+
|
| 44 |
+
def enhanced_car_analysis(image):
|
| 45 |
+
"""Enhanced version with more detailed analysis"""
|
| 46 |
if image is None:
|
| 47 |
+
return "Please upload an image of a car"
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
# Get basic predictions
|
| 51 |
+
predictions = car_classifier(image)
|
| 52 |
+
|
| 53 |
+
# Create detailed analysis
|
| 54 |
+
analysis = "🚗 **Detailed Car Analysis**\n\n"
|
| 55 |
+
|
| 56 |
+
# Top prediction
|
| 57 |
+
top_pred = predictions[0]
|
| 58 |
+
brand = top_pred['label'].replace('_', ' ').title()
|
| 59 |
+
confidence = top_pred['score']
|
| 60 |
+
|
| 61 |
+
analysis += f"**Primary Identification:**\n"
|
| 62 |
+
analysis += f"• Brand: {brand}\n"
|
| 63 |
+
analysis += f"• Confidence: {confidence:.2%}\n\n"
|
| 64 |
+
|
| 65 |
+
# Top 3 alternatives
|
| 66 |
+
analysis += "**Alternative Possibilities:**\n"
|
| 67 |
+
for i, pred in enumerate(predictions[1:4], 2):
|
| 68 |
+
alt_brand = pred['label'].replace('_', ' ').title()
|
| 69 |
+
alt_conf = pred['score']
|
| 70 |
+
analysis += f"{i}. {alt_brand} ({alt_conf:.2%})\n"
|
| 71 |
+
|
| 72 |
+
# Confidence level interpretation
|
| 73 |
+
if confidence > 0.8:
|
| 74 |
+
analysis += f"\n✅ **High confidence** - Very likely a {brand}"
|
| 75 |
+
elif confidence > 0.6:
|
| 76 |
+
analysis += f"\n⚠️ **Moderate confidence** - Probably a {brand}"
|
| 77 |
+
else:
|
| 78 |
+
analysis += f"\n❓ **Low confidence** - Could be a {brand} or similar brand"
|
| 79 |
+
|
| 80 |
+
# Tips for better identification
|
| 81 |
+
analysis += "\n\n**💡 Tips for better identification:**"
|
| 82 |
+
analysis += "\n• Use clear, front/side views of the car"
|
| 83 |
+
analysis += "\n• Ensure good lighting and focus"
|
| 84 |
+
analysis += "\n• Avoid images with multiple cars"
|
| 85 |
+
analysis += "\n• Crop close to the car for better accuracy"
|
| 86 |
+
|
| 87 |
+
return analysis
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
return f"Error processing image: {str(e)}"
|
| 91 |
+
|
| 92 |
+
# Create a more advanced interface with tabs
|
| 93 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 94 |
+
gr.Markdown("# 🚗 Car Brand & Model Identifier")
|
| 95 |
+
gr.Markdown("Upload a photo of a car to identify its brand and model!")
|
| 96 |
+
|
| 97 |
+
with gr.Tab("Quick Identification"):
|
| 98 |
+
gr.Markdown("### Fast car brand identification")
|
| 99 |
+
with gr.Row():
|
| 100 |
+
with gr.Column():
|
| 101 |
+
image_input = gr.Image(label="Upload Car Photo", type="pil")
|
| 102 |
+
quick_btn = gr.Button("Identify Car", variant="primary")
|
| 103 |
+
with gr.Column():
|
| 104 |
+
quick_output = gr.Textbox(label="Identification Results", lines=8)
|
| 105 |
|
| 106 |
+
with gr.Tab("Detailed Analysis"):
|
| 107 |
+
gr.Markdown("### Comprehensive car analysis")
|
| 108 |
+
with gr.Row():
|
| 109 |
+
with gr.Column():
|
| 110 |
+
detail_input = gr.Image(label="Upload Car Photo", type="pil")
|
| 111 |
+
detail_btn = gr.Button("Analyze Car", variant="primary")
|
| 112 |
+
with gr.Column():
|
| 113 |
+
detail_output = gr.Textbox(label="Detailed Analysis", lines=15)
|
| 114 |
+
|
| 115 |
+
with gr.Tab("About"):
|
| 116 |
+
gr.Markdown("""
|
| 117 |
+
## About This Car Identifier
|
| 118 |
+
|
| 119 |
+
**How it works:**
|
| 120 |
+
- Uses a deep learning model trained on car images
|
| 121 |
+
- Identifies car brands/makes from photographs
|
| 122 |
+
- Provides confidence scores for each prediction
|
| 123 |
+
|
| 124 |
+
**Best practices:**
|
| 125 |
+
- Use clear, well-lit photos
|
| 126 |
+
- Front or side views work best
|
| 127 |
+
- Avoid blurry or distant shots
|
| 128 |
+
- Single car images yield better results
|
| 129 |
+
|
| 130 |
+
**Supported brands include:** Toyota, Honda, Ford, BMW, Mercedes, Audi,
|
| 131 |
+
Volkswagen, Nissan, Chevrolet, Hyundai, and many more!
|
| 132 |
+
""")
|
| 133 |
+
|
| 134 |
+
# Examples section
|
| 135 |
+
gr.Markdown("### Example Car Images")
|
| 136 |
+
gr.Examples(
|
| 137 |
+
examples=[
|
| 138 |
+
["https://cdn.pixabay.com/photo/2015/01/19/13/51/car-604019_1280.jpg"], # Audi
|
| 139 |
+
["https://cdn.pixabay.com/photo/2012/11/02/13/02/car-63930_1280.jpg"], # Ford Mustang
|
| 140 |
+
["https://cdn.pixabay.com/photo/2015/09/02/12/25/bmw-918408_1280.jpg"], # BMW
|
| 141 |
+
["https://cdn.pixabay.com/photo/2014/09/07/22/34/car-tire-438467_1280.jpg"] # Mercedes
|
| 142 |
+
],
|
| 143 |
+
inputs=image_input,
|
| 144 |
+
outputs=quick_output,
|
| 145 |
+
fn=identify_car,
|
| 146 |
+
cache_examples=True
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
# Connect buttons
|
| 150 |
+
quick_btn.click(identify_car, inputs=image_input, outputs=quick_output)
|
| 151 |
+
detail_btn.click(enhanced_car_analysis, inputs=detail_input, outputs=detail_output)
|
| 152 |
+
|
| 153 |
+
# For better performance on Hugging Face Spaces
|
| 154 |
+
def load_model():
|
| 155 |
+
# This will cache the model loading
|
| 156 |
+
return pipeline("image-classification",
|
| 157 |
+
model="dima806/car_brand_classification")
|
| 158 |
+
|
| 159 |
+
# Pre-load the model when the app starts
|
| 160 |
+
car_classifier = load_model()
|
| 161 |
+
|
| 162 |
+
if __name__ == "__main__":
|
| 163 |
+
demo.launch(share=True)
|