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}
class Face: def __init__(self, x, y, size): # Face position and size. self.x = x self.y = y self.size = size # Create the eyes. self.lefteye = Eye(x - 15, y - 15, 10) self.righteye = Eye(x + 15, y - 15, 10) # Make this Face look at location lx, ly. def look_at(self, lx, ly): self.lefteye.look_at(lx, ly) self.righteye.look_at(lx, ly) def draw(self): enable_stroke() set_fill_color(1, 1, 1) draw_circle(self.x, self.y, self.size) # draw face self.lefteye.draw() # draw eyes self.righteye.draw() # draw eyes draw_line(self.x, self.y, self.x, self.y + 15) # draw nose draw_line(self.x - 15, self.y + 20, self.x + 15, self.y + 20) # draw mouth
def computeBits(self): bits = [] self.eyes = [] self.faces = [] for image in self.images: face = Face(image) eye = Eye(face.frame, face.canvas, padding=10) eye.draw(face) eye.iris.normalizeIris() self.eyes.append(eye) self.faces.append(face) bits.append(eye.iris.bits_pattern) self.bits = np.array(bits)
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()
class Face: def __init__(self,resolution,color=(0, 0, 0)): self.screen=pygame.display.set_mode(resolution) self.background = pygame.Surface(self.screen.get_size()) self.background = self.background.convert() self.background.fill(color) center_x=self.background.get_width()/2 self.left_eye=Eye(center_x-250,250) self.right_eye=Eye(center_x+250,250) self.mouth=Mouth(center_x,500) self.photo=Photo() self.draw() def draw(self): self.screen.blit(self.background,(0,0)) self.left_eye.draw(self.screen) self.right_eye.draw(self.screen) self.mouth.draw(self.screen) pygame.display.flip() def surprise(self): self.left_eye.draw(self.screen,"up") self.right_eye.draw(self.screen,"up") def sad(self): self.left_eye.draw(self.screen,"brownleft") self.right_eye.draw(self.screen,"brownright") self.mouth.draw(self.screen,"sad") def angry(self): self.left_eye.draw(self.screen,"brownright") self.right_eye.draw(self.screen,"brownleft") def look_left(self): self.left_eye.draw(self.screen,"left") self.right_eye.draw(self.screen,"left") def look_right(self): self.left_eye.draw(self.screen,"right") self.right_eye.draw(self.screen,"right") def blink_left(self): self.left_eye.blink(self.screen) def blink_right(self): self.right_eye.blink(self.screen) def close_both(self): self.left_eye.draw(self.screen,"close") self.right_eye.draw(self.screen,"close") def open_both(self): self.left_eye.status="open" self.right_eye.status="open" self.left_eye.draw(self.screen) self.right_eye.draw(self.screen) def blink_both(self): self.close_both() time.sleep(0.10) self.open_both()
class Eyes: """ 両目描画クラス 【責務】 ・両目を描画する """ def __init__(self): """ コンストラクタが呼ばれた後に呼ばれるメソッド """ # Raspberry Pi pin configuration: RST = 24 # 128x64 display with hardware I2C: self.__disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST) # Initialize library. self.__disp.begin() # Get display width and height. self.__width = self.__disp.width self.__height = self.__disp.height # Clear display. self.__disp.clear() self.__disp.display() # Create image buffer. # Make sure to create image with mode '1' for 1-bit color. self.__image = Image.new('1', (self.__width, self.__height)) # Create drawing object. self.__draw = ImageDraw.Draw(self.__image) # Clear image buffer by drawing a black filled box. self.__draw.rectangle((0, 0, self.__width, self.__height), outline=0, fill=0) # Draw the image buffer. self.__disp.image(self.__image) self.__disp.display() # 左右の目のインスタンスを作成 # ここでの左右はディスプレイとしての左右であり、顔としての左右とは逆である self.__eye_l = Eye() self.__eye_r = Eye() self.set_nomal() def set_nomal(self): """ 通常の目を描画 """ self.__eye_l.set_white_eye(30, 32, 60, 60) self.__eye_l.set_black_eye(0, 10, 30) self.__eye_l.set_eyelid(0, 0, 0, 0) self.__eye_l.draw(self.__draw) self.__eye_r.set_white_eye(96, 32, 60, 60) self.__eye_r.set_black_eye(0, 10, 30) self.__eye_r.set_eyelid(0, 0, 0, 0) self.__eye_r.draw(self.__draw) self.__disp.image(self.__image) self.__disp.display() def set_anger(self, level): """ 怒った目を描画 :param int level: 怒りのレベル(1~10) """ if level < 1: level = 1 elif level > 10: level = 10 rate_out = 0.1 * level / 2 rate_in = 0.2 * level / 3 # 描画領域クリア self.__draw.rectangle((0, 0, self.__width, self.__height), outline=0, fill=0) self.__eye_l.set_eyelid(rate_out, rate_in, 0.1, 0.1) self.__eye_l.draw(self.__draw) self.__eye_r.set_eyelid(rate_in, rate_out, 0.1, 0.1) self.__eye_r.draw(self.__draw) self.__disp.image(self.__image) self.__disp.display() def set_surprized(self, level): """ 驚いた目を描画 :param int level: 驚きのレベル(1~5) """ if level < 1: level = 1 elif level > 5: level = 5 size = 50 + level * 3 # 描画領域クリア self.__draw.rectangle((0, 0, self.__width, self.__height), outline=0, fill=0) self.__eye_l.set_white_eye(30, 32, size, size) self.__eye_l.draw(self.__draw) self.__eye_r.set_white_eye(96, 32, size, size) self.__eye_r.draw(self.__draw) self.__disp.image(self.__image) self.__disp.display() def set_black_eyes(self, dir_vec, rate): """ 目を動かす :param vector dir_vec: 方向単位ベクトル :param float rate: 比率 """ if rate < -1: rate = -1 elif rate > 1: rate = 1 # 描画領域クリア self.__draw.rectangle((0, 0, self.__width, self.__height), outline=0, fill=0) self.__eye_l.set_black_eye(30, 32, size, size) self.__eye_l.draw(self.__draw) self.__eye_r.set_white_eye(96, 32, size, size) self.__eye_r.draw(self.__draw) self.__disp.image(self.__image) self.__disp.display()