以下のコードを打つと次のようなエラーが出ます。なぜでしょうか。
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
回答1件
あなたの回答
tips
プレビュー