Spaces:
Sleeping
Sleeping
File size: 8,288 Bytes
4dc7e79 4e9052b 4dc7e79 4e9052b ba7de4b 4e9052b 4dc7e79 d9bc8e2 4fd31eb 4dc7e79 4e9052b 4dc7e79 4e9052b 4dc7e79 98de563 4dc7e79 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
import gradio as gr
import numpy as np
import torch
import torch.nn.functional as F
from PIL import Image
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import io
import base64
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
import plotly.express as px
import plotly.graph_objects as go
from datetime import datetime
import json
import os
import tempfile
import zipfile
import huggingface_hub
from huggingface_hub import hf_hub_download
# Import your PaveCLIP model (adjust import based on your model structure)
from paveclip_training import PaveCLIPEvaluator
# Download model from Hugging Face Hub if needed
def download_model():
"""Download model from Hugging Face Hub"""
try:
# Replace with your actual model repository
model_path = hf_hub_download(
repo_id="your-username/paveclip-model", # Update this
filename="paveclip_best.pt"
)
return model_path
except:
# Fallback to local path if available
return "./paveclip_best.pt"
def download_model_from_hf():
"""Download model from separate HF model repository"""
try:
print("π₯ Downloading PaveCLIP model...")
model_path = hf_hub_download(
repo_id="Blessing988/paveclip-model", # Your model repo
filename="paveclip_best.pt",
cache_dir="./models"
)
print("β
Model downloaded successfully!")
return model_path
except Exception as e:
print(f"β Download failed: {e}")
return None
class PavementAnalysisApp:
def __init__(self, model_path):
"""Initialize the Pavement Analysis App"""
model_path = download_model_from_hf()
if model_path:
self.evaluator = PaveCLIPEvaluator(model_path, {})
# Pavement-specific class definitions
self.distress_classes = [
"pavement with longitudinal crack",
"pavement with lateral crack",
"pavement with fatigue crack",
"pavement with pothole",
"road with patching",
"pavement image showing fatigue cracks",
"pavement image with network of cracks",
]
self.material_classes = [
"a rutting pavement image",
"snow on a pavement",
"asphalt road surface",
"wet asphalt surface",
"wet concrete surface",
"a severe wet concrete surface",
"concrete road surface",
"gravel road surface",
"dry and smooth asphalt surface",
"a severe dry concrete pavement surface",
]
self.condition_classes = [
"smooth road surface",
"slightly uneven road surface",
"severely damaged road surface",
"well-maintained pavement",
"deteriorated pavement"
]
# Store embeddings for comparison
self.image_embeddings = {}
self.text_embeddings = {}
def analyze_single_image(self, image, analysis_type="all"):
"""Analyze a single uploaded image"""
if image is None:
return "Please upload an image first.", {}, {}, {}
# Save temporary image
temp_path = "temp_image.jpg"
image.save(temp_path)
results = {}
try:
if analysis_type in ["distress", "all"]:
distress_result = self.evaluator.zero_shot_classification([temp_path], self.distress_classes)
results["distress"] = self._format_results(distress_result, self.distress_classes)
if analysis_type in ["material", "all"]:
material_result = self.evaluator.zero_shot_classification([temp_path], self.material_classes)
results["material"] = self._format_results(material_result, self.material_classes)
if analysis_type in ["condition", "all"]:
condition_result = self.evaluator.zero_shot_classification([temp_path], self.condition_classes)
results["condition"] = self._format_results(condition_result, self.condition_classes)
# Generate summary text
summary = self._generate_summary(results)
# Clean up
os.remove(temp_path)
return summary, results.get("distress", {}), results.get("material", {}), results.get("condition", {})
except Exception as e:
os.remove(temp_path) if os.path.exists(temp_path) else None
return f"Error analyzing image: {str(e)}", {}, {}, {}
# ... (Include all other methods from the main app class)
def _format_results(self, result, class_names):
"""Format classification results for display"""
predictions = result["predictions"]
similarities = result["similarities"]
formatted = {}
for i, class_name in enumerate(class_names):
confidence = float(similarities[0][i])
formatted[class_name] = confidence
return formatted
def _generate_summary(self, results):
"""Generate text summary of analysis"""
summary_parts = ["π **Pavement Analysis Results**\n"]
for category, result in results.items():
if result:
best_match = max(result.items(), key=lambda x: x[1])
category_name = category.capitalize()
summary_parts.append(f"**{category_name}:** {best_match[0]} (confidence: {best_match[1]:.3f})")
return "\n".join(summary_parts)
def create_demo():
"""Create the Gradio demo"""
# Download/load model
model_path = download_model()
app = PavementAnalysisApp(model_path)
# Create interface
with gr.Blocks(title="π£οΈ PaveCLIP: Advanced Pavement Analysis") as demo:
gr.Markdown("""
# π£οΈ PaveCLIP: Advanced Pavement Analysis Platform
**Professional pavement condition assessment using state-of-the-art computer vision**
Upload pavement images to get comprehensive analysis including distress detection,
material classification, and condition assessment.
""")
with gr.Tab("πΌοΈ Single Image Analysis"):
with gr.Row():
with gr.Column():
input_image = gr.Image(type="pil", label="Upload Pavement Image")
analysis_type = gr.Radio(
choices=["all", "distress", "material", "condition"],
value="all",
label="Analysis Type"
)
analyze_btn = gr.Button("π Analyze Image", variant="primary")
with gr.Column():
analysis_summary = gr.Markdown(label="Analysis Summary")
with gr.Row():
distress_output = gr.JSON(label="Distress Classification")
material_output = gr.JSON(label="Material Classification")
condition_output = gr.JSON(label="Condition Assessment")
analyze_btn.click(
fn=app.analyze_single_image,
inputs=[input_image, analysis_type],
outputs=[analysis_summary, distress_output, material_output, condition_output]
)
# Add examples
gr.Examples(
examples=[
["examples/longitudinal-image.jpg", "distress"],
["examples/202202122309381-dry-asphalt-severe.jpg", "condition"],
["examples/202205031731377-wet-concrete-severe.jpg", "condition"],
["examples/202202122342019-dry-concrete-slight.jpg", "condition"]
],
inputs=[input_image, analysis_type],
outputs=[analysis_summary, distress_output, material_output, condition_output],
fn=app.analyze_single_image,
cache_examples=True
)
return demo
if __name__ == "__main__":
demo = create_demo()
demo.launch() |