■ CascadeClassifier 클래스의 detectMultiScale 메소드를 사용해 Haar 직렬형 분류기를 만드는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import cv2 faceCascadeClassifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") eyeCascadeClassifier = cv2.CascadeClassifier("haarcascade_eye.xml") imageNDArray = cv2.imread("source.jpg") grayscaleImageNDArray = cv2.cvtColor(imageNDArray, cv2.COLOR_BGR2GRAY) faceNDArray = faceCascadeClassifier.detectMultiScale(grayscaleImageNDArray, 1.3, 5) for (faceX, faceY, faceWidth, faceHeight) in faceNDArray: left = faceX top = faceY right = faceX + faceWidth bottom = faceY + faceHeight cv2.rectangle(imageNDArray, (left, top), (right, bottom), (255, 0, 0), 2) faceImageNDArray = imageNDArray[top:bottom, left:right] faceGrayscaleImageNDArray = grayscaleImageNDArray[top:bottom, left:right] eyeNDArray = eyeCascadeClassifier.detectMultiScale(faceGrayscaleImageNDArray) for(eyeX, eyeY, eyeWidth, eyeHeight) in eyeNDArray: cv2.rectangle(faceImageNDArray, (eyeX, eyeY), (eyeX + eyeWidth, eyeY + eyeHeight), (0, 255, 0), 2) cv2.imwrite("target.jpg", imageNDArray) cv2.imshow("image", imageNDArray) cv2.waitKey(0) cv2.destroyAllWindows() |