-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
262 lines (210 loc) · 8.08 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# utils.py
import pyrealsense2 as rs
import cv2
import numpy as np
import time
import os
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import math
from ultralytics import YOLO
from FastSAM.fastsam import FastSAM, FastSAMPrompt
import torch
should_save_image = False
close_captured_image_window = False
def get_color(color_name):
color_dict = {
'red': (0, 0, 255),
'green': (0, 255, 0),
'blue': (255, 0, 0),
'yellow': (0, 255, 255),
'purple': (128, 0, 128),
'orange': (0, 165, 255),
'cyan': (255, 255, 0),
'magenta': (255, 0, 255),
'pink': (203, 192, 255),
'teal': (128, 128, 0),
'lime': (0, 255, 0),
'brown': (42, 42, 165),
'maroon': (0, 0, 128),
'navy': (128, 0, 0),
'olive': (0, 128, 128),
'gray': (128, 128, 128),
'silver': (192, 192, 192),
'gold': (0, 215, 255),
'turquoise': (208, 224, 64),
'violet': (211, 0, 148),
'indigo': (130, 0, 75),
'lavender': (208, 184, 170),
'peach': (255, 218, 185),
'salmon': (114, 128, 250),
'sky_blue': (235, 206, 135),
'tan': (140, 180, 210),
'dark_green': (0, 100, 0),
'dark_red': (0, 0, 139),
'dark_blue': (139, 0, 0),
}
return color_dict.get(color_name, (0, 0, 255)) # Default to red if color_name is not found
def check_camera_connection():
"""Check if camera is connected"""
try:
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
profile = pipeline.start(config)
context = rs.context()
devices = context.query_devices()
for device in devices:
print("Device Information:")
print("Device Product Line: ", str(device.get_info(rs.camera_info.product_line)))
print("Device Serial Number: ", str(device.get_info(rs.camera_info.serial_number)))
pipeline.stop()
except:
print("Camera not connected!")
exit()
def start_realtime_stream():
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
profile = pipeline.start(config)
align_to = rs.stream.color
align = rs.align(align_to)
global should_save_image, close_captured_image_window
try:
while True:
frames = pipeline.wait_for_frames()
aligned_frames = align.process(frames)
aligned_depth_frame = aligned_frames.get_depth_frame()
color_frame = aligned_frames.get_color_frame()
if not aligned_depth_frame or not color_frame:
continue
color_image = np.asanyarray(color_frame.get_data())
cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
cv2.imshow('RealSense', color_image)
key = cv2.waitKey(1)
if key == ord('c'):
capture_image(color_image)
elif key == ord('s'):
should_save_image = True
if close_captured_image_window:
if cv2.getWindowProperty('Captured Image', cv2.WND_PROP_VISIBLE) <= 0:
close_captured_image_window = False
else:
cv2.destroyWindow('Captured Image')
if key == 27:
break
finally:
pipeline.stop()
cv2.destroyAllWindows()
def capture_image(image):
cv2.namedWindow('Captured Image', cv2.WINDOW_AUTOSIZE)
cv2.imshow('Captured Image', image)
cv2.setWindowTitle('RealSense', 'Press \'s\' to save')
global should_save_image, close_captured_image_window
while True:
key = cv2.waitKey(25) & 0xFF
if key == ord('s'):
should_save_image = True
close_captured_image_window = True
elif key == 27:
break
if close_captured_image_window:
if cv2.getWindowProperty('Captured Image', cv2.WND_PROP_VISIBLE) <= 0:
close_captured_image_window = False
else:
cv2.destroyWindow('Captured Image')
break
if should_save_image:
save_image(image)
def save_image(image):
global should_save_image
if should_save_image:
if not os.path.exists('dataset'):
os.makedirs('dataset')
image_name = 'dataset/' + str(int(time.time() * 1000)) + '.png'
cv2.imwrite(image_name, image)
print('Image saved at:', image_name)
should_save_image = False
def detect_and_visualize_yolo(input_data, yolo_model_path=None):
if isinstance(input_data, str):
frame = cv2.imread(input_data)
else:
frame = input_data
if yolo_model_path is None:
yolo_model_path = 'yolov8m.pt'
model = YOLO(yolo_model_path)
results = model.predict(source=frame, conf=0.50)
predicted_boxes = results[0].boxes.xyxy.cpu().numpy()
_, ax = plt.subplots(1)
ax.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
predicted_boxes = results[0].boxes.xyxy.cpu().numpy() # Convert to NumPy on CPU
for box in predicted_boxes:
x, y, w, h = box[:4]
rect = patches.Rectangle((x, y), w - x, h - y, linewidth=1, edgecolor='r', facecolor='none')
ax.add_patch(rect)
plt.axis('off')
plt.show()
def perform_yolo_inference(frame, model, confidence_threshold=0.6):
results = model(frame, stream=True)
detections = []
predicted_boxes = []
for r in results:
boxes = r.boxes
for box in boxes:
if box.conf[0] >= confidence_threshold:
# Bounding box coordinates
x1, y1, x2, y2 = box.xyxy[0]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
# Confidence and class name
confidence = math.ceil((box.conf[0] * 100)) / 100
cls = int(box.cls[0])
class_name = model.names[cls]
detections.append({
'bounding_box': (x1, y1, x2, y2),
'confidence': confidence,
'class_name': class_name
})
predicted_boxes.append([x1, y1, x2, y2])
return detections, predicted_boxes
def run_realtime_object_detection():
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
pipeline.start(config)
yolo_model = YOLO("yolo_train/runs/detect/train/weights/best.pt")
fastsam_model = FastSAM('FastSAM-s.pt')
DEVICE = torch.device(
"cuda:0"
if torch.cuda.is_available()
else "mps"
if torch.backends.mps.is_available()
else "cpu"
)
print(DEVICE)
try:
while True:
frames = pipeline.wait_for_frames()
color_frame = frames.get_color_frame()
if not color_frame:
continue
color_image = np.asanyarray(color_frame.get_data())
yolo_results = yolo_model.predict(source=color_image, conf=0.50)
predicted_boxes = yolo_results[0].boxes.xyxy.cpu().numpy()
if len(predicted_boxes) > 0:
first_box = predicted_boxes[0]
bounding_box = [int(i) for i in first_box[:4]]
fastsam_results = fastsam_model(color_image, device=DEVICE, retina_masks=True, imgsz=640, conf=0.3, iou=0.7)
prompt_process = FastSAMPrompt(color_image, fastsam_results, device=DEVICE)
ann = prompt_process.box_prompt(bbox=bounding_box)
img_with_annotations = prompt_process.plot_to_result(annotations=ann)
cv2.imshow('RealSense + YOLO + FastSAM', cv2.addWeighted(color_image, 1, img_with_annotations, 0.5, 0))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
pipeline.stop()
cv2.destroyAllWindows()
if __name__ == '__main__':
check_camera_connection()
start_realtime_stream()