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

Change images produced by segment/predict.py #12908

Closed
1 task done
osamamer opened this issue Apr 11, 2024 · 2 comments
Closed
1 task done

Change images produced by segment/predict.py #12908

osamamer opened this issue Apr 11, 2024 · 2 comments
Labels
question Further information is requested Stale

Comments

@osamamer
Copy link

Search before asking

Question

Hello,
I'm trying to change the segment/predict,py file in order to make it produce a black-and-white image, the white portion of which is the detections, and everything else is black. This requires me to change the opacity of the image to be fully opaque, which I'm having trouble figuring out. I tried changing the alpha parameter in the Annotator's masks function, but that raises a tensor dimension mismatch error. What changes do I need to make to the predict file to get the required output?

Additional

No response

@osamamer osamamer added the question Further information is requested label Apr 11, 2024
@glenn-jocher
Copy link
Member

@osamamer hello! 👋 It sounds like you're working on a custom output for your segmentation tasks using YOLOv5. To achieve a black-and-white image where detections are white and the rest is black, you indeed need to modify how the mask is applied and how the final image is constructed.

Here's a simplified approach:

  1. After detection, instead of using the Annotator's mask function to overlay colored masks, you'll directly work with the detection results to create a binary mask. For each detected object, fill the corresponding area with white (value = 255) on a black (value = 0) background.
  2. Ensure the image you're starting with is a black numpy array of the same dimensions as your input image, but single channel for BW.

Here is a conceptual snippet:

import numpy as np

# Assuming 'results' is your detection output from model
img_bw = np.zeros((results.imgs[0].shape[0], results.imgs[0].shape[1]), dtype=np.uint8)  # Creating BW base

for det in results.xyxy[0]:  # Loop through detections
    bbox = [int(x) for x in det[:4]]  # Convert to int
    img_bw[bbox[1]:bbox[3], bbox[0]:bbox[2]] = 255  # Fill detected areas with white

# 'img_bw' now is your black-white image as desired

This code snippet assumes that your model's detections are stored in results and that results.xyxy[0] contains the bounding boxes for the detections in the format [x1, y1, x2, y2, confidence, class]. Adjust your array indexing as necessary depending on your output structure. This will provide a simplistic start towards generating a B&W mask where detections are highlighted.

Feel free to tweak this further based on your project's specific requirements. Happy coding! 🚀

Copy link
Contributor

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

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 YOLO 🚀 and Vision AI ⭐

@github-actions github-actions bot added the Stale label May 13, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale
Projects
None yet
Development

No branches or pull requests

2 participants