Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -79,10 +79,26 @@ os.makedirs(CAPTURED_FRAMES_DIR, exist_ok=True)
|
|
| 79 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 80 |
|
| 81 |
def initialize_media(media_file: Optional[Any] = None) -> str:
|
| 82 |
-
global media_loaded, is_video, static_image, log_entries
|
| 83 |
release_video()
|
| 84 |
static_image = None
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
if not os.path.exists(media_path):
|
| 88 |
status = f"Error: Media file '{media_path}' not found."
|
|
@@ -167,9 +183,14 @@ def generate_line_chart() -> Optional[str]:
|
|
| 167 |
ax.grid(True)
|
| 168 |
fig.tight_layout()
|
| 169 |
chart_path = "chart_temp.png"
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
def monitor_feed() -> Tuple[
|
| 175 |
Optional[np.ndarray],
|
|
@@ -205,7 +226,13 @@ def monitor_feed() -> Tuple[
|
|
| 205 |
if is_video:
|
| 206 |
frame = get_next_video_frame()
|
| 207 |
if frame is None:
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
else:
|
| 210 |
frame = static_image.copy()
|
| 211 |
except RuntimeError as e:
|
|
@@ -253,8 +280,13 @@ def monitor_feed() -> Tuple[
|
|
| 253 |
all_detected_items.extend(generic_dets)
|
| 254 |
|
| 255 |
# Apply shadow detection
|
| 256 |
-
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
# Apply thermal processing if frame is grayscale
|
| 260 |
if len(frame.shape) == 2:
|
|
@@ -288,7 +320,11 @@ def monitor_feed() -> Tuple[
|
|
| 288 |
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
| 289 |
|
| 290 |
# Save temporary image
|
| 291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
|
| 293 |
except Exception as e:
|
| 294 |
log_entries.append(f"Processing Error: {str(e)}")
|
|
@@ -307,7 +343,9 @@ def monitor_feed() -> Tuple[
|
|
| 307 |
if detection_types:
|
| 308 |
try:
|
| 309 |
captured_frame_path = os.path.join(CAPTURED_FRAMES_DIR, f"detected_{frame_count}.jpg")
|
| 310 |
-
cv2.imwrite(captured_frame_path, frame)
|
|
|
|
|
|
|
| 311 |
for item in all_detected_items:
|
| 312 |
dtype = item.get("type", "")
|
| 313 |
if dtype == "plant":
|
|
@@ -343,7 +381,9 @@ def monitor_feed() -> Tuple[
|
|
| 343 |
# Save processed frame
|
| 344 |
try:
|
| 345 |
frame_path = os.path.join(OUTPUT_DIR, f"frame_{frame_count:04d}.jpg")
|
| 346 |
-
cv2.imwrite(frame_path, frame)
|
|
|
|
|
|
|
| 347 |
except Exception as e:
|
| 348 |
log_entries.append(f"Error saving output frame: {str(e)}")
|
| 349 |
logging.error(f"Error saving output frame: {str(e)}")
|
|
@@ -361,10 +401,17 @@ def monitor_feed() -> Tuple[
|
|
| 361 |
missing_detected = len([item for item in all_detected_items if item.get("type") == "missing_patch"])
|
| 362 |
detected_counts.append(plant_detected + crack_detected + hole_detected + missing_detected)
|
| 363 |
|
| 364 |
-
# Log frame processing details
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
log_entries.append(log_message)
|
| 369 |
logging.info(log_message)
|
| 370 |
|
|
|
|
| 79 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 80 |
|
| 81 |
def initialize_media(media_file: Optional[Any] = None) -> str:
|
| 82 |
+
global media_loaded, is_video, static_image, log_entries, frame_count
|
| 83 |
release_video()
|
| 84 |
static_image = None
|
| 85 |
+
frame_count = 0 # Reset frame count on new media load
|
| 86 |
+
|
| 87 |
+
# Validate media_file
|
| 88 |
+
if media_file is None:
|
| 89 |
+
media_path = DEFAULT_VIDEO_PATH
|
| 90 |
+
elif not hasattr(media_file, 'name') or not media_file.name:
|
| 91 |
+
status = "Error: Invalid media file uploaded."
|
| 92 |
+
log_entries.append(status)
|
| 93 |
+
logging.error(status)
|
| 94 |
+
media_loaded = False
|
| 95 |
+
return status
|
| 96 |
+
else:
|
| 97 |
+
media_path = media_file.name
|
| 98 |
+
|
| 99 |
+
# Log the media path for debugging
|
| 100 |
+
log_entries.append(f"Attempting to load media from path: {media_path}")
|
| 101 |
+
logging.info(f"Attempting to load media from path: {media_path}")
|
| 102 |
|
| 103 |
if not os.path.exists(media_path):
|
| 104 |
status = f"Error: Media file '{media_path}' not found."
|
|
|
|
| 183 |
ax.grid(True)
|
| 184 |
fig.tight_layout()
|
| 185 |
chart_path = "chart_temp.png"
|
| 186 |
+
try:
|
| 187 |
+
fig.savefig(chart_path)
|
| 188 |
+
plt.close(fig)
|
| 189 |
+
return chart_path
|
| 190 |
+
except Exception as e:
|
| 191 |
+
log_entries.append(f"Error generating chart: {str(e)}")
|
| 192 |
+
logging.error(f"Error generating chart: {str(e)}")
|
| 193 |
+
return None
|
| 194 |
|
| 195 |
def monitor_feed() -> Tuple[
|
| 196 |
Optional[np.ndarray],
|
|
|
|
| 226 |
if is_video:
|
| 227 |
frame = get_next_video_frame()
|
| 228 |
if frame is None:
|
| 229 |
+
# Reset video if it ends
|
| 230 |
+
log_entries.append("Video ended, resetting to start.")
|
| 231 |
+
logging.info("Video ended, resetting to start.")
|
| 232 |
+
reset_video_index()
|
| 233 |
+
frame = get_next_video_frame()
|
| 234 |
+
if frame is None:
|
| 235 |
+
raise RuntimeError("Failed to retrieve frame after reset.")
|
| 236 |
else:
|
| 237 |
frame = static_image.copy()
|
| 238 |
except RuntimeError as e:
|
|
|
|
| 280 |
all_detected_items.extend(generic_dets)
|
| 281 |
|
| 282 |
# Apply shadow detection
|
| 283 |
+
try:
|
| 284 |
+
cv2.imwrite(TEMP_IMAGE_PATH, frame)
|
| 285 |
+
shadow_issue = detect_shadow_coverage(TEMP_IMAGE_PATH)
|
| 286 |
+
except Exception as e:
|
| 287 |
+
log_entries.append(f"Error saving temp image for shadow detection: {str(e)}")
|
| 288 |
+
logging.error(f"Error saving temp image: {str(e)}")
|
| 289 |
+
shadow_issue = False
|
| 290 |
|
| 291 |
# Apply thermal processing if frame is grayscale
|
| 292 |
if len(frame.shape) == 2:
|
|
|
|
| 320 |
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
| 321 |
|
| 322 |
# Save temporary image
|
| 323 |
+
try:
|
| 324 |
+
cv2.imwrite(TEMP_IMAGE_PATH, frame, [int(cv2.IMWRITE_JPEG_QUALITY), 95])
|
| 325 |
+
except Exception as e:
|
| 326 |
+
log_entries.append(f"Error saving temp image: {str(e)}")
|
| 327 |
+
logging.error(f"Error saving temp image: {str(e)}")
|
| 328 |
|
| 329 |
except Exception as e:
|
| 330 |
log_entries.append(f"Processing Error: {str(e)}")
|
|
|
|
| 343 |
if detection_types:
|
| 344 |
try:
|
| 345 |
captured_frame_path = os.path.join(CAPTURED_FRAMES_DIR, f"detected_{frame_count}.jpg")
|
| 346 |
+
success = cv2.imwrite(captured_frame_path, frame)
|
| 347 |
+
if not success:
|
| 348 |
+
raise RuntimeError(f"Failed to save captured frame: {captured_frame_path}")
|
| 349 |
for item in all_detected_items:
|
| 350 |
dtype = item.get("type", "")
|
| 351 |
if dtype == "plant":
|
|
|
|
| 381 |
# Save processed frame
|
| 382 |
try:
|
| 383 |
frame_path = os.path.join(OUTPUT_DIR, f"frame_{frame_count:04d}.jpg")
|
| 384 |
+
success = cv2.imwrite(frame_path, frame)
|
| 385 |
+
if not success:
|
| 386 |
+
raise RuntimeError(f"Failed to save output frame: {frame_path}")
|
| 387 |
except Exception as e:
|
| 388 |
log_entries.append(f"Error saving output frame: {str(e)}")
|
| 389 |
logging.error(f"Error saving output frame: {str(e)}")
|
|
|
|
| 401 |
missing_detected = len([item for item in all_detected_items if item.get("type") == "missing_patch"])
|
| 402 |
detected_counts.append(plant_detected + crack_detected + hole_detected + missing_detected)
|
| 403 |
|
| 404 |
+
# Log frame processing details in the requested format
|
| 405 |
+
detection_summary = {
|
| 406 |
+
"timestamp": last_timestamp,
|
| 407 |
+
"frame": frame_count,
|
| 408 |
+
"plants": plant_detected,
|
| 409 |
+
"cracks": crack_detected,
|
| 410 |
+
"holes": hole_detected,
|
| 411 |
+
"missing_patches": missing_detected,
|
| 412 |
+
"gps": gps_coord
|
| 413 |
+
}
|
| 414 |
+
log_message = json.dumps(detection_summary, indent=2)
|
| 415 |
log_entries.append(log_message)
|
| 416 |
logging.info(log_message)
|
| 417 |
|