lokesh341 commited on
Commit
80193dd
·
1 Parent(s): ab8a910

Update services/shadow_detection.py

Browse files
Files changed (1) hide show
  1. services/shadow_detection.py +49 -36
services/shadow_detection.py CHANGED
@@ -1,45 +1,58 @@
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}
 
 
 
 
 
 
 
 
 
1
  import cv2
2
  import numpy as np
3
+ import logging
4
+
5
+ # Setup logging
6
+ logging.basicConfig(
7
+ filename="app.log",
8
+ level=logging.INFO,
9
+ format="%(asctime)s - %(levelname)s - %(message)s"
10
+ )
11
 
12
  def detect_shadows(frame):
13
  """
14
+ Detect shadows in a frame using brightness thresholding.
15
  Args:
16
  frame: Input frame (numpy array)
17
  Returns:
18
+ dict: Results with detections and annotated frame
 
19
  """
20
+ try:
21
+ # Convert to grayscale
22
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
23
+
24
+ # Threshold to detect dark areas (shadows)
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 {"detections": [], "frame": frame}