1. Harr cascade detection이란?
: 이번 챕터에서 face recognition에 대해 다룰 것이다.
-> face detection을 위해 Object detection module 또는 Deep learning module(dnn module)이 사용 가능하다.
:서로 융합하면 장단점이 있다.
-> Object Detection: Haar cascade object detection 사용.
-> 장점: 굉장히 빠르게 object detection을 이용한다. 단점: 아무래도 CNN 보다 정확도가 떨어진다. (예외 상황에 인식 힘듦 : 얼굴 돌리거나, 선글라스 낀다던가...)
2. Haar 방식을 이용한 Face detection
-> face_cascade/ eyes_cascade 함수가 모두 이미 opencv안에 구현되어 있었다.
아 ㅁㅊ 콧구멍이 눈인줄 알았나봄ㅋㅋㅋㅋㅋ=͟͟͞͞개=͟͟͞͞웃=͟͟͞͞겨=͟͟͞͞개=͟͟͞͞웃=͟͟͞͞겨=͟͟͞͞개=͟͟͞͞웃=͟͟͞͞겨=͟͟͞͞개=͟͟͞͞웃=͟͟͞͞겨
# -*- coding: utf-8 -*-
import cv2
import numpy as np
def detectAndDisplay(frame):
frame_gray= cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame_gray= cv2.equalizeHist(frame_gray)
#-- Detect faces
faces= face_cascade.detectMultiScale(frame_gray)
for(x,y,w,h) in faces:
center=(x+w//2,y+h//2)
#center location of face
frame=cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),4)
faceROI=frame_gray[y:y+h,x:x+w]
#region of interest
eyes=eyes_cascade.detectMultiScale(faceROI)
#drawing a picture using location
for(x2,y2,w2,h2) in eyes:
eye_center=(x+x2+w2//2, y+y2+h2//2)
radius=int(round((w2+h2)*0.25))
frame=cv2.circle(frame,eye_center,radius,(255,0,0),4)
cv2.imshow('Capture-face detection',frame)
print("OpenCV version:")
print(cv2.__version__)
img=cv2.imread("ryujin.jpg")
print("width:{} pixels".format(img.shape[1]))
print("height:{} pixels".format(img.shape[0]))
print("channels: {}".format(img.shape[2]))
cv2.imshow("ryujin", img)
face_cascade_name = 'haarcascade_frontalface_alt.xml'
eyes_cascade_name = 'haarcascade_eye_tree_eyeglasses.xml'
face_cascade = cv2.CascadeClassifier()
eyes_cascade = cv2.CascadeClassifier()
#recognizing face & eyes
#-- 1. Load the cascades
if not face_cascade.load(cv2.samples.findFile(face_cascade_name)):
print('--(!)Error loading face cascade')
exit(0)
if not eyes_cascade.load(cv2.samples.findFile(eyes_cascade_name)):
print('--(!)Error loading eyes cascade')
exit(0)
detectAndDisplay(img)
cv2.waitKey(0)
cv2.destroyAllWindows()
=> haar module에는 이미 eye, face등의 특정한 부위를 인식하는 mask가 구현되어 있는 것 같다. 위치만 설정해줘서 얼굴 사진에 맞게끔만 씌워주면 된다. 옆얼굴이나, 모자를 썼거나, 얼굴이 제대로 안보이면 눈인식도 잘 못하는듯.. 쩝
<GUI로도 만들어줄 수 있다>
import cv2
import numpy as np
from tkinter import *
from PIL import Image
from PIL import ImageTk
from tkinter import filedialog
face_cascade_name = '../opencv/data/haarcascades/haarcascade_frontalface_alt.xml'
eyes_cascade_name = '../opencv/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml'
file_name = 'image/marathon_01.jpg'
title_name = 'Haar cascade object detection'
frame_width = 500
def selectFile():
file_name = filedialog.askopenfilename(initialdir = "./image",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
print('File name : ', file_name)
read_image = cv2.imread(file_name)
(height, width) = read_image.shape[:2]
frameSize = int(sizeSpin.get())
ratio = frameSize / width
dimension = (frameSize, int(height * ratio))
read_image = cv2.resize(read_image, dimension, interpolation = cv2.INTER_AREA)
image = cv2.cvtColor(read_image, cv2.COLOR_BGR2RGB)
image = Image.fromarray(image)
imgtk = ImageTk.PhotoImage(image=image)
detectAndDisplay(read_image)
def detectAndDisplay(frame):
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame_gray = cv2.equalizeHist(frame_gray)
#-- Detect faces
faces = face_cascade.detectMultiScale(frame_gray)
for (x,y,w,h) in faces:
center = (x + w//2, y + h//2)
frame = cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 4)
faceROI = frame_gray[y:y+h,x:x+w]
#-- In each face, detect eyes
eyes = eyes_cascade.detectMultiScale(faceROI)
for (x2,y2,w2,h2) in eyes:
eye_center = (x + x2 + w2//2, y + y2 + h2//2)
radius = int(round((w2 + h2)*0.25))
frame = cv2.circle(frame, eye_center, radius, (255, 0, 0 ), 4)
#cv2.imshow('Capture - Face detection', frame)
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image = Image.fromarray(image)
imgtk = ImageTk.PhotoImage(image=image)
detection.config(image=imgtk)
detection.image = imgtk
#main
main = Tk()
main.title(title_name)
main.geometry()
read_image = cv2.imread("image/marathon_01.jpg")
(height, width) = read_image.shape[:2]
ratio = frame_width / width
dimension = (frame_width, int(height * ratio))
read_image = cv2.resize(read_image, dimension, interpolation = cv2.INTER_AREA)
image = cv2.cvtColor(read_image, cv2.COLOR_BGR2RGB)
image = Image.fromarray(image)
imgtk = ImageTk.PhotoImage(image=image)
face_cascade_name = '../opencv/data/haarcascades/haarcascade_frontalface_alt.xml'
eyes_cascade_name = '../opencv/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml'
face_cascade = cv2.CascadeClassifier()
eyes_cascade = cv2.CascadeClassifier()
#-- 1. Load the cascades
if not face_cascade.load(cv2.samples.findFile(face_cascade_name)):
print('--(!)Error loading face cascade')
exit(0)
if not eyes_cascade.load(cv2.samples.findFile(eyes_cascade_name)):
print('--(!)Error loading eyes cascade')
exit(0)
label=Label(main, text=title_name)
label.config(font=("Courier", 18))
label.grid(row=0,column=0,columnspan=4)
sizeLabel=Label(main, text='Frame Width : ')
sizeLabel.grid(row=1,column=0)
sizeVal = IntVar(value=frame_width)
sizeSpin = Spinbox(main, textvariable=sizeVal,from_=0, to=2000, increment=100, justify=RIGHT)
sizeSpin.grid(row=1, column=1)
Button(main,text="File Select", height=2,command=lambda:selectFile()).grid(row=1, column=2, columnspan=2, sticky=(W, E))
detection=Label(main, image=imgtk)
detection.grid(row=2,column=0,columnspan=4)
detectAndDisplay(read_image)
main.mainloop()
'Computer Science > 영상처리 + FACE-ID project' 카테고리의 다른 글
DNN 모듈 집중 분석 (3) | 2020.04.30 |
---|---|
영상처리의 기본~ 이미지 가공 (0) | 2020.03.18 |