Retrieving object detection prediction in Custom Python Block in Roboflow Workflows

I want to retrieve prediction from my object detection model such as “detected class name and confidence” in the custom oython block. I am trying example from How to Build a Computer Vision App using Workflows and Custom Python Blocks as below:

def run(self, detections) -> BlockResult:
       classname = detections.data.get("class", [])
       return { "result" : classname}

but empty block result is returned.

I got it working. Just posting code for community. The following code gets the class_name and confidence score from object detection model.

def run(self, detections) -> BlockResult:
    # Extract class names from the detections
    class_names = detections.data.get("class_name", [])
    # Extract confidence scores
    confidence_scores = detections.confidence

    # Convert class_names to a regular list if it's in array format
    class_names_list = class_names.tolist() if isinstance(class_names, np.ndarray) else class_names
    confidence_scores_list = confidence_scores.tolist() if isinstance(confidence_scores, np.ndarray) else confidence_scores

    # Combine class names with their corresponding confidence scores
    results = [{"class_name": class_name, "confidence": confidence} 
               for class_name, confidence in zip(class_names_list, confidence_scores_list)]

    # Print the results for debugging
    print("Detected class names and confidence scores:", results)

    # Return the result
    return {"response": results}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.