Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import os
|
|
| 6 |
import json
|
| 7 |
import logging
|
| 8 |
import matplotlib.pyplot as plt
|
|
|
|
| 9 |
from datetime import datetime
|
| 10 |
from collections import Counter
|
| 11 |
from typing import List, Dict, Any, Optional
|
|
@@ -15,6 +16,14 @@ import piexif
|
|
| 15 |
import zipfile
|
| 16 |
from fpdf import FPDF # For PDF generation
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Set up logging
|
| 19 |
logging.basicConfig(
|
| 20 |
filename="app.log",
|
|
@@ -34,10 +43,12 @@ os.chmod(OUTPUT_DIR, 0o777)
|
|
| 34 |
os.chmod(FLIGHT_LOG_DIR, 0o777)
|
| 35 |
|
| 36 |
# Force CPU mode for Hugging Face Spaces
|
| 37 |
-
device = 'cpu'
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Global variables
|
| 43 |
log_entries: List[str] = []
|
|
@@ -50,6 +61,12 @@ SAVE_IMAGE_INTERVAL = 1
|
|
| 50 |
# Detection classes
|
| 51 |
DETECTION_CLASSES = ["Longitudinal", "Pothole", "Transverse"]
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
# Check image resolution quality
|
| 54 |
def check_image_quality(frame: np.ndarray, input_resolution: int) -> bool:
|
| 55 |
height, width, _ = frame.shape
|
|
@@ -374,3 +391,51 @@ def process_video(video, resize_width=4000, resize_height=3000, frame_skip=5):
|
|
| 374 |
logs_zip,
|
| 375 |
output_path # For video download
|
| 376 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import json
|
| 7 |
import logging
|
| 8 |
import matplotlib.pyplot as plt
|
| 9 |
+
import csv
|
| 10 |
from datetime import datetime
|
| 11 |
from collections import Counter
|
| 12 |
from typing import List, Dict, Any, Optional
|
|
|
|
| 16 |
import zipfile
|
| 17 |
from fpdf import FPDF # For PDF generation
|
| 18 |
|
| 19 |
+
# Set YOLO config directory (ensure settings are created only once)
|
| 20 |
+
settings_path = '/home/user/.config/Ultralytics/settings.json'
|
| 21 |
+
|
| 22 |
+
# Check if the settings file exists, if not, create it
|
| 23 |
+
if not os.path.exists(settings_path):
|
| 24 |
+
os.makedirs(os.path.dirname(settings_path), exist_ok=True)
|
| 25 |
+
logging.info(f"Creating new Ultralytics settings file at {settings_path}")
|
| 26 |
+
|
| 27 |
# Set up logging
|
| 28 |
logging.basicConfig(
|
| 29 |
filename="app.log",
|
|
|
|
| 43 |
os.chmod(FLIGHT_LOG_DIR, 0o777)
|
| 44 |
|
| 45 |
# Force CPU mode for Hugging Face Spaces
|
| 46 |
+
device = 'cpu' # Ensure we are using CPU
|
| 47 |
+
logging.info(f"Using device: {device}")
|
| 48 |
+
|
| 49 |
+
# Load the model with the device set to CPU
|
| 50 |
+
model = YOLO('./data/best.pt').to(device) # Ensure model is moved to CPU if necessary
|
| 51 |
+
model.float() # Ensure model is using full precision (on CPU)
|
| 52 |
|
| 53 |
# Global variables
|
| 54 |
log_entries: List[str] = []
|
|
|
|
| 61 |
# Detection classes
|
| 62 |
DETECTION_CLASSES = ["Longitudinal", "Pothole", "Transverse"]
|
| 63 |
|
| 64 |
+
# Debug: Check environment
|
| 65 |
+
print(f"Torch version: {torch.__version__}")
|
| 66 |
+
print(f"Gradio version: {gr.__version__}")
|
| 67 |
+
print(f"Ultralytics version: {ultralytics.__version__}")
|
| 68 |
+
print(f"CUDA available: {torch.cuda.is_available()}")
|
| 69 |
+
|
| 70 |
# Check image resolution quality
|
| 71 |
def check_image_quality(frame: np.ndarray, input_resolution: int) -> bool:
|
| 72 |
height, width, _ = frame.shape
|
|
|
|
| 391 |
logs_zip,
|
| 392 |
output_path # For video download
|
| 393 |
)
|
| 394 |
+
|
| 395 |
+
# Gradio interface
|
| 396 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange")) as iface:
|
| 397 |
+
gr.Markdown("# NHAI Road Defect Detection Dashboard")
|
| 398 |
+
with gr.Row():
|
| 399 |
+
with gr.Column(scale=3):
|
| 400 |
+
video_input = gr.Video(label="Upload Video (12MP recommended for NHAI compliance)")
|
| 401 |
+
width_slider = gr.Slider(320, 4000, value=4000, label="Output Width", step=1)
|
| 402 |
+
height_slider = gr.Slider(240, 3000, value=3000, label="Output Height", step=1)
|
| 403 |
+
skip_slider = gr.Slider(1, 10, value=5, label="Frame Skip", step=1)
|
| 404 |
+
process_btn = gr.Button("Process Video", variant="primary")
|
| 405 |
+
with gr.Column(scale=1):
|
| 406 |
+
metrics_output = gr.Textbox(label="Detection Metrics", lines=5, interactive=False)
|
| 407 |
+
with gr.Row():
|
| 408 |
+
video_output = gr.Video(label="Processed Video")
|
| 409 |
+
issue_gallery = gr.Gallery(label="Detected Issues", columns=4, height="auto", object_fit="contain")
|
| 410 |
+
with gr.Row():
|
| 411 |
+
chart_output = gr.Image(label="Detection Trend")
|
| 412 |
+
map_output = gr.Image(label="Issue Locations Map")
|
| 413 |
+
with gr.Row():
|
| 414 |
+
logs_output = gr.Textbox(label="Logs", lines=5, interactive=False)
|
| 415 |
+
with gr.Row():
|
| 416 |
+
gr.Markdown("## Download Results")
|
| 417 |
+
with gr.Row():
|
| 418 |
+
json_download = gr.File(label="Download Data Lake JSON")
|
| 419 |
+
images_zip_download = gr.File(label="Download Geotagged Images (ZIP)")
|
| 420 |
+
logs_zip_download = gr.File(label="Download Flight Logs (ZIP)")
|
| 421 |
+
video_download = gr.File(label="Download Processed Video")
|
| 422 |
+
|
| 423 |
+
process_btn.click(
|
| 424 |
+
fn=process_video,
|
| 425 |
+
inputs=[video_input, width_slider, height_slider, skip_slider],
|
| 426 |
+
outputs=[
|
| 427 |
+
video_output,
|
| 428 |
+
metrics_output,
|
| 429 |
+
logs_output,
|
| 430 |
+
issue_gallery,
|
| 431 |
+
chart_output,
|
| 432 |
+
map_output,
|
| 433 |
+
json_download,
|
| 434 |
+
images_zip_download,
|
| 435 |
+
logs_zip_download,
|
| 436 |
+
video_download
|
| 437 |
+
]
|
| 438 |
+
)
|
| 439 |
+
|
| 440 |
+
if __name__ == "__main__":
|
| 441 |
+
iface.launch()
|