def smile_detection(self): x1,y1,x2,y2 = self.mouth_rect() mouth_rect = self.img[y1:y2, x1:x2] smiles = d.get('haarcascade_smile').detectMultiScale(mouth_rect, 1.1, 4) #mouthes = d.get('haarcascade_mcs_mouth').detectMultiScale(mouth_rect, 1.5, 2) i = None if len(smiles) != 0: i = self.check_mouth_distance(smiles, x1, y1) if i != None: self.organs_dict["smile"] = Mouth(smiles[i],True)
def face_detection(self, prev_faces): # find faces faces = d.get('haarcascade_frontalface_default').detectMultiScale(self.frame, 1.5, 2) p_faces = d.get('haarcascade_profileface').detectMultiScale(self.frame, 1.85, 2) if len(faces) == 0: faces = p_faces elif len(p_faces) != 0: faces = np.concatenate((faces, p_faces), 0) # choose the face with most face-organs for i,face in enumerate(faces): x,y,w,h = face.ravel() face_object = Face(face, self.frame, prev_faces,(255,0,0)) if face_object.organs_counter > 0: if not self.is_face_exists(face): self.faces.append(face_object) else: face1,j = self.search_face(face_object) if face_object.organs_counter > face1.organs_counter: self.faces[j] = face_object
def eye_detection(self, p_r_eye, p_l_eye): # define the eyes rectangle x1 = self.x x2 = x1 + self.w y1 = self.y y2 = y1 + self.h*2/3 eye_rect = self.img[y1:y2, x1:x2] #eyes1 = d.get('haarcascade_mcs_eyepair_big').detectMultiScale(self.eye_rect, 1.2, 1) eyes = d.get('haarcascade_eye').detectMultiScale(eye_rect, 1.1, 1) for eye in eyes: eye[0] += x1 eye[1] += y1 r_eye = self.search_eye(eyes,p_r_eye,p_l_eye,"r") l_eye = self.search_eye(eyes,p_l_eye,p_r_eye,"l") # search for eyes pair if r_eye is None and l_eye is None: for i,eye in enumerate(eyes): x,y,w,h = eye.ravel() eye_center = (x+w/2,y+h/2) for j in range(i+1,len(eyes)): x1,y1,w1,h1 = eyes[j].ravel() eye1_center = (x1+w1/2,y1+h1/2) dx = abs(x1-x) dy = abs(y1-y) if dx > (w+w1)/2 and dx < 2*self.w/3 and dy < (h+h1)/2: if x < x1: r_eye = eye l_eye = eyes[j] else: r_eye = eyes[j] l_eye = eye if r_eye is not None: self.organs_dict["r_eye"] = Eye(self.img,r_eye,'r') self.organs_counter += 1 if l_eye is not None: self.organs_dict["l_eye"] = Eye(self.img,l_eye,'l') self.organs_counter += 1
def nose_detection(self, prev_nose,frame): # define the nose rectangle N = 6 x1,y1,x2,y2 = self.nose_rect(N) nose_rect = self.img[y1:y2, x1:x2] noses = d.get('haarcascade_mcs_nose').detectMultiScale(nose_rect, 1.2, 1) distance = self.h s = self.w*self.h index = 0 for i,nose in enumerate(noses): nose[0] += x1 nose[1] += y1 nose_d = self.w/2 new_nose = None # search for the best nose for nose in noses: x,y,w,h = nose.ravel() nose_center = (x+w/2,y+h/2) # find nose using the previous nose location if prev_nose is not None: if self.is_contain_point((x,y,w,h),prev_nose): new_d = calc_d(nose_center,prev_nose) if new_d < nose_d: nose_d = new_d new_nose = nose # find nose using the face forizontal line else: new_d = abs(self.y+self.h/2 - y) if new_d < distance: new_nose = nose distance = new_d if new_nose is not None: self.organs_dict["nose"] = Nose(new_nose) self.organs_counter += 1 self.center = self.organs_dict["nose"].get_center()