Created
June 28, 2018 09:21
-
-
Save NTU-P04922004/e5d01b97216df6792d3f165b1307be2d to your computer and use it in GitHub Desktop.
temp python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| """ | |
| import os | |
| import cv2 | |
| IMAGE_WIDTH = 640 | |
| IMAGE_HEIGHT = 480 | |
| def draw_fingertip(img_path, labels): | |
| img = cv2.imread(img_path) | |
| if img is None: | |
| return | |
| x = labels[0] | |
| y = labels[1] | |
| cv2.circle(img, (int(x), int(y)), 2, (0,255,0), -1) | |
| # Draw grid lines | |
| # | |
| # print(img.shape) | |
| screen_w = IMAGE_WIDTH | |
| screen_h = IMAGE_HEIGHT | |
| # cv2.line(img, (int(screen_w * 0.25), 0), (int(screen_w * 0.25), int(screen_h)), (255, 0, 0), 1, 1) | |
| # cv2.line(img, (int(screen_w * 0.5), 0), (int(screen_w * 0.5), int(screen_h)), (255, 0, 0), 1, 1) | |
| # cv2.line(img, (int(screen_w * 0.75), 0), (int(screen_w * 0.75), int(screen_h)), (255, 0, 0), 1, 1) | |
| # cv2.line(img, (0, int(screen_h * 0.25)), (int(screen_w), int(screen_h * 0.25)), (255, 0, 0), 1, 1) | |
| # cv2.line(img, (0, int(screen_h * 0.5)), (int(screen_w), int(screen_h * 0.5)), (255, 0, 0), 1, 1) | |
| # cv2.line(img, (0, int(screen_h * 0.75)), (int(screen_w), int(screen_h * 0.75)), (255, 0, 0), 1, 1) | |
| filename = os.path.basename(img_path) | |
| out_dir_name = os.path.basename(os.path.dirname(img_path)) + '_auto_test' | |
| # print(out_dir_name) | |
| # cv2.imwrite((out_dir_name + '/%s' % filename), img) | |
| cv2.imshow('image', img) | |
| cv2.waitKey(0) | |
| def auto_label_test(base_path, filename): | |
| file_path = base_path + '/' + filename | |
| with open(file_path, 'r') as in_fp: | |
| lines = in_fp.readlines() | |
| dir_name = os.path.basename(base_path) + '_auto_test' | |
| if not os.path.exists(dir_name): | |
| os.makedirs(dir_name) | |
| label_data_str = '' | |
| img_base_path = base_path | |
| count = 0 | |
| for line in lines: | |
| if len(line) > 1: | |
| tokens = line.split() | |
| img_name = os.path.basename(tokens[0]) | |
| index_finger_x = float(tokens[1]) | |
| index_finger_y = float(tokens[2]) | |
| print(img_name, index_finger_x, index_finger_y) | |
| draw_fingertip(img_base_path + '/' + img_name, [float(tokens[1]), float(tokens[2])]) | |
| count += 1 | |
| # if count == 100: | |
| # break | |
| def generate_keras_hand_label_file(base_path, filename): | |
| file_path = base_path + '/' + filename | |
| with open(file_path, 'r') as in_fp: | |
| lines = in_fp.readlines() | |
| label_data_str = '' | |
| img_base_path = base_path # + '/simplified_dataset5' | |
| for line in lines: | |
| if len(line) > 1: | |
| tokens = line.split() | |
| img_path = tokens[0] | |
| print(img_path) | |
| img = cv2.imread(img_base_path + '/' + img_path) | |
| img_width = img.shape[1] | |
| img_height = img.shape[0] | |
| index_finger_x = float(tokens[1]) / img_width | |
| index_finger_y = float(tokens[2]) / img_height | |
| label_data_str += \ | |
| '%s %.6f %.6f %.6f %.6f %.6f\n' % (img_path, \ | |
| int(round(img_width)), int(round(img_height)), index_finger_x, index_finger_y, \ | |
| 0) | |
| print(img_base_path + '/' + img_path) | |
| # print(float(tokens[3]), float(tokens[4])) | |
| draw_fingertip(img_base_path + '/' + img_path, [float(tokens[3]) * img_width, float(tokens[4]) * img_height]) | |
| # draw_fingertip(img_base_path + '/' + img_path, [float(tokens[5]), float(tokens[6])]) | |
| # out_path = base_path + '/' + filename.replace('.txt', '') + '_labels.txt' | |
| # with open(out_path, 'w') as out_fp: | |
| # out_fp.write(label_data_str + '\n') | |
| base_path = 'D:/Dataset/Fingertips' | |
| generate_keras_hand_label_file(base_path, '20180613_154704_frame_hand_label.txt') | |
| # auto_label_test('D:/Dataset/Fingertips/9901_0615', '9901_0615_label.txt') | |
| cv2.destroyAllWindows() | |
| # Draw ground truth | |
| # | |
| # filename_list = ['color_3_0002.png'] | |
| # for filename in filename_list: | |
| # label_path = 'labels/I_SuperMarket/' + filename.replace('.png', '.txt') | |
| # img_path = 'images/I_SuperMarket/' + filename | |
| # gt = [] | |
| # with open(label_path, 'r') as in_fp: | |
| # line = in_fp.readline() | |
| # tokens = line.split() | |
| # print(filename, tokens) | |
| # gt = [float(tokens[t]) for t in range(1, len(tokens))] | |
| # draw_fingertip(img_path, gt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment