質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

1124閲覧

cv2.rectangleのエラーの解決法

hoku_mhib

総合スコア1

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/09/05 06:27

前提・実現したいこと

読み込んだ画像中に矩形を作成したい

発生している問題・エラーメッセージ

File "yolo_validation_classify.py", line 560, in detect_img out = cv2.rectangle(out, (gtan[0], gtan[1]), (gtan[2], gtan[3]), (255,255,191), thickness=3) TypeError: Argument given by name ('thickness') and position (4)

該当のソースコード

必要だと思われる箇所のみ掲載しておきます。

Python

1 2def detect_img(yolo): 3 4 check_temporalyflie() 5 6 output_path = base_path + 'outputs_A/' 7 output_path_B = base_path + 'outputs_B/' 8 output_path_with_correct = base_path + 'outputs_ans_A/' 9 output_path_with_correct_B = base_path + 'outputs_ans_B/' 10 output_path_c = base_path + 'boundingbox/' 11 12 output_cnn = base_path + 'output_CNN_ResNet/' 13 14 iou_thresh = 0.25 15 cnn_thresh = 0.5 16 17 model_cnn = keras.models.load_model( base_path + 'epoch006.hdf5') 18 19 if not os.path.exists(output_path): 20 os.mkdir(output_path) 21 if not os.path.exists(output_path_B): 22 os.mkdir(output_path_B) 23 if not os.path.exists(output_path_with_correct): 24 os.mkdir(output_path_with_correct) 25 if not os.path.exists(output_path_with_correct_B): 26 os.mkdir(output_path_with_correct_B) 27 if not os.path.exists(output_cnn): 28 os.mkdir(output_cnn) 29 30 dirlist = [] 31 lines = [] 32 amed_val_lines = [] 33 timer = 0.0 34 loops = 0 35 36 with open( base_path + 'test.txt' ) as f: 37 for i in f: 38 dirlist.append(i.rstrip('\n')) 39 40 for i, d in enumerate(dirlist): 41 print('\rDetecting: {}/{}'.format(i, len(dirlist)), end='') 42 annotationsline = d.split(' ') 43 pic_path = annotationsline[0] 44 gt_annotation = [] 45 46 amedvalidationtool_mes = '' 47 48 for j, anno in enumerate(annotationsline): 49 if j != 0: 50 x1, y1, x2, y2, c = anno.split(',') 51 x1 = int(x1) 52 y1 = int(y1) 53 x2 = int(x2) 54 y2 = int(y2) 55 c = int(c) 56 gt_annotation.append( [x1, y1, x2, y2, c] ) 57 58 bn = os.path.basename(pic_path) 59 pic_name_list = bn.split('_') 60 61 amedvalidationtool_mes += pic_path 62 63 try: 64 row_img = cv2.imread(pic_path, 1) 65 h, w, c = row_img.shape 66 67 image = cv2.cvtColor(row_img, cv2.COLOR_BGR2RGB) 68 69 image = Image.fromarray(image) 70 image = image.convert('RGB') 71 72 cnn_img = cv2.imread(pic_path, 1) 73 74 except: 75 print('COF') 76 continue 77 else: 78 r_image, box, time = yolo.detect_image(image,model_cnn) 79 timer = timer + time 80 loops = i 81 img = np.asarray(r_image)[..., ::-1] 82 pic_id = str( pic_name_list[0] ) 83 84 acc = get_meaniou_section(box, gt_annotation, iou_thresh=iou_thresh, cnn_thresh=cnn_thresh) 85 acc_combie = get_iou_section(box, gt_annotation, iou_thresh=iou_thresh, cnn_thresh=cnn_thresh) 86 for gtan in gt_annotation: 87 cnn_img = cv2.rectangle(cnn_img, (gtan[0], gtan[1]), (gtan[2],gtan[3]), (255,255,191), thickness=3) 88 89 # if i % 2 == 0: 90 if True: 91 for z, reg in enumerate(box): 92 coma = row_img[reg[1]:reg[3], reg[0]:reg[2]] 93 p = os.path.join(output_path_c, str(z) + bn[:-4] + str(i) + '.png') 94 cv2.imwrite(p, coma) 95 96 amedvalidationtool_mes += ' {},{},{},{},{},{}'.format( 97 reg[0], reg[1], reg[2], reg[3], reg[6], reg[5]) 98 99 if reg[4] >= cnn_thresh: 100 score = '{}:{:.3f}({:.3f})'.format(reg[6], reg[4], reg[5]) 101 # score = '{}:{:.3f}'.format(reg[6], reg[5]) 102 # score = '{:.3f}'.format(reg[5]) 103 104 if reg[7] == 1: 105 cnn_img = cv2.rectangle(cnn_img, (reg[0], reg[1]), (reg[2], reg[3]), (0,0,255), thickness=3) 106 cnn_img = cv2.putText(cnn_img, score, (reg[0], reg[3] + 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 255), thickness=2 ) 107 else: 108 cnn_img = cv2.rectangle(cnn_img, (reg[0], reg[1]), (reg[2], reg[3]), (0,0,255), thickness=3) 109 cnn_img = cv2.putText(cnn_img, score, (reg[0] + 20, reg[3] + 60), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 0, 255), thickness=2 ) 110 111 112 113 cnnmes = 'YOLO&CNN({3}) - TP:{0}, FN:{1}, FP:{2}'.format(acc[2], acc[3], acc[4], cnn_thresh) 114 115 116 # cnnmes = 'YOLO Only - TP:{6}, FN:{7}, FP:{8}'.format(acc[2], acc[3], acc[4], acc[5], acc[6], acc[7], acc_combie[2], acc_combie[3], acc_combie[4]) 117 118 # cnnmes = 'YOLO Only - Cyst(TP:{0}, FN:{1}, FP:{2}) Tumor(TP:{3}, FN:{4}, FP:{5}) | WithoutClass(TP:{6}, FN:{7}, FP:{8})'.format(acc[2], acc[3], acc[4], acc[5], acc[6], acc[7], acc_combie[2], acc_combie[3], acc_combie[4]) 119 120 121 cnn_img = cv2.putText(cnn_img, cnnmes, (20,50), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 255), thickness=2 ) 122 123 124 125 out = img 126 for gtan in gt_annotation: 127 print(gtan[0]) 128 print(gtan[1]) 129 out = cv2.rectangle(out, (gtan[0], gtan[1]), (gtan[2], gtan[3]), (255,255,191), thickness=3) 130 131 if i % 2 == 0: 132 cv2.imwrite( output_path + bn[:-4] + str(i) + '.png' , img) 133 cv2.imwrite( output_path_with_correct + bn[:-4] + str(i) + '.png' , out) 134 cv2.imwrite( output_cnn + bn[:-4] + str(i) + '.png' , cnn_img) 135 else: 136 cv2.imwrite( output_path_B + bn[:-4] + str(i) + '.png' , img) 137 cv2.imwrite( output_path_with_correct_B + bn[:-4] + str(i) + '.png' , out) 138 cv2.imwrite( output_cnn + bn[:-4] + str(i) + '.png' , cnn_img) 139 140 # cv2.imwrite(output_cnn + bn[:-4] + str(i) + '.png' , cnn_img) 141 142 # lines.append( pic_id + '\t' + pic_path + '\t' + str(acc[0]) + '\t' + str(acc[1]) + '\t' + str(acc[2]) + '\t' + str(acc[3]) + '\t' + str(acc[4]) + '\n') 143 144 lines.append(pic_id + '\t' + pic_path + '\t' + str(acc[2]) + '\t' + str(acc[3]) + '\t' + str(acc[4]) + '\t' + str(acc[5]) + '\t' + str(acc[6]) + '\t' + str(acc[7]) + '\t' + str(acc_combie[2]) + '\t' + str(acc_combie[3]) + '\t' + str(acc_combie[4]) + '\n') 145 146 amed_val_lines.append(amedvalidationtool_mes + '\n') 147 148 149 150 with open( base_path + 'result.txt', 'w') as f: 151 f.writelines(lines) 152 153 with open('detect_result.txt', 'w') as f: 154 f.writelines(amed_val_lines) 155 156 print('time', timer) 157 print('ave', timer/(loops+1)) 158 159 yolo.close_session() 160 161if __name__ == '__main__': 162 detect_img(YOLO()) 163 # detect_video(YOLO()) 164

試したこと

調べたところ、intにキャストするというものが出てきたのですが既にキャスト済みです。
そのほかにどのようなことが原因になり得るか知りたいです。

補足情報(FW/ツールのバージョンなど)

Python 3.6.9
VSCode

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tiitoi

2020/09/05 06:38

cv2.rectangle() を呼び出す直前で type() で gtan[0], gtan[1] gtan[2], gtan[3] の4つの型を print() してその出力結果を教えて下さい
hoku_mhib

2020/09/05 06:42

情報ありがとうございます。 試してみた所、全て <class 'int'> となりました。
tiitoi

2020/09/05 06:53 編集

4箇所 cv2.rectangle() がありますが4箇所ともエラーが起こる直前で int でしたか? もし、点を指定している2つの引数を例えば、(1, 1), (2, 2) のように定数にしたらエラーが起こらないのだとすると、点の引数の型に問題があるとしか思えません あと1つ目の引数が uint8 の形状が (H, W, 3) の ndarray 型であることも確認してください
hoku_mhib

2020/09/05 06:56

先ほどの物はout=cv2.rectangleの直前です。 全部試した所、引数がgtanの箇所はint、引数がregの箇所はnumpy.int32でした。
tiitoi

2020/09/05 07:00

だとしたら、こちらではエラーが再現しないのでわからないです。すみません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問