Giter VIP home page Giter VIP logo

Comments (6)

github-actions avatar github-actions commented on June 24, 2024

πŸ‘‹ Hello @codinglearningnovice, thank you for your interest in Ultralytics YOLOv8 πŸš€! We recommend a visit to the Docs for new users where you can find many Python and CLI usage examples and where many of the most common questions may already be answered.

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.

Join the vibrant Ultralytics Discord 🎧 community for real-time conversations and collaborations. This platform offers a perfect space to inquire, showcase your work, and connect with fellow Ultralytics users.

Install

Pip install the ultralytics package including all requirements in a Python>=3.8 environment with PyTorch>=1.8.

pip install ultralytics

Environments

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

Status

Ultralytics CI

If this badge is green, all Ultralytics CI tests are currently passing. CI tests verify correct operation of all YOLOv8 Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

from ultralytics.

glenn-jocher avatar glenn-jocher commented on June 24, 2024

@codinglearningnovice hello,

Thank you for reaching out and providing detailed information about your issue. It looks like you're encountering an error because the results object is a list of Results objects, and you're trying to access the masks attribute directly from the list.

To resolve this, you need to iterate over the results list and then access the masks attribute from each Results object. Here's a modified version of your code snippet that should work:

while(count < TRAIN_SIZE):
    try:
        ret, frame = cap.read()

        if currentFrame % FRAME_SKIP == 0:
            count += 1
            if count % int(TRAIN_SIZE/10) == 0:
                print(str((count/TRAIN_SIZE)*100) + "% done")

            # Perform human segmentation
            results = model(frame)

            for result in results:
                person_masks = result.masks[result.boxes.cls == 0]
                person_mask_3ch = cv2.cvtColor(person_masks, cv2.COLOR_GRAY2BGR)
                masked_frame = cv2.bitwise_and(frame, person_mask_3ch)

                inverted_mask = cv2.bitwise_not(person_mask_3ch)

                result_frame = cv2.bitwise_and(masked_frame, inverted_mask)

                resized_frame = cv2.resize(result_frame, (output_width, output_height))

                name = 'trydata/resized_frame.jpg' + str(count) + '.jpg'

                cv2.imwrite(name, resized_frame)

                video.write(resized_frame.astype('uint8'))

    except Exception as e:
        print(e)
        break

    currentFrame += 1

print(str(count) + " Frames collected")
cap.release()
video.release()

Additionally, please ensure that you are using the latest versions of torch and ultralytics. You can upgrade them using the following commands:

pip install --upgrade torch ultralytics

If the issue persists, please provide a minimum reproducible example so we can investigate further. You can find more details on how to create one here.

I hope this helps! If you have any further questions, feel free to ask. 😊

from ultralytics.

codinglearningnovice avatar codinglearningnovice commented on June 24, 2024

thanks for your reply, tried this, it doesnt give me the result, it issues this error below

0: 384x640 1 person, 190.0ms
Speed: 3.1ms preprocess, 190.0ms inference, 3.8ms postprocess per image at shape (1, 3, 384, 640)
OpenCV(4.8.0) πŸ‘Ž error: (-5:Bad argument) in function 'cvtColor'

Overload resolution failed:

  • src is not a numpy array, neither a scalar
  • Expected Ptrcv::UMat for argument 'src'

1 Frames collected

am i doing something wrongly?

from ultralytics.

glenn-jocher avatar glenn-jocher commented on June 24, 2024

Hello @codinglearningnovice,

Thank you for your update. It looks like the error you're encountering is related to the cvtColor function from OpenCV, which expects a numpy array but is receiving a different type.

To help us investigate further, could you please provide a minimum reproducible example of your code? This will allow us to reproduce the issue on our end and find a solution more effectively. You can find guidelines on how to create one here.

In the meantime, let's ensure that the person_masks variable is indeed a numpy array before passing it to cvtColor. Here’s a revised snippet that includes a check:

while(count < TRAIN_SIZE):
    try:
        ret, frame = cap.read()

        if currentFrame % FRAME_SKIP == 0:
            count += 1
            if count % int(TRAIN_SIZE/10) == 0:
                print(str((count/TRAIN_SIZE)*100) + "% done")

            # Perform human segmentation
            results = model(frame)

            for result in results:
                person_masks = result.masks[result.boxes.cls == 0].numpy()  # Ensure masks are numpy arrays
                person_mask_3ch = cv2.cvtColor(person_masks, cv2.COLOR_GRAY2BGR)
                masked_frame = cv2.bitwise_and(frame, person_mask_3ch)

                inverted_mask = cv2.bitwise_not(person_mask_3ch)

                result_frame = cv2.bitwise_and(masked_frame, inverted_mask)

                resized_frame = cv2.resize(result_frame, (output_width, output_height))

                name = 'trydata/resized_frame.jpg' + str(count) + '.jpg'

                cv2.imwrite(name, resized_frame)

                video.write(resized_frame.astype('uint8'))

    except Exception as e:
        print(e)
        break

    currentFrame += 1

print(str(count) + " Frames collected")
cap.release()
video.release()

Additionally, please ensure you are using the latest versions of torch and ultralytics. You can upgrade them using the following commands:

pip install --upgrade torch ultralytics

If the issue persists, please share the minimum reproducible example so we can assist you further. Thank you for your cooperation! 😊

from ultralytics.

codinglearningnovice avatar codinglearningnovice commented on June 24, 2024

Bug description:

When running inference on a video to segment the person and manipulate each frame, I get an error related to the expected input from the cv2.cvt, seems to be a type mismatch

MRE:

import cv2
from ultralytics import YOLO

# Load the YOLOv8 segmentation model
model = YOLO("yolov8n-seg.pt")

cap = cv2.VideoCapture('dancee.mp4')
output_width, output_height = 96, 64  # Adjust as needed
video = cv2.VideoWriter('output_video.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 30, (output_width, output_height))


            # Perform human segmentation
            results = model(frame)

            for result in results:
                person_masks = result.masks[result.boxes.cls == 0].numpy()  # Ensure masks are numpy arrays
                person_mask_3ch = cv2.cvtColor(person_masks, cv2.COLOR_GRAY2BGR)
                masked_frame = cv2.bitwise_and(frame, person_mask_3ch)

                inverted_mask = cv2.bitwise_not(person_mask_3ch)

                result_frame = cv2.bitwise_and(masked_frame, inverted_mask)

                resized_frame = cv2.resize(result_frame, (output_width, output_height))

                name = 'trydata/resized_frame.jpg' + str(count) + '.jpg'

                cv2.imwrite(name, resized_frame)

                video.write(resized_frame.astype('uint8'))

    except Exception as e:
        print(e)
        break

    currentFrame += 1

print(str(count) + " Frames collected")
cap.release()
video.release()

Error message:

OpenCV(4.8.0) πŸ‘Ž error: (-5:Bad argument) in function 'cvtColor'

Overload resolution failed:

  • src is not a numpy array, neither a scalar
  • Expected Ptrcv::UMat for argument 'src'

Dependencies:

ultralytics==8.2.0

from ultralytics.

glenn-jocher avatar glenn-jocher commented on June 24, 2024

Hello @codinglearningnovice,

Thank you for providing a detailed description of the issue and the minimum reproducible example (MRE). It looks like the error is due to a type mismatch when using cv2.cvtColor. Let's ensure that the person_masks variable is indeed a numpy array before passing it to cv2.cvtColor.

First, please make sure you are using the latest versions of torch and ultralytics. You can upgrade them using the following commands:

pip install --upgrade torch ultralytics

Here’s a revised version of your code snippet that includes a check to ensure person_masks is a numpy array:

import cv2
from ultralytics import YOLO

# Load the YOLOv8 segmentation model
model = YOLO("yolov8n-seg.pt")

cap = cv2.VideoCapture('dancee.mp4')
output_width, output_height = 96, 64  # Adjust as needed
video = cv2.VideoWriter('output_video.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 30, (output_width, output_height))

count = 0
TRAIN_SIZE = 1000  # Adjust as needed
FRAME_SKIP = 5  # Adjust as needed
currentFrame = 0

while count < TRAIN_SIZE:
    try:
        ret, frame = cap.read()
        if not ret:
            break

        if currentFrame % FRAME_SKIP == 0:
            count += 1
            if count % int(TRAIN_SIZE / 10) == 0:
                print(f"{(count / TRAIN_SIZE) * 100}% done")

            # Perform human segmentation
            results = model(frame)

            for result in results:
                person_masks = result.masks[result.boxes.cls == 0].numpy()  # Ensure masks are numpy arrays
                if person_masks.size == 0:
                    continue  # Skip if no person masks are found

                person_mask_3ch = cv2.cvtColor(person_masks[0], cv2.COLOR_GRAY2BGR)  # Convert the first mask to 3 channels
                masked_frame = cv2.bitwise_and(frame, person_mask_3ch)

                inverted_mask = cv2.bitwise_not(person_mask_3ch)

                result_frame = cv2.bitwise_and(masked_frame, inverted_mask)

                resized_frame = cv2.resize(result_frame, (output_width, output_height))

                name = f'trydata/resized_frame_{count}.jpg'

                cv2.imwrite(name, resized_frame)

                video.write(resized_frame.astype('uint8'))

    except Exception as e:
        print(e)
        break

    currentFrame += 1

print(f"{count} Frames collected")
cap.release()
video.release()

This code ensures that person_masks is a numpy array and handles cases where no person masks are found. Additionally, it converts the first mask to 3 channels before applying cv2.cvtColor.

If the issue persists, please provide any additional details or errors you encounter. This will help us further investigate and provide a more accurate solution.

Thank you for your patience and cooperation! 😊

from ultralytics.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.