in agora/contoso_motors/src/webapp-decode/yolov8.py [0:0]
def postprocess(self, input_image, output):
self.log("Postprocessing the output...")
# Transpose and squeeze the output to match the expected shape
outputs = np.transpose(np.squeeze(output[0]))
# Get the number of rows in the outputs array
rows = outputs.shape[0]
# Lists to store the bounding boxes, scores, and class IDs of the detections
boxes = []
scores = []
class_ids = []
# Calculate the scaling factors for the bounding box coordinates
x_factor = self.img_width / self.input_width
y_factor = self.img_height / self.input_height
# Iterate over each row in the outputs array
for i in range(rows):
# Extract the class scores from the current row
classes_scores = outputs[i, 4:]
# Find the maximum score among the class scores
max_score = np.max(classes_scores)
# If the maximum score is above the confidence threshold
if max_score >= self.confidence_thres:
# Get the class ID with the highest score
class_id = np.argmax(classes_scores)
# Extract the bounding box coordinates from the current row
x, y, w, h = outputs[i, 0:4]
# Calculate the scaled coordinates of the bounding box
left = int((x - w / 2) * x_factor)
top = int((y - h / 2) * y_factor)
width = int(w * x_factor)
height = int(h * y_factor)
# Add the class ID, score, and box coordinates to the respective lists
class_ids.append(class_id)
scores.append(max_score)
boxes.append([left, top, width, height])
# Apply non-maximum suppression to filter out overlapping bounding boxes
indices = cv2.dnn.NMSBoxes(boxes, scores, self.confidence_thres, self.iou_thres)
# Check if indices are returned as a numpy array and access them correctly
if len(indices) > 0:
indices = indices.flatten() # This ensures indices are flattened properly
elif(self.verbose):
print("No boxes to display after NMS.")
# Prepare data for tabulate
table_data = []
# Iterate over the selected indices after non-maximum suppression
for i in indices:
# Get the box, score, and class ID corresponding to the index
box = boxes[i]
score = scores[i]
class_id = class_ids[i]
# Draw the detection on the input image
self.draw_detections(input_image, box, score, class_id)
table_data.append([i, box, score, self.class_names[class_id]])
# Draw the FPS counter on the image
self.draw_fps(input_image)
# Print the table
# headers = ["Index", "Box", "Score", "Class"]
#self.log(str(tabulate(table_data, headers=headers, tablefmt="grid")))
# Return the modified input image
return input_image