I tried to use a plot as image for my further code. I load an image in my pretrained model and my output is a tensor variable. In the next step I plot it with Image(img_hr).show(figsize=(18,15))
. And after this I would like to use the picture from the plot to convert the colors. But the problem is, I cant use the variable img_hr
because the type is tensor.
My idea was in the third last line to read the plot. The input for imagehsv = cv2.cvtColor(img_hr, cv2.COLOR_BGR2HSV)
need to be an array and I dont know how to convert the plot.
Here is the error:
imagehsv = cv2.cvtColor(img_hr, cv2.COLOR_BGR2HSV) error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'cvtColor' Overload resolution failed:
- src is not a numpy array, neither a scalar
- Expected Ptr<cv::UMat> for argument 'src'
I am new in python so please forgive me for any bad description or wrong vocabulary. I hope everything is clear otherwise feel free to ask.
Here is the picture of the plot:
Does anyone have an idea? Thanks a lot
from fastai import *
from fastai.vision import *
from fastai.callbacks.hooks import *
from fastai.utils.mem import *
import numpy as np
import cv2 as cv2
import matplotlib as mpl
import matplotlib.pyplot as plt
def acc_camvid(input, target):
target = target.squeeze(1)
mask = target != void_code
return (input.argmax(dim=1)[mask]==target[mask]).float().mean()
learn=load_learner(r'C:\pretrained_model')
image= r"C:\image.png"
img = open_image(image); img.shape
_,img_hr,b = learn.predict(img)
Image(img_hr).show(figsize=(18,15))
#image = cv2.imread(Image(img_hr))
imagehsv = cv2.cvtColor(img_hr, cv2.COLOR_BGR2HSV)
plt.imshow(fixColor(imagehsv))
fastai
works with PIL Image types.
So your variable img_hr
is an Image
.
OpenCV uses NumPy ndarray
types. You need to convert your Image
to a ndarray
:
_, img_hr, b = learn.predict(img)
img_hr = np.array(img_hr)
# PIL uses RGB channel order, not BGR like OpenCV default
imagehsv = cv2.cvtColor(img_hr, cv2.COLOR_RGB2HSV)
# Display your result in HSV color space
cv2.imshow("Image HSV", imagehsv)
cv2.waitKey()