Exciting things you can build with the new UbiOps operators
#1 Parallel processing of video frames
On the 24th of November, we introduced pipeline operators in UbiOps. Pipeline operators are a library of pipeline objects that you can use to quickly create pipelines for more complex use cases. They can help to make certain branches of your pipeline execute only if specific conditions are met, allow you to parallelize parts of your pipeline, and so much more.
To give you an idea of what’s possible I will walk you through a complete example use case today that makes use of the create subrequests and the collect subrequests operators specifically. Let’s dive in!
The example use case: video processing
Let’s have a look at a video processing use case. In this case we have an input video that contains some hand motions, and we want to end up with a processed video where all the hand positions are marked like this:
The set-up
In order to achieve this I came up with the following workflow:
- Split the video into individual frames
- Process each frame with a hand gesture recognition algorithm
- Combine the processed frames back together into a full video
That translates to three different deployments:
- A video-splitter deployment that returns a list of frames
- A frame-processor deployment
- A video-compiler that compiles the processed video
Since we will need to process a bunch of frames in exactly the same way, I want to make sure that multiple frames are processed in parallel, making use of UbiOps’s autoscaling functionality. This will greatly speed things up! I can do that by using the create subrequests, and the collect subrequests operators. The final pipeline I want should then look something like this:
The create subrequests operator will split out the returned list of frames from the video splitter into separate subrequests to be processed by the frame-processor. If I set my max_instances from the frame-processor to a number that is higher than 1, let’s say 10, these subrequests can be handled in parallel.
Note: The amount of parallelization is equal to the maximum number of instances of the frame-processor. If you want to use parallelization make sure to increase the number of max instances!
To be able to combine all the processed frames into a single video again with the video-compiler, it of course needs access to the full list of processed frames. That is exactly what the collect subrequests operator does. It collects all the individual subrequests and forwards it to the video-compiler as a single list.
Splitting the video into frames
To split the video into frames we can use opencv. I simply create a list that will hold all the frames and use opencv to read all the individual frames from the video and add them to the list. My code looks like this:
The deployment has the following input/output definition:
- Input:
video of type blob - Output:
frame of type blob
Please note that I return a list of dictionaries, as opposed to a single dictionary as you normally would in a UbiOps deployment. The reason for this is that I want to use the create subrequests operator to split this list into individual subrequests. Every dictionary in the list still needs to be formatted according to the output format from the deployment, which in my case is a single output field called frame. I do not need to change the output data type to be an array of some kind, as long as each dictionary in my list is formatted correctly.
Processing a frame
Our second deployment should process a video frame to detect hands and draw them on the frame. I again use opencv but this time in combination with mediapipe for their built-in hand detection functions. My code looks like this:
There’s nothing too special happening in this code and it could of course be swapped with any kind of image processing code that you need for your use case.
The input/output definition of this deployment is:
- Input:
frame of type blob - Output:
processed_frame of type blob
Since we want this frame processor to work through frames in parallel it is very important to set the maximum number of instances of this deployment to a higher number than 1, I used 10.
Compiling the processed video
Now that we have some frame processing code it’s time to look at putting all these processed frames back together to a full video. My code looks like this:
You might notice that I used a function called requests instead of the standard request function. This is because I want to work with a full list of frames and compile them together in one go. If the API detects the requests function and there are subrequests being sent to that deployment, the API will send those subrequests over as a full list, instead of sending one item at a time. If I would use the standard request method, I would only have access to one processed frame in my code, which wouldn’t be very helpful. Right now the passed data parameter is a list of dictionaries, each dictionary containing a processed frame.
The input/output definition of this deployment is:
- Input:
processed_frame of type blob - Output:
processed_video of type blob
Putting everything together in a UbiOps pipeline
Now that we have our three main deployments, it’s time to put them together in a pipeline. We need a pipeline with the following input/output definition:
The input/output definition of this deployment is:
- Input:
video of type blob - Output:
processed_video of type blob
We can start by going to edit mode and adding all our deployments onto our pipeline canvas.
Now that we have all our deployments in, we can start from left to right and add the connections and operators we need as we go. First up is connecting pipeline start to the video-splitter object. After that we can add in the create subrequest operator from the sidebar (you can see the operators by going to the operators tab). Once added to your pipeline you will be prompted to provide a source object, in our case that’s the video-splitter object. You can also adjust the batch size, which dictates how many parallel batches there are. As a rule of thumb you can set the batch size equal to the number of expected items divided by the maximum number of instances. In our case I had about 200 frames, and max instances set to 10, so I used batch size 20.
When you now click save you should see your operator on the canvas, properly connected to the video-splitter You can connect the operator to the frame-processor via drag and drop and then your pipeline should look like this:
We also need to collect all the subrequests again before passing the processed frames to the video-compiler. To do this we need to add a collect subrequests operator. When you add it to your canvas you will need to provide a destination object, in this case, that’s the video-compiler. When you press save you should see it on your canvas correctly connected to the video-compiler. You just need to add a connection between the frame-processor and the collect subrequests operator still, and a final connection from the video-compiler to the pipeline end to also output the processed video properly.
With this final step out of the way your pipeline is now complete! Just hit save and exit the editor and it’s ready for use.
Using the pipeline
To use your newly created video processing pipeline you can create a request with a small input video on which you can see hands. I personally created a quick recording with my webcam to test it with and it worked well!
I hope this gave you inspiration on what to do with the create and collect subrequests operators. If you have any questions, do not hesitate to reach out to us via email or slack!
Make sure you check out the release notes for the other operators we added.
The full deployment packages for each deployment can be downloaded from here: