Spaces:
Sleeping
Sleeping
Create services/shadow_detection.py
Browse files- services/shadow_detection.py +44 -4
services/shadow_detection.py
CHANGED
@@ -1,5 +1,45 @@
|
|
1 |
-
|
|
|
|
|
2 |
|
3 |
-
def
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# services/shadow_detection.py
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
|
5 |
+
def detect_shadows(frame):
|
6 |
+
"""
|
7 |
+
Detect shadows in a frame using basic image processing.
|
8 |
+
Args:
|
9 |
+
frame: Input frame (numpy array)
|
10 |
+
Returns:
|
11 |
+
dict: Shadow detection results with numbered labels
|
12 |
+
numpy array: Annotated frame
|
13 |
+
"""
|
14 |
+
# Convert to grayscale
|
15 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
16 |
+
# Apply thresholding to detect dark areas (potential shadows)
|
17 |
+
_, shadow_mask = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY_INV)
|
18 |
+
contours, _ = cv2.findContours(shadow_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
19 |
+
|
20 |
+
detections = []
|
21 |
+
line_counter = 1 # Initialize counter for numbered labels
|
22 |
+
|
23 |
+
for contour in contours:
|
24 |
+
area = cv2.contourArea(contour)
|
25 |
+
if area < 100: # Filter small areas
|
26 |
+
continue
|
27 |
+
x, y, w, h = cv2.boundingRect(contour)
|
28 |
+
x_min, y_min, x_max, y_max = x, y, x + w, y + h
|
29 |
+
|
30 |
+
# Add numbered label
|
31 |
+
detection_label = f"Line {line_counter} - Shadow"
|
32 |
+
detections.append({
|
33 |
+
"label": detection_label,
|
34 |
+
"coordinates": [x_min, y_min, x_max, y_max]
|
35 |
+
})
|
36 |
+
|
37 |
+
# Draw bounding box and label
|
38 |
+
color = (128, 128, 128) # Gray for shadows
|
39 |
+
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
|
40 |
+
cv2.putText(frame, detection_label, (x_min, y_min - 10),
|
41 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
42 |
+
|
43 |
+
line_counter += 1
|
44 |
+
|
45 |
+
return {"detections": detections, "frame": frame}
|