-
Notifications
You must be signed in to change notification settings - Fork 0
/
rstest.py
72 lines (46 loc) · 2.02 KB
/
rstest.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
import time
from pydarknet import Detector, Image
import pyrealsense2 as rs
# Import Numpy for easy array manipulation
import numpy as np
# Import OpenCV for easy image rendering
import cv2
config = rs.config()
config.enable_stream(rs.stream.depth, 1280, 720, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)
pipeline = rs.pipeline()
profile = pipeline.start(config)
depth_sensor = profile.get_device().first_depth_sensor()
depth_scale = depth_sensor.get_depth_scale()
print("Depth Scale is: ", depth_scale)
clipping_distance_in_meters = 1 # 1 meter
clipping_distance = clipping_distance_in_meters / depth_scale
align_to = rs.stream.color
align = rs.align(align_to)
if __name__ == "__main__":
net = Detector(bytes("cfg/yolov3.cfg", encoding="utf-8"), bytes("weights/yolov3.weights", encoding="utf-8"), 0,
bytes("cfg/coco.data", encoding="utf-8"))
while True:
frames = pipeline.wait_for_frames()
depth_frame = frames.get_depth_frame()
color_frame = frames.get_color_frame()
start_time = time.time()
color_image = np.asanyarray(color_frame.get_data())
pydark_image=Image(color_image)
results = net.detect(pydark_image)
del pydark_image
for cat, score, bounds in results:
x, y, w, h = bounds
depthVal = depth_frame.get_distance(int(x),int(y))
depthVal =round(depthVal,2)
cv2.rectangle(color_image, (int(x-w/2),int(y-h/2)),(int(x+w/2),int(y+h/2)),(0,255,127),2)
cv2.putText(color_image, str(cat.decode("utf-8")+" in "+str(depthVal)+"(meter)"), (int(x-w/2 ), int(y-h/2)), cv2.FONT_HERSHEY_TRIPLEX, 2, (0, 255, 127))
cv2.namedWindow("preview", cv2.WINDOW_NORMAL);
cv2.resizeWindow("preview", 1920, 1080)
cv2.imshow("preview", color_image)
end_time = time.time()
print("Elapsed Time:", end_time - start_time)
key = cv2.waitKey(1)
if key == 0xFF & ord("q"):
pipeline.stop()
break