-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from pipeless-ai/improve_examples
fix(examples): Improve yolo onnx example
- Loading branch information
Showing
3 changed files
with
125 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,50 @@ | ||
import cv2 | ||
import numpy as np | ||
|
||
def resize_rgb_frame(frame, target_dim): | ||
target_height = target_dim[0] | ||
target_width = target_dim[1] | ||
channels = target_dim[2] | ||
# Scale the image maintaining aspect ratio | ||
width_ratio = target_width / frame.shape[1] | ||
height_ratio = target_height / frame.shape[0] | ||
def is_cuda_available(): | ||
return cv2.cuda.getCudaEnabledDeviceCount() > 0 | ||
|
||
""" | ||
Resize and pad image. Uses CUDA when available | ||
""" | ||
def resize_and_pad(frame, target_dim, pad_top, pad_bottom, pad_left, pad_right): | ||
target_height, target_width = target_dim | ||
if is_cuda_available(): | ||
# FIXME: due to the memory allocation here could be even slower than running on CPU. We must provide the frame from GPU memory to the hook | ||
frame_gpu = cv2.cuda_GpuMat(frame) | ||
resized_frame_gpu = cv2.cuda.resize(frame_gpu, (target_width, target_height), interpolation=cv2.INTER_CUBIC) | ||
padded_frame_gpu = cv2.cuda.copyMakeBorder(resized_frame_gpu, pad_top, pad_bottom, pad_left, pad_right, cv2.BORDER_CONSTANT, value=(0, 0, 0)) | ||
result = padded_frame_gpu.download() | ||
return result | ||
else: | ||
resized_frame = cv2.resize(frame, (target_width, target_height), interpolation=cv2.INTER_CUBIC) | ||
padded_frame = cv2.copyMakeBorder(resized_frame, pad_top, pad_bottom, pad_left, pad_right, | ||
borderType=cv2.BORDER_CONSTANT, value=(0, 0, 0)) | ||
return padded_frame | ||
|
||
def resize_with_padding(frame, target_dim): | ||
target_height, target_width, _ = target_dim | ||
frame_height, frame_width, _ = frame.shape | ||
|
||
width_ratio = target_width / frame_width | ||
height_ratio = target_height / frame_height | ||
# Choose the minimum scaling factor to maintain aspect ratio | ||
scale_factor = min(width_ratio, height_ratio) | ||
# Calculate new dimensions after resizing | ||
new_width = int(frame.shape[1] * scale_factor) | ||
new_height = int(frame.shape[0] * scale_factor) | ||
new_width = int(frame_width * scale_factor) | ||
new_height = int(frame_height * scale_factor) | ||
# Calculate padding dimensions | ||
pad_width = (target_width - new_width) // 2 | ||
pad_height = (target_height - new_height) // 2 | ||
# Create a canvas with the desired dimensions and padding | ||
canvas = np.zeros((target_height, target_width, channels), dtype=np.uint8) | ||
# Resize the image and place it on the canvas | ||
resized_image = cv2.resize(frame, (new_width, new_height)) | ||
canvas[pad_height:pad_height+new_height, pad_width:pad_width+new_width] = resized_image | ||
return canvas | ||
|
||
def hook(frame_data, context): | ||
padded_image = resize_and_pad(frame, (new_height, new_width), pad_height, pad_height, pad_width, pad_width) | ||
return padded_image | ||
|
||
def hook(frame_data, _): | ||
frame = frame_data["original"].view() | ||
yolo_input_shape = (640, 640, 3) # h,w,c | ||
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | ||
frame = resize_rgb_frame(frame, yolo_input_shape) | ||
frame = cv2.normalize(frame, None, 0.0, 1.0, cv2.NORM_MINMAX) | ||
frame = resize_with_padding(frame, yolo_input_shape) | ||
frame = np.array(frame) / 255.0 # Normalize pixel values | ||
frame = np.transpose(frame, axes=(2,0,1)) # Convert to c,h,w | ||
inference_inputs = frame.astype("float32") | ||
frame_data['inference_input'] = inference_inputs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,8 @@ | ||
import numpy as np | ||
import time | ||
|
||
def hook(frame, context): | ||
rgb_frame = frame['original'] | ||
model = context['model'] | ||
input_fps = frame['fps'] | ||
delay = time.time() - frame['input_ts'] | ||
if input_fps > 0 and delay > 1 / input_fps: | ||
print('Skipping frame to maintain real-time') | ||
else: | ||
prediction = next(model(rgb_frame, stream=True)) | ||
bboxes = prediction.boxes.data.tolist() if prediction.boxes else [] | ||
frame['inference_output'] = np.array(bboxes, dtype="float32") | ||
prediction = next(model(rgb_frame, stream=True)) | ||
bboxes = prediction.boxes.data.tolist() if prediction.boxes else [] | ||
frame['inference_output'] = np.array(bboxes, dtype="float32") |