Error strTypeError: 'str'

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

ptrblck: <p>I guess <code>args.inputPath</code> is set to a string containing the path, so “calling” it won’t work:</p>
<pre><code class="lang-python">inputPath = 'PATH'
inputPath("/")
&gt; TypeError: 'str' object is not callable
</code></pre>
<p>What are you trying to do with the <code>args.inputPath</code> there?</p>

Achrouf: <p>I’m trying to make the project work:</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://github.com/SHIDCenter/PathoNet#datasets">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">

      <a href="https://github.com/SHIDCenter/PathoNet#datasets" target="_blank" rel="noopener nofollow ugc">GitHub</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/345;"><img src="https://opengraph.githubassets.com/5357203ac489167c7fc04a0b1c69e531e9f90682e88bb440977f83e25ff5b66f/SHIDCenter/PathoNet" class="thumbnail" width="690" height="345"></div>

<h3><a href="https://github.com/SHIDCenter/PathoNet#datasets" target="_blank" rel="noopener nofollow ugc">SHIDCenter/PathoNet</a></h3>

  <p>Contribute to SHIDCenter/PathoNet development by creating an account on GitHub.</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<p>
I want to annotate an image, so I have two directory input and output</p>
<p>from pipeline import Pipeline<br>
from config import Config<br>
import os<br>
import glob<br>
import imageio<br>
import cv2<br>
import argparse<br>
import numpy as np</p>
<p>def get_parser():</p>
<pre><code>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
</code></pre>
<p>def visualizer(img,points):<br>
r=1<br>
colors=[<br>
(255,0,0),<br>
(0,255,0),<br>
(0,0,255)<br>
]<br>
image=np.copy(img)<br>
for p in points:<br>
x,y,c=p[0],p[1],p[2]<br>
cv2.circle(image, (int(x), int(y)), int(r), colors[int(c)], 2)<br>
return image</p>
<p>def demo(args=None):<br>
parser = get_parser()<br>
args = parser.parse_args(args)<br>
conf=Config()<br>
conf.load(args.configPath)<br>
pipeline=Pipeline(conf)<br>
if os.path.isdir(args.inputPath):<br>
data = [args.inputPath+"/"+f for f in os.listdir(args.inputPath) if ‘.jpg’ in f]<br>
for d in data:<br>
print(d)<br>
img=imageio.imread(d)<br>
pred=pipeline.predict(img)<br>
output=visualizer(img,pred)<br>
imageio.imwrite(args.outputPath+d.split("/")[-1],output)</p>
<pre><code>else:
    img=imageio.imread(args.inputPath)

    #imageio.imwrite(args.outputPath+args.inutPath("/")[-1])
</code></pre>
<p>if <strong>name</strong> == “<strong>main</strong>”:<br>
demo()</p>

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

Achrouf: <p>yes it works with that but i still have an error<br>
File “demo.py”, line 52, in demo<br>
imageio.imwrite(args.outputPath+args.inputPath.split("/")[-1])<br>
TypeError: imwrite() missing 1 required positional argument: ‘im’</p>
<p>après j’ai essayé ca :     imageio.imwrite(args.outputPath+args.inputPath.split("/")[-1], 1024*1024, format=‘png’)<br>
et j’ai comme erreur : ValueError: Image must be 2D (grayscale, RGB, or RGBA).<br>
et je sais pas comment on peut changer l’image en RGB par exemple avec juste le répertoire</p>

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

Achrouf: <p>thanks a lot for your answer<br>
else:<br>
img=imageio.imread(args.inputPath)<br>
image0=img0.open(r"C:\Users\ETTAQI\Documents\STAGE\imagecode\img2.png")<br>
image0_array=np.array(image0)<br>
imageio.imwrite(args.inputPath.split("/")[-1], image0_array)</p>
<p>I used this, I opened the image and defined it as an array, does that convert the image to an array?<br>
because I have an error in another file:</p>
<p>File “C:\Users\ETTAQI\Documents\STAGE\PathoNet-master1\utils.py”, line 15, in generator<br>
batch=np.random.choice(self.dataList,size=self.batchSize,replace=False)<br>
File “mtrand.pyx”, line 908, in numpy.random.mtrand.RandomState.choice<br>
ValueError: ‘a’ cannot be empty unless no samples are taken</p>
<p>the code is <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"><br>
class DataLoader:<br>
def <strong>init</strong>(self,batchSize,inputShape,dataList,guaMaxValue):<br>
self.inputShape=inputShape<br>
self.batchSize=batchSize<br>
self.dataList=dataList<br>
self.guaMaxValue=guaMaxValue<br>
def generator(self):<br>
while(1):<br>
batch=np.random.choice(self.dataList,size=self.batchSize,replace=False)<br>
images=[]<br>
labels=[]<br>
for b in batch:<br>
img=imread(b)<br>
images.append(img)<br>
temp=np.load(b.replace(".jpg",".npy")).astype(int)<br>
np.place(temp,temp==255,self.guaMaxValue)<br>
labels.append(temp)<br>
images=np.array(images)<br>
yield (np.array(images)/255.).astype(np.float32),np.array(labels)</p>

ptrblck: <p>No, I don’t think you can <code>open</code> the <code>png</code> directly and convert it to a numpy array.<br>
I also don’t know what <code>img0</code> is, but wouldn’t <code>imread</code> work?</p>

Achrouf: <p>it’s the library: from PIL import Image as img0,<br>
thank you very much for your answer<br>
now i am trying the following code:<br>
class DataLoader:<br>
def <strong>init</strong>(self,batchSize,inputShape,dataList,guaMaxValue):<br>
self.inputShape=inputShape<br>
self.batchSize=batchSize<br>
self.dataList=dataList<br>
self.guaMaxValue=guaMaxValue<br>
def generator(self):<br>
while(1):<br>
batch=np.random.choice(self.dataList,size=self.batchSize,replace=False) <span class="hashtag">#Génère</span> un échantillon aléatoire à partir d’un tableau 1D donné<br>
images=[]<br>
labels=[]<br>
for b in batch:<br>
img=imread(b)<br>
images.append(img)<br>
temp=np.load(b.replace(".jpg",".npy")).astype(int)<br>
np.place(temp,temp==255,self.guaMaxValue)<br>
labels.append(temp)<br>
images=np.array(images)<br>
yield (np.array(images)/255.).astype(np.float32),np.array(labels)</p>
<p>but I have as an error:<br>
batch=np.random.choice(self.dataList,size=self.batchSize,replace=True)<br>
File “mtrand.pyx”, line 908, in numpy.random.mtrand.RandomState.choice<br>
ValueError: ‘a’ cannot be empty unless no samples are taken</p>
<p>do you have a solution for me please?<br>
thank you in advance</p>

ptrblck: <p>Based on the error message <code>self.dataList</code> is empty and cannot be used in <code>np.random.choice</code>:</p>
<pre><code class="lang-python">a = np.random.randn(0)
np.random.choice(a, size=10, replace=True)
&gt; ValueError: 'a' cannot be empty unless no samples are taken
</code></pre>

Achrouf: <p>Thank you for your answer</p>