Goat Detection and Counting Using YOLOv11
INTRODUCTION:
YOLOv11 is a computer vision model architecture developed by Ultralytics, the creators of the YOLOv5 and YOLOv8 models. YOLOv11 supports object detection, segmentation, classification, keypoint detection, and oriented bounding box (OBB) detection

CODE 😃👇:
import cv2
from ultralytics import YOLO
from google.colab.patches import cv2_imshow
# Load the YOLOv11 model (replace ‘/content/aa.pt’ with your model path)
model = YOLO(‘/content/aa.pt’)
# Load the custom image
image_path = ‘/content/m.jpg’ # Replace with your custom image path
image = cv2.imread(image_path)
# Perform detection
results = model.predict(source=image, conf=0.1) # Set confidence threshold
# Initialize goat count
goat_count = 0
# Process each detection result
for r in results:
for box in r.boxes:
class_id = int(box.cls[0]) # Class ID of the detected object
confidence = float(box.conf[0])
# Confidence score
# Check if the detection is for a goat and meets the confidence threshold
if class_id == 0 and confidence >= 0.1: # Assuming class_id for ‘goat’ is 0
goat_count += 1
x1, y1, x2, y2 = map(int, box.xyxy[0]) # Bounding box coordinates
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(image, f”Goat: {confidence:.2f}”, (x1, y1–10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
# Display the results
cv2.putText(image, f”Total Goats: {goat_count}”, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
cv2_imshow(image)
cv2.waitKey(0)
cv2.destroyAllWindows()
No comments:
Post a Comment