Spaces:
Sleeping
Sleeping
Update services/shadow_detection.py
Browse files- services/shadow_detection.py +10 -42
services/shadow_detection.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
-
import
|
| 2 |
-
import numpy as np
|
| 3 |
import logging
|
| 4 |
|
| 5 |
# Setup logging
|
|
@@ -9,50 +8,19 @@ logging.basicConfig(
|
|
| 9 |
format="%(asctime)s - %(levelname)s - %(message)s"
|
| 10 |
)
|
| 11 |
|
| 12 |
-
def
|
| 13 |
"""
|
| 14 |
-
Detect
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
Returns:
|
| 18 |
-
|
| 19 |
"""
|
| 20 |
try:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
_, shadow_mask = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY_INV)
|
| 26 |
-
contours, _ = cv2.findContours(shadow_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 27 |
-
|
| 28 |
-
detections = []
|
| 29 |
-
line_counter = 1 # Initialize counter for numbered labels
|
| 30 |
-
|
| 31 |
-
for contour in contours:
|
| 32 |
-
area = cv2.contourArea(contour)
|
| 33 |
-
if area < 500: # Filter small areas
|
| 34 |
-
continue
|
| 35 |
-
x, y, w, h = cv2.boundingRect(contour)
|
| 36 |
-
x_min, y_min, x_max, y_max = x, y, x + w, y + h
|
| 37 |
-
|
| 38 |
-
# Add numbered label
|
| 39 |
-
detection_label = f"Line {line_counter} - Shadow"
|
| 40 |
-
detections.append({
|
| 41 |
-
"type": "shadow",
|
| 42 |
-
"label": detection_label,
|
| 43 |
-
"coordinates": [x_min, y_min, x_max, y_max]
|
| 44 |
-
})
|
| 45 |
-
|
| 46 |
-
# Draw bounding box and label
|
| 47 |
-
color = (128, 128, 128) # Gray for shadows
|
| 48 |
-
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
|
| 49 |
-
cv2.putText(frame, detection_label, (x_min, y_min - 10),
|
| 50 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
| 51 |
-
|
| 52 |
-
line_counter += 1
|
| 53 |
-
|
| 54 |
-
logging.info(f"Detected {len(detections)} shadows in frame.")
|
| 55 |
-
return {"detections": detections, "frame": frame}
|
| 56 |
except Exception as e:
|
| 57 |
logging.error(f"Error detecting shadows: {str(e)}")
|
| 58 |
-
return
|
|
|
|
| 1 |
+
import random
|
|
|
|
| 2 |
import logging
|
| 3 |
|
| 4 |
# Setup logging
|
|
|
|
| 8 |
format="%(asctime)s - %(levelname)s - %(message)s"
|
| 9 |
)
|
| 10 |
|
| 11 |
+
def detect_shadow_coverage(image_path: str) -> bool:
|
| 12 |
"""
|
| 13 |
+
Detect shadow coverage in an image (simulated).
|
| 14 |
Args:
|
| 15 |
+
image_path: Path to the image
|
| 16 |
Returns:
|
| 17 |
+
bool: True if significant shadow coverage is detected
|
| 18 |
"""
|
| 19 |
try:
|
| 20 |
+
shadow_percent = random.randint(25, 40)
|
| 21 |
+
result = shadow_percent > 30
|
| 22 |
+
logging.info(f"Shadow detection: {shadow_percent}% coverage, significant={result}")
|
| 23 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
logging.error(f"Error detecting shadows: {str(e)}")
|
| 26 |
+
return False
|