I am running a Laravel 10 with passport 11 hyn multitenant application. It's a legacy application that is a few years old and has worked very well until I upgraded my Laravel from 9 to 10 and passport from 10 to 11. I when I try to create a tenancy, everything works until the "$company = Tenant::create($request);" which also works quite well up to a point. It creates an entry into the tency.hostnames and tenancy.websites tables respectively and also creates a custom database for the new tenancy. How ever, It attempts to recreate all the system tables that resides in the tenant database. These migration files are used by the entire app and are located in migrations folder whiles the others that make up each tenncies's database are in migrations/tenant folder. I do'nt really undersatand why creating a new tenancy is trying to recreate the tables in the tenancy database which obviously already exists. I get this error when this happens

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'websites' already exists (Connection: system, SQL: create table websites (id bigint unsigned not null auto_increment primary key, uuid varchar(191) not null, created_at timestamp null, updated_at timestamp null, deleted_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

this is my register function
public function register(Request $request)
    {
        $facility_name = Hostname::where('tenant_facility_name', $request->tenant_facility_name)->first();
        $checkEmail = Hostname::where('email', $request->email)-`>`first();
        $fqdn = Hostname::where('subdomain', $request->fqdn)->first();

        if($facility_name){
            return response(['message' => 'A facility with this name already exist'], 409);
        }
        if ($checkEmail) {
            return response(['message' => 'A facility with this email already exist'], 409);
        }
        if ($fqdn) {
            return response(['message' => 'A facility with this name already exist'], 409);
        }

        // Validate the incoming request
        $this->validator($request->all())->validate();

        try {
            // create Tenant Account
            $company = Tenant::create($request);
        } catch(\Exception $e) {
            return response($e->getMessage(), 405);
        }

        event(new Registered($user = $this->create($request->all())));
        // set trial period for new account without actual subscription for 30 days
        // $host = HostnameModel::where('fqdn', $company->hostname->fqdn)->first();
        // $host->trial_ends_at = now()->addDays(30);
        // $host->save();
        $this->createTrialPeriod($request->email);

        // Function to send email
        $name = $request->othernames . ' ' . $request->surname;
        $email = $request->email;
        $facilityName = $request->tenant_facility_name;

        // Send email to new account admin
        try {
            Mail::to($email)->send(new TenantAccountCreation($name, $email, $facilityName));
        } catch(\Exception $e) {
            // sending 200 so that the registration continues without queing email to new account user
            return response($e->getMessage(), 200);
        }

        return response()->json(['message' => 'Account Has Been Created Successfully'], 200);
    }

and this is my Tenant.php file
<?php

namespace App\Models\General;

use Illuminate\Support\Facades\Artisan;
use Hyn\Tenancy\Environment;
use Hyn\Tenancy\Models\Hostname;
use Hyn\Tenancy\Models\Website;
use Illuminate\Support\Str;
use Hyn\Tenancy\Contracts\Repositories\HostnameRepository;
use Hyn\Tenancy\Contracts\Repositories\WebsiteRepository;

class Tenant
{

    public function __construct(Website $website = null, Hostname $hostname = null)
    {

        $this->website = $website ?? $sub->website;
        $this->hostname = $hostname ?? $sub->websites->hostnames->first();
    }

    public function delete()
    {
        app(HostnameRepository::class)->delete($this->hostname, true);
        app(WebsiteRepository::class)->delete($this->website, true);
    }

    public static function create($request): Tenant
    {
        // Create New Website
        $website = new Website;

        // Attached the fqdn to a random string of 5
        $website->uuid = $request->fqdn.'_'.Str::random(5);

        app(WebsiteRepository::class)->create($website);

        // associate the website with a hostname
        $hostname = new Hostname;
        $hostname->subdomain = $request->fqdn;
        $hostname->email = $request->email;
        $hostname->currency = $request->currency;

        // Add the facility name to hostname table
        $hostname->tenant_facility_name = $request->tenant_facility_name;

        // merge
        // $request->merge(['fqdn' => $request->fqdn . '.' . env('APP_URL_BASE')]);
        $fqdn = $request->fqdn . '.' .config('services.environment');

        $hostname->fqdn = $fqdn;
        // $hostname->fqdn = $request->fqdn;

        app(HostnameRepository::class)->attach($hostname, $website);

        // make hostname current
        app(Environment::class)->tenant($website);

        Artisan::call('passport:install');

        return new Tenant($website, $hostname);
    }

    public static function tenantExists($name)
    {
        return Hostname::where('fqdn', $name)->exists();
    }

}

and this is my database.php file
<?php

use Illuminate\Support\Str;

return [

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

    'default' => env('DB_CONNECTION', 'mysql'),

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

    'connections' => [

        'system' => [
            'driver' => 'mysql',
            'host' => env('TENANCY_HOST', '127.0.0.1'),
            'port' => env('TENANCY_PORT', '3306'),
            'database' => env('TENANCY_DATABASE', 'tenancy'),
            'username' => env('TENANCY_USERNAME', 'User1'),
            'password' => env('TENANCY_PASSWORD', 'mypassword'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

        'sqlite' => [
            'driver' => 'sqlite',
            'url' => env('DATABASE_URL'),
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
        ],

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

        'sqlsrv' => [
            'driver' => 'sqlsrv',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Migration Repository Table
    |--------------------------------------------------------------------------
    |
    | This table keeps track of all the migrations that have already run for
    | your application. Using this information, we can determine which of
    | the migrations on disk haven't actually been run in the database.
    |
    */

    'migrations' => 'migrations',

    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer body of commands than a typical key-value system
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */

    'redis' => [

        'client' => env('REDIS_CLIENT', 'predis'),

        'options' => [
            'cluster' => env('REDIS_CLUSTER', 'predis'),
            'prefix' => Str::slug(env('APP_NAME', 'laravel'), '_').'_database_',
        ],

        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_DB', 0),
        ],

        'cache' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_CACHE_DB', 1),
        ],

    ],

];

updating the create function in my Tenant.php to:
    public static function create($request): Tenant
    {
        try {
            // Create New Website
            \Log::info('creating new website info');
            $website = new Website;

            // Attached the fqdn to a random string of 5
            $website->uuid = $request->fqdn . '_' . Str::random(5);

            app(WebsiteRepository::class)->create($website);
            \Log::info('website created');

            // Associate the website with a hostname
            $hostname = new Hostname;
            $hostname->subdomain = $request->fqdn;
            $hostname->email = $request->email;
            $hostname->currency = $request->currency;
            $hostname->tenant_facility_name = $request->tenant_facility_name;

            $fqdn = $request->fqdn . '.' . config('services.environment');
            $hostname->fqdn = $fqdn;

            app(HostnameRepository::class)->attach($hostname, $website);
            \Log::info('creating new hostname');

            // Make hostname current
            app(Environment::class)->tenant($website);
            \Log::info('make tenancy current');

            // Run Passport install
            \Log::info('starting passport install');
            Artisan::call('passport:install');
            \Log::info('passport installed');

            return new Tenant($website, $hostname);

        } catch (\Exception $e) {
            \Log::error('Error creating tenant: ' . $e->getMessage());
            throw $e;
        }
    }

this is what is logged in the laravel log file

[2024-06-21 00:16:01] local.INFO: creating new website info
[2024-06-21 00:17:15] local.INFO: website created
[2024-06-21 00:17:15] local.INFO: creating new hostname
[2024-06-21 00:17:15] local.INFO: make tenancy current
[2024-06-21 00:17:15] local.INFO: starting passport install
[2024-06-21 00:17:16] local.ERROR: Error creating tenant: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'websites' already exists (Connection: system, SQL: create table websit

which clearly proves that the line
Artisan::call('passport:install')
is responsible for the error.
Question is : Why is this,
Artisan::call('passport:install')
command trying to run all migration files in the application all over again?


In my Controller that extends the Illuminate\Routing\Controller, i am trying to implement some middlware validations using the new HasMiddleware.
This is the code that i am trying to run:
<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller;
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware;

class BaseController extends Controller implements HasMiddleware
{
    public static $readPermission = null;

    public static $customPermissionChecks = [];

    public static function middleware(): array
    {
        return array_merge([
            'auth:api',
            new Middleware('permission:' . self::$readPermission, ['only' => ['index', 'show']]),
            new Middleware('permission:write.' . self::$readPermission, ['only' => ['edit', 'store', 'update', 'destroy', 'restore']])
        ], self::$customPermissionChecks);
    }
}

However  Illuminate\Routing\Controller's middlware() is confliting with the HasMiddleware's static method giving me the following error:
Cannot make non static method Illuminate\Routing\Controller::middleware() static in class App\Http\Controllers\BaseController


I'd like to insert the user-id of the current user into a table in column "user_id". The field is a relation to the user table.
Migration / database schema
        Schema::create('pdlocations', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->decimal('lon', 10, 7);
            $table->decimal('lat', 10, 7);
            $table->string('map');
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')
                ->references('id')
                ->on('users');
        });

In the controler (PdlocationController.php)
   public function store(PdlocationStoreRequest $request): RedirectResponse
    {
        $request->merge([
            'user_id' => auth()->user()->id,
//            'user_id' => auth()->user(),
        ]);

        $this->validate($request, [
            'user_id' => 'required|exists:users,id',
        ]);

        Pdlocation::create($request->validated());

        return redirect()->route('admin.pdlocation.index')
                         ->with('success', 'Pdlocation created successfully.');
    }

If merging into the request the current userID auth()->user()->id I get the following error message:
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value

insert into
  `pdlocations` (`map`, `lon`, `lat`, `updated_at`, `created_at`)
values
  (test, 66, 55, 2024 -06 -21 18: 42: 57, 2024 -06 -21 18: 42: 57)

If merging into the request instead the user object auth()->user() the validator says The selected user id is invalid.
Any idea or suggestion what I'm missing?


Struggling to get the correct results for a belongsToMany that references the same model.
Relationship definition in the Status model:
public function represents(): BelongsToMany {
   return $this->belongsToMany(Status::class, 'status_represents', 'parent_id', 'child_id');
}

parent_id and child_id both reference the status table id.
Data in the status_represents table:

I want to get all the Status records with a parent_id = 7
Query I tried:
$activeStatus = Status::whereHas('represents', function (Builder $query){
    $query->where('status_represents.parent_id', 7);
})->get();

That returns the status record with id = 7.
Not the status records with parent_id = 7. Expecting records with id: 1,2,3.
In SQL this works:
select *
from statuses as s
inner join status_represents as sr on sr.child_id = s.id
where sr.parent_id = 7


I am trying to understad the keypoint output of the yolov7, but I didn't find enough information about that.
I have the following output:
array([          0,           0,      430.44,      476.19,      243.75,         840,     0.94348,      402.75,       128.5,     0.99902,       417.5,      114.25,     0.99658,       385.5,         115,     0.99609,      437.75,       125.5,     0.89209,      366.75,         128,     0.66406,         471,      229.62,
           0.97754,      346.75,      224.88,     0.97705,         526,      322.75,     0.95654,       388.5,      340.75,     0.95898,       424.5,      314.75,     0.94873,       483.5,       335.5,      0.9502,       465.5,      457.75,     0.99219,       381.5,      456.25,     0.99219,       451.5,         649,
           0.98584,      379.25,       649.5,     0.98633,       446.5,         818,     0.92285,         366,       829.5,      0.9248])

the paper https://arxiv.org/pdf/2204.06806.pdf tells "So, in total there are 51 elements for 17 keypoints associated with an anchor. " but the length is 58.
there are 18 numbers that probably are confidences of a keypoint:
array([     0.94348,     0.99902,,     0.99658,     0.99609,     0.89209,     0.66406, 0.97754,     0.97705,     0.95654,     0.95898,     0.94873, 0.9502,     0.99219,     0.99219,
           0.98584,     0.98633,     0.92285,     0.9248])

But the paper tells that are 17 keypoints.
In this repo https://github.com/retkowsky/Human_pose_estimation_with_YoloV7/blob/main/Human_pose_estimation_YoloV7.ipynb tells that the keypoints are the following:

but that shape doesn't match the prediction:

Is the first image right about the keypoints?
and what are the first four digits?
  0,           0,      430.44,      476.19

Thanks
EDIT
This is not a complet answer but editing the plot function I can get the following information
Given the following output keypoint:
array([[          0,           0,      312.31,         486,      291.75,       916.5,     0.94974,       304.5,      118.75,     0.99902,      320.75,      102.25,     0.99756,      287.75,      103.25,     0.99658,         345,         112,     0.96338,      268.25,      115.25,     0.69531,         394,
             226.25,     0.98145,      228.25,      230.12,     0.98389,       428.5,       358.5,     0.95898,      192.88,      364.75,     0.96533,         407,      464.25,     0.95166,      215.75,      464.25,      0.9585,      363.75,         491,     0.99219,      257.75,       491.5,     0.99268,
              361.5,         680,      0.9834,      250.88,         679,     0.98438,         361,       861.5,     0.91064,         247,         863,     0.91504]])

from this position ouput[7:] you can get the points of each keypoint, with the following sort as you can see in the image

array([      304.5,      118.75,     0.99902,      320.75,      102.25,     0.99756,      287.75,      103.25,     0.99658,         345,         112,     0.96338,      268.25,      115.25,     0.69531,         394,      226.25,     0.98145,      228.25,      230.12,     0.98389,       428.5,       358.5,     0.95898,
            192.88,      364.75,     0.96533,         407,      464.25,     0.95166,      215.75,      464.25,      0.9585,      363.75,         491,     0.99219,      257.75,       491.5,     0.99268,       361.5,         680,      0.9834,      250.88,         679,     0.98438,         361,       861.5,     0.91064,
               247,         863,     0.91504])

but I am not sure about what are the rest of the values:
0,           0,      312.31,         486,      291.75,       916.5,     0.94974,


I have trained a YOLOv7 model using the Roboflow notebook and my own dataset: https://colab.research.google.com/drive/1X9A8odmK4k6l26NDviiT6dd6TgR-piOa
I worked with these notebooks before, but never had images with more than 100 objects, but now, I have trained a model to detect microbiologic colonies, and the model is detecting up to a max of 100 objects.
At first I thought it was a problem of the network not being able of detecting all the objects because it was not well trained or due to precision (My dataset is composed of 500 images, and the final accuracy is about 80%).
But, I have some images that has between 15-30 objects, and it detects all fine. In my images that objects are clearly more than 100, the network always counts up to 100 objects, never more.
Is there any limit to yolov7 in object quantity? Or maybe a parameter that has to be changed in training phase?


I have trained a YOLOv8 object detection model using a custom dataset, and I want to convert it to a Core ML model so that I can use it on iOS.
After exporting the model, I have a converted model to core ml, but I need the coordinates or boxes of the detected objects as output in order to draw rectangular boxes around the detected objects.
As a beginner in this area, I am unsure how to achieve this. Can anyone help me with this problem?
Training model:
!yolo task=detect mode=train model=yolov8s.pt data= data.yaml epochs=25 imgsz=640 plots=True

Validation:
!yolo task=detect mode=val model=runs/detect/train/weights/best.pt data=data.yaml

Export this model to coreML:
!yolo mode=export model=runs/detect/train/weights/best.pt format=coreml

How can I get the co ordinate output?


def convert_coco_to_yolov8(coco_file):

    with open(coco_file) as f:
        coco = json.load(f)

    images = coco['images']
    annotations = coco['annotations']
    categories = {cat['id']: cat['name'] for cat in coco['categories']}

    os.makedirs('labels', exist_ok=True)

    for image in tqdm(images, desc='Converting images'):
        image_id = image['id']
        filename = image['file_name']

        label_filename = filename.split('.png')[0]
        label_path = os.path.join('labels', f'{label_filename}.txt')
        with open(label_path, 'w') as f:

            for ann in annotations:
                if ann['image_id'] != image_id:
                    continue

                img_width = image['width']
                img_height = image['height']

                xmin, ymin, width, height = ann['bbox']

                xmax = xmin + width
                ymax = ymin + height
                xcen = (xmin + xmax) / 2
                ycen = (ymin + ymax) / 2
                # xcen = (xmin + xmax) / 2 / img_width
                # ycen = (ymin + ymax) / 2 / img_height
                w = xmax - xmin
                h = ymax - ymin
                label = categories[ann['category_id']]
                label_id = ann['category_id']

                segmentation_points_list = []
                for segmentation in ann['segmentation']:
                    segmentation_points = [str(point / img_width) for point in segmentation]
                    segmentation_points_list.append(' '.join(segmentation_points))
                segmentation_points_string = ' '.join(segmentation_points_list)

                line = '{} {} {} {} {} {}\n'.format(label_id, xcen / img_width, ycen / img_height, w / img_width, h / img_height, segmentation_points_string )
                f.write(line)

the script is getting the labels but when i train for YOLOv8 the labels are seems wrong ,I need to convert a coco json to YOLOV8 txt file . label should contain segmentation also. Note my JSON file have different image size for all images


I'm training YOLOv8 in Colab on a custom dataset. How can I save the model after some epochs and continue the training later. I did the first epoch like this:
import torch

model = YOLO("yolov8x.pt")
model.train(data="/image_datasets/Website_Screenshots.v1-raw.yolov8/data.yaml", epochs=1)

While looking for the options it seems that with YOLOv5 it would be possible to save the model or the weights dict. I tried these but either the save or load doesn't seem to work in this case:
torch.save(model, 'yolov8_model.pt')
torch.save(model.state_dict(), 'yolov8x_model_state.pt')


I have a laptop with following configurations
 Processor : AMD Ryzen 7 4800H with Radeon Graphics            2.90 GHz
 Installed RAM : 16.0 GB (15.4 GB usable)
 Windows Edition : Windows 11 Home Single Language
 Version : 22H2
 OS : 22621.1555
 NVIDIA GTX GEFORCE 1650 GRAPHICS CARD
 NVIDIA DRIVER : 31.0.15.3161

Its a brand new laptop
With following Python and CUDA installed:
Python 3.10.11

---------------------------------------------------------------------------------------+
| NVIDIA-SMI 531.61                 Driver Version: 531.61       CUDA Version: 12.1     |
|-----------------------------------------+----------------------+----------------------+
| GPU  Name                      TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf            Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                                         |                      |               MIG M. |
|=========================================+======================+======================|
|   0  NVIDIA GeForce GTX 1650       WDDM | 00000000:01:00.0 Off |                  N/A |
| N/A   44C    P0               15W /  N/A|      0MiB /  4096MiB |      0%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+

+---------------------------------------------------------------------------------------+
| Processes:                                                                            |
|  GPU   GI   CI        PID   Type   Process name                            GPU Memory |
|        ID   ID                                                             Usage      |
|=======================================================================================|
|  No running processes found                                                           |
+---------------------------------------------------------------------------------------+

But whenever I try to run my YOLOV8 model for object detection on this, it shuts down during 1st epoch only. Not sure why its happening. Any help is highly appreciated.
My Python  code
import tensorflow as tf
from ultralytics import YOLO
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
print(tf.test.is_built_with_cuda())
print(tf.config.list_physical_devices('GPU'))

# Create a TensorFlow session with GPU growth enabled
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.compat.v1.Session(config=config)
physical_devices = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
print(tf.test.is_built_with_cuda())
# Run your code in the session
with sess.as_default():
   # Load the model.
   model = YOLO('yolov8n.pt')

   # Training.
   results = model.train(
      data='data.yaml',
      imgsz=640,
      epochs=5,
      batch=8,
      name='yolov8n_custom')

PS I would also like to know what are the hardware requirements for YOLOV8


We are trying to get the detected object names using Python and YOLOv8 with the following code.
import cv2
from ultralytics import YOLO

def main():
    cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

    model = YOLO("yolov8n.pt")

    while True:
        ret, frame = cap.read()
        result = model(frame, agnostic_nms=True)[0]

        print(result)

        if cv2.waitKey(30) == 27:
            break

    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

The following two types are shown on the log.
0: 384x640 1 person, 151.2ms
Speed: 0.6ms preprocess, 151.2ms inference, 1.8ms postprocess per image at shape (1, 3, 640, 640)

The second log is the one we displayed using print, how do we get the person from now on? Presumably we get the person by giving 0 to the names, but where do we get the 0 from?
ultralytics.yolo.engine.results.Results object with attributes:

boxes: ultralytics.yolo.engine.results.Boxes object
keypoints: None
keys: ['boxes']
masks: None
names: {0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus', 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'}
orig_img: array([[[51, 58, 64],
        [52, 59, 65],
        [54, 59, 65],
        ...,
        [64, 68, 74],
        [62, 67, 73],
        [62, 67, 73]],

       [[51, 58, 64],
        [53, 59, 65],
        [54, 59, 65],
        ...,
        [63, 68, 74],
        [62, 67, 73],
        [62, 67, 73]],

       [[53, 58, 64],
        [53, 58, 64],
        [53, 58, 64],
        ...,
        [61, 67, 73],
        [61, 67, 73],
        [61, 67, 73]],

       ...,

       [[43, 48, 58],
        [42, 47, 57],
        [41, 46, 56],
        ...,
        [24, 35, 49],
        [23, 34, 48],
        [23, 34, 48]],

       [[44, 48, 59],
        [43, 47, 57],
        [42, 46, 56],
        ...,
        [26, 35, 49],
        [26, 35, 49],
        [24, 33, 48]],

       [[45, 48, 59],
        [43, 45, 56],
        [40, 43, 54],
        ...,
        [26, 35, 49],
        [26, 35, 49],
        [25, 33, 48]]], dtype=uint8)
orig_shape: (720, 1280)
path: 'image0.jpg'
probs: None
speed: {'preprocess': 1.6682147979736328, 'inference': 79.47301864624023, 'postprocess': 1.0020732879638672}

We would like to know the solution in this way. But if it is not possible, we can use another method if it is a combination of Python and YOLOv8. We plan to display bounding boxes and object names.
Additional Information
I changed the code as follows.
        ret, frame = cap.read()
        # result = model(frame, agnostic_nms=True)[0]
        result = model([frame])[0]

        boxes = result.boxes
        masks = result.masks
        probs = result.probs

        print("[boxes]==============================")
        print(boxes)
        print("[masks]==============================")
        print(masks)
        print("[probs]==============================")
        print(probs)

After all, the following person is not included. How should we determine that?
[boxes]==============================
WARNING ⚠ 'Boxes.boxes' is deprecated. Use 'Boxes.data' instead.
ultralytics.yolo.engine.results.Boxes object with attributes:

boxes: tensor([[4.7356e+01, 7.2858e+00, 1.1974e+03, 7.1092e+02, 8.6930e-01, 0.0000e+00]])
cls: tensor([0.])
conf: tensor([0.8693])
data: tensor([[4.7356e+01, 7.2858e+00, 1.1974e+03, 7.1092e+02, 8.6930e-01, 0.0000e+00]])
id: None
is_track: False
orig_shape: tensor([ 720, 1280])
shape: torch.Size([1, 6])
xywh: tensor([[ 622.4028,  359.1004, 1150.0942,  703.6293]])
xywhn: tensor([[0.4863, 0.4988, 0.8985, 0.9773]])
xyxy: tensor([[  47.3557,    7.2858, 1197.4500,  710.9150]])
xyxyn: tensor([[0.0370, 0.0101, 0.9355, 0.9874]])
[masks]==============================
None
[probs]==============================
None


I'm currently working in a project in which I'm using Flask and Yolov8 together.
When I run this code
from ultralytics import YOLO

model = YOLO("./yolov8n.pt")

results = model.predict(source="../TEST/doggy.jpg", save=True, save_txt=True)

the output will be saved in this default directory /run/detect/
like
Ultralytics YOLOv8.0.9  Python-3.10.8 torch-2.0.0+cpu CPU
Fusing layers...
YOLOv8n summary: 168 layers, 3151904 parameters, 0 gradients, 8.7 GFLOPs
Results saved to d:\runs\detect\predict4
1 labels saved to d:\runs\detect\predict4\labels

and what I want is the predict directory number or the entire directory path in a variable.
I tried capturing the path using sys.stdout methods but i want a direct solution.


I want to segment an image using yolo8 and then create a mask for all objects in the image with specific class.
I have developed this code:
img=cv2.imread('images/bus.jpg')
model = YOLO('yolov8m-seg.pt')
results = model.predict(source=img.copy(), save=False, save_txt=False)
class_ids = np.array(results[0].boxes.cls.cpu(), dtype="int")
for i in range(len(class_ids)):
    if class_ids[i]==0:
         empty_image = np.zeros((height, width,3), dtype=np.uint8)
         res_plotted = results[0][i].plot(boxes=0, img=empty_image)

In the above code, res_plotted is the mask for one object, in RGB. I want to add all of these images to each other and create a mask for all objects with class 0 (it is a pedestrian in this example)
My questions:

How can I complete this code?
Is there any better way to achieve this without having a loop?
Is there any utility function in the yolo8 library to do this?


I have this output that was generated by model.predict()
0: 480x640 1 Hole, 234.1ms
Speed: 3.0ms preprocess, 234.1ms inference, 4.0ms postprocess per image at shape (1, 3, 640, 640)

0: 480x640 1 Hole, 193.6ms
Speed: 3.0ms preprocess, 193.6ms inference, 3.5ms postprocess per image at shape (1, 3, 640, 640)

...

How do I hide the output from terminal?
I can't find out the information in this official link
https://docs.ultralytics.com/modes/predict/#arguments


I am trying to draw a segmentation mask from a YOLO segmentation mask dataset. The annotation line I am reading looks like this:
36 0.6158357764423077 0.814453125 0.6158357764423077 0.8095703125 0.6070381225961539 0.8095703125 0.6041055721153846 0.8115234375 0.5894428149038462 0.8154296875 0.5747800576923077 0.8125 0.5513196490384615 0.8134765625 0.5483870961538462 0.81640625 0.5923753653846154 0.818359375 0.6158357764423077 0.814453125
I am using cv2.polylines to draw the shape but am getting an error:
image_height, image_width, c = img.shape
isClosed = True
color = (255, 0, 0)
thickness = 2

with open(annotation_file) as f:
    for line in f:
        split_line = line.split()
        class_id = split_line[0]
        mask_shape = [float(numeric_string) for numeric_string in split_line[1:len(split_line)]]
        mask_points = []
        for i in range(0,len(mask_shape),2):
            x,y = mask_shape[i:i+2]
            mask_points.append((x * image_width, y * image_height))
        points = np.array([mask_points])
        image = cv2.polylines(img, points,
                              isClosed, color, thickness)
        break

Error:
OpenCV(4.7.0) /Users/xperience/GHA-OCV-Python/_work/opencv-python/opencv-python/opencv/modules/imgproc/src/drawing.cpp:2434: error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function 'polylines'


getting IndexError: index 1 is out of bounds for dimension 1 with size 1  while training yolo v8
I am trying defect detection with yolov8 and i am expecting a detection of those defect on full scale image
Input is 416 * 416 image
using  Python 3.8.6,windows11 pro
data.yaml file:
path: /Users/pksha/yolo
train: train/images
val: val/images
nc: 0
names: ['particle']
**python code:
pip install ultralytics
from ultralytics import YOLO
# LOAD model
 model=YOLO("yolov8l.yaml")
 results=model.train(data="data.yaml",epochs=1)

I have tried both increasing and decresing to check if it works but both showed error.


My project aims to detect object labels and coordinates and then convert them into a string which is converted into voice using gTTS but I keep getting an attribute error in the prediction labels. I am new to this framework, any help will be appreciated.
Code:
import cv2
from gtts import gTTS
import os
from ultralytics import YOLO

def convert_labels_to_text(labels):
    text = ", ".join(labels)
    return text

class YOLOWithLabels(YOLO):
    def __call__(self, frame):
        results = super().__call__(frame)
        labels = results.pred[0].get_field("labels").tolist()
        annotated_frame = results.render()
        return annotated_frame, labels

cap = cv2.VideoCapture(0)
model = YOLOWithLabels('yolov8n.pt')

while cap.isOpened():
    success, frame = cap.read()

    if success:
        annotated_frame, labels = model(frame)

        message = convert_labels_to_text(labels)

        tts_engine = gTTS(text=message)  # Initialize gTTS with the message

        tts_engine.save("output.mp3")
        os.system("output.mp3")

        cv2.putText(annotated_frame, message, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
        cv2.imshow("YOLOv8 Inference", annotated_frame)

        if cv2.waitKey(1) & 0xFF == ord("q"):
            break

    else:
        break

cap.release()
cv2.destroyAllWindows()

Error
File "C:\Users\alien\Desktop\YOLOv8 project files\gtts service\testservice.py", line 13, in __call__
    labels = results.pred[0].get_field("labels").tolist()
             ^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'pred'

print(results)
orig_shape: (480, 640)
path: 'image0.jpg'
probs: None
save_dir: None
speed: {'preprocess': 3.1604766845703125, 'inference': 307.905912399292, 'postprocess': 2.8924942016601562}]
0: 480x640 1 person, 272.4ms
Speed: 3.0ms preprocess, 272.4ms inference, 4.0ms postprocess per image at shape (1, 3, 640, 640)
[ultralytics.yolo.engine.results.Results object with attributes:
    boxes: ultralytics.yolo.engine.results.Boxes object
    keypoints: None
    keys: ['boxes']
    masks: None
    names: {0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus', 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'}
    orig_img: array([[[168, 167, 166],
            [165, 165, 165],
            [165, 166, 167],
            ...,
            [183, 186, 178],
            [183, 186, 178],
            [184, 187, 179]],

           [[168, 167, 165],
            [166, 165, 165],
            [166, 167, 166],
            ...,
            [184, 187, 179],
            [183, 186, 178],
            [184, 187, 179]],

           [[168, 167, 164],
            [167, 167, 164],
            [167, 167, 165],
            ...,
            [184, 187, 178],
            [184, 187, 179],
            [183, 186, 178]],

           ...,

           [[196, 192, 185],
            [196, 192, 185],
            [196, 192, 185],
            ...,
            [ 25,  29,  38],
            [ 22,  25,  35],
            [ 20,  24,  34]],

           [[199, 195, 187],
            [197, 193, 186],
            [197, 193, 186],
            ...,
            [ 23,  26,  35],
            [ 22,  25,  35],
            [ 22,  25,  35]],

           [[199, 195, 187],
            [199, 195, 187],
            [199, 195, 187],
            ...,
            [ 20,  24,  33],
            [ 19,  23,  33],
            [ 19,  23,  33]]], dtype=uint8)


I have a YOLOv8 object detection model trained on custom. It takes image as input and annotates the different objects my question is How do I get coordinates of different objects? I want these coordinate data to further crop the images.
If the object detected is a person I want coordinates of that same for cat and dog.
Thanks for help in advance.
My code looks something like this
infer = YOLO(r'path_to_trained_model.pt')
image_path = r'image'
results = infer(image_path)

class_name = "Person"
confidence_threshold = 0.5

for r in results:
    r.boxes.xyxy ## I was also getting this error 'list' object has no attribute 'xyxy'

I know I am missing quite a bit of data here since I have no idea how it works.


I want to train the YOLO v8 in transfer learning on my custom dataset.
I have different classes than the base training on the COCO dataset.
Yet I don't want to learn again the feature extraction.
Hence I though following the Ultralytics YOLOv8 Docs - Train.
Yet, When I train on my small dataset I want to freeze the backbone.
How can I do that?
I looked at the documentation and couldn't find how to do so.


I am working on an Android app where I am already using OpenCV, I got a model which is in onnx format from YOLOv8 after conversion. Here is the output metadata of it.

name - output0
type - float32[1,5,8400]

So far I am successfully running the model but in the end, the output that I got I can't comprehend.
This is the print statement from the output
Mat [ 1* 5* 8400*CV_32FC1, isCont=true, isSubmat=true, nativeObj=0x72345b4840, dataAddr=0x723076b000 ]
class Detector(private val context: Context) {
    private var net: Net? = null

    fun detect(frame: Bitmap) {
        // preprocess image
        val mat = Mat()
        Utils.bitmapToMat(resizedBitmap, mat)
        Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGBA2RGB)
        val inputBlob = Dnn.blobFromImage(mat, 1.0/255.0, Size(640.0, 640.0), Scalar(0.0), true, false)
        net?.setInput(inputBlob)
        val outputBlob = net?.forward() ?: return
        println(outputBlob)
    }

    fun setupDetector() {
        val modelFile = File(context.cacheDir, MODEL_NAME)
        if (!modelFile.exists()) {
            try {
                val inputStream = context.assets.open(MODEL_NAME)
                val size = inputStream.available()
                val buffer = ByteArray(size)
                inputStream.read(buffer)
                inputStream.close()
                val outputStream = FileOutputStream(modelFile)
                outputStream.write(buffer)
                outputStream.close()
                net = Dnn.readNetFromONNX(modelFile.absolutePath)
            } catch (e: Exception) {
                throw RuntimeException(e)
            }
        } else {
            net = Dnn.readNetFromONNX(modelFile.absolutePath)
        }
    }

    companion object {
        private const val MODEL_NAME = "model.onnx"
        private const val TENSOR_WIDTH = 640
        private const val TENSOR_HEIGHT = 640
    }
}

What could be the general approach to get bounding box, the confidence score and class labels? And  if you have any solution for onnx model with OpenCV then you can provide as well. Also this question isn't android specific.


I have found a solution but the problem is I have way too many points and I want to reduce them. Which is not a major issue, but still I would like to have fewer points.
I have searched for other answers here but I have found nothing pertaining to my issue. I have attached a sample mask for reference.
The sample binarised mask
Just to be clear, I have written a script in Python that does just that. The steps I had taken were:

Convert the colorised mask to a binarized one (since I have only one class).
Apply contours on the image and find the edge points of the mask.
Ravel the detected contour points.
Based on major shifts in angles, noted down only those points.
Save all the points in YOLO segmentation text format (cls_name x1 y1 x2 y2 .... xn yn)

EDIT: I did manage to do all these. But in the end I came to a horrible realization that, because it considers the outside borders, and if the labels are all overlapped, it becomes one big blob that contains unwanted region too. So I had to drop this and use those masks in UNET where I am getting the outputs I desire.


What is the use of imgsz in inference on Yolov8 model ?
Looking at current documentation's example, we can write :
model.predict(source, save=True, imgsz=320, conf=0.5)
(https://docs.ultralytics.com/modes/predict/#inference-sources).
It is not documented as argument, but can be passed.
Can it differ from the value used in training ?


Get interested in yolov8 and after few youtube tutorials i tried to train custom dataset. After all manipulations i got no prediction results :( 2nd image - val_batch0_labels, 3rd image - val_batch0_pred

I tried to do this in pycharm and google colab (same results) and here's the code:
# main.py
from ultralytics import YOLO

model = YOLO("yolov8n.yaml")

results = model.train(data="config.yaml", epochs=1)

# config.yaml
path: Y:\coding\python\yolo_test\data_airplane\data # dataset root dir
train: images  # train images (relative to 'path')
val: images  # val images (relative to 'path')

# Classes
names:
  0: airplane

And for more information here's all what i get after run this:
              from  n    params  module                                       arguments

0                  -1  1       464  ultralytics.nn.modules.conv.Conv             [3, 16, 3, 2]
1                  -1  1      4672  ultralytics.nn.modules.conv.Conv             [16, 32, 3, 2]
2                  -1  1      7360  ultralytics.nn.modules.block.C2f             [32, 32, 1, True]
3                  -1  1     18560  ultralytics.nn.modules.conv.Conv             [32, 64, 3, 2]
4                  -1  2     49664  ultralytics.nn.modules.block.C2f             [64, 64, 2, True]
5                  -1  1     73984  ultralytics.nn.modules.conv.Conv             [64, 128, 3, 2]
6                  -1  2    197632  ultralytics.nn.modules.block.C2f             [128, 128, 2, True]
7                  -1  1    295424  ultralytics.nn.modules.conv.Conv             [128, 256, 3, 2]
8                  -1  1    460288  ultralytics.nn.modules.block.C2f             [256, 256, 1, True]
9                  -1  1    164608  ultralytics.nn.modules.block.SPPF            [256, 256, 5]
10                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']
11             [-1, 6]  1         0  ultralytics.nn.modules.conv.Concat           [1]
12                  -1  1    148224  ultralytics.nn.modules.block.C2f             [384, 128, 1]
13                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']
14             [-1, 4]  1         0  ultralytics.nn.modules.conv.Concat           [1]
15                  -1  1     37248  ultralytics.nn.modules.block.C2f             [192, 64, 1]
16                  -1  1     36992  ultralytics.nn.modules.conv.Conv             [64, 64, 3, 2]
17            [-1, 12]  1         0  ultralytics.nn.modules.conv.Concat           [1]
18                  -1  1    123648  ultralytics.nn.modules.block.C2f             [192, 128, 1]
19                  -1  1    147712  ultralytics.nn.modules.conv.Conv             [128, 128, 3, 2]
20             [-1, 9]  1         0  ultralytics.nn.modules.conv.Concat           [1]
21                  -1  1    493056  ultralytics.nn.modules.block.C2f             [384, 256, 1]
22        [15, 18, 21]  1    897664  ultralytics.nn.modules.head.Detect           [80, [64, 128, 256]]
YOLOv8n summary: 225 layers, 3157200 parameters, 3157184 gradients
Ultralytics YOLOv8.0.141  Python-3.10.8 torch-2.0.1+cpu CPU (Intel Core(TM) i3-10105F 3.70GHz)
engine\trainer: task=detect, mode=train, model=yolov8n.yaml, data=config.yaml, epochs=1, patience=50, batch=16, imgsz=640, save=True, save_period=-1, cache=False, device=None, workers=8, project=None, name=None, exist_ok=False, pretrained=True, optimizer=auto, verbose=True, seed=0, deterministic=True, single_cls=False, rect=False, cos_lr=False, close_mosaic=10, resume=False, amp=True, fraction=1.0, profile=False, overlap_mask=True, mask_ratio=4, dropout=0.0, val=True, split=val, save_json=False, save_hybrid=False, conf=None, iou=0.7, max_det=300, half=False, dnn=False, plots=True, source=None, show=False, save_txt=False, save_conf=False, save_crop=False, show_labels=True, show_conf=True, vid_stride=1, line_width=None, visualize=False, augment=False, agnostic_nms=False, classes=None, retina_masks=False, boxes=True, format=torchscript, keras=False, optimize=False, int8=False, dynamic=False, simplify=False, opset=None, workspace=4, nms=False, lr0=0.01, lrf=0.01, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=7.5, cls=0.5, dfl=1.5, pose=12.0, kobj=1.0, label_smoothing=0.0, nbs=64, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.0, copy_paste=0.0, cfg=None, tracker=botsort.yaml, save_dir=runs\detect\train22
Overriding model.yaml nc=80 with nc=1
               from  n    params  module                                       arguments

0                  -1  1       464  ultralytics.nn.modules.conv.Conv             [3, 16, 3, 2]
1                  -1  1      4672  ultralytics.nn.modules.conv.Conv             [16, 32, 3, 2]
2                  -1  1      7360  ultralytics.nn.modules.block.C2f             [32, 32, 1, True]
3                  -1  1     18560  ultralytics.nn.modules.conv.Conv             [32, 64, 3, 2]
4                  -1  2     49664  ultralytics.nn.modules.block.C2f             [64, 64, 2, True]
5                  -1  1     73984  ultralytics.nn.modules.conv.Conv             [64, 128, 3, 2]
6                  -1  2    197632  ultralytics.nn.modules.block.C2f             [128, 128, 2, True]
7                  -1  1    295424  ultralytics.nn.modules.conv.Conv             [128, 256, 3, 2]
8                  -1  1    460288  ultralytics.nn.modules.block.C2f             [256, 256, 1, True]
9                  -1  1    164608  ultralytics.nn.modules.block.SPPF            [256, 256, 5]
10                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']
11             [-1, 6]  1         0  ultralytics.nn.modules.conv.Concat           [1]
12                  -1  1    148224  ultralytics.nn.modules.block.C2f             [384, 128, 1]
13                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']
14             [-1, 4]  1         0  ultralytics.nn.modules.conv.Concat           [1]
15                  -1  1     37248  ultralytics.nn.modules.block.C2f             [192, 64, 1]
16                  -1  1     36992  ultralytics.nn.modules.conv.Conv             [64, 64, 3, 2]
17            [-1, 12]  1         0  ultralytics.nn.modules.conv.Concat           [1]
18                  -1  1    123648  ultralytics.nn.modules.block.C2f             [192, 128, 1]
19                  -1  1    147712  ultralytics.nn.modules.conv.Conv             [128, 128, 3, 2]
20             [-1, 9]  1         0  ultralytics.nn.modules.conv.Concat           [1]
21                  -1  1    493056  ultralytics.nn.modules.block.C2f             [384, 256, 1]
22        [15, 18, 21]  1    751507  ultralytics.nn.modules.head.Detect           [1, [64, 128, 256]]
YOLOv8n summary: 225 layers, 3011043 parameters, 3011027 gradients
train: Scanning Y:\coding\python\yolo_test\data_airplane\data\labels.cache... 3 images, 0 backgrounds, 0 corrupt: 100%|██████████| 3/3 [00:00<?, ?it/s]
val: Scanning Y:\coding\python\yolo_test\data_airplane\data\labels.cache... 3 images, 0 backgrounds, 0 corrupt: 100%|██████████| 3/3 [00:00<?, ?it/s]
Plotting labels to runs\detect\train22\labels.jpg...
optimizer: AdamW(lr=0.002, momentum=0.9) with parameter groups 57 weight(decay=0.0), 64 weight(decay=0.0005), 63 bias(decay=0.0)
Image sizes 640 train, 640 val
Using 0 dataloader workers
Logging results to runs\detect\train22
Starting training for 1 epochs..
  Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
    1/1         0G      3.156      3.899      4.605         33        640: 100%|██████████| 1/1 [00:02<00:00,  2.21s/it]
             Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100%|██████████| 1/1 [00:01<00:00,  1.14s/it]
               all          3         16    0.00111     0.0625   0.000607   6.07e-05

1 epochs completed in 0.001 hours.
Optimizer stripped from runs\detect\train22\weights\last.pt, 6.2MB
Optimizer stripped from runs\detect\train22\weights\best.pt, 6.2MB
Validating runs\detect\train22\weights\best.pt...
Ultralytics YOLOv8.0.141  Python-3.10.8 torch-2.0.1+cpu CPU (Intel Core(TM) i3-10105F 3.70GHz)
YOLOv8n summary (fused): 168 layers, 3005843 parameters, 0 gradients
Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100%|██████████| 1/1 [00:01<00:00,  1.08s/it]
all          3         16    0.00114     0.0625    0.00086    8.6e-05
Speed: 2.0ms preprocess, 123.7ms inference, 0.0ms loss, 1.7ms postprocess per image
Results saved to runs\detect\train22
Process finished with exit code 0


I need to extract data from any middle layer.
I need it for making comparing images, the idea is that if the model recognizes a cat it would check if that image of a cat matches the previous images of cats.
I have done the following:
model = YOLO("yolov8s")
del model.model.model[-1]

This way I am deleting the final layer, but I can't extract data from it.
When I call the predict method I get the following:
File "\Lib\site-packages\ultralytics\utils\ops.py", line 219, in non_max_suppression
    x = x[xc[xi]]  # confidence
        ~^^^^^^^^
IndexError: The shape of the mask [12, 20] at index 0 does not match the shape of the indexed tensor [512, 20, 12] at index 0

What can I do differently to get the data? It would be better not create a model and delete the final layer( or x amount) because I still need the final result.


as the title says, how do I set parameters for augmentation while using YOLOv8? I want to use the Python SDK and not the CLI commands.
The mantainer of the repo refer several times to
https://docs.ultralytics.com/modes/predict/#arguments
but I don't understand where to store these parameters?
Also, I found this.
[Comment section from this site](https://i.sstatic.net/4qWX2.png)
It would be nice to seperate the settings from data.yaml, but a working solution is the priority


I am working on a project involving creating an AI-powered bot to full play Counter-Strike by itself. I have a working script that uses the YOLOv8 object detection algorithm to identify players on the screen, and draw a the bounding box around them.
I'm encountering an issue where the bot will consistently look downward when an enemy is detected on the screen. I have tried adjusting the mouse movement calculations and implementing crosshair adjustments, but the issue persists. I even looked at a similar implementation using win32, when I tried implementing that code to my project it still would have the bot look as far down as it could. Has anyone encountered a similar behavior or have suggestions on what might be causing this? I would greatly appreciate any insights, advice, or strategies that could help me pinpoint and address the cause of the issue.
Thank You!
Here is my code below:
import os
import time
from ultralytics import YOLO
import cv2
import numpy as np
from windowcap import WindowCapture
import win32gui, win32ui, win32con, win32api
import pydirectinput

wincap = WindowCapture('Counter-Strike: Global Offensive - Direct3D 9')
model_path = os.path.join('.','runs','detect','train10','weights','last.pt')
model = YOLO(model_path)

while True:

    frame = wincap.get_screenshot()

    H, W, _ = frame.shape
    threshold = 0.5

    results = model(frame)[0]

    for result in results.boxes.data.tolist():
        x1, y1, x2, y2, score, class_id = result

        if score > threshold:
            cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 4)
            cv2.putText(frame, results.names[int(class_id)].upper(), (int(x1), int(y1 - 10)),
                        cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)

            box_center_x = (x1 + x2) / 2
            box_center_y = (y1 + y2) / 2
            screen_width = win32api.GetSystemMetrics(0)
            screen_height = win32api.GetSystemMetrics(1)

            # moving crosshair to model box
            target_x = int(screen_width * box_center_x / W)
            target_y = int(screen_height * box_center_y / H)
            pydirectinput.moveTo(target_x, target_y)

    cv2.imshow("COMPUTER VISION",frame)

    if cv2.waitKey(1) == ord('q'):
        cv2.destroyAllWindows()


I'm trying to hide the bounding boxes and the labels after a prediction in YOLOv8. I've set the necessary attributes but I still see the bounding boxes and labels in the final render.show(). What am I doing wrong?
# load model
model = YOLO('ultralyticsplus/yolov8m')

# set model parameters
conf = 0.2
iou = 0.5
ag_nms = False

# try to turn off bounding boxes and labels
model.overrides['hide_labels'] = True
model.overrides['hide_conf'] = True
model.overrides['show'] = False

# set image
image = 'pic.jpg'

# perform inference
results = model.predict(
    image,
    show=False,
    hide_labels=True,
    hide_conf=True,
    conf=conf,
    iou=iou,
)

# observe results
render = render_result(model=model, image=image, result=results[0])
render.show() # still sees the bounding boxes here


I'm using the Ultralytics YOLOv8 implementation to perform object detection on an image. However, when I try to retrieve the classification probabilities using the probs attribute from the results object, it returns None. Here's my code:
from ultralytics import YOLO

# Load a model
model = YOLO('yolov8n.pt')  # pretrained YOLOv8n model

# Run batched inference on a list of images
results = model('00000.png')  # return a list of Results objects

# Process results list
for result in results:
    boxes = result.boxes  # Boxes object for bbox outputs
    masks = result.masks  # Masks object for segmentation masks outputs
    keypoints = result.keypoints  # Keypoints object for pose outputs
    probs = result.probs  # Probs object for classification outputs

print(probs)

When I run the above code, the output for print(probs) is None. The remaining output is
image 1/1 00000.png: 640x640 1 person, 1 zebra, 7.8ms
Speed: 2.6ms preprocess, 7.8ms inference, 1.3ms postprocess per image at shape (1, 3, 640, 640)

Why is the probs attribute returning None, and how can I retrieve the classification probabilities for each detected object? Is there a specific design reason behind this behavior in the Ultralytics YOLOv8 implementation?


this is the code
from ultralytics import YOLO
license_plate_detector = YOLO('./model/best.pt')
license_plates = license_plate_detector('./42.jpg')

and this the output
640x608 1 number-plate, 342.0ms
Speed: 12.4ms preprocess, 342.0ms inference, 3.0ms postprocess per image at shape (1, 3, 640, 608)

i want to convert this output to image and save it to use with esayocr
the class don't have any save method so how to do this


I am trying to export .engine from onnx for the pretrained yolov8m model but get into trtexec issue. Note that I am targeting for a model supporting dynamic batch-size.
I got the onnx by following the official instructions from ultralytics.
from ultralytics import YOLO

# Load a model
model = YOLO('yolov8m.pt')  # load an official model
model = YOLO('path/to/best.pt')  # load a custom trained

# Export the model
model.export(format='onnx',dynamic=True) # Note the dynamic arg

I get the corresponding onnx. Now when I try to run trtexec
trtexec --onnx=yolov8m.onnx --workspace=8144 --fp16 --minShapes=input:1x3x640x640 --optShapes=input:2x3x640x640 --maxShapes=input:10x3x640x640 --saveEngine=my.engine

I get
[08/10/2023-23:53:10] [I] TensorRT version: 8.2.5
[08/10/2023-23:53:11] [I] [TRT] [MemUsageChange] Init CUDA: CPU +336, GPU +0, now: CPU 348, GPU 4361 (MiB)
[08/10/2023-23:53:11] [I] [TRT] [MemUsageSnapshot] Begin constructing builder kernel library: CPU 348 MiB, GPU 4361 MiB
[08/10/2023-23:53:12] [I] [TRT] [MemUsageSnapshot] End constructing builder kernel library: CPU 483 MiB, GPU 4393 MiB
[08/10/2023-23:53:12] [I] Start parsing network model
[08/10/2023-23:53:12] [I] [TRT] ----------------------------------------------------------------
[08/10/2023-23:53:12] [I] [TRT] Input filename:   yolov8m.onnx
[08/10/2023-23:53:12] [I] [TRT] ONNX IR version:  0.0.8
[08/10/2023-23:53:12] [I] [TRT] Opset version:    17
[08/10/2023-23:53:12] [I] [TRT] Producer name:    pytorch
[08/10/2023-23:53:12] [I] [TRT] Producer version: 2.0.1
[08/10/2023-23:53:12] [I] [TRT] Domain:
[08/10/2023-23:53:12] [I] [TRT] Model version:    0
[08/10/2023-23:53:12] [I] [TRT] Doc string:
[08/10/2023-23:53:12] [I] [TRT] ----------------------------------------------------------------
[08/10/2023-23:53:12] [W] [TRT] onnx2trt_utils.cpp:366: Your ONNX model has been generated with INT64 weights, while TensorRT does not natively support INT64. Attempting to cast down to INT32.
[08/10/2023-23:53:12] [E] [TRT] ModelImporter.cpp:773: While parsing node number 305 [Range -> "/model.22/Range_output_0"]:
[08/10/2023-23:53:12] [E] [TRT] ModelImporter.cpp:774: --- Begin node ---
[08/10/2023-23:53:12] [E] [TRT] ModelImporter.cpp:775: input: "/model.22/Constant_8_output_0"
input: "/model.22/Cast_output_0"
input: "/model.22/Constant_9_output_0"
output: "/model.22/Range_output_0"
name: "/model.22/Range"
op_type: "Range"

[08/10/2023-23:53:12] [E] [TRT] ModelImporter.cpp:776: --- End node ---
[08/10/2023-23:53:12] [E] [TRT] ModelImporter.cpp:779: ERROR: builtin_op_importers.cpp:3353 In function importRange:
[8] Assertion failed: inputs.at(0).isInt32() && "For range operator with dynamic inputs, this version of TensorRT only supports INT32!"
[08/10/2023-23:53:12] [E] Failed to parse onnx file
[08/10/2023-23:53:12] [I] Finish parsing network model
[08/10/2023-23:53:12] [E] Parsing model failed
[08/10/2023-23:53:12] [E] Failed to create engine from model.

I am aware that some people suggest upgrading to latest TRT version but I am looking for an alternate solution.


I have a YOLOv7 model trained on my custom dataset. I exported the model to TensorFlow lite successfully and was able to use it for inference in Python. But when I try to use the same model in Android, using the object detection project with TensorFlow lite, it throws this error:
java.lang.IllegalArgumentException: Error occurred when initializing ObjectDetector: The input tensor should have dimensions 1 x height x width x 3. Got 1 x 3 x 640 x 640.
Is it possible to change the input shape for the ObjectDetector class, or export the YOLOv7 or YOLOv5 model with corresponding input shape?
I tried to tweak the export process to change the input shape of ONNX model which is the intermediate model in exporting from PyTorch to TensorFlow Lite but it throws this error:
ONNX export failure: Given groups=1, weight of size [32, 3, 3, 3], expected input[1, 640, 640, 3] to have 3 channels, but got 640 channels instead
update: I used onnx2tf to export .tflite model with NHWC input shape. Now the Android project throws this error:
java.lang.RuntimeException: Error occurred when initializing ObjectDetector: Input tensor has type kTfLiteFloat32: it requires specifying NormalizationOptions metadata to preprocess input images.
I couldn't find a way to add normalization options to metadata using this doc. Any solutions?


I have a trained model and I have detected my required object using following code
import cv2
from PIL import Image
from ultralytics import YOLO

image = cv2.imread("screenshot.png")
model = YOLO('runs/detect/train4/weights/best.pt')
results = model.predict(image, show=True, stream=True, classes=0, imgsz=512)
for result in results:
    for box in result.boxes:
        class_id = result.names[box.cls[0].item()]
        if (class_id == "myclassname"):
            cords = box.xyxy[0].tolist()
            cords = [round(x) for x in cords]
            conf = round(box.conf[0].item(), 2)
            print("Object type:", class_id)
            print("Coordinates:", cords)
            print("Probability:", conf)
            print("---")

From this detected portion of image I need to detect an other class how I can do that?
I have searched enough but I could not see any post for this.


I trained a model to detect traffic lights and classify thier color, [red,green,yellow,off].
I want to only display the lights with a confidence above 50%, but I cant figure out how to do that with yolo v8. Ive tried using .conf and .prob as the documentation states but its all empty. Here is my current script. It streams webcam view and looks for stoplights.
Any help to get confidence values or even just the classification values from this would be amazing. I have uploaded the model to github here for people that want to test.
I reccomend using the best_traffic_nano_yolo.pt model as its the most lightweight.
Googling traffic light at intersection and holding an your phone up to the webcam should be enough for the model to detect and classify the light.
import cv2
from PIL import Image
from ultralytics import YOLO

# Load a pretrained YOLOv8n model
model = YOLO('/Models/best_traffic_nano_yolo.pt')

# open a video file or start a video stream
cap = cv2.VideoCapture(0)  # replace with 0 for webcam

while cap.isOpened():
    # Capture frame-by-frame
    ret, frame = cap.read()
    if not ret:
        break

    # flip the image
    # frame = cv2.flip(frame, -1)

    # Run inference on the current frame
    results = model(frame)  # results list

    for r in results:
        frame = r.plot()

    # Display the resulting frame
    cv2.imshow('frame', frame)

    # Press 'q' on keyboard to  exit
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# After the loop release the cap object and destroy all windows
cap.release()
cv2.destroyAllWindows()


I am currently working with Ultralytics Yolov8 model for object detection.
I trained my model with a custom dataset. I annotated my dataset on Roboflow and exported in Yolov8 format.
The training result looks good. However there is a mismatch between the coordinates of bounding boxes of my manual annotation on Roboflow and that of prediction.
For example I have this image called image_1.jpg which is annotated on Roboflow and is part of training set. Roboflow defined the coordinates (as the part of the label):

[0 0.36953125 0.39609375 0.54765625 0.7875]

where the last 4 numbers showing the x1,x2,y1,y2 coordinates of the bounding boxes. However when I passed this image into predict method (just for checking) using the following piece of code
model = YOLO(‘best_weights.pt’)
result = model.predict(path_of_example_image, save = True)
boxes = result[0].boxes.xyxy

I got the coordinates:

tensor([[ 22.5558,   9.7429, 619.7601, 345.7614]])

When I draw the bounding box with the  predicted coordinates, the area covers the object completely (with .99% accuracy on the (correctly) predicted class). On the other hand, when I draw the bounding box with the coordinates from manual annotation, the object is not covered at all as, obviously, all coordinates are below 1. So I just wonder what kind of preprocessing is applied within the model so that the coordinates from Roboflow's labelling make sense and my training result is actually good?


So, I'm trying to train a YOLO classification model on a custom dataset that contains jpg-images of two different classes. But when I launch training, I get following error:
Traceback (most recent call last):
  File "C:\pythonProject\main.py", line 5, in <module>
    model.train(data='./training_dataset', epochs=1, imgsz=64)
  File "C:\pythonProject\venv\lib\site-packages\ultralytics\engine\model.py", line 341, in train
    self.trainer.train()
  File "C:\pythonProject\venv\lib\site-packages\ultralytics\engine\trainer.py", line 192, in train
    self._do_train(world_size)
  File "C:\pythonProject\venv\lib\site-packages\ultralytics\engine\trainer.py", line 288, in _do_train
    self._setup_train(world_size)
  File "C:\pythonProject\venv\lib\site-packages\ultralytics\engine\trainer.py", line 255, in _setup_train
    self.test_loader = self.get_dataloader(self.testset, batch_size=batch_size * 2, rank=-1, mode='val')
  File "C:\pythonProject\venv\lib\site-packages\ultralytics\models\yolo\classify\train.py", line 88, in get_dataloader
    dataset = self.build_dataset(dataset_path, mode)
  File "C:\pythonProject\venv\lib\site-packages\ultralytics\models\yolo\classify\train.py", line 83, in build_dataset
    return ClassificationDataset(root=img_path, args=self.args, augment=mode == 'train', prefix=mode)
  File "C:\pythonProject\venv\lib\site-packages\ultralytics\data\dataset.py", line 220, in __init__
    super().__init__(root=root)
  File "C:\pythonProject\venv\lib\site-packages\torchvision\datasets\folder.py", line 309, in __init__
    super().__init__(
  File "C:\pythonProject\venv\lib\site-packages\torchvision\datasets\folder.py", line 145, in __init__
    samples = self.make_dataset(self.root, class_to_idx, extensions, is_valid_file)
  File "C:\pythonProject\venv\lib\site-packages\torchvision\datasets\folder.py", line 189, in make_dataset
    return make_dataset(directory, class_to_idx, extensions=extensions, is_valid_file=is_valid_file)
  File "C:\pythonProject\venv\lib\site-packages\torchvision\datasets\folder.py", line 61, in make_dataset
    directory = os.path.expanduser(directory)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\ntpath.py", line 293, in expanduser
    path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not NoneType

I've tried two different datasets and that problem occured on both of them.
My full code:
from ultralytics import YOLO

model = YOLO('yolov8n-cls.pt')

model.train(data='./training_dataset', epochs=1, imgsz=64)

Dataset structure:
training_dataset/train/cats;
training_dataset/train/dogs


I have trained my YoloV8 detection model.
After training the model there are plenty of files stored within in the train folder.
One of the files are the train_batch.jpg and the val_batch.jpg.
The images within the train_batch.jpg are cutted of and scrambled.

The images within the val_batch.jpg looks fine.

The imagesize of the images within train_batch and val_batch are the same.
Does someone know if this is a problem?
I have tried to change the image sizes and used various batch sizes.


What are the class IDs and their corresponding class names for YOLOv8 models? I understand there are approximately 80 classes in the object detection model of YOLOv8. However, I'm looking to specifically identify each class along with their respective class IDs. Additionally, do all YOLO models (yolov3, yolov5, yolov7, yolov8) have the same number of classes and corresponding class IDs?


I need some help as I will be needing this to work for my final thesis project for the Model has 2 classes inheat and non-inheat, I did the codes here below but when it tries to predict or detect by frames of the video it detects the non-inheat but it only stacks in the ininheat frames instead of the non_ inheat frames I might be doing a wrong process here for by frames in stacking them up even its detecting non-inheat it stacks to the in_heat frames.
codes:
def process_video_with_second_model(video_path):
    cap = cv2.VideoCapture(video_path)
    class_counts = {'inheat': 0, 'non-inheat': 0}

    in_heat_frames = []
    non_in_heat_frames = []

    while True:
        ret, frame = cap.read()
        if frame is None:
            break # Break the loop when no more frames are available

        # Resize the frame to a smaller size (e.g., 400x400)
        frame_small = cv2.resize(frame, (400, 400))

        # Use the second model to detect in-heat behavior
        results_in_heat = yolov8_model_in_heat.predict(source=frame_small, show=True, conf=0.8)

        # Print results to inspect structure
        for results_in_heat_instance in results_in_heat:
            # Access bounding box coordinates
            boxes = results_in_heat_instance.boxes

            # CONFIDENCE 0.5
            if len(boxes) > 0:
                class_name = results_in_heat_instance.names[0]

                # Use a dictionary to store the counts for each class
                class_counts[class_name] += 1

                # Add the frame to the corresponding list based on the class name
                if class_name == 'non-inheat':
                    non_in_heat_frames.append(frame)
                elif class_name == 'inheat':
                    in_heat_frames.append(frame)

        print(f"Class Counts: {class_counts}")

        # Check if either condition is met (50 frames for inheat and 50 frames for non-inheat)
        if class_counts['inheat'] >= 50 and class_counts['non-inheat'] >= 50:
            break

    # Release resources for the second model
    cap.release()
    cv2.destroyAllWindows()

    # Stack the in-heat and non-in-heat frames vertically
    stacked_in_heat_frames = np.vstack(in_heat_frames)
    stacked_non_in_heat_frames = np.vstack(non_in_heat_frames)

    # Display the stacked in-heat and non-in-heat frames
    cv2.imshow('Stacked In-Heat Frames', stacked_in_heat_frames)
    cv2.imshow('Stacked Non-In-Heat Frames', stacked_non_in_heat_frames)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    # Compare the counts and return the label with the higher count
    if class_counts['inheat'] > class_counts['non-inheat']:
        return 'inheat'
    elif class_counts['non-inheat'] > class_counts['inheat']:
        return 'non-inheat'

I did read the YOLOv8 Documents for predicting but still cant do it. I did this in the Pycharm IDE


I am using Ultralytics YOLO for license plate detection, and I'm encountering an issue when trying to extract bounding box coordinates from the Results.boxes object. I have inspected the structure of the Results.boxes object, but I am having difficulty accessing the bounding box information correctly.
class ImageProcessing:
    def __init__(self, model_path: Path, input_image: Path, output_image: Path):
        if not isinstance(model_path, Path):
            raise TypeError("model_path must be a pathlib.Path instance")
        if not isinstance(input_image, Path) or not isinstance(output_image, Path):
            raise TypeError("input_image and output_image must be pathlib.Path instances")
        # Load the YOLO model from the provided path
        self.model = YOLO(str(model_path))
        self.input_image = input_image
        self.output_image = output_image

    def ascertain_license_plates_as_image(self, threshold: float = 0.5, fontscale: float = 1.3, color: tuple = (0, 255, 0), thickness: int = 3):
        image = opencv.imread(str(self.input_image))
        results = self.model(image)

        # Check if results is a list and get the first result
        if isinstance(results, list):
            results = results[0]

        # Iterate through each detected object
        for box in results.boxes:
            # Extract coordinates, confidence, and class ID
            x1, y1, x2, y2, conf, class_id = box.data[0][0], box.data[0][1], box.data[0][2], box.data[0][3], box.conf.item(), int(box.cls.item())
            if conf > threshold:
                opencv.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), color, thickness)
                label = results.names[class_id].upper() if results.names else f'class {class_id}'
                opencv.putText(image, label, (int(x1), int(y1) - 10), opencv.FONT_HERSHEY_SIMPLEX, fontscale, color, thickness, opencv.LINE_AA)

        opencv.imwrite(str(self.output_image), image)
        return results

However, I'm getting an IndexError, and it seems that my indexing might be incorrect for this particular Boxes object. Or even worse, cv2 is not highlighting the license plate.


I’ve custom trained a yolov8n model and I would like to run it on a Jetson Nano. Has anyone managed to do this? If yes, would you be so kind to help me out?
I’ve got a .pt of the custom trained model. The OS image offered by NVidia on their website is an Ubuntu 18.04 and I have run into many compatibility issues. I’m interested in finding out if anyone has managed to get yolo running on the Jetson specifically the yolov8n model from ultralytics.
Thanks in advance!


I'm currently working on an object detection project using YOLOv8 and a customized dataset. In my dataset, each image is accompanied by its corresponding label file (Data_1.png -> Data_1.txt).
The label file Data_1.txt follows the format:
Class_type, x_1 min, y_1 min, ..., x_4 min, y_4 min.

I'm interested in applying a perspective augmentation to my dataset, and I've decided to use the YOLOv8 augmentation functionality. However, I am unsure whether YOLOv8 generates labels for the augmented data or not. If it does not, I would greatly appreciate any suggestions or alternative approaches to handle the generation of labels for the augmented data.


How do I get the class names of segmented instances when detecting multiple classes in YOLOv8? The detections do not have a .cls attribute like here YOLOv8 get predicted class name. Also the docs do not seem to mention anything e.g. here
When I use the show=true argument in the prediction function, the classes are distinguished in the resulting image, but I cannot get them programmatically. My code that gets me all detections I wanjt but does not let me know which one is which:
from ultralytics import YOLO
model = YOLO("path/to/best.pt")
result = model.predict(os.path.join(cut_dir, im_name), save_conf=True, show=True)
if result[0].masks is not None:
    for counter, detection in enumerate(result[0].masks.data):
         detected = np.asarray(detection.cpu())


I want to close the detection window on a key press and not have to stop the entire code. I am able to do it for my image detection but since video runs on a loop I can't find how.
            root.video = filedialog.askopenfilename(initialdir="/desktop", title="Select video to detect")
            cap = cv2.VideoCapture(root.video)

            model = YOLO('best.pt')

            classNames = ['Orange', 'Orange', 'Pomegranate', 'Pomegranate', 'apple', 'apple', 'banana', 'banana',
                          'fruits', 'guava', 'guava', 'guava', 'lime', 'lime']
            while True:
                success, vid = cap.read()
                results = model(img, stream=True)

                        cv2.imshow("DetectVideo", vid)
                        cv2.waitKey(1)

I have tried the following
k = cv2.waitKey(0) #also tried with (1)
print(k)
if k == 27:  # close on ESC key
    cv2.destroyAllWindows()

which basically makes it so it just closes each frame of the video then continues to next frame on key press.


def extract_and_process_tracks(self, tracks):
    boxes = tracks[0].boxes.xyxy.cpu()
    clss = tracks[0].boxes.cls.cpu().tolist()
    track_ids = tracks[0].boxes.id.int().cpu().tolist()

    self.annotator = Annotator(self.im0, self.tf, self.names)
    self.annotator.draw_region(reg_pts=self.reg_pts, color=(0, 255, 0))

    for box, track_id, cls in zip(boxes, track_ids, clss):
        self.annotator.box_label(box, label=self.names[cls], color=colors(int(cls), True))

        # Draw Tracks
        track_line = self.track_history[track_id]
        track_line.append((float((box[0] + box[2]) / 2), float((box[0] + box[2]) / 2))
        track_line.pop(0) if len(track_line) > 30 else None

        if self.draw_tracks:
            self.annotator.draw_centroid_and_tracks(track_line,
                                                    color=(0, 255, 0),
                                                    track_thickness=self.track_thickness)

The object_counter.py provided by ultralytics can achieve the counting job, the track_line.append store the center of the
box(float((box[0] + box[2]) / 2), float((box[0] + box[2]) / 2),
but how to change the center into the keypoints coordinate, e.g. I want to count the head keypoints of animal cross a specified line.
How to get the keypoints coordinate in yolo_pose?


I have my own pre-trained YOLO model, and I want to have different colors of LEDs light up on Arduino Uno breadbroad when different labels are being detected from my webcam.
So, I was thinking of first assigning the command for different LEDs to light up. ("G" for green led, "R" for red led, "Y" for yellow led, "A" for turning both red and yellow led at the same time, and "0" to turn off all led.
)
I started with my Arduino code as below:
char command;

void setup() {
    Serial.begin(9600);
    pinMode(2, OUTPUT);  // Green LED pin
    pinMode(3, OUTPUT);  // Red LED pin
    pinMode(4, OUTPUT);  // Yellow LED pin
}

void loop() {
    if (Serial.available() > 0) {
        command = Serial.read();
        if (command == 'G') {
            // Turn on green LED
            digitalWrite(2, HIGH);
            digitalWrite(3, LOW);  // Turn off red LED
            digitalWrite(4, LOW);  // Turn off yellow LED
        } else if (command == 'R') {
            // Turn on red LED
            digitalWrite(2, LOW);   // Turn off green LED
            digitalWrite(3, HIGH);
            digitalWrite(4, LOW);   // Turn off yellow LED
        } else if (command == 'Y') {
            // Turn on yellow LED
            digitalWrite(2, LOW);   // Turn off green LED
            digitalWrite(3, LOW);   // Turn off red LED
            digitalWrite(4, HIGH);
        } else if (command == 'A') {
            // Turn on both red and yellow LEDs
            digitalWrite(2, LOW);   // Turn off green LED
            digitalWrite(3, HIGH);
            digitalWrite(4, HIGH);
        } else if (command == '0') {
            // Turn off all LEDs
            digitalWrite(2, LOW);
            digitalWrite(3, LOW);
            digitalWrite(4, LOW);
        }
    }
}

In my YOLO model, there are three classes
#Classes
names:
  0: aaa
  1: bbb
  2: ccc

Therefore, for my python code I started with first turning on the webcam and loading then using serial library to connect to arduino.
from ultralytics import YOLO
import serial
import time
import sys

# Load my YOLO model using
model = YOLO("best.pt")

# Open the serial port for communication with Arduino
arduino = serial.Serial('COM3', 9600)  # Change 'COM3' to your Arduino port

after that, I want to assign the labels with command to Arduino, and I also try to do a little
# Map the class labels to corresponding Arduino commands
label_commands = {
    'aaa': 'G',
    'bbb': 'R',
    'ccc': 'Y',
    'aaa_bbb': 'A',
    'aaa_bbb_ccc': 'A',
    'none': '0'
}

while True:
    try:
        results = model.predict(source="0", show=True)  # assumes '0' is your source identifier

        # Extract the detected labels from results
        detected_labels = [item['label'] for item in results.xyxy[0].numpy()]

        # Determine the Arduino command based on detected labels
        if 'aaa' in detected_labels and 'bbb' in detected_labels and 'ccc' in detected_labels:
            command = label_commands['aaa_bbb_ccc']
        elif 'aaa' in detected_labels and 'bbb' in detected_labels:
            command = label_commands['aaa_bbb']
        elif 'aaa' in detected_labels:
            command = label_commands['aaa']
        elif 'bbb' in detected_labels and 'ccc' in detected_labels:
            command = label_commands['aaa_bbb_ccc']
        elif 'bbb' in detected_labels:
            command = label_commands['bbb']
        elif 'ccc' in detected_labels:
            command = label_commands['ccc']
        else:
            command = label_commands['none']

        # Print the command for debugging
        print(f"Sending command to Arduino: {command}")
        sys.stdout.flush()  # Flush the standard output

        # Send the command to Arduino
        arduino.write(command.encode())

    except Exception as e:
        print(f"Error: {e}")
        sys.stdout.flush()  # Flush the standard output

    # Wait for 5 seconds before sending the next command
    time.sleep(5)

BUT, this part of code doesn't work at all. I know my arduino is connected for sure, and I have also tested with from randoming sending the letter command to arduino from python, which worked perfectly.
Also when I run this code, the run window are showing only things like:
1/1: 0... Success ✅ (inf frames of shape 640x480 at 25.00 FPS)

WARNING ⚠ inference results will accumulate in RAM unless `stream=True` is passed, causing potential out-of-memory
errors for large sources or long-running streams and videos. See https://docs.ultralytics.com/modes/predict/ for help.

Example:
    results = model(source=..., stream=True)  # generator of Results objects
    for r in results:
        boxes = r.boxes  # Boxes object for bbox outputs
        masks = r.masks  # Masks object for segment masks outputs
        probs = r.probs  # Class probabilities for classification outputs

0: 480x640 (no detections), 141.0ms
0: 480x640 1 aaa, 182.2ms
0: 480x640 1 aaa, 134.3ms
0: 480x640 1 aaa, 122.1ms
0: 480x640 (no detections), 121.4ms
0: 480x640 (no detections), 147.5ms
0: 480x640 (no detections), 131.5ms
0: 480x640 (no detections), 140.3ms
0: 480x640 1 aaa, 127.1ms
0: 480x640 1 aaa, 124.1ms
0: 480x640 1 aaa, 123.2ms
0: 480x640 1 aaa, 172.2ms
...

even with my debugging script, it doesn't tell me what command is being sent to the arduino broad
        # Print the command for debugging
        print(f"Sending command to Arduino: {command}")
        sys.stdout.flush()  # Flush the standard output


can you please help me.........
I have my custom trained model (best.pt), it detects two things person and headlight. Now I want the output according to these conditions: 1. If model detect only headlight return 0, 2. If model detect only person return 1, 3. If model detect headlight and person both return 0.
import cv2
from ultralytics import YOLO

video_path = 'data/video1.mp4'
video_out_path = 'out.mp4'

cap = cv2.VideoCapture(video_path)

# Check if the video file is opened successfully
if not cap.isOpened():
    print("Error: Could not open the video file.")
    exit()

ret, frame = cap.read()

# Check if the first frame is read successfully
if not ret:
    print("Error: Could not read the first frame from the video.")
    exit()

cap_out = cv2.VideoWriter(video_out_path, cv2.VideoWriter_fourcc(*'MP4V'), cap.get(cv2.CAP_PROP_FPS),
                          (int(cap.get(3)), int(cap.get(4))))  # Use cap.get(3) and cap.get(4) for width and height

model = YOLO("bestall5.pt")

detection_threshold = 0.5
while ret:
    results_list = model(frame)

    headlight_detected = False
    person_detected = False

    # Iterate through the list of results
    for results in results_list:
        # Check if the current result has the necessary attributes
        if hasattr(results, 'xyxy'):
            for result in results.xyxy:
                x1, y1, x2, y2, score, class_id = result.tolist()
                x1, x2, y1, y2 = int(x1), int(x2), int(y1), int(y2)

                # Assuming class_id is the index of the class in the model's class list
                class_name = model.names[class_id]

                if class_name == "headlight" and score > detection_threshold:
                    headlight_detected = True
                elif class_name == "person" and score > detection_threshold:
                    person_detected = True

    # Output based on the specified conditions
    if headlight_detected and person_detected:
        output = 0
    elif headlight_detected:
        output = 0
    elif person_detected:
        output = 1
    else:
        output = -1  # No person or headlight detected

    print("Output:", output)

    cap_out.write(frame)

    cv2.imshow('Object Detection', frame)

    # Break the loop if 'q' key is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    ret, frame = cap.read()

cap.release()
cap_out.release()
cv2.destroyAllWindows()

I tried this but getting only -1 as output but my video has both headlight and person


I am currently working with YOLOv8 and I'm wondering if there is a method similar to results.pandas().xyxy available in YOLOv5 to obtain structured results in tabular form.
With YOLOv5, it's possible to get results easily using the following code:
results = model(img)
df = results.pandas().xyxy[0]  # Get results in tabular format
print(df)
#      xmin    ymin    xmax   ymax  confidence  class    name
# 0  749.50   43.50  1148.0  704.5    0.874023      0  person
# 1  433.50  433.50   517.5  714.5    0.687988     27     tie
# 2  114.75  195.75  1095.0  708.0    0.624512      0  person
# 3  986.00  304.00  1028.0  420.0    0.286865     27     tie


When my nn starts learning, the kernel crashes
The Kernel crashed while executing code in the current cell or a previous cell.

Please review the code in the cell(s) to identify a possible cause of the failure.

Click here for more info.

View Jupyter log for further details.

Yolo doesn't use my GPU resources for learning instead it trying to use CPU and I think that it is a reason why kernel crashes.
How I can fix it?
screenshot
I tried to install CUDA 7.5 (for my RTX 2060). I recently started to study neural networks and I am not sure that I need CUDA.
I also checked similar topics here but I am not sure that there is my case.


Let's say I have a folder called 'test' with folders inside, 'images' and 'labels'. I also have a YOLOv8 model which I've trained called 'best.pt. My labels are polygons (yolo-obb .txt files).
I want to find the mean average precision (MAP) of my YOLOv8 model on this test set.
I've read both the documentation for predicting and benchmarking, however, I'm struggling to find an example of calculating map from some test images.
https://docs.ultralytics.com/modes/predict/
https://docs.ultralytics.com/modes/benchmark/
from ultralytics import YOLO

# Load a pretrained YOLOv8n model
model = YOLO('best.pt')

# Run inference on an image
results = model(['test/images/bus.jpg', 'test/images/zidane.jpg'])  # list of 2 Results objects

I imagine I have to put the list of images in the above, then write code to calculate map for everything in the test folder and average it. Are there packages that have already done this?
What's the code to achieve this task?


I have the following code
from ultralytics import YOLO
import cv2
import math
import os
import time

# Start webcam
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)

# Load custom model
model_path = os.path.join('.', 'runs', 'detect', 'train', 'weights', 'last.pt')
model = YOLO(model_path)  # load a custom model

# Define your custom object classes
classNames = ["reese_pretzel"]  # Update with your custom classes

# Confidence threshold
confidence_threshold = 0.5

# Initialize variables for tracking time
start_time = None
end_time = None
object_detected = False

while True:
    success, img = cap.read()
    results = model(img, stream=True)

    # Process results
    for r in results:
        boxes = r.boxes

        for box in boxes:
            # Extract box coordinates
            x1, y1, x2, y2 = box.xyxy[0]
            x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)

            # Confidence
            confidence = math.ceil((box.conf[0]*100))/100

            # Check confidence threshold
            if confidence > confidence_threshold:
                # Class name
                cls = int(box.cls[0])
                class_name = classNames[cls]

                # Draw bounding box
                cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 3)

                # Object details
                org = [x1, y1]
                font = cv2.FONT_HERSHEY_SIMPLEX
                fontScale = 1
                color = (255, 0, 0)
                thickness = 2

                cv2.putText(img, class_name, org, font, fontScale, color, thickness)

                # Set start time when an object is first detected
                if not object_detected:
                    start_time = time.time()
                    object_detected = True
            else:
                # Reset start time when no object is detected
                start_time = None
                object_detected = False

    cv2.imshow('Webcam', img)
    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

I keep getting outputs to my console like
0: 480x640 (no detections), 49.2ms
Speed: 0.9ms preprocess, 49.2ms inference, 0.2ms postprocess per image at shape (1, 3, 480, 640)

or
0: 384x640 1 reese_pretzel, 75.0ms
Speed: 4.8ms preprocess, 75.0ms inference, 0.3ms postprocess per image at shape (1, 3, 384, 640)

Where are these messages getting printed from?
I tried looking at the YOLO model.py file but that too doesn't have any print statements in it. I want to do something like if there are no detections, print('no detect), and if something is detected, print the name of the object.


When training a Yolo model like below:
from ultralytics import YOLO

# Load a model
model = YOLO('yolov8s.pt')

results = model.train(data='coco128.yaml',
epochs=100, imgsz=640, save_period=1)

The save_period option will save every epoch. When the best epoch is found the file is saved as best.pt.
The file size of best.pt is ~27MB and each epoch is ~120MB. Is it possible to use the compression applied to the best epoch at the end of training to every epoch (even if this is done after training).


I'm encountering a problem with creating space after uploading app.py and requirements.txt. For loading the model, I'm using this code: from ultralyticsplus import YOLO, render_result.
(I use Gradio to create a website.)
The main task for uploading app.py to the space is that I want the HTML code to be embedded on the Google site.
model_path = ('(my model path on huggingface')
model = YOLO(model_path)

If I use another method to load the model instead of using YOLO, is it possible to fix this error?
The error said
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/ultralyticsplus/ultralytics_utils.py", line 59, in __init__
    self._load_from_hf_hub(model, hf_token=hf_token)
  File "/usr/local/lib/python3.10/site-packages/ultralyticsplus/ultralytics_utils.py", line 91, in _load_from_hf_hub
    ) = self._assign_ops_from_task()
  File "/usr/local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1614, in __getattr__
    raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'YOLO' object has no attribute '_assign_ops_from_task'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/user/app/app.py", line 95, in <module>
    gr.Interface(fn=detect_objects,
  File "/usr/local/lib/python3.10/site-packages/gradio/interface.py", line 518, in __init__
    self.render_examples()
  File "/usr/local/lib/python3.10/site-packages/gradio/interface.py", line 851, in render_examples
    self.examples_handler = Examples(
  File "/usr/local/lib/python3.10/site-packages/gradio/helpers.py", line 71, in create_examples
    examples_obj.create()
  File "/usr/local/lib/python3.10/site-packages/gradio/helpers.py", line 298, in create
    client_utils.synchronize_async(self.cache)
  File "/usr/local/lib/python3.10/site-packages/gradio_client/utils.py", line 889, in synchronize_async
    return fsspec.asyn.sync(fsspec.asyn.get_loop(), func, *args, **kwargs)  # type: ignore
  File "/usr/local/lib/python3.10/site-packages/fsspec/asyn.py", line 103, in sync
    raise return_result
  File "/usr/local/lib/python3.10/site-packages/fsspec/asyn.py", line 56, in _runner
    result[0] = await coro
  File "/usr/local/lib/python3.10/site-packages/gradio/helpers.py", line 360, in cache
    prediction = await Context.root_block.process_api(
  File "/usr/local/lib/python3.10/site-packages/gradio/blocks.py", line 1695, in process_api
    result = await self.call_function(
  File "/usr/local/lib/python3.10/site-packages/gradio/blocks.py", line 1235, in call_function
    prediction = await anyio.to_thread.run_sync(
  File "/usr/local/lib/python3.10/site-packages/anyio/to_thread.py", line 56, in run_sync
    return await get_async_backend().run_sync_in_worker_thread(
  File "/usr/local/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 2144, in run_sync_in_worker_thread
    return await future
  File "/usr/local/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 851, in run
    result = context.run(func, *args)
  File "/usr/local/lib/python3.10/site-packages/gradio/utils.py", line 692, in wrapper
    response = f(*args, **kwargs)
  File "/home/user/app/app.py", line 24, in detect_objects
    model = YOLO(model_path)
  File "/usr/local/lib/python3.10/site-packages/ultralyticsplus/ultralytics_utils.py", line 65, in __init__
    raise NotImplementedError(
NotImplementedError: Unable to load model='MvitHYF/v8mvitcocoaseed2024'. As an example try model='yolov8n.pt' or model='yolov8n.yaml'

I also ran app.py on VSCode, and everything ran perfectly (run on localhost). However, I encountered this error when trying to create the space. I tried adding yolov8n.pt to both the model and the space site, but nothing changed. At first, I thought it might fix the error.
Thank you for you help


I downloaded one of the pretrained yolo models from the link:
https://github.com/WongKinYiu/yolov7/releases
In this case, yolov7-tiny.pt is downloaded.
Then tried to run the code to load the model and convert it to onnx file:
import torch
import onnx

model = torch.load('./yolo_custom/yolov7-tiny.pt')
input_shape = (1, 3, 640, 640)
torch.onnx.export(model, torch.randn(input_shape), 'yolov7-tiny.onnx', opset_version=11)

An error occurs on
model = torch.load('./yolo_custom/yolov7-tiny.pt')

and the error message is:
ModuleNotFoundError: No module named 'models'

The issue is reproducible even on Colab. Is there anything wrong on the steps?