Amina commited on
Commit
b27fe2a
·
1 Parent(s): 40a75ab

debugging

Browse files
Files changed (2) hide show
  1. app.py +21 -11
  2. front/index.html +11 -20
app.py CHANGED
@@ -2,19 +2,24 @@ import os
2
  from fastapi import FastAPI, File, UploadFile
3
  from PIL import Image
4
  import io
5
-
6
- # --- NEW CODE: Import YOLO directly ---
7
  from ultralytics import YOLO
8
 
9
- # Set the cache directory for PyTorch Hub to a writable location
10
- # (This line is still good practice, so we'll keep it)
 
11
  os.environ['TORCH_HOME'] = '/tmp/torch_cache'
12
 
13
- # Initialize the FastAPI app
14
  app = FastAPI(title="YOLOv8 Car Damage Detection API")
15
 
16
- # --- MODIFIED LINE: Load the model directly ---
17
- # This is the new, recommended way to load a local model
 
 
 
 
 
 
18
  model = YOLO('best.pt')
19
 
20
 
@@ -25,13 +30,10 @@ def read_root():
25
 
26
  @app.post("/detect")
27
  async def detect_damage(file: UploadFile = File(...)):
28
- # ... (the rest of your code remains the same)
29
  contents = await file.read()
30
  image = Image.open(io.BytesIO(contents)).convert("RGB")
31
  results = model(image)
32
 
33
- # --- IMPORTANT CHANGE FOR NEW METHOD ---
34
- # The output format is slightly different. We need to access the results differently.
35
  output = []
36
  for result in results:
37
  boxes = result.boxes
@@ -42,4 +44,12 @@ async def detect_damage(file: UploadFile = File(...)):
42
  if confidence > 0.5:
43
  output.append({'name': class_name, 'confidence': confidence})
44
 
45
- return {"detections": output}
 
 
 
 
 
 
 
 
 
2
  from fastapi import FastAPI, File, UploadFile
3
  from PIL import Image
4
  import io
5
+ from fastapi.middleware.cors import CORSMiddleware
 
6
  from ultralytics import YOLO
7
 
8
+ # --- DEBUGGING STEP 1 ---
9
+ print("Application script is starting...")
10
+
11
  os.environ['TORCH_HOME'] = '/tmp/torch_cache'
12
 
 
13
  app = FastAPI(title="YOLOv8 Car Damage Detection API")
14
 
15
+ app.add_middleware(
16
+ CORSMiddleware,
17
+ allow_origins=["*"],
18
+ allow_credentials=True,
19
+ allow_methods=["*"],
20
+ allow_headers=["*"],
21
+ )
22
+
23
  model = YOLO('best.pt')
24
 
25
 
 
30
 
31
  @app.post("/detect")
32
  async def detect_damage(file: UploadFile = File(...)):
 
33
  contents = await file.read()
34
  image = Image.open(io.BytesIO(contents)).convert("RGB")
35
  results = model(image)
36
 
 
 
37
  output = []
38
  for result in results:
39
  boxes = result.boxes
 
44
  if confidence > 0.5:
45
  output.append({'name': class_name, 'confidence': confidence})
46
 
47
+ return {"detections": output}
48
+
49
+
50
+ # --- DEBUGGING STEP 2 ---
51
+ # This will print all routes that FastAPI has successfully registered.
52
+ print("\n--- Registered Routes ---")
53
+ for route in app.routes:
54
+ print(f"Path: {route.path}, Methods: {', '.join(route.methods)}")
55
+ print("-------------------------\n")
front/index.html CHANGED
@@ -11,17 +11,13 @@
11
  #image-preview { display: none; max-width: 100%; height: auto; margin-top: 20px; border-radius: 4px; border: 1px solid #ddd; }
12
  .upload-area { border: 2px dashed #007bff; border-radius: 8px; padding: 40px; margin: 20px 0; cursor: pointer; background-color: #f8f9fa; }
13
  .upload-area p { margin: 0; color: #555; }
14
- .button-group { display: flex; justify-content: center; gap: 10px; }
15
  button { font-size: 1.1em; padding: 12px 25px; cursor: pointer; border: none; border-radius: 5px; transition: background-color 0.3s; }
16
  #check-button { background-color: #007bff; color: white; }
17
  #check-button:hover { background-color: #0056b3; }
18
  #check-button:disabled { background-color: #aaa; cursor: not-allowed; }
19
-
20
- /* --- NEW CSS FOR DELETE BUTTON --- */
21
- #delete-button { background-color: #dc3545; color: white; display: none; /* Hidden by default */ }
22
  #delete-button:hover { background-color: #c82333; }
23
- /* --- END OF NEW CSS --- */
24
-
25
  #results { margin-top: 30px; font-size: 1.1em; text-align: left; }
26
  #results ul { list-style-type: none; padding: 0; }
27
  #results li { background-color: #e9ecef; padding: 10px; margin-bottom: 8px; border-radius: 4px; }
@@ -43,7 +39,7 @@
43
  <div class="button-group">
44
  <button id="check-button" disabled>Check for Damage</button>
45
  <button id="delete-button">Delete</button>
46
- </div>
47
 
48
  <img id="image-preview" src="" alt="Your uploaded image">
49
 
@@ -51,19 +47,15 @@
51
  </div>
52
 
53
  <script>
54
- // ⚠️ IMPORTANT: Make sure this URL is correct
55
- const API_URL = "https://your-name-your-space.hf.space/detect";
56
 
57
  const uploadArea = document.getElementById('upload-area');
58
  const imageUpload = document.getElementById('image-upload');
59
  const checkButton = document.getElementById('check-button');
60
  const imagePreview = document.getElementById('image-preview');
61
  const resultsDiv = document.getElementById('results');
62
-
63
- // --- NEW JAVASCRIPT FOR DELETE BUTTON ---
64
  const deleteButton = document.getElementById('delete-button');
65
- // --- END OF NEW JAVASCRIPT ---
66
-
67
  let selectedFile = null;
68
 
69
  uploadArea.addEventListener('click', () => imageUpload.click());
@@ -75,23 +67,22 @@
75
  imagePreview.src = URL.createObjectURL(selectedFile);
76
  imagePreview.style.display = 'block';
77
  checkButton.disabled = false;
 
78
  deleteButton.style.display = 'inline-block'; // Show delete button
 
79
  resultsDiv.innerHTML = '';
80
  }
81
  }
82
 
83
- // --- NEW JAVASCRIPT LOGIC FOR DELETE BUTTON ---
84
  deleteButton.addEventListener('click', () => {
85
- // Reset everything to the initial state
86
  selectedFile = null;
87
- imageUpload.value = ''; // Clear the file input
88
  imagePreview.src = '';
89
  imagePreview.style.display = 'none';
90
  resultsDiv.innerHTML = '';
91
  checkButton.disabled = true;
92
- deleteButton.style.display = 'none'; // Hide delete button again
93
  });
94
- // --- END OF NEW JAVASCRIPT ---
95
 
96
  checkButton.addEventListener('click', async () => {
97
  if (!selectedFile) {
@@ -101,7 +92,7 @@
101
 
102
  resultsDiv.innerHTML = "<p>Analyzing, please wait... 🧐</p>";
103
  checkButton.disabled = true;
104
- deleteButton.disabled = true; // Also disable delete button during analysis
105
 
106
  const formData = new FormData();
107
  formData.append('file', selectedFile);
@@ -124,7 +115,7 @@
124
  console.error("Error:", error);
125
  } finally {
126
  checkButton.disabled = false;
127
- deleteButton.disabled = false; // Re-enable delete button after analysis
128
  }
129
  });
130
 
 
11
  #image-preview { display: none; max-width: 100%; height: auto; margin-top: 20px; border-radius: 4px; border: 1px solid #ddd; }
12
  .upload-area { border: 2px dashed #007bff; border-radius: 8px; padding: 40px; margin: 20px 0; cursor: pointer; background-color: #f8f9fa; }
13
  .upload-area p { margin: 0; color: #555; }
14
+ .button-group { display: flex; justify-content: center; gap: 10px; margin-top: 15px; }
15
  button { font-size: 1.1em; padding: 12px 25px; cursor: pointer; border: none; border-radius: 5px; transition: background-color 0.3s; }
16
  #check-button { background-color: #007bff; color: white; }
17
  #check-button:hover { background-color: #0056b3; }
18
  #check-button:disabled { background-color: #aaa; cursor: not-allowed; }
19
+ #delete-button { background-color: #dc3545; color: white; display: none; }
 
 
20
  #delete-button:hover { background-color: #c82333; }
 
 
21
  #results { margin-top: 30px; font-size: 1.1em; text-align: left; }
22
  #results ul { list-style-type: none; padding: 0; }
23
  #results li { background-color: #e9ecef; padding: 10px; margin-bottom: 8px; border-radius: 4px; }
 
39
  <div class="button-group">
40
  <button id="check-button" disabled>Check for Damage</button>
41
  <button id="delete-button">Delete</button>
42
+ </div>
43
 
44
  <img id="image-preview" src="" alt="Your uploaded image">
45
 
 
47
  </div>
48
 
49
  <script>
50
+ // ⚠️ IMPORTANT: REPLACE THIS URL with your actual Hugging Face Space URL!
51
+ const API_URL = "https://amn23-car-image-classification.hf.space/detect";
52
 
53
  const uploadArea = document.getElementById('upload-area');
54
  const imageUpload = document.getElementById('image-upload');
55
  const checkButton = document.getElementById('check-button');
56
  const imagePreview = document.getElementById('image-preview');
57
  const resultsDiv = document.getElementById('results');
 
 
58
  const deleteButton = document.getElementById('delete-button');
 
 
59
  let selectedFile = null;
60
 
61
  uploadArea.addEventListener('click', () => imageUpload.click());
 
67
  imagePreview.src = URL.createObjectURL(selectedFile);
68
  imagePreview.style.display = 'block';
69
  checkButton.disabled = false;
70
+ // --- THIS IS THE FIXED LINE ---
71
  deleteButton.style.display = 'inline-block'; // Show delete button
72
+ // --- END OF FIX ---
73
  resultsDiv.innerHTML = '';
74
  }
75
  }
76
 
 
77
  deleteButton.addEventListener('click', () => {
 
78
  selectedFile = null;
79
+ imageUpload.value = '';
80
  imagePreview.src = '';
81
  imagePreview.style.display = 'none';
82
  resultsDiv.innerHTML = '';
83
  checkButton.disabled = true;
84
+ deleteButton.style.display = 'none';
85
  });
 
86
 
87
  checkButton.addEventListener('click', async () => {
88
  if (!selectedFile) {
 
92
 
93
  resultsDiv.innerHTML = "<p>Analyzing, please wait... 🧐</p>";
94
  checkButton.disabled = true;
95
+ deleteButton.disabled = true;
96
 
97
  const formData = new FormData();
98
  formData.append('file', selectedFile);
 
115
  console.error("Error:", error);
116
  } finally {
117
  checkButton.disabled = false;
118
+ deleteButton.disabled = false;
119
  }
120
  });
121