yolov8-seg-onnxruntime-web
yolov8-seg-onnxruntime-web copied to clipboard
Get pixel coordinates
Hello i work your project, it works, thanks but i cant get pixel coordinates. My code on colab is below
from ultralytics import YOLO
# Load a pretrained YOLOv8n model
model = YOLO('yolov8x-seg.pt')
# Run inference on 'bus.jpg'
results = model('../images/31_set1-middle.jpg') # results list
# Show the results
for r in results:
print(r.masks.xy)
# print(r.boxes)
im_array = r.plot() # plot a BGR numpy array of predictions
im = Image.fromarray(im_array[..., ::-1]) # RGB PIL image
im.show() # show image
im.save('results-seg-x-middle.jpg') # save image
In this code i take detected object's pixel coordinates that present border but how can i take in your project these coordinates? Can you help me?
Hey, if by pixel coordinates you mean the bounding box coordinates, you can access them using the code below:
from PIL import Image
from ultralytics import YOLO
# Load a pretrained YOLOv8n model
model = YOLO('yolov8x-seg.pt')
# Run inference on 'bus.jpg'
results = model('../images/31_set1-middle.jpg') # results list
# Show the results
for r in results.xyxy[0]:
# Extract bounding box coordinates
x1, y1, x2, y2 = r[:4]
# Print pixel coordinates
print(f"Top-left: ({x1}, {y1}), Bottom-right: ({x2}, {y2})")
# Optional: Draw bounding box on the image
r.render() # Draw bounding box on the image
# Save the annotated image
results.save('results-seg-x-middle.jpg') # save image
In this code, results.xyxy[0] gives you the detected objects' bounding boxes in the format [x1, y1, x2, y2, conf, cls]. You can then extract the top-left (x1, y1) and bottom-right (x2, y2) coordinates of each bounding box and print them out. Optionally, you can also draw the bounding boxes on the image using r.render(). Finally, you can save the annotated image using results.save().
I want to access object's edge coordinates after doing segmentation in react app for area calculation. For example; i detected and did segmentation in first image and then i need to take red line coordinates in second image . Have you ever do this before?
Hey @teengineer, what backend framework are you using? You'll need to perform object detection and segmentation on the server-side using a Python library like the one you're using, such as YOLO or any other segmentation model.
Then you can expose an API endpoint on your backend server that accepts an image file and returns the segmented image along with the coordinates of the edges of the segmented objects.
hi @gauritomar i use asp net core for backend but i think i can solve to take coordinates in frontend with react not backend.
Hello,
I think that's a good idea to solve it on the frontend but the computation of every device isn't the same so you must take attention of that.
For the mask (pixel coordinate) you can easily access the overlay variable on utils/detect.js.
https://github.com/Hyuto/yolov8-seg-onnxruntime-web/blob/2f404048359f26bc7d00f80e9a6f10e3b19b8ced/src/utils/detect.js#L49
The concept is stacking the first mask to the last. If you wanted to process every object one by one i suggest to reset the overlay with zero for every process on the loop.
Hope that helps
Thank you @Hyuto, may you show me how can access pixel coordinates on detect.js?