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

mss grab color conversion problem using TorchHub #1735

Closed
gpierard opened this issue Dec 19, 2020 · 7 comments
Closed

mss grab color conversion problem using TorchHub #1735

gpierard opened this issue Dec 19, 2020 · 7 comments
Labels
bug Something isn't working Stale Stale and schedule for closing soon

Comments

@gpierard
Copy link

gpierard commented Dec 19, 2020

🐛 Bug

Images grabbed with mss need to be converted to RGB before being fed as input to torch's models

However the proposed solution to do this conversion to RGB does not work img2 = cv2.imread('bus.jpg')[:, :, ::-1]

Instead, this has to be used:

result = model(cv2.cvtColor(scr, cv2.COLOR_BGR2RGB), size=400)

To Reproduce (REQUIRED)

Input:

See full code and resolution on SO

Output:
result.show() outputting a non-normal color channel image, with dramatically reduced model accuracy of course (since it was trained on normal color channels).

Expected behavior

Converting the image to RGB using result = model(cv2.cvtColor(scr, cv2.COLOR_BGR2RGB), size=400) fixed the problem.

  • TorchHub shows a normal image with result.show()
  • Accuracy of the model back to normal

Environment

If applicable, add screenshots to help explain your problem.

  • OS: [Windows 10]
  • GPU [e.g. RTX 2080 SUPER]
@gpierard gpierard added the bug Something isn't working label Dec 19, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Dec 19, 2020

Hello @gpierard, 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.

@glenn-jocher
Copy link
Member

@gpierard thanks for raising this issue. I can't reproduce on Windows actually because I don't have access to a suitable environment. Can you try to run the following code in your example and post a screenshot here of the result?

import cv2
import numpy as np

file = 'data/images/bus.jpg'
im1 = cv2.imread(file)[:, :, ::-1]
im2 = cv2.cvtColor(cv2.imread(file), cv2.COLOR_BGR2RGB)
print(np.allclose(im1, im2))

On MacOS I see:
Screen Shot 2020-12-20 at 10 02 21 AM

In Colab notebook I see:
Screen Shot 2020-12-20 at 10 03 08 AM

@gpierard
Copy link
Author

gpierard commented Dec 21, 2020

My aim is to use sct.grab and not cv2.imread. This returns True.

import cv2
import numpy as np
import os
os.chdir("C:/GP/")
os.getcwd()
file = 'test.jpg'
im1 = cv2.imread(file)[:, :, ::-1]
im2 = cv2.cvtColor(cv2.imread(file), cv2.COLOR_BGR2RGB)
print(np.allclose(im1, im2))

However this returns ValueError: operands could not be broadcast together with shapes (500,300,4) (500,300,3)

from mss import mss
sct = mss()
monitor = {"top": 160, "left": 160, "width": 300, "height": 500}
screen = np.array(sct.grab(monitor))

cv2.imshow("img",screen)
if cv2.waitKey(20000) & 0xFF == ord('q'):
    cv2.destroyAllWindows()

screen.shape
im1 = screen[:, :, ::-1]
im2 = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)
print(np.allclose(im1, im2))

so now I have this equal to screen. This image is displaying colors correctly.
cv2.imshow("screen",screen)
image

Problem is, AFTER passing this screen image as argument to model, then displaying the result, the color channel has changed.

import torch
model = torch.hub.load('ultralytics/yolov5', 'custom', path_or_model='runs/train/exp29/weights/best.pt')
#model = model.fuse()  # for PIL/cv2/np inputs and NMS
model = model.autoshape()  # for PIL/cv2/np inputs and NMS
result = model(screen, size=400) 
result.display(save=False, show=True, pprint= True)

image

And I have to convert screen using the above mentioned method before passing it to model otherwise the accuracy is very low, since the color channel of the input seems to have been not correctly fed to model() somehow.

Displaying these two images shows the difference in color channels. Only the second solution brings my model accuracy as well as the result.display back to normal, not the first one as advised.

im1 = screen[:, :, ::-1]
im2 = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)

Cheers.

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the Stale Stale and schedule for closing soon label Jan 21, 2021
@wb-08
Copy link

wb-08 commented Sep 10, 2022

Try: result = model(screen, size=640)
cv2.imshow('Screen', result.render()[0])

@cakiryusuff
Copy link

Try: result = model(screen, size=640) cv2.imshow('Screen', result.render()[0])

This worked, thanks.

@glenn-jocher
Copy link
Member

@cakiryusuff you're welcome! If you have any more questions or need further assistance, feel free to ask. Good luck with your project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Stale Stale and schedule for closing soon
Projects
None yet
Development

No branches or pull requests

4 participants