From 1ccc68bb925bbeb4e588fb63f00a30afacd906e4 Mon Sep 17 00:00:00 2001 From: Junhan Hu Date: Thu, 29 Dec 2022 15:39:05 -0800 Subject: [PATCH 1/2] add gui alignment tool --- eyelign/gui_manual_align.py | 73 +++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 eyelign/gui_manual_align.py diff --git a/eyelign/gui_manual_align.py b/eyelign/gui_manual_align.py new file mode 100644 index 0000000..3e8d4b1 --- /dev/null +++ b/eyelign/gui_manual_align.py @@ -0,0 +1,73 @@ +import logging +import cv2 +import os +import click +import json +import shutil +logging.basicConfig( + level=logging.INFO, + format="%(asctime)s [%(levelname)s]: %(message)s", + datefmt="%Y-%m-%d %H:%M:%S", +) + + +@click.command() +@click.argument( + "input_dir", + type=click.Path(exists=True, readable=True), + envvar="EYELIGN_INPUT_DIR", +) + +def cli( + input_dir +): + logging.info('Click left eye and right eye in order\n \ + If click wrong, press ESC to exit and align agin \ + Aligned image info would not lost') + read_json_path=os.path.join(input_dir,'.eyelign') + align_file=json.load(open(read_json_path,'r')) + + # mouse callback helper function + feature_points_2d = [] + FEATURE_NUM = 2 + def draw_circle(event, x, y, flags, param): + if event == cv2.EVENT_LBUTTONDOWN: + logging.info('Left eye aligned, now click right eye') + cv2.circle(img,(x,y),10,(0,0,255),-1) + feature_points_2d.append([x, y]) + + exit_flag=False + for file_name in align_file: + ## Check if the eyes are detected + data=align_file[file_name] + lx=data['lx'] + if lx is None: + logging.info(f'Manual align file {file_name}') + img_path=os.path.join(input_dir,file_name) + img = cv2.imread(img_path) + cv2.namedWindow("image") + cv2.setMouseCallback("image", draw_circle) + while True: + cv2.imshow("image", img) + if len(feature_points_2d) == FEATURE_NUM: + if len(feature_points_2d)==2: + align_file[file_name]['lx']=feature_points_2d[0][0] + align_file[file_name]['ly']=feature_points_2d[0][1] + align_file[file_name]['rx']=feature_points_2d[1][0] + align_file[file_name]['ry']=feature_points_2d[1][1] + feature_points_2d = [] # reset + break + if (cv2.waitKey(20) & 0xFF == 27): + exit_flag=True + break + if exit_flag is True: + break + backup_path=os.path.join(input_dir,'.eyelign.bak') + shutil.copy(read_json_path,backup_path) + logging.info(f'Backup the original align file to {backup_path}') + with open(read_json_path, 'w') as outfile: + json.dump(align_file, outfile,indent=2) + + +if __name__ == "__main__": + cli() From e06e948806d8b384c4dbb2570e24af450582e689 Mon Sep 17 00:00:00 2001 From: Junhan Hu Date: Thu, 29 Dec 2022 15:53:49 -0800 Subject: [PATCH 2/2] fix typo and misleading output --- eyelign/gui_manual_align.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/eyelign/gui_manual_align.py b/eyelign/gui_manual_align.py index 3e8d4b1..415caf6 100644 --- a/eyelign/gui_manual_align.py +++ b/eyelign/gui_manual_align.py @@ -22,7 +22,7 @@ def cli( input_dir ): logging.info('Click left eye and right eye in order\n \ - If click wrong, press ESC to exit and align agin \ + If click wrong, press ESC to exit and align agin\n \ Aligned image info would not lost') read_json_path=os.path.join(input_dir,'.eyelign') align_file=json.load(open(read_json_path,'r')) @@ -32,7 +32,6 @@ def cli( FEATURE_NUM = 2 def draw_circle(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: - logging.info('Left eye aligned, now click right eye') cv2.circle(img,(x,y),10,(0,0,255),-1) feature_points_2d.append([x, y])