Spaces:
Sleeping
Sleeping
File size: 8,668 Bytes
b60b6c5 |
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 |
## Feed custom video source to `predict_batch` to DeGirum PySDK This guide is for developers who want to learn how to configure a custom video generator and feed it to DeGirum PySDK to use it for model predictions. In this guide, we will walk you through the steps on creating your own custom video generator and pass it to model object's `predict_batch` function to perform prediction over it. -------------------------------------------------------------------------------- ### Simple predictions on video stream To begin with, lets take a look at how to run predictions on a simple video stream as is. For this, we will start with a simple frame generator function as below: -------------------------------------------------------------------------------- # Define generator function to produce video frames def frame_source(stream): while True: ret, frame = stream.read() if not ret: break # end of file yield frame -------------------------------------------------------------------------------- We use this function to pass it to our `predict_batch` function as below: -------------------------------------------------------------------------------- for result in model.predict_batch(frame_source(stream)): # Print raw results for each frame print(result) -------------------------------------------------------------------------------- Putting it all together, lets run inference of a video source using simple video generator and `predict_batch` function: -------------------------------------------------------------------------------- import degirum as dg import cv2 # Declaring variables # Set your model, inference host address, model zoo, and token in these variables. your_model_name = "model-name" your_host_address = "@local" # Can be dg.CLOUD, host:port, or dg.LOCAL your_model_zoo = "degirum/hailo" your_token = "<token>" # Specify the video you will run inference on your_video = "path/video.mp4" # Loading a model model = dg.load_model( model_name = your_model_name, inference_host_address = your_host_address, zoo_url = your_model_zoo, token = your_token # optional parameters, such as overlay_show_probabilities = True ) # Open your video file stream = cv2.VideoCapture(your_video) # Define generator function to produce video frames def frame_source(stream): while True: ret, frame = stream.read() if not ret: break # end of file yield frame # Run predict_batch on stream of frames from video file for result in model.predict_batch(frame_source(stream)): # Print raw results for each frame print(result) # Release stream stream.release() -------------------------------------------------------------------------------- #### Modify video generator to use webcam or Rpi stream In order to use webcam or an Rpi stream as video source, we will just modify our video source_path as below: -------------------------------------------------------------------------------- ##### Webcam input -------------------------------------------------------------------------------- import degirum as dg import cv2 # Declaring variables # Set your model, inference host address, model zoo, and token in these variables. your_model_name = "model-name" your_host_address = "@local" # Can be dg.CLOUD, host:port, or dg.LOCAL your_model_zoo = "degirum/hailo" your_token = "<token>" # Loading a model model = dg.load_model( model_name = your_model_name, inference_host_address = your_host_address, zoo_url = your_model_zoo, token = your_token # optional parameters, such as overlay_show_probabilities = True ) # Open your video file stream = cv2.VideoCapture(0) # Webcam source : 0 # Define generator function to produce video frames def frame_source(stream): while True: ret, frame = stream.read() if not ret: break # end of file yield frame # Run predict_batch on stream of frames from video file for result in model.predict_batch(frame_source(stream)): # Print raw results for each frame print(result) # Release stream stream.release() -------------------------------------------------------------------------------- #### Rpi Camera using PiCamera Module Similar to above example where we used OpenCV to capture frames from webcam, we can use `Picamera2` module to fetch frames from an Rpi Camera inside our frame generator function and pass that to `predict_batch` -------------------------------------------------------------------------------- import cv2 import degirum as dg import numpy as np from picamera2 import Picamera2 your_model_name = "model-name" your_host_address = "@local" # Can be dg.CLOUD, host:port, or dg.LOCAL your_model_zoo = "degirum/hailo" your_token = "<token>" # Load the model model = dg.load_model( model_name = your_model_name, inference_host_address = your_host_address, zoo_url = your_model_zoo, token = your_token # optional parameters, such as overlay_show_probabilities = True ) # Define frame generator using Picamera2 def picamera2_frame_generator(): picam2 = Picamera2() picam2.configure(picam2.preview_configuration(main={"format": 'BGR888'})) picam2.start() try: while True: frame = picam2.capture_array() yield frame finally: picam2.stop() # Run inference and display for result in model.predict_batch(picamera2_frame_generator(rotate=True)): cv2.imshow("AI Inference PiCamera2", result.image_overlay) if cv2.waitKey(1) & 0xFF == ord("q"): break cv2.destroyAllWindows() -------------------------------------------------------------------------------- #### Transform your video source before predictions Lets a take a look at how can you perform pre-processing or transformation on your input video source like rotation, resize, crop etc and perform model prediction over it. In this example, we will take camera input and rotate it 90' clockwise before passing it to `predict_batch` function. -------------------------------------------------------------------------------- For this, we will modify our video generator as follows: -------------------------------------------------------------------------------- def rotated_frame_generator(video_source): stream = cv2.VideoCapture(video_source) try: while True: ret, frame = stream.read() if not ret: break rotated_frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE) # Rotate frame 90 degrees clockwise yield rotated_frame finally: stream.release() -------------------------------------------------------------------------------- Putting it all together, it looks like this: -------------------------------------------------------------------------------- import degirum as dg import degirum_tools.video_support as vs import cv2 import degirum_tools hw_location = "@local" model_zoo_url = "model_zoo_url" model_name = "your_model_name" video_source_path = 0 # Webcam source : 0 degirum_cloud_token = "<token>" # Optional, only needed for cloud inference # Load the model model = dg.load_model( model_name=model_name, inference_host_address=hw_location, zoo_url=model_zoo_url, token=degirum_cloud_token, overlay_color=(0, 255, 0) ) # Define rotated frame generator def rotated_frame_generator(video_source): stream = cv2.VideoCapture(video_source) try: while True: ret, frame = stream.read() if not ret: break rotated_frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE) # Rotate frame 90 degrees clockwise yield rotated_frame finally: stream.release() # Run inference and display for result in model.predict_batch(rotated_frame_generator(video_source_path)): cv2.imshow("AI Inference", result.image_overlay) if cv2.waitKey(1) & 0xFF == ord("q"): break cv2.destroyAllWindows() -------------------------------------------------------------------------------- #### Conclusion In this guide, we have walked through the steps of setting up a custom video frame generator and pass it to model predictions using DeGirum PySDK. These steps allows you to modify and transform your video source feed and perform different operations such as rotation, crop etc and use the transformed video frames for model prediction. Such a process is useful especially when you want to modify your original source input for better accuracy on model predictions. -------------------------------------------------------------------------------- |