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

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

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

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

534閲覧

なぜエラーが出るのかわからない。

DynMat

総合スコア12

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2018/04/22 02:36

編集2018/04/22 02:43

以下のコードを打つと次のようなエラーが出ます。なぜでしょうか。

File "detecter.py", line 80, in <module>
detect(img)
File "detecter.py", line 23, in detect
frame[(hypot - rows) * 0.5:(hypot + rows) * 0.5, (hypot - cols) * 0.5:(hypot + cols) * 0.5] = gray
TypeError: slice indices must be integers or None or have an index method

python

1import cv2 2import numpy as np 3import math 4from math import sin, cos 5from os import path 6 7cascades_dir = path.normpath(path.join(cv2.__file__, '..', '..', '..', '..', 'share', 'OpenCV', 'haarcascades')) 8max_size = 720 9 10def detect(img): 11 cascade_f = cv2.CascadeClassifier(path.join(cascades_dir, 'haarcascade_frontalface_alt2.xml')) 12 cascade_e = cv2.CascadeClassifier(path.join(cascades_dir, 'haarcascade_eye.xml')) 13 # resize if learch image 14 rows, cols, _ = img.shape 15 if max(rows, cols) > max_size: 16 l = max(rows, cols) 17 img = cv2.resize(img, (cols * max_size / l, rows * max_size / l)) 18 rows, cols, _ = img.shape 19 # create gray image for rotate 20 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 21 hypot = int(math.ceil(math.hypot(rows, cols))) 22 frame = np.zeros((hypot, hypot), np.uint8) 23 frame[(hypot - rows) * 0.5:(hypot + rows) * 0.5, (hypot - cols) * 0.5:(hypot + cols) * 0.5] = gray 24 25 def translate(coord, deg): 26 x, y = coord 27 rad = math.radians(deg) 28 return { 29 'x': ( cos(rad) * x + sin(rad) * y - hypot * 0.5 * cos(rad) - hypot * 0.5 * sin(rad) + hypot * 0.5 - (hypot - cols) * 0.5) / float(cols) * 100.0, 30 'y': (- sin(rad) * x + cos(rad) * y + hypot * 0.5 * sin(rad) - hypot * 0.5 * cos(rad) + hypot * 0.5 - (hypot - rows) * 0.5) / float(rows) * 100.0, 31 } 32 # rotate and detect faces 33 results = [] 34 for deg in range(-48, 49, 6): 35 M = cv2.getRotationMatrix2D((hypot * 0.5, hypot * 0.5), deg, 1.0) 36 rotated = cv2.warpAffine(frame, M, (hypot, hypot)) 37 faces = cascade_f.detectMultiScale(rotated, 1.08, 2) 38 print (deg, len(faces)) 39 for face in faces: 40 x, y, w, h = face 41 # eyes in face? 42 y_offset = int(h * 0.1) 43 roi = rotated[y + y_offset: y + h, x: x + w] 44 eyes = cascade_e.detectMultiScale(roi, 1.05) 45 eyes = filter(lambda e: (e[0] > w / 2 or e[0] + e[2] < w / 2) and e[1] + e[3] < h / 2, eyes) 46 if len(eyes) == 2 and abs(eyes[0][0] - eyes[1][0]) > w / 4: 47 score = math.atan2(abs(eyes[1][1] - eyes[0][1]), abs(eyes[1][0] - eyes[0][0])) 48 if eyes[0][1] == eyes[1][1]: 49 score = 0.0 50 results.append({ 51 'center': translate([x + w * 0.5, y + h * 0.5], -deg), 52 'w': float(w) / float(cols) * 100.0, 53 'h': float(h) / float(rows) * 100.0, 54 'eyes': [translate([x + e[0] + e[2] * 0.5, y + y_offset + e[1] + e[3] * 0.5], -deg) for e in eyes], 55 'score': score, 56 }) 57 # unify duplicate faces 58 faces = [] 59 for result in results: 60 x, y = result['center']['x'], result['center']['y'] 61 exists = False 62 for i in range(len(faces)): 63 face = faces[i] 64 if (face['center']['x'] - face['w'] * 0.5 < x < face['center']['x'] + face['w'] * 0.5 and 65 face['center']['y'] - face['h'] * 0.5 < y < face['center']['y'] + face['h'] * 0.5): 66 exists = True 67 if result['score'] < face['score']: 68 faces[i] = result 69 break 70 if not exists: 71 faces.append(result) 72 for face in faces: 73 del face['score'] 74 return faces 75 76filename = 'nogi1.jpeg' 77# 画像ファイルパスから読み込み 78img = cv2.imread(filename) 79 80detect(img) 81

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

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

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

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

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

hayataka2049

2018/04/22 02:38

どこでエラーが出ているかわからないと理由を考えるのが大変なので、エラーメッセージは省略せずすべて貼ってください
DynMat

2018/04/22 02:43

すいません。うっかりしておりました。追記しました。
guest

回答1

0

ベストアンサー

とりあえず思いつく原因は、* 0.5するとぜんぶfloatになるので、そのせいでslice indicesとして不適切になっているというものです。
ぜんぶint((hypot - rows) * 0.5)等のようにintでくくって型変換してあげれば、それだけは改善するでしょう。他に問題があるかどうかまでは見ていません。

投稿2018/04/22 02:49

編集2018/04/22 02:51
hayataka2049

総合スコア30933

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

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

DynMat

2018/04/22 03:00 編集

回答ありがとうございます。この問題となっていたエラーは解決できました。 File "detecter.py", line 23 frame[int((hypot - rows) * 0.5:(hypot + rows) * 0.5, (hypot - cols) * 0.5:(hypot + cols) * 0.5)] = gray SyntaxError: invalid syntax と出てしまいました。 frame[int((hypot - rows) * 0.5:(hypot + rows)...の:の部分に^が付いています。
hayataka2049

2018/04/22 03:00

すみません、私の説明が悪かったです。インデックスの値を一つずつ型変換してください frame[int((hypot - rows) * 0.5):int((hypot + rows) * 0.5), int((hypot - cols) * 0.5):int((hypot + cols) * 0.5)] = gray
tachikoma

2018/04/22 04:48

hypot, rows, colsが全てintなら(hypot - rows) * 0.5の代わりに(hypot - rows) // 2とすればいい気もします。2重のスラッシュは型が保たれる割り算の演算子です。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問