Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculation of Centroids from the Detection Results #6636

Closed
TehseenHasan opened this issue Feb 14, 2022 · 10 comments
Closed

Calculation of Centroids from the Detection Results #6636

TehseenHasan opened this issue Feb 14, 2022 · 10 comments
Labels

Comments

@TehseenHasan
Copy link

Hello, I see there are some people searching and asking here how to calculate the centroids from the detected objects. I have written some demo code for that purpose. If anyone has this requirement you can check my code. Thanks!

Link: https://github.com/TehseenHasan/yolo5-object-detection-and-centroid-finding

The output of my code is:
Result

@github-actions
Copy link
Contributor

github-actions bot commented Feb 14, 2022

👋 Hello @TehseenHasan, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email support@ultralytics.com.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

@TehseenHasan nice work!

@github-actions
Copy link
Contributor

github-actions bot commented Mar 17, 2022

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

Access additional Ultralytics ⚡ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐!

@manar-majdi
Copy link

hello @glenn-jocher
i work with this code but when i doesn't but the bounding box at the object detected (Sorry for bad english) :
import torch
import numpy as np
import cv2
import sys
import json
from ultralytics import YOLO

Load YOLOv8 model

model = YOLO("C:\Users\manar\OneDrive\Desktop\roboflow\runs\detect\train\weights\best.pt")

Check if model loaded successfully

if model is None:
print("Error: Model loading failed.")
sys.exit(1)

Configuring Model

model.conf = 0.25 # NMS confidence threshold
model.iou = 0.45 # NMS IoU threshold
model.agnostic = False # NMS class-agnostic
model.multi_label = False # NMS multiple labels per box
model.classes = None # (optional list) filter by class, i.e. = [0, 15, 16] for COCO persons, cats and dogs
model.max_det = 20 # maximum number of detections per image
model.amp = False # Automatic Mixed Precision (AMP) inference

Function to draw Centroids on the detected objects and return updated image

def draw_centroids_on_image(output_image, detections):
# Accessing each individual object and then getting its xmin, ymin, xmax, and ymax to draw bounding box and centroid
for detection in detections:
xmin, ymin, xmax, ymax, confidence = detection[:5] # Assuming first 5 elements represent bounding box coordinates and confidence

    # Draw bounding box
    cv2.rectangle(output_image, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (0, 255, 0), 2)

    # Centroid Coordinates of detected object
    cx = int((xmin + xmax) / 2)
    cy = int((ymin + ymax) / 2)

    # Draw centroid
    cv2.circle(output_image, (cx, cy), 2, (0, 0, 255), 2, cv2.FILLED)
    cv2.putText(output_image, f"({cx}, {cy})", (cx - 40, cy + 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)

return output_image

if name == "main":
# Start reading camera feed
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("Error: Failed to capture image")
break

    image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)  # OpenCV image (BGR to RGB)

    # Inference
    results = model(image)  # includes NMS

    all_detections = []
    for res in results:
        # Accessing bounding boxes and confidence scores directly from the Results object
        boxes = res.boxes.xyxy.cpu().numpy()  # Extract bounding box coordinates
        confidences = res.boxes.conf.cpu().numpy()  # Extract confidence scores
        for box, confidence in zip(boxes, confidences):
            xmin, ymin, xmax, ymax = box
            all_detections.append([xmin, ymin, xmax, ymax, confidence])

    # Render detections on image
    output_image = draw_centroids_on_image(frame.copy(), all_detections)

    cv2.imshow("Output", output_image)  # Show the output image after rendering

    # Exit by pressing 'q' key
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows() 

@manar-majdi
Copy link

manar-majdi commented May 9, 2024

i want with this code to give me the possition of the object detected i worked yolov8 .
please help me .... i need it

@glenn-jocher
Copy link
Member

Hi there! If you're using YOLOv8 and want to extract the position of the detected objects, your approach seems almost correct. However, it appears there might be a misunderstanding in accessing the output.

Here's a corrected snippet on how you might process the detection results to extract positions:

results = model(image)  # Perform inference
detections = results.xyxy[0]  # Extract detections; xyxy returns [xmin, ymin, xmax, ymax, confidence, class]

for detection in detections:
    xmin, ymin, xmax, ymax, conf, cls = detection
    cx = (xmin + xmax) / 2
    cy = (ymin + ymax) / 2
    # Now you can use cx, cy as the centroid coordinates

Ensure that you convert the tensor to numpy if necessary and handle the data types appropriately. This should give you the centroid positions of the bounding boxes. If you encounter any further issues, feel free to ask! Happy coding! 😊

@manar-majdi
Copy link

manar-majdi commented May 13, 2024

hello @glenn-jocher , i am work in arm robot (pick and place ) with 4 DOF how can i use this value to change it to an order to pick the object detected ( i work it with yolov8)

@glenn-jocher
Copy link
Member

Hello! To integrate YOLOv8 detections with your 4-DOF robot for a pick-and-place task, you'll need to translate the detected object's centroid coordinates into robot arm commands.

Here's a simplified approach:

  1. Calculate the centroid using cx = (xmin + xmax) / 2 and cy = (ymin + ymax) / 2 from the detection bounding box.
  2. Convert these image coordinates cx and cy into real-world coordinates (considering camera calibration, if necessary).
  3. Based on your robot's workspace and kinematics, map these real-world coordinates to robot joint angles or positions.

Sample code snippet:

results = model(image)
detections = results.xyxy[0]  # Detections with [xmin, ymin, xmax, ymax, confidence, class]

for detection in detections:
    xmin, ymin, xmax, ymax, conf, cls = detection
    # Calculate centroid
    cx = (xmin + xmax) / 2
    cy = (ymin + ymax) / 2
    # Convert to real-world coordinates (assuming simple scale factor for demo)
    real_x = scale_factor * cx
    real_y = scale_factor * cy
    # Generate robot arm commands based on converted coordinates
    robot_arm.move_to(real_x, real_y)

Make sure to replace scale_factor with the actual calculation/conversion method you choose!

Feel free to reach out if you need more detailed help or specific adjustments. Happy building! 😊

@manar-majdi
Copy link

hello @glenn-jocher, u good ?I used this code to measure the angle to my detected object, but it seems to be producing inaccurate values. What can I do to correct it?
import cv2
from ultralytics import YOLO

Set custom confidence threshold for the 'person' class

confidence_threshold = 0.1

Load yolov8 model

model = YOLO("C:\Users\manar\OneDrive\Desktop\roboflow\runs\detect\train\weights\best.pt")

Load webcam

cap = cv2.VideoCapture(0) # 0 for the default webcam; adjust the index if you have multiple webcams

Camera-specific parameters (replace with your actual values)

horizontal_fov_degrees = 90
vertical_fov_degrees = 100

Main processing loop

while True:
ret, frame = cap.read()

if not ret:
    break  # End of video

# Resize frame (optional)
frame = cv2.resize(frame, (960, 540))

# Object detection
results = model.predict(frame)

# Filter detections (optional)
filtered_results = []
for r in results:
    for box in r.boxes.xyxy:  # Assuming 'box' is in [x1, y1, x2, y2] format
        centroid_x = int((box[0] + box[2]) / 2)
        centroid_y = int((box[1] + box[3]) / 2)

        # Convert pixel coordinates to degrees
        x_degrees = (centroid_x / frame.shape[1]) * horizontal_fov_degrees
        y_degrees = (centroid_y / frame.shape[0]) * vertical_fov_degrees

        print(f"Centroid Coordinates (Degrees): ({x_degrees:.2f}, {y_degrees:.2f})")

        # Draw bounding box
        cv2.rectangle(frame, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (0, 255, 0), 2)
        
        # Display centroid coordinates
        cv2.putText(frame, f"({x_degrees:.2f}, {y_degrees:.2f})", (centroid_x, centroid_y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

# Display frame with bounding boxes and centroids
cv2.imshow('frame', frame)

# Exit condition
if cv2.waitKey(25) & 0xFF == ord('q'):
    break

Release resources

cap.release()
cv2.destroyAllWindows()

@glenn-jocher
Copy link
Member

Hello! If you're experiencing inaccuracies in angle measurement from your detected object, here are a few things you might consider checking or adjusting:

  1. Camera Calibration: Ensure your camera's field of view (FOV) and other intrinsic parameters are accurately defined. Errors in these parameters can lead to significant discrepancies in angle calculations.

  2. Accuracy of Detection: Low confidence thresholds can lead to inaccurate or noisy detections. Try increasing your confidence_threshold to filter out less certain detections.

  3. Frame Resizing: When you resize the frame, make sure to adjust FOV values accordingly or recalibrate them based on the new resolution to keep scaling consistent.

Here’s a tiny refactor to ensure FOV adjustments based on resized frame dimensions:

x_degrees = (centroid_x / 960) * horizontal_fov_degrees  # Use resized width
y_degrees = (centroid_y / 540) * vertical_fov_degrees    # Use resized height

Make these adjustments and see if your angle measurements improve! 😊 If you’re still facing issues, ensure the centroid calculation alignment with the detection bounding box is accurate and consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants