Spaces:
Runtime error
Runtime error
Update kaggle_sam2_gpu_1.py
Browse files- kaggle_sam2_gpu_1.py +8 -8
kaggle_sam2_gpu_1.py
CHANGED
@@ -17,9 +17,9 @@ sys.path.append("/kaggle/florence-sam-kaggle")
|
|
17 |
from utils.video import generate_unique_name, create_directory, delete_directory
|
18 |
from utils.sam import load_sam_image_model, run_sam_inference, load_sam_video_model
|
19 |
|
20 |
-
|
21 |
DEVICE = [torch.device(f'cuda:{i}') for i in range(torch.cuda.device_count())][-1]
|
22 |
-
|
23 |
# DEVICE = torch.device("cpu")
|
24 |
|
25 |
torch.autocast(device_type="cuda", dtype=torch.bfloat16).__enter__()
|
@@ -32,7 +32,7 @@ SAM_VIDEO_MODEL = load_sam_video_model(device=DEVICE)
|
|
32 |
VIDEO_SCALE_FACTOR = 1
|
33 |
VIDEO_TARGET_DIRECTORY = "/kaggle/"
|
34 |
create_directory(directory_path=VIDEO_TARGET_DIRECTORY)
|
35 |
-
with open('/kaggle/
|
36 |
output_video = pickle.load(file)
|
37 |
print(output_video)
|
38 |
video_input= output_video
|
@@ -43,7 +43,7 @@ frame = next(frame_generator)
|
|
43 |
frame = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
44 |
|
45 |
|
46 |
-
with open('/kaggle/
|
47 |
detections_list = pickle.load(file)
|
48 |
print(detections_list)
|
49 |
detections = sv.Detections.merge(detections_list)
|
@@ -99,11 +99,11 @@ frames_generator = sv.get_video_frames_generator(video_input)
|
|
99 |
masks_generator = SAM_VIDEO_MODEL.propagate_in_video(inference_state)
|
100 |
n = 1
|
101 |
|
102 |
-
out_dir = "/kaggle/
|
103 |
os.makedirs(out_dir, exist_ok=True)
|
104 |
import shutil
|
105 |
-
shutil.rmtree('/kaggle/
|
106 |
-
os.makedirs('/kaggle/
|
107 |
with sv.VideoSink(video_path, video_info=video_info) as sink:
|
108 |
for frame, (_, tracker_ids, mask_logits) in zip(frames_generator, masks_generator):
|
109 |
frame = sv.scale_image(frame, VIDEO_SCALE_FACTOR)
|
@@ -146,7 +146,7 @@ with sv.VideoSink(video_path, video_info=video_info) as sink:
|
|
146 |
frame = np.where(all_mask[:, :, None] > 0, masked_image, frame)
|
147 |
# result 即为只保留 all_mask 遮罩内容的图像
|
148 |
#frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
149 |
-
cv2.imwrite("/kaggle/
|
150 |
n = n + 1
|
151 |
|
152 |
|
|
|
17 |
from utils.video import generate_unique_name, create_directory, delete_directory
|
18 |
from utils.sam import load_sam_image_model, run_sam_inference, load_sam_video_model
|
19 |
|
20 |
+
DEVICE = torch.device("cuda")
|
21 |
DEVICE = [torch.device(f'cuda:{i}') for i in range(torch.cuda.device_count())][-1]
|
22 |
+
DEVICE = [torch.device(f'cuda:{i}') for i in range(torch.cuda.device_count())][0]
|
23 |
# DEVICE = torch.device("cpu")
|
24 |
|
25 |
torch.autocast(device_type="cuda", dtype=torch.bfloat16).__enter__()
|
|
|
32 |
VIDEO_SCALE_FACTOR = 1
|
33 |
VIDEO_TARGET_DIRECTORY = "/kaggle/"
|
34 |
create_directory(directory_path=VIDEO_TARGET_DIRECTORY)
|
35 |
+
with open('/kaggle/output_video1.pkl', 'rb') as file:
|
36 |
output_video = pickle.load(file)
|
37 |
print(output_video)
|
38 |
video_input= output_video
|
|
|
43 |
frame = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
44 |
|
45 |
|
46 |
+
with open('/kaggle/detections_list1.pkl', 'rb') as file:
|
47 |
detections_list = pickle.load(file)
|
48 |
print(detections_list)
|
49 |
detections = sv.Detections.merge(detections_list)
|
|
|
99 |
masks_generator = SAM_VIDEO_MODEL.propagate_in_video(inference_state)
|
100 |
n = 1
|
101 |
|
102 |
+
out_dir = "/kaggle/output1"
|
103 |
os.makedirs(out_dir, exist_ok=True)
|
104 |
import shutil
|
105 |
+
shutil.rmtree('/kaggle/output1', ignore_errors=True)
|
106 |
+
os.makedirs('/kaggle/output1', exist_ok=True)
|
107 |
with sv.VideoSink(video_path, video_info=video_info) as sink:
|
108 |
for frame, (_, tracker_ids, mask_logits) in zip(frames_generator, masks_generator):
|
109 |
frame = sv.scale_image(frame, VIDEO_SCALE_FACTOR)
|
|
|
146 |
frame = np.where(all_mask[:, :, None] > 0, masked_image, frame)
|
147 |
# result 即为只保留 all_mask 遮罩内容的图像
|
148 |
#frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
149 |
+
cv2.imwrite("/kaggle/output1/" + str(n) + ".jpeg", frame)
|
150 |
n = n + 1
|
151 |
|
152 |
|