1 import pyrealsense2
as rs
4 import tensorflow
as tf
7 pipeline = rs.pipeline()
9 config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)
11 print(
"[INFO] Starting streaming...")
12 pipeline.start(config)
13 print(
"[INFO] Camera ready.")
16 print(
"[INFO] Loading model...")
17 PATH_TO_CKPT =
"frozen_inference_graph.pb" 20 detection_graph = tf.Graph()
21 with detection_graph.as_default():
22 od_graph_def = tf.compat.v1.GraphDef()
23 with tf.compat.v1.gfile.GFile(PATH_TO_CKPT,
'rb')
as fid:
24 serialized_graph = fid.read()
25 od_graph_def.ParseFromString(serialized_graph)
26 tf.compat.v1.import_graph_def(od_graph_def, name=
'')
27 sess = tf.compat.v1.Session(graph=detection_graph)
30 image_tensor = detection_graph.get_tensor_by_name(
'image_tensor:0')
33 detection_boxes = detection_graph.get_tensor_by_name(
'detection_boxes:0')
36 detection_scores = detection_graph.get_tensor_by_name(
'detection_scores:0')
37 detection_classes = detection_graph.get_tensor_by_name(
'detection_classes:0')
39 num_detections = detection_graph.get_tensor_by_name(
'num_detections:0')
41 print(
"[INFO] Model loaded.")
44 frames = pipeline.wait_for_frames()
45 color_frame = frames.get_color_frame()
48 color_image = np.asanyarray(color_frame.get_data())
49 scaled_size = (color_frame.width, color_frame.height)
52 image_expanded = np.expand_dims(color_image, axis=0)
54 (boxes, scores, classes, num) = sess.run([detection_boxes, detection_scores, detection_classes, num_detections],
55 feed_dict={image_tensor: image_expanded})
57 boxes = np.squeeze(boxes)
58 classes = np.squeeze(classes).astype(np.int32)
59 scores = np.squeeze(scores)
61 for idx
in range(int(num)):
66 if class_
not in colors_hash:
67 colors_hash[class_] = tuple(np.random.choice(range(256), size=3))
70 left = int(box[1] * color_frame.width)
71 top = int(box[0] * color_frame.height)
72 right = int(box[3] * color_frame.width)
73 bottom = int(box[2] * color_frame.height)
78 r, g, b = colors_hash[class_]
79 cv2.rectangle(color_image, p1, p2, (int(r), int(g), int(b)), 2, 1)
81 cv2.namedWindow(
'RealSense', cv2.WINDOW_AUTOSIZE)
82 cv2.imshow(
'RealSense', color_image)
85 print(
"[INFO] stop streaming ...")
static std::string print(const transformation &tf)