kadirnar commited on
Commit
f0573ed
1 Parent(s): afb81ba

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +14 -1
  2. demo.py +110 -0
app.py CHANGED
@@ -86,18 +86,31 @@ def video_app():
86
  value=64,
87
  label="Points per Batch",
88
  )
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  seg_automask_video_predict = gr.Button(value="Generator")
91
  with gr.Column():
92
  output_video = gr.Video()
93
 
94
  seg_automask_video_predict.click(
95
- fn=SegAutoMaskGenerator().save_image,
96
  inputs=[
97
  seg_automask_video_file,
98
  seg_automask_video_model_type,
99
  seg_automask_video_points_per_side,
100
  seg_automask_video_points_per_batch,
 
 
101
  ],
102
  outputs=[output_video],
103
  )
 
86
  value=64,
87
  label="Points per Batch",
88
  )
89
+ with gr.Row():
90
+ with gr.Column():
91
+ seg_automask_video_min_area = gr.Number(
92
+ value=1000,
93
+ label="Min Area",
94
+ )
95
+
96
+ seg_automask_video_max_area = gr.Number(
97
+ value=10000,
98
+ label="Max Area",
99
+ )
100
 
101
  seg_automask_video_predict = gr.Button(value="Generator")
102
  with gr.Column():
103
  output_video = gr.Video()
104
 
105
  seg_automask_video_predict.click(
106
+ fn=SegAutoMaskGenerator().save_video,
107
  inputs=[
108
  seg_automask_video_file,
109
  seg_automask_video_model_type,
110
  seg_automask_video_points_per_side,
111
  seg_automask_video_points_per_batch,
112
+ seg_automask_video_min_area,
113
+ seg_automask_video_max_area,
114
  ],
115
  outputs=[output_video],
116
  )
demo.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import torch
4
+
5
+ from metaseg import SamAutomaticMaskGenerator, sam_model_registry
6
+ from metaseg.utils.file import download_model
7
+
8
+
9
+ class SegAutoMaskGenerator:
10
+ def __init__(self):
11
+ self.model = None
12
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
13
+
14
+ def load_model(self, model_type):
15
+ if self.model is None:
16
+ model_path = download_model(model_type)
17
+ model = sam_model_registry[model_type](checkpoint=model_path)
18
+ model.to(device=self.device)
19
+ self.model = model
20
+
21
+ return self.model
22
+
23
+ def load_image(self, image_path):
24
+ image = cv2.imread(image_path)
25
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
26
+ return image
27
+
28
+ def load_video(self, video_path):
29
+ cap = cv2.VideoCapture(video_path)
30
+ frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
31
+ frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
32
+ fourcc = cv2.VideoWriter_fourcc(*"XVID")
33
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
34
+ out = cv2.VideoWriter("output.mp4", fourcc, fps, (frame_width, frame_height))
35
+
36
+ return cap, out
37
+
38
+ def predict(self, frame, model_type, points_per_side, points_per_batch):
39
+ model = self.load_model(model_type)
40
+ mask_generator = SamAutomaticMaskGenerator(
41
+ model, points_per_side=points_per_side, points_per_batch=points_per_batch
42
+ )
43
+ masks = mask_generator.generate(frame)
44
+
45
+ return frame, masks
46
+
47
+ def save_image(self, source, model_type, points_per_side, points_per_batch):
48
+ read_image = self.load_image(source)
49
+ image, anns = self.predict(read_image, model_type, points_per_side, points_per_batch)
50
+ if len(anns) == 0:
51
+ return
52
+
53
+ sorted_anns = sorted(anns, key=(lambda x: x["area"]), reverse=True)
54
+ mask_image = np.zeros((anns[0]["segmentation"].shape[0], anns[0]["segmentation"].shape[1], 3), dtype=np.uint8)
55
+ colors = np.random.randint(0, 255, size=(256, 3), dtype=np.uint8)
56
+ for i, ann in enumerate(sorted_anns):
57
+ m = ann["segmentation"]
58
+ img = np.ones((m.shape[0], m.shape[1], 3), dtype=np.uint8)
59
+ color = colors[i % 256]
60
+ for i in range(3):
61
+ img[:, :, 0] = color[0]
62
+ img[:, :, 1] = color[1]
63
+ img[:, :, 2] = color[2]
64
+ img = cv2.bitwise_and(img, img, mask=m.astype(np.uint8))
65
+ img = cv2.addWeighted(img, 0.35, np.zeros_like(img), 0.65, 0)
66
+ mask_image = cv2.add(mask_image, img)
67
+
68
+ combined_mask = cv2.add(image, mask_image)
69
+ cv2.imwrite("output.jpg", combined_mask)
70
+
71
+ return "output.jpg"
72
+
73
+ def save_video(self, source, model_type, points_per_side, points_per_batch, min_area, max_area):
74
+ cap, out = self.load_video(source)
75
+ colors = np.random.randint(0, 255, size=(256, 3), dtype=np.uint8)
76
+
77
+ while True:
78
+ ret, frame = cap.read()
79
+ if not ret:
80
+ break
81
+
82
+ image, anns = self.predict(frame, model_type, points_per_side, points_per_batch)
83
+ if len(anns) == 0:
84
+ continue
85
+
86
+ sorted_anns = sorted(anns, key=(lambda x: x["area"]), reverse=True)
87
+ mask_image = np.zeros(
88
+ (anns[0]["segmentation"].shape[0], anns[0]["segmentation"].shape[1], 3), dtype=np.uint8
89
+ )
90
+
91
+ for i, ann in enumerate(sorted_anns):
92
+ if max_area > ann["area"] > min_area:
93
+ m = ann["segmentation"]
94
+ color = colors[i % 256] # Her nesne için farklı bir renk kullan
95
+ img = np.zeros((m.shape[0], m.shape[1], 3), dtype=np.uint8)
96
+ img[:, :, 0] = color[0]
97
+ img[:, :, 1] = color[1]
98
+ img[:, :, 2] = color[2]
99
+ img = cv2.bitwise_and(img, img, mask=m.astype(np.uint8))
100
+ img = cv2.addWeighted(img, 0.35, np.zeros_like(img), 0.65, 0)
101
+ mask_image = cv2.add(mask_image, img)
102
+
103
+ combined_mask = cv2.add(frame, mask_image)
104
+ out.write(combined_mask)
105
+
106
+ out.release()
107
+ cap.release()
108
+ cv2.destroyAllWindows()
109
+
110
+ return "output.mp4"