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

Unable to Detect faces in single face image and giving false positives #13089

Open
1 of 2 tasks
Raghucharan16 opened this issue Jun 13, 2024 · 7 comments
Open
1 of 2 tasks
Labels
bug Something isn't working Stale

Comments

@Raghucharan16
Copy link

Search before asking

  • I have searched the YOLOv5 issues and found no similar bug report.

YOLOv5 Component

Detection

Bug

I know this sound silly but I'm stuck at face detection using yolov5. The algorithm works fine for some images with multiple faces with great accuracy. but if we feed single face images, [only one face in image it is not detecting the face and also giving false positive which is not a face. [for another person's face image, i got this image as output]

face_1

here are the examples;

Input Image:

image(2)(1)

Output Image for code below:

image(3)(1)

Is there any processing is to be done to image? and in result you can see rotated image given by yolo.
and below is another example which it perfectly works with same code.

Input Image:

image(6)(1)

Output Image:

image(5)(1)

Environment

  • YOLO- YOLOv5 CUDA:0,CPU:i7-1270P, 16GB RAM
  • OS - Ubuntu-24.04LTS
  • Python - Python 3.11.6

Minimal Reproducible Example

import os
import logging
import cv2
from PIL import Image, ImageDraw


def upscale_faces_with_boxes(input_image_path, output_image_path, a, confidence_threshold=0.5):
    """
    Detect faces in an image using YOLO, draw bounding boxes around them, and save the resulting image.

    :param input_image_path: Path to the input image
    :param output_image_path: Path to the output image where the result will be saved
    :param a: An instance of YOLODetector already initialized
    :param confidence_threshold: Minimum confidence required to consider a detected face valid (default is 0.5)
    """
    # Read the input image
    img = cv2.imread(input_image_path)
    if img is None:
        logging.error(f"Could not read image {input_image_path}")
        return

    # Detect faces using YOLO
    bounding_boxes = a.predict(img)[0]
    logging.info('facial features: %s', bounding_boxes)

    # Load the original image using PIL
    original_image = Image.open(input_image_path)
    if original_image.mode == 'RGBA':
        original_image = original_image.convert('RGB')

    # Draw bounding boxes on the original image
    draw = ImageDraw.Draw(original_image)
    for bbox_group in bounding_boxes:
        for bbox in bbox_group:
            left, top, right, bottom = bbox
            draw.rectangle([left, top, right, bottom], outline="red", width=3)

    # Save the image with bounding boxes
    original_image.save(output_image_path)
    logging.info(f"Image with bounding boxes saved to {output_image_path}")
input_image_path = 'IMG_20240613_165627217.jpg'
output_image_path = 'outputfolder/image_with_boxes.jpg'
from yoloface_master.YOLOFaceDetector import YoloDetector
a = YoloDetector()
upscale_faces_with_boxes(input_image_path, output_image_path, a)

Additional

It is not working with single faced images and i tried a lot by adjusting parameters in yolo detector initialiser. is there a fix for this??

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!
@Raghucharan16 Raghucharan16 added the bug Something isn't working label Jun 13, 2024
Copy link
Contributor

👋 Hello @Raghucharan16, 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 a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Requirements

Python>=3.8.0 with all requirements.txt installed including PyTorch>=1.8. 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

YOLOv5 CI

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

Introducing YOLOv8 🚀

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 🚀!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics

@glenn-jocher
Copy link
Member

@Raghucharan16 hello,

Thank you for your detailed report and for providing the minimal reproducible example. This is very helpful! Let's work through this issue together.

Steps to Verify

  1. Ensure Latest Versions: First, please make sure you are using the latest versions of torch and YOLOv5. You can update them using the following commands:

    pip install --upgrade torch
    pip install --upgrade git+https://github.com/ultralytics/yolov5.git
  2. Reproduce the Issue: If the issue persists after updating, we need to ensure that we can reproduce it. The code snippet you provided is quite comprehensive, but it would be helpful to have the exact version of the YOLO model you are using (YOLOv5s, YOLOv5m, etc.) and any specific configurations or weights you have applied.

Potential Solutions

  1. Model Confidence Threshold: Sometimes, adjusting the confidence threshold can help in detecting faces more accurately. You can try lowering the confidence_threshold parameter in your upscale_faces_with_boxes function to see if it improves detection for single-face images.

  2. Image Preprocessing: Ensure that the input images are preprocessed correctly. Sometimes, resizing or normalizing the images before feeding them into the model can improve detection accuracy.

  3. Bounding Box Post-Processing: Verify the bounding box coordinates and ensure they are within the image dimensions. Sometimes, incorrect bounding box values can lead to false positives or missed detections.

Example Code Adjustment

Here's an example of how you might adjust the confidence threshold and add some preprocessing steps:

import os
import logging
import cv2
from PIL import Image, ImageDraw
import numpy as np

def upscale_faces_with_boxes(input_image_path, output_image_path, a, confidence_threshold=0.3):
    # Read the input image
    img = cv2.imread(input_image_path)
    if img is None:
        logging.error(f"Could not read image {input_image_path}")
        return

    # Preprocess the image (resize and normalize)
    img_resized = cv2.resize(img, (640, 640))
    img_normalized = img_resized / 255.0

    # Detect faces using YOLO
    bounding_boxes = a.predict(img_normalized)[0]
    logging.info('facial features: %s', bounding_boxes)

    # Load the original image using PIL
    original_image = Image.open(input_image_path)
    if original_image.mode == 'RGBA':
        original_image = original_image.convert('RGB')

    # Draw bounding boxes on the original image
    draw = ImageDraw.Draw(original_image)
    for bbox_group in bounding_boxes:
        for bbox in bbox_group:
            if bbox[4] >= confidence_threshold:  # Check confidence score
                left, top, right, bottom = bbox[:4]
                draw.rectangle([left, top, right, bottom], outline="red", width=3)

    # Save the image with bounding boxes
    original_image.save(output_image_path)
    logging.info(f"Image with bounding boxes saved to {output_image_path}")

input_image_path = 'IMG_20240613_165627217.jpg'
output_image_path = 'outputfolder/image_with_boxes.jpg'
from yoloface_master.YOLOFaceDetector import YoloDetector
a = YoloDetector()
upscale_faces_with_boxes(input_image_path, output_image_path, a)

Next Steps

  1. Update your packages and try running the adjusted code.
  2. Share any error messages or unexpected outputs you encounter after making these changes.

If the issue persists, please provide more details about the specific YOLO model and weights you are using. This will help us further diagnose the problem.

Thank you for your patience and cooperation. The YOLO community and the Ultralytics team are here to help!

@Raghucharan16
Copy link
Author

Hey @glenn-jocher Thanks for the response, I'll try with adjusting threshold and let you know the results.

@glenn-jocher
Copy link
Member

Hello @Raghucharan16,

Thank you for your prompt response! Adjusting the confidence threshold is a great first step. It can often help in fine-tuning the model's sensitivity to detect faces more accurately.

Next Steps

  1. Adjust Confidence Threshold: As mentioned, try lowering the confidence threshold in your detection function. This can help the model pick up on faces that it might otherwise miss.

  2. Update Packages: Ensure you are using the latest versions of torch and YOLOv5. This can be done with the following commands:

    pip install --upgrade torch
    pip install --upgrade git+https://github.com/ultralytics/yolov5.git
  3. Image Preprocessing: Consider adding preprocessing steps like resizing and normalizing the input images. This can sometimes improve detection accuracy.

Example Code Adjustment

Here's a quick example of how you might adjust the confidence threshold and add some preprocessing steps:

import os
import logging
import cv2
from PIL import Image, ImageDraw
import numpy as np

def upscale_faces_with_boxes(input_image_path, output_image_path, a, confidence_threshold=0.3):
    # Read the input image
    img = cv2.imread(input_image_path)
    if img is None:
        logging.error(f"Could not read image {input_image_path}")
        return

    # Preprocess the image (resize and normalize)
    img_resized = cv2.resize(img, (640, 640))
    img_normalized = img_resized / 255.0

    # Detect faces using YOLO
    bounding_boxes = a.predict(img_normalized)[0]
    logging.info('facial features: %s', bounding_boxes)

    # Load the original image using PIL
    original_image = Image.open(input_image_path)
    if original_image.mode == 'RGBA':
        original_image = original_image.convert('RGB')

    # Draw bounding boxes on the original image
    draw = ImageDraw.Draw(original_image)
    for bbox_group in bounding_boxes:
        for bbox in bbox_group:
            if bbox[4] >= confidence_threshold:  # Check confidence score
                left, top, right, bottom = bbox[:4]
                draw.rectangle([left, top, right, bottom], outline="red", width=3)

    # Save the image with bounding boxes
    original_image.save(output_image_path)
    logging.info(f"Image with bounding boxes saved to {output_image_path}")

input_image_path = 'IMG_20240613_165627217.jpg'
output_image_path = 'outputfolder/image_with_boxes.jpg'
from yoloface_master.YOLOFaceDetector import YoloDetector
a = YoloDetector()
upscale_faces_with_boxes(input_image_path, output_image_path, a)

Additional Resources

For further assistance, you can refer to our YOLOv5 documentation which provides comprehensive guides and examples.

Please let us know how it goes after making these adjustments. If the issue persists, feel free to share any additional details or outputs. The YOLO community and the Ultralytics team are here to support you!

@Raghucharan16
Copy link
Author

Hey @glenn-jocher This is the repo i refered to for yolov5 face detection. and i tried adjusting the threshold as above and also tried lowering to 0.1 and results are same. like the same picture is getting saved without bounding boxes.

@glenn-jocher
Copy link
Member

Hello @Raghucharan16,

Thank you for sharing the repository link and for trying the suggested adjustments. I appreciate your efforts in troubleshooting the issue.

Next Steps

  1. Minimum Reproducible Example: To help us better understand and reproduce the issue, could you please provide a minimal reproducible code example? This will enable us to investigate the problem more effectively. You can refer to our guide on creating a minimum reproducible example for more details.

  2. Ensure Latest Versions: Please verify that you are using the latest versions of torch and YOLOv5. You can update them using the following commands:

    pip install --upgrade torch
    pip install --upgrade git+https://github.com/ultralytics/yolov5.git

Additional Suggestions

  1. Model Weights: Ensure that you are using the correct model weights for face detection. Sometimes, using pre-trained weights specifically fine-tuned for face detection can yield better results.

  2. Debugging Output: Add some debugging output to check the bounding boxes and confidence scores returned by the model. This can help identify if the issue lies in the detection step or the post-processing step.

Here’s an example of how you might add debugging output:

import os
import logging
import cv2
from PIL import Image, ImageDraw
import numpy as np

def upscale_faces_with_boxes(input_image_path, output_image_path, a, confidence_threshold=0.3):
    # Read the input image
    img = cv2.imread(input_image_path)
    if img is None:
        logging.error(f"Could not read image {input_image_path}")
        return

    # Preprocess the image (resize and normalize)
    img_resized = cv2.resize(img, (640, 640))
    img_normalized = img_resized / 255.0

    # Detect faces using YOLO
    bounding_boxes = a.predict(img_normalized)[0]
    logging.info('facial features: %s', bounding_boxes)

    # Debugging output
    print(f"Detected bounding boxes: {bounding_boxes}")

    # Load the original image using PIL
    original_image = Image.open(input_image_path)
    if original_image.mode == 'RGBA':
        original_image = original_image.convert('RGB')

    # Draw bounding boxes on the original image
    draw = ImageDraw.Draw(original_image)
    for bbox_group in bounding_boxes:
        for bbox in bbox_group:
            if bbox[4] >= confidence_threshold:  # Check confidence score
                left, top, right, bottom = bbox[:4]
                draw.rectangle([left, top, right, bottom], outline="red", width=3)

    # Save the image with bounding boxes
    original_image.save(output_image_path)
    logging.info(f"Image with bounding boxes saved to {output_image_path}")

input_image_path = 'IMG_20240613_165627217.jpg'
output_image_path = 'outputfolder/image_with_boxes.jpg'
from yoloface_master.YOLOFaceDetector import YoloDetector
a = YoloDetector()
upscale_faces_with_boxes(input_image_path, output_image_path, a)

Community and Support

The YOLO community and the Ultralytics team are here to support you. If you continue to experience issues, please provide the requested details, and we'll do our best to assist you further.

Thank you for your patience and cooperation!

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 Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Stale
Projects
None yet
Development

No branches or pull requests

2 participants