krisha06 commited on
Commit
ee5ee3a
·
verified ·
1 Parent(s): a38d262

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -0
app.py CHANGED
@@ -56,6 +56,27 @@ if uploaded_video:
56
  for mask, class_id, box in zip(masks, class_ids, boxes):
57
  label = names[class_id]
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  # Resize mask
60
  resized_mask = cv2.resize(mask, (frame.shape[1], frame.shape[0]))
61
  mask_bool = resized_mask > 0.5
 
56
  for mask, class_id, box in zip(masks, class_ids, boxes):
57
  label = names[class_id]
58
 
59
+ # Resize mask to match original frame
60
+ resized_mask = cv2.resize(mask, (frame.shape[1], frame.shape[0]))
61
+ mask_bool = resized_mask > 0.5
62
+
63
+ # Create colored overlay
64
+ colored_mask = np.zeros_like(frame, dtype=np.uint8)
65
+ colored_mask[mask_bool] = [0, 255, 0] # green
66
+ frame = cv2.addWeighted(frame, 1.0, colored_mask, 0.5, 0)
67
+
68
+ # Get contours to find where to place the label
69
+ contours, _ = cv2.findContours(mask_bool.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
70
+ if contours:
71
+ x, y, w, h = cv2.boundingRect(contours[0])
72
+
73
+ # Draw background rectangle for better visibility
74
+ cv2.rectangle(frame, (x, y - 25), (x + len(label) * 12, y), (0, 0, 0), -1)
75
+
76
+ # Put label text
77
+ cv2.putText(frame, label, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2, cv2.LINE_AA)
78
+
79
+
80
  # Resize mask
81
  resized_mask = cv2.resize(mask, (frame.shape[1], frame.shape[0]))
82
  mask_bool = resized_mask > 0.5