OpenCVで顔検出をやりたい。画像は以下のサッカー中の二人。
元画像

### 前準備
– 顔検出に使うカスケードファイルを使います。
haarcascades
$ mkdir haarcascades
$ cd haarcascades
$ wget https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_alt.xml
###
import cv2
cascade_file = "haarcascades/haarcascade_frontalface_alt.xml"
cascade = cv2.CascadeClassifier(cascade_file)
img = cv2.imread("1.jpg")
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_list = cascade.detectMultiScale(img_gray, minSize=(150,150))
if len(face_list) == 0:
print("false")
quit()
for (x,y,w,h) in face_list:
print(" 顔の座標=", x, y, w, h)
red = (0, 0, 255)
cv2.rectangle(img, (x, y), (x+w, y+h), red, thickness=20)
cv2.imwrite("face-detect.png", img)
$ python3 app.py
SystemError:
ん?
haarcascade_frontalface_alt.xmlを、opencvの公式からダウンロードして、上書き
$ python3 app.py
顔の座標= 141 47 60 60
顔の座標= 318 47 59 59

なんだこれ、やべーな。