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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -36
app.py CHANGED
@@ -24,13 +24,12 @@ if uploaded_video:
24
  video_path = tfile.name
25
 
26
  st.video(video_path)
27
-
28
  st.markdown("### ⏳ Processing video...")
29
 
30
  cap = cv2.VideoCapture(video_path)
31
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
32
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
33
- fps = int(cap.get(cv2.CAP_PROP_FPS))
34
 
35
  output_path = os.path.join(tempfile.gettempdir(), "output_segmentation.mp4")
36
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
@@ -56,33 +55,12 @@ if uploaded_video:
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
83
 
84
- # Overlay color
85
- color = (0, 255, 0) # green
86
  colored_mask = np.zeros_like(frame, dtype=np.uint8)
87
  colored_mask[mask_bool] = color
88
  frame = cv2.addWeighted(frame, 1.0, colored_mask, 0.5, 0)
@@ -91,20 +69,21 @@ if uploaded_video:
91
  x1, y1, x2, y2 = box.astype(int)
92
  label_text = label
93
  font = cv2.FONT_HERSHEY_SIMPLEX
94
- font_scale = 0.6
95
  thickness = 2
96
 
97
- # Get text size to create background rectangle
98
  (text_width, text_height), _ = cv2.getTextSize(label_text, font, font_scale, thickness)
99
- text_x, text_y = x1, y1 - 10 if y1 - 10 > 10 else y1 + 20
 
100
 
101
- # Background rectangle
102
- cv2.rectangle(frame, (text_x, text_y - text_height - 4),
103
- (text_x + text_width + 4, text_y + 4), color, -1)
104
 
105
- # Text
106
- cv2.putText(frame, label_text, (text_x + 2, text_y),
107
- font, font_scale, (0, 0, 0), thickness=1, lineType=cv2.LINE_AA)
108
 
109
  out.write(frame)
110
  frame_index += 1
 
24
  video_path = tfile.name
25
 
26
  st.video(video_path)
 
27
  st.markdown("### ⏳ Processing video...")
28
 
29
  cap = cv2.VideoCapture(video_path)
30
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
31
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
32
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
33
 
34
  output_path = os.path.join(tempfile.gettempdir(), "output_segmentation.mp4")
35
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
 
55
  for mask, class_id, box in zip(masks, class_ids, boxes):
56
  label = names[class_id]
57
 
58
+ # Resize and apply mask
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  resized_mask = cv2.resize(mask, (frame.shape[1], frame.shape[0]))
60
  mask_bool = resized_mask > 0.5
61
 
62
+ # Overlay color (green for now)
63
+ color = (0, 255, 0)
64
  colored_mask = np.zeros_like(frame, dtype=np.uint8)
65
  colored_mask[mask_bool] = color
66
  frame = cv2.addWeighted(frame, 1.0, colored_mask, 0.5, 0)
 
69
  x1, y1, x2, y2 = box.astype(int)
70
  label_text = label
71
  font = cv2.FONT_HERSHEY_SIMPLEX
72
+ font_scale = 0.7
73
  thickness = 2
74
 
75
+ # Get text size for background box
76
  (text_width, text_height), _ = cv2.getTextSize(label_text, font, font_scale, thickness)
77
+ text_x = x1
78
+ text_y = y1 - 10 if y1 - 10 > 10 else y1 + text_height + 10
79
 
80
+ # Draw background rectangle
81
+ cv2.rectangle(frame, (text_x - 2, text_y - text_height - 4),
82
+ (text_x + text_width + 2, text_y + 4), (0, 0, 0), -1)
83
 
84
+ # Put text on top of rectangle
85
+ cv2.putText(frame, label_text, (text_x, text_y),
86
+ font, font_scale, (255, 255, 255), thickness=2, lineType=cv2.LINE_AA)
87
 
88
  out.write(frame)
89
  frame_index += 1