YOLO V3 錯誤總結


問題1

TypeError: function takes exactly 1 argument (3 given)

報錯說PIL庫中的函數只接收到一個參數,應該給三個,自己在這里記錄下解決方法,出錯的地方在yolo.py中,在yolo中在測試時需要對檢測到的區域進行畫出標記框和類別數字,因為作者測試的coco等圖庫都是RGB圖像,會有三個參數輸入給rectangle函數,不會發生報錯,而在測試圖像為灰度圖時,就會出錯。在解決錯誤是參考了參考文獻[1]中的提示,很感謝!

對於這個錯誤原因,個人認為是這個函數在圖像上繪制矩形框時,要求輸入的圖像的顏色空間維度要和矩形框顏色維度一致,比如:我們輸入的灰度圖(不做維度擴充),只能使用灰度進行畫矩形框;輸入的是RGB三色圖,可以使用RGB顏色畫矩形框。

解決方法:

1.對灰度圖進行轉換,

使用 image = image.convert('RGB')

比如部分yolo.py函數如下:

 

 

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]
 
            label = '{} {:.2f}'.format(predicted_class, score)
            # 對灰度圖像進行RGB轉換,輸入的原圖為(0)一維 轉為(0,0,0)三維
            image = image.convert('RGB')
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)
 
            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))
 
            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])
 
            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle(
                    [left + i, top + i, right - i, bottom - i],
                    outline=self.colors[c])
                    #outline=(255))
            draw.rectangle(
                [tuple(text_origin), tuple(text_origin + label_size)],
                fill=self.colors[c])
                #outline=(255))
            #draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            draw.text(text_origin, label, fill=(0), font=font)
            del draw

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM