Hi All,
Im new to Pi and Hailo 8L. Mostly an Arduino & ESP32 guy, but after building my own Siren Detection System with Edge Impulse, I wanted to move into object recognition and thought its about time I started to learn.
I have managed to completely setup the Hailo 8L and my new PI and everything is working. What I need to do now is count the different COCO elements I have listed in my code as follows:
# Define custom labels to detect (edit as needed)
MY_LABELS = ["person", "car", "truck", "motorcycle", "bicycle", "cell phone"] # Modify this list with your chosen labels
# Define confidence threshold
CONFIDENCE_THRESHOLD = 0.95 # Adjust the confidence threshold as needed
Looking online, I am supposed to configure NVM’s which should allow me to count each COCO object detected, which led to this:
def app_callback(pad, info, user_data):
buffer = info.get_buffer()
if buffer is None:
return Gst.PadProbeReturn.OK
user_data.increment()
string_to_print = f"Frame count: {user_data.get_count()}\n"
# Get the video frame information
format, width, height = get_caps_from_pad(pad)
frame = None
if user_data.use_frame and format is not None and width is not None and height is not None:
frame = get_numpy_from_buffer(buffer, format, width, height)
# Get detections from the buffer
roi = hailo.get_roi_from_buffer(buffer)
detections = roi.get_objects_typed(hailo.HAILO_DETECTION)
detection_count = 0
for detection in detections:
label = detection.get_label()
bbox = detection.get_bbox()
confidence = detection.get_confidence()
# Filter detections by confidence level
if confidence >= CONFIDENCE_THRESHOLD:
xmin = int(bbox.xmin())
ymin = int(bbox.ymin())
xmax = int(bbox.xmax())
ymax = int(bbox.ymax())
# Update detection count for the label
user_data.update_detection_count(label)
# Draw bounding box and label on the frame
if user_data.use_frame:
cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
text = f"{label}: {confidence:.2f}"
cv2.putText(frame, text, (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
string_to_print += f"Detected {label} with confidence {confidence:.2f} at [{xmin}, {ymin}, {xmax}, {ymax}]\n"
detection_count += 1
# Print the detection counts
if user_data.use_frame:
cv2.putText(frame, f"Detections: {detection_count}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.putText(frame, f"{user_data.new_function()} {user_data.new_variable}", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
user_data.set_frame(frame)
for label, count in user_data.detection_counts.items():
string_to_print += f"Total {label} detections (>= {CONFIDENCE_THRESHOLD*100:.0f}% confidence): {count}\n"
print(string_to_print)
return Gst.PadProbeReturn.OK
if __name__ == "__main__":
user_data = user_app_callback_class()
app = GStreamerDetectionApp(app_callback, user_data)
app.run()
But I get dozens of counts when there is only one of me in the shot and simply adjusting the confidence level isnt accurate enough.
Does anyone have any code that I could plagiarise please?