Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get original pixel coordinates ? #2807

Closed
asaf0813 opened this issue Apr 16, 2021 · 4 comments
Closed

How to get original pixel coordinates ? #2807

asaf0813 opened this issue Apr 16, 2021 · 4 comments
Labels
question Further information is requested

Comments

@asaf0813
Copy link

asaf0813 commented Apr 16, 2021

❔Question

I used the 'xywh2xyxy' function in 'general.py' to get the 'xyxy' value. I thought this was the original pixel coordinates. But it seemed to come out as the pixel coordinates coming out of each grid.

ex.

0: 384x640 2 persons, 1 backpack, 1 chair, 1 tv, 1 keyboard, Done. (1.326s)

[object num x y w h]
56 0.316146 0.593982 0.159375 0.410185
24 0.316406 0.59537 0.160938 0.405556
62 0.0744792 0.437963 0.0572917 0.116667
0 0.527083 0.417593 0.0666667 0.238889
66 0.815625 0.873148 0.365625 0.246296
0 0.442969 0.465741 0.139062 0.366667

[x y x y]
[[0.23645833134651184, 0.3888888955116272, 0.3958333432674408, 0.799074113368988], [0.23593750596046448, 0.39259257912635803, 0.3968749940395355, 0.7981481552124023], [0.04583333432674408,
0.37962961196899414, 0.10312500596046448, 0.4962962865829468], [0.4937500059604645, 0.29814815521240234, 0.5604166984558105, 0.5370370149612427], [0.6328125, 0.75, 0.9984375238418579, 0.9
962962865829468], [0.3734375238418579, 0.28240740299224854, 0.512499988079071, 0.6490740776062012]]

The above values ​​look like coordinates for each grid. Is that right?
But I want to get the bounding box x1,y1,x2,y2 values ​​at the original ('384x640') pixel.

@asaf0813 asaf0813 added the question Further information is requested label Apr 16, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Apr 16, 2021

👋 Hello @haspberry, thank you for your interest in 🚀 YOLOv5! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

Environments

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

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

@GiorgioSgl
Copy link

You can use the function scale_coords in utils.general that is in this repository

Btw this is my code hope it can help to understand how to use it. Search for the name of the function with CTRL+F and you can find it and understand deeper

from pathlib import Path

import cv2
import torch
import torch.backends.cudnn as cudnn
from numpy import random

from models.experimental import attempt_load
from utils.datasets import LoadStreams, LoadImages
from utils.general import check_img_size, check_requirements, check_imshow, non_max_suppression, apply_classifier, \
    scale_coords, xyxy2xywh, strip_optimizer, set_logging, increment_path
from utils.plots import plot_one_box
from utils.torch_utils import select_device, load_classifier, time_synchronized
import torch.nn as nn

iou_thres = 0.45
conf_thres = 0.01
device_input = ''
weights = "yolov5s.pt"
augment = None

def detect_single_image(image_path,image_name):
    imgsz = 320
    source = image_path + image_name

    device = select_device(device_input)
    half = device.type != 'cpu'  # half precision only supported on CUDA

    # Load model
    model = attempt_load(weights, map_location=device)  # load FP32 model
    stride = int(model.stride.max())  # model stride
    imgsz = check_img_size(imgsz, s=stride)  # check img_size
    if half:
        model.half()  # to FP16

    # Set Dataloader
    vid_path, vid_writer = None, None
    save_img = True
    dataset = LoadImages(source, img_size=imgsz, stride=stride)

    # Get names and colors
    names = model.module.names if hasattr(model, 'module') else model.names
    colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]
    # Run inference
    if device.type != 'cpu':
        model(torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters())))  # run once
    
    img_ = ''
    for path, img, im0s, vid_cap in dataset:
      img_=img
    #img = dataset[0]
    img = torch.from_numpy(img_).to(device)
    img = img.half() if half else img.float()  # uint8 to fp16/32
    img /= 255.0  # 0 - 255 to 0.0 - 1.0
        
    if img.ndimension() == 3:
        img = img.unsqueeze(0)

    pred = model(img, augment=False)[0]
    pred = non_max_suppression(pred, conf_thres, iou_thres, None, None)

    result = []
    # Process detections
    for i, det in enumerate(pred):  # detections per image
        p, s, im0, frame = path, '', im0s, getattr(dataset, 'frame', 0)

        s += '%gx%g ' % img.shape[2:]  # print string
        gn = torch.tensor(im0.shape)[[1, 0, 1, 0]]  # normalization gain whwh
        if len(det):
            # Rescale boxes from img_size to im0 size
            det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()

            # Print results
            #for c in det[:, -1].unique():
            #    n = (det[:, -1] == c).sum()  # detections per class
            #    s += f"{n} {names[int(c)]}{'s' * (n > 1)}, "  # add to string

            # Write results
            for *xyxy, conf, cls in reversed(det):
                '''
                if save_txt:  # Write to file
                    xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist()  # normalized xywh
                    line = (cls, *xywh, conf) if opt.save_conf else (cls, *xywh)  # label format
                    with open(txt_path + '.txt', 'a') as f:
                        f.write(('%g ' * len(line)).rstrip() % line + '\n')
                '''
                x_top = float(xyxy[0])
                y_top = float(xyxy[1])
                width = float(xyxy[2] - xyxy[0])
                height = float(xyxy[3] - xyxy[1])
                #print(x_top," ",y_top, " ", width, " ", height)
                # to visulaize image
                label = f'{names[int(cls)]} {conf:.2f}'
                plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=1)
                result.append([os.path.splitext(image_name)[0], names[int(cls)], x_top, y_top, width, height, float(conf)])

        #cv2_imshow(im0)
        #print(np.shape(im0))
        #cv2.waitKey(1)  # 1 millisecond
        
        return result

    


  
detect_single_image("/content/","1.jpg")

@glenn-jocher
Copy link
Member

glenn-jocher commented Apr 16, 2021

@haspberry yes you can use scale_coords() from within detect.py as @GiorgioSgl mentioned, or you can also use YOLOv5 PyTorch Hub models to return detections in xyxy native image space:

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')

# Images
dir = 'https://github.com/ultralytics/yolov5/raw/master/data/images/'
imgs = [dir + f for f in ('zidane.jpg', 'bus.jpg')]  # batch of images

# Inference
results = model(imgs)
results.print()  # or .show(), .save()

print(results.pandas().xyxy[0])
Downloading https://github.com/ultralytics/yolov5/releases/download/v5.0/yolov5s.pt to yolov5s.pt...
100%|██████████| 14.1M/14.1M [00:00<00:00, 22.7MB/s]
Adding autoShape... 
YOLOv5 🚀 2021-4-15 torch 1.8.1 CPU
image 1/2: 720x1280 2 persons, 2 ties
image 2/2: 1080x810 4 persons, 1 bus
Speed: 710.2ms pre-process, 275.3ms inference, 1.5ms NMS per image at shape (2, 3, 640, 640)

         xmin        ymin         xmax        ymax  confidence  class    name
0  749.472046   43.656982  1147.645874  704.565063    0.874176      0  person
1  433.666870  433.769470   517.574951  714.643982    0.688576     27     tie
2  115.053345  195.761230  1095.530273  708.221436    0.625422      0  person
3  986.198303  304.159058  1028.016602  420.192871    0.286980     27     tie

See PyTorch Hub Tutorial for details:

YOLOv5 Tutorials

@asaf0813
Copy link
Author

@glenn-jocher @GiorgioSgl
thx so much for solving this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants