example3 - opencv deploy.py
Go to the documentation of this file.
1 import pyrealsense2 as rs
2 import numpy as np
3 import cv2
4 
5 W = 848
6 H = 480
7 
8 # Configure depth and color streams
9 pipeline = rs.pipeline()
10 config = rs.config()
11 config.enable_stream(rs.stream.depth, W, H, rs.format.z16, 30)
12 config.enable_stream(rs.stream.color, W, H, rs.format.bgr8, 30)
13 
14 
15 print("[INFO] start streaming...")
16 pipeline.start(config)
17 
18 aligned_stream = rs.align(rs.stream.color) # alignment between color and depth
19 point_cloud = rs.pointcloud()
20 
21 print("[INFO] loading model...")
22 # download model from: https://github.com/opencv/opencv/wiki/TensorFlow-Object-Detection-API#run-network-in-opencv
23 net = cv2.dnn.readNetFromTensorflow("frozen_inference_graph.pb", "faster_rcnn_inception_v2_coco_2018_01_28.pbtxt")
24 while True:
25  frames = pipeline.wait_for_frames()
26  frames = aligned_stream.process(frames)
27  color_frame = frames.get_color_frame()
28  depth_frame = frames.get_depth_frame().as_depth_frame()
29 
30  points = point_cloud.calculate(depth_frame)
31  verts = np.asanyarray(points.get_vertices()).view(np.float32).reshape(-1, W, 3) # xyz
32 
33  # Convert images to numpy arrays
34  depth_image = np.asanyarray(depth_frame.get_data())
35  # skip empty frames
36  if not np.any(depth_image):
37  continue
38  print("[INFO] found a valid depth frame")
39  color_image = np.asanyarray(color_frame.get_data())
40 
41  scaled_size = (int(W), int(H))
42  net.setInput(cv2.dnn.blobFromImage(color_image, size=scaled_size, swapRB=True, crop=False))
43  detections = net.forward()
44 
45  print("[INFO] drawing bounding box on detected objects...")
46 
47  for detection in detections[0,0]:
48  score = float(detection[2])
49  idx = int(detection[1])
50  print(" [DEBUG] classe : ",idx)
51 
52  if score > 0.8 and idx == 0:
53  left = detection[3] * W
54  top = detection[4] * H
55  right = detection[5] * W
56  bottom = detection[6] * H
57  width = right - left
58  height = bottom - top
59 
60  bbox = (int(left), int(top), int(width), int(height))
61 
62  p1 = (int(bbox[0]), int(bbox[1]))
63  p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
64  cv2.rectangle(color_image, p1, p2, (255, 0, 0), 2, 1)
65 
66  # x,y,z of bounding box
67  obj_points = verts[int(bbox[1]):int(bbox[1] + bbox[3]), int(bbox[0]):int(bbox[0] + bbox[2])].reshape(-1, 3)
68  zs = obj_points[:,2]
69 
70  z = np.median(zs)
71 
72  ys = obj_points[:,1]
73  ys = np.delete(ys, np.where((zs < z - 1) | (zs > z + 1))) # take only y for close z to prevent including background
74 
75  my = np.amin(ys, initial=1)
76  My = np.amax(ys, initial=-1)
77 
78  height = (My - my) # add next to rectangle print of height using cv library
79  height = float("{:.2f}".format(height))
80  print("[INFO] object height is: ", height, "[m]")
81  height_txt = str(height)+"[m]"
82 
83  # Write some Text
84  font = cv2.FONT_HERSHEY_SIMPLEX
85  bottomLeftCornerOfText = (p1[0], p1[1]+20)
86  fontScale = 1
87  fontColor = (255, 255, 255)
88  lineType = 2
89  cv2.putText(color_image, height_txt,
90  bottomLeftCornerOfText,
91  font,
92  fontScale,
93  fontColor,
94  lineType)
95 
96  # Show images
97  cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
98  cv2.imshow('RealSense', color_image)
99  cv2.waitKey(1)
100 
101 # Stop streaming
102 pipeline.stop()
void reshape(GLFWwindow *window, int w, int h)
Definition: boing.c:215
static std::string print(const transformation &tf)


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:14