Skip to content

Commit 6d06ef0

Browse files
authored
v1.1
1 parent 3ee4de1 commit 6d06ef0

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

Diff for: SAMandYOLOv8video.py

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
!pip install ultralytics
2+
!pip install supervision
3+
!pip install roboflow
4+
!{sys.executable} -m pip install 'git+https://github.com/facebookresearch/segment-anything.git'
5+
!wget -q https://dl.fbaipublicfiles.com/segment_anything/sam_vit_l_0b3195.pth
6+
7+
import numpy as np
8+
import cv2
9+
import os
10+
import sys
11+
import torch
12+
13+
#ROBOFLOW
14+
from roboflow import Roboflow
15+
rf = Roboflow(api_key="Pho4XVVaJnNcR3y3HNrK")
16+
project = rf.workspace("team12").project("airplane-bp8fl")
17+
dataset = project.version(2).download("yolov8")
18+
19+
# YOLO
20+
from ultralytics import YOLO
21+
yolo_model = YOLO('yolov8x.pt')
22+
23+
# SEGMENT-Anything
24+
from segment_anything import sam_model_registry, SamAutomaticMaskGenerator, SamPredictor
25+
import supervision as sv
26+
27+
DEVICE = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
28+
MODEL_TYPE = "vit_l"
29+
30+
CHECKPOINT_PATH = os.path.join("sam_vit_l_0b3195.pth")
31+
sam = sam_model_registry[MODEL_TYPE](checkpoint=CHECKPOINT_PATH).to(device=DEVICE)
32+
33+
mask_predictor = SamPredictor(sam)
34+
35+
# Run Code
36+
37+
VIDEO_SOURCE = '/content/airplane2.mp4'
38+
VIDEO_DESTINATION = '/content/result2.avi'
39+
40+
# Video kaynağını aç
41+
cap = cv2.VideoCapture(VIDEO_SOURCE)
42+
43+
# Video kaydedicisini ayarla
44+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
45+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
46+
fourcc = cv2.VideoWriter_fourcc(*'XVID')
47+
FPS = 30.0
48+
out = cv2.VideoWriter(VIDEO_DESTINATION, fourcc, FPS, (width, height))
49+
50+
while True:
51+
# Frame oku
52+
ret, frame = cap.read()
53+
54+
# Okuma başarısızsa video bitti
55+
if not ret:
56+
break
57+
58+
# Girdi frame'ini SuperAnnotate'e uygun formata getir
59+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
60+
results = yolo_model.predict(source=frame)
61+
62+
boxes_class_name = np.array([])
63+
for r in results:
64+
for c in r.boxes.cls:
65+
boxes_class_name = np.append(boxes_class_name,yolo_model.names[int(c)])
66+
boxes_class_name
67+
68+
k = 0
69+
result_np_son = np.array([])
70+
result_class = np.array([])
71+
result_score = np.array([])
72+
for result in results:
73+
result_np = np.array(result.boxes.data.cpu())
74+
75+
for i in result_np:
76+
result_np_son = np.append(result_np_son,i[:][:-2])
77+
result_class = np.append(result_class,i[:][-1:])
78+
result_score = np.append(result_score,i[:][-2:-1])
79+
k += 1
80+
81+
82+
boxes = result_np_son.reshape((k,4))
83+
boxes_class = result_class.reshape((k,))
84+
boxes_score = result_score.reshape((k,))
85+
86+
87+
segmented_image = frame.copy()
88+
box_image = frame.copy()
89+
boxes_class_value = {'0':sv.Color.white(),
90+
'1':sv.Color.blue(),
91+
'2':sv.Color.blue(),
92+
'3':sv.Color.blue(),
93+
'4':sv.Color.red(),
94+
'5':sv.Color.blue(),
95+
'7':sv.Color.black(),
96+
'8':sv.Color.green(),
97+
'9':sv.Color.blue(),
98+
'37':sv.Color.blue()}
99+
mask_predictor.set_image(frame)
100+
101+
boxes = result.boxes.xyxy
102+
if len(boxes) != 0:
103+
masks, scores, logits = mask_predictor.predict_torch(
104+
point_coords = None,
105+
point_labels = None,
106+
boxes = boxes * 1040/max(frame.shape),
107+
multimask_output=True
108+
)
109+
110+
masks = masks.cpu().numpy()
111+
112+
i = 0
113+
for box in boxes.cpu().numpy():
114+
115+
box_annotator = sv.BoxAnnotator(color= boxes_class_value[str(int(boxes_class[i]))], text_padding=10)
116+
mask_annotator = sv.MaskAnnotator(color= boxes_class_value[str(int(boxes_class[i]))])
117+
118+
detections = sv.Detections(
119+
xyxy=sv.mask_to_xyxy(masks=masks[i]),
120+
mask=masks[i]
121+
)
122+
123+
detections = detections[detections.area == np.max(detections.area)]
124+
box_image = box_annotator.annotate(scene=box_image, detections=detections, skip_label=True)
125+
segmented_image = mask_annotator.annotate(scene=segmented_image, detections=detections)
126+
i += 1
127+
128+
# Video kaydediciye yaz
129+
out.write(cv2.cvtColor(segmented_image, cv2.COLOR_RGB2BGR))
130+
131+
# Video kaynak ve kaydediciyi serbest bırak
132+
cap.release()
133+

0 commit comments

Comments
 (0)