def test_image_draw_mask(): mask = '../rainbow.jpg' mask = Image.open(mask).convert('RGB') mask = image_resize(mask, target_size=(500, 500)) visualize_image(mask, vis=True) image_path = '../lena.png' print('test with pil image') img = Image.open(image_path).convert('RGB') masked_img = image_draw_mask(image_resize(img, target_size=(500, 500)), mask) visualize_image(img, vis=True) visualize_image(masked_img, vis=True) print('test with numpy image with different transparency') img = np.array( Image.open(image_path).convert('RGB')).astype('float32') / 255. img_bak = img.copy() masked_img = image_draw_mask(image_resize(img, target_size=(500, 500)), mask, transparency=0.9) visualize_image(img, vis=True) visualize_image(masked_img, vis=True) masked_img += 1 assert CHECK_EQ_NUMPY( img_bak, img), 'the original image should be equal to the backup version' print('\n\nDONE! SUCCESSFUL!!\n')
def test_image_draw_mask(): # mask = '../rainbow.jpg' mask = '/home/xinshuo/Dropbox/test7.png' mask = Image.open(mask).convert('RGB') # mask = image_resize(mask, target_size=(500, 500)) mask = image_resize(mask, target_size=(1148, 749)) visualize_image(mask, vis=True) # image_path = '../lena.png' image_path = '/home/xinshuo/test8.png' print('test with pil image') img = Image.open(image_path).convert('RGB') img = image_resize(img, target_size=(1148, 749)) print(img.shape) print(mask.shape) masked_img = image_draw_mask(img, mask, transparency=0.5) # visualize_image(img, vis=True) # visualize_image(masked_img, vis=True) save_image(masked_img, save_path='7716_new.png') print('test with numpy image with different transparency') img = np.array( Image.open(image_path).convert('RGB')).astype('float32') / 255. img_bak = img.copy() masked_img = image_draw_mask(image_resize(img, target_size=(500, 500)), mask, transparency=0.9) visualize_image(img, vis=True) visualize_image(masked_img, vis=True) masked_img += 1 assert CHECK_EQ_NUMPY( img_bak, img), 'the original image should be equal to the backup version' print('\n\nDONE! SUCCESSFUL!!\n')
def test_image_resize(): print('test input image as numpy uint8 image with resize_factor') img = (np.random.rand(400, 300, 1) * 255.).astype('uint8') resized_img = image_resize(img, resize_factor=0.5) assert resized_img.shape == (200, 150) image_path = '../lena.png' img = Image.open(image_path).convert('RGB') im_height, im_width = img.size visualize_image(img, vis=True) print('test input image as pil image') resized_img = image_resize(img, resize_factor=0.3) assert resized_img.shape == (int(round(im_height * 0.3)), int(round(im_width * 0.3)), 3) print('test input image as numpy float32 image with target_size') resized_img = image_resize(img, target_size=(1000, 1000)) assert resized_img.shape == (1000, 1000, 3) visualize_image(resized_img, vis=True) print( 'test input image as numpy float32 image with target_size and bilinear' ) resized_img = image_resize(img, target_size=(1000, 1000), interp='bilinear') assert resized_img.shape == (1000, 1000, 3) visualize_image(resized_img, vis=True) ######################################## test failure cases print('test random interp') try: resized_img = image_resize(img, target_size=(800, 600), interp='random') sys.exit('\nwrong! never should be here\n\n') except AssertionError: print('the interp is not correct') print('test both resize_factor and target_size') try: resized_img = image_resize(img, resize_factor=0.4, target_size=(800, 600), interp='random') sys.exit('\nwrong! never should be here\n\n') except AssertionError: print('the resize_factor and target_size coexist') print('\n\nDONE! SUCCESSFUL!!\n')
def emotion_demo(): # parameters for loading data and images detection_model_path = '../trained_models/detection_models/haarcascade_frontalface_default.xml' emotion_model_path = '../trained_models/emotion_models/fer2013_mini_XCEPTION.102-0.66.hdf5' emotion_labels = get_labels('fer2013') # 目のカスケードファイル追加 lefteyecc__path = "../trained_models/detection_models/haarcascade_lefteye_2splits.xml" righteyecc_path = "../trained_models/detection_models/haarcascade_righteye_2splits.xml" nose_path = "../trained_models/detection_models/data_haarcascades_haarcascade_mcs_nose.xml" lefteyecc = cv2.CascadeClassifier(lefteyecc__path) righteyecc = cv2.CascadeClassifier(righteyecc_path) nose = cv2.CascadeClassifier(nose_path) lex = 0; ley = 0; lew = 0; leh = 0 rex = 0; rey = 0; rew = 0; reh = 0 nox = 0; noy = 0; now = 0; noh = 0 # dlib dlib_ini() # hyper-parameters for bounding boxes shape frame_window = 10 emotion_offsets = (20, 40) # loading models face_detection = load_detection_model(detection_model_path) emotion_classifier = load_model(emotion_model_path, compile=False) # getting input model shapes for inference emotion_target_size = emotion_classifier.input_shape[1:3] # starting lists for calculating modes emotion_window = [] global img, flag, slp_count img = cv2.imread('../img/happy.png') flag = 0 slp_count = 0 # dlib用グローバル変数 global gray_image, rgb_image, gray_face, mark_list # starting video streaming cv2.namedWindow('window_frame', cv2.WINDOW_NORMAL) video_capture = cv2.VideoCapture(0) # 0は内蔵カメラ, 1はUSBカメラ while True: bgr_image = video_capture.read()[1] gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY) rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGRA2RGBA) faces = detect_faces(face_detection, gray_image) for face_coordinates in faces: # 目や鼻認識用 (x,y,w,h) = face_coordinates video_face = gray_image[y:y+h,x:x+w] x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets) gray_face = gray_image[y1:y2, x1:x2] try: gray_face = cv2.resize(gray_face, (emotion_target_size)) except: continue # ランドマーク検出 marks_list = marks_list_def(bgr_image, x, y, w, h) print(marks_list) gray_face = preprocess_input(gray_face, True) gray_face = np.expand_dims(gray_face, 0) gray_face = np.expand_dims(gray_face, -1) emotion_prediction = emotion_classifier.predict(gray_face) emotion_probability = np.max(emotion_prediction) emotion_label_arg = np.argmax(emotion_prediction) emotion_text = emotion_labels[emotion_label_arg] emotion_window.append(emotion_text) if len(emotion_window) > frame_window: emotion_window.pop(0) try: emotion_mode = mode(emotion_window) except: continue if flag == 0 or flag == 1: if emotion_text == 'angry': img = cv2.imread('../img/angry.png', -1) color = emotion_probability * np.asarray((255, 0, 0)) elif emotion_text == 'sad': img = cv2.imread('../img/sad.png', -1) # 関数にする color = emotion_probability * np.asarray((0, 0, 255)) elif emotion_text == 'happy': img = cv2.imread('../img/happy.png', -1) color = emotion_probability * np.asarray((255, 255, 0)) elif emotion_text == 'surprise': img = cv2.imread('../img/odoroki.png', -1) color = emotion_probability * np.asarray((0, 255, 255)) else : img = cv2.imread('../img/neutral.png', -1) color = emotion_probability * np.asarray((0, 255, 0)) else: if emotion_text == 'angry': img = cv2.imread('../img/ikari.png', -1) color = emotion_probability * np.asarray((255, 0, 0)) elif emotion_text == 'sad': img = cv2.imread('../img/shock.png', -1) color = emotion_probability * np.asarray((0, 0, 255)) elif emotion_text == 'happy': img = cv2.imread('../img/kirakira.png', -1) color = emotion_probability * np.asarray((255, 255, 0)) elif emotion_text == 'surprise': img = cv2.imread('../img/bikkuri.png', -1) color = emotion_probability * np.asarray((0, 255, 255)) else : img = cv2.imread('../img/toumei.png', -1) color = emotion_probability * np.asarray((0, 255, 0)) color = color.astype(int) color = color.tolist() if flag == 0: draw_bounding_box(face_coordinates, rgb_image, color) elif flag == 1: rgb_image = draw_bounding_box2(face_coordinates, rgb_image, color, img, marks_list) elif flag == 2: overlay_pic = draw_bounding_box3(face_coordinates, rgb_image, color, img, marks_list) rgb_image = overlay_pic draw_text(face_coordinates, rgb_image, emotion_mode, color, 0, -45, 1, 1) bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR) if flag == 0: img = image_resize(img) cv2.imshow('image', img) cv2.destroyWindow('Window_frame') elif flag == 1 or flag == 2: cv2.destroyWindow('image') cv2.imshow('window_frame', bgr_image) cv2.waitKey(10) # cv2.imshow('own_window', bgr_image) if cv2.waitKey(1) & 0xFF == ord('z'): flag = 0 elif cv2.waitKey(1) & 0xFF == ord('x'): flag = 1 elif cv2.waitKey(1) & 0xFF == ord('c'): flag = 2 elif cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release() cv2.destroyAllWindows()