def detect(self): faces = [] eyes = [] rgb_green = (0, 255, 0) rgb_blue = (0, 0, 255) detected_faces = self.__detect_faces() for (x, y, w, h) in detected_faces: face = Face(self.image, x, y, w, h, rgb_green) face.draw(2) faces.append(face) detected_eyes = self.__detect_eyes(face.data()) if len(detected_eyes) > 0: for(eye_x, eye_y, eye_w, eye_h) in detected_eyes: eye = Eye(self.image, face.x + eye_x, face.y + eye_y, eye_w, eye_h, rgb_blue) eye.draw(2) eyes.append(eye) detected_smile = self.__detect_smile(face.data()) if len(detected_smile) > 0: smileness = detected_smile[0][1] smile_text_position = ((x - 15), y + (h + 25)) if smileness >= 15: cv2.putText(self.image, "Smiling: Yes", smile_text_position, cv2.FONT_HERSHEY_DUPLEX, 1, (255,255,255)) else: cv2.putText(self.image, "Smiling: No", smile_text_position, cv2.FONT_HERSHEY_DUPLEX, 1, (255,255,255)) return { "faces" : faces, "eyes" : eyes}
def detect(self): faces = [] eyes = [] rgb_green = (0, 255, 0) rgb_blue = (0, 0, 255) detected_faces = self.__detect_faces() for (x, y, w, h) in detected_faces: face = Face(self.image, x, y, w, h, rgb_green) face.draw(2) faces.append(face) detected_eyes = self.__detect_eyes(face.data()) if len(detected_eyes) > 0: for (eye_x, eye_y, eye_w, eye_h) in detected_eyes: eye = Eye(self.image, face.x + eye_x, face.y + eye_y, eye_w, eye_h, rgb_blue) eye.draw(2) eyes.append(eye) detected_smile = self.__detect_smile(face.data()) if len(detected_smile) > 0: smileness = detected_smile[0][1] smile_text_position = ((x - 15), y + (h + 25)) if smileness >= 15: cv2.putText(self.image, "Smiling: Yes", smile_text_position, cv2.FONT_HERSHEY_DUPLEX, 1, (255, 255, 255)) else: cv2.putText(self.image, "Smiling: No", smile_text_position, cv2.FONT_HERSHEY_DUPLEX, 1, (255, 255, 255)) return {"faces": faces, "eyes": eyes}
def run(self): """Main loop """ self.startCapture() data_collector = DataCollector(self.dataset) modeFullFace = 0 modeOneEye = 1 modePictures = 2 modeTwoEyes = 3 modeImgDB = 4 modeDemo = 5 mode = modeDemo keepLoop = True current_t = time.clock() previous_t = current_t while keepLoop: pressed_key = cv2.waitKey(1) current_t = time.clock() #print('\nclock : ', current_t - previous_t) previous_t = current_t img = self.getCameraImage() if(mode == modeOneEye): ex = 300 ey = 50 eh = 200 ew = 200 face = Face(img.frame, img.canvas, 0, 0, 640, 480) eye = Eye(face.frame, face.canvas, ex, ey, ew, eh) eye.draw(face) eye.iris.normalizeIris() elif(mode == modeTwoEyes): face = Face(img.frame, img.canvas, 0, 0, 640, 480) left_eye = Eye(face.frame, face.canvas, 50, 50, 200, 200, EyeType.LEFT) left_eye.draw(face) left_eye.iris.normalizeIris() right_eye = Eye(face.frame, face.canvas, 400, 50, 200, 200, EyeType.RIGHT) right_eye.draw(face) right_eye.iris.normalizeIris() elif(mode == modeFullFace): face, left_eye, right_eye = img.detectEyes(self.bufferFace, self.bufferLeftEye, self.bufferRightEye) if face: face.draw(img) if left_eye: left_eye.draw(face) left_eye.iris.normalizeIris() if right_eye: right_eye.draw(face) right_eye.iris.normalizeIris() elif(mode == modeImgDB): img_db = ImageDB("./IR_Database/MMU Iris Database/") print(img_db.estimateUser(img_db.bits[0])) exit() elif(mode == modeDemo): path = "./TB_Database/" face = Face(img.frame, img.canvas, 0, 0, 640, 480) left_eye = Eye(face.frame, face.canvas, 50, 50, 200, 200, EyeType.LEFT) left_eye.draw(face) left_eye.iris.normalizeIris() right_eye = Eye(face.frame, face.canvas, 400, 50, 200, 200, EyeType.RIGHT) right_eye.draw(face) right_eye.iris.normalizeIris() if(ord('0') <= pressed_key & 0xFF <= ord('9')): print('0-9') id_person = chr(pressed_key & 0xFF) while((pressed_key & 0xFF) not in [ord('a'), ord('p')]): pressed_key = cv2.waitKey(50) print(pressed_key & 0xFF) if(pressed_key & 0xFF == ord('a')): cv2.imwrite(path + id_person + '/left/' + str(int(time.time() * 1000)) + '.bmp', left_eye.frame) cv2.imwrite(path + id_person + '/right/' + str(int(time.time() * 1000)) + '.bmp', right_eye.frame) else: path = "./IR_Database/MMU Iris Database/" for dir in os.listdir(path): subpath = path + dir + '/' if(os.path.isdir(subpath)): print(dir) for subdir in os.listdir(subpath): subsubpath = subpath + subdir + '/' if(os.path.isdir(subsubpath)): print('\t', subdir) for fname in os.listdir(subsubpath): fpath = subsubpath + fname if(os.path.isfile(fpath) and os.path.splitext(fname)[1] == '.bmp'): print('\t\t', fname) img = Image(cv2.imread(fpath)) face = Face(img.frame, img.canvas) eye = Eye(face.frame, face.canvas, padding=10) eye.draw(face) eye.iris.normalizeIris() img.show() pressed_key = cv2.waitKey(1000) exit() # Controls if pressed_key & 0xFF == ord('q'): keepLoop = False #if pressed_key & 0xFF == ord('s'): # self.dataset.save() #if pressed_key & 0xFF == ord('l'): # self.dataset.load() #if pressed_key & 0xFF == ord('m'): # self.showMoments = not self.showMoments #if pressed_key & 0xFF == ord('e'): # self.showEvaluation = not self.showEvaluation #data_collector.step(img.canvas, pressed_key, left_eye, right_eye) #txt = 'Dataset: {} (s)ave - (l)oad'.format(len(self.dataset)) #cv2.putText(img.canvas, txt, (21, img.canvas.shape[0] - 29), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (32, 32, 32), 2) #cv2.putText(img.canvas, txt, (20, img.canvas.shape[0] - 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 126, 255), 2) #if left_eye and right_eye: # direction = self.dataset.estimateDirection(left_eye.computeMomentVectors(), right_eye.computeMomentVectors()) # txt = 'Estimated direction: {}'.format(direction.name) # cv2.putText(img.canvas, txt, (21, img.canvas.shape[0] - 49), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (32, 32, 32), 2) # cv2.putText(img.canvas, txt, (20, img.canvas.shape[0] - 50), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 126, 255), 2) img.show() #if self.showEvaluation: # fig = self.dataset.showValidationScoreEvolution() # plt.show() # self.showEvaluation = False #if self.showMoments: # fig = self.dataset.drawVectorizedMoments() # plt.show() # # cv2.imshow('moments', self.fig2cv(fig)) # # plt.close(fig) # self.showMoments = False self.stopCapture()