Error strTypeError: 'str'

hello, I am trying to execute code with model cnn, I have the following error
File “demo.py”, line 49, in demo
imageio.imwrite(args.outputPath+args.inputPath("/")[-1])
TypeError: ‘str’ object is not callable

if you have a solution, thank you

I guess args.inputPath is set to a string containing the path, so “calling” it won’t work:

inputPath = 'PATH'
inputPath("/")
> TypeError: 'str' object is not callable

What are you trying to do with the args.inputPath there?

I’m trying to make the project work:

I want to annotate an image, so I have two directory input and output

from pipeline import Pipeline
from config import Config
import os
import glob
import imageio
import cv2
import argparse
import numpy as np

def get_parser():

parser = argparse.ArgumentParser('demo')
parser.add_argument('--inputPath', '-i', required=True, default=r"C:\Users\ETTAQI\Documents\STAGE\imagecode\img2.png", help='path to input image')
parser.add_argument('--outputPath', '-o', required=True, default=r"C:\Users\ETTAQI\Documents\STAGE\imagecode", help='path to input image')
parser.add_argument('--configPath', '-c', required=True, default=r"C:\Users\ETTAQI\Documents\STAGE\PathoNet-master1\configs\demo.json", help='path to input image')
return parser

def visualizer(img,points):
r=1
colors=[
(255,0,0),
(0,255,0),
(0,0,255)
]
image=np.copy(img)
for p in points:
x,y,c=p[0],p[1],p[2]
cv2.circle(image, (int(x), int(y)), int(r), colors[int(c)], 2)
return image

def demo(args=None):
parser = get_parser()
args = parser.parse_args(args)
conf=Config()
conf.load(args.configPath)
pipeline=Pipeline(conf)
if os.path.isdir(args.inputPath):
data = [args.inputPath+"/"+f for f in os.listdir(args.inputPath) if ‘.jpg’ in f]
for d in data:
print(d)
img=imageio.imread(d)
pred=pipeline.predict(img)
output=visualizer(img,pred)
imageio.imwrite(args.outputPath+d.split("/")[-1],output)

else:
    img=imageio.imread(args.inputPath)

    #imageio.imwrite(args.outputPath+args.inutPath("/")[-1])

if name == “main”:
demo()

You are still trying to call inputPath, which won’t work.
Maybe you want to use inputPath.split('/') instead, as you cannot use inputPath as a function?

yes it works with that but i still have an error
File “demo.py”, line 52, in demo
imageio.imwrite(args.outputPath+args.inputPath.split("/")[-1])
TypeError: imwrite() missing 1 required positional argument: ‘im’

après j’ai essayé ca : imageio.imwrite(args.outputPath+args.inputPath.split("/")[-1], 1024*1024, format=‘png’)
et j’ai comme erreur : ValueError: Image must be 2D (grayscale, RGB, or RGBA).
et je sais pas comment on peut changer l’image en RGB par exemple avec juste le répertoire

imageio.imwrite expects at least two input arguments: the path where the image should be stored, and the image itself (passed as a numpy array).
In your current code snippet you are calling imwrite with the path only without providing the actual image array.

thanks a lot for your answer
else:
img=imageio.imread(args.inputPath)
image0=img0.open(r"C:\Users\ETTAQI\Documents\STAGE\imagecode\img2.png")
image0_array=np.array(image0)
imageio.imwrite(args.inputPath.split("/")[-1], image0_array)

I used this, I opened the image and defined it as an array, does that convert the image to an array?
because I have an error in another file:

File “C:\Users\ETTAQI\Documents\STAGE\PathoNet-master1\utils.py”, line 15, in generator
batch=np.random.choice(self.dataList,size=self.batchSize,replace=False)
File “mtrand.pyx”, line 908, in numpy.random.mtrand.RandomState.choice
ValueError: ‘a’ cannot be empty unless no samples are taken

the code is :slight_smile:
class DataLoader:
def init(self,batchSize,inputShape,dataList,guaMaxValue):
self.inputShape=inputShape
self.batchSize=batchSize
self.dataList=dataList
self.guaMaxValue=guaMaxValue
def generator(self):
while(1):
batch=np.random.choice(self.dataList,size=self.batchSize,replace=False)
images=[]
labels=[]
for b in batch:
img=imread(b)
images.append(img)
temp=np.load(b.replace(".jpg",".npy")).astype(int)
np.place(temp,temp==255,self.guaMaxValue)
labels.append(temp)
images=np.array(images)
yield (np.array(images)/255.).astype(np.float32),np.array(labels)

No, I don’t think you can open the png directly and convert it to a numpy array.
I also don’t know what img0 is, but wouldn’t imread work?

it’s the library: from PIL import Image as img0,
thank you very much for your answer
now i am trying the following code:
class DataLoader:
def init(self,batchSize,inputShape,dataList,guaMaxValue):
self.inputShape=inputShape
self.batchSize=batchSize
self.dataList=dataList
self.guaMaxValue=guaMaxValue
def generator(self):
while(1):
batch=np.random.choice(self.dataList,size=self.batchSize,replace=False) #Génère un échantillon aléatoire à partir d’un tableau 1D donné
images=[]
labels=[]
for b in batch:
img=imread(b)
images.append(img)
temp=np.load(b.replace(".jpg",".npy")).astype(int)
np.place(temp,temp==255,self.guaMaxValue)
labels.append(temp)
images=np.array(images)
yield (np.array(images)/255.).astype(np.float32),np.array(labels)

but I have as an error:
batch=np.random.choice(self.dataList,size=self.batchSize,replace=True)
File “mtrand.pyx”, line 908, in numpy.random.mtrand.RandomState.choice
ValueError: ‘a’ cannot be empty unless no samples are taken

do you have a solution for me please?
thank you in advance

Based on the error message self.dataList is empty and cannot be used in np.random.choice:

a = np.random.randn(0)
np.random.choice(a, size=10, replace=True)
> ValueError: 'a' cannot be empty unless no samples are taken

Thank you for your answer