forked from yinguobing/image_utility
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_local_area.py
More file actions
136 lines (107 loc) · 4.18 KB
/
Copy pathextract_local_area.py
File metadata and controls
136 lines (107 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
This script shows how to extract local area and corsponding facial
landmark points from IBUG dataset.
"""
import json
import os
import numpy as np
import cv2
import mark_detector as md
import pts_tools as pt
DATA_DIR = "/home/robin/Documents/landmark/223K"
TARGET_DIR = "/home/robin/Desktop/export"
TARGET_SIZE = 24
def read_image(img_file):
"""Read the corsponding image."""
if os.path.exists(img_file):
img = cv2.imread(img_file)
return img
def extract_local_img(image, feature_index):
"""Extract face area from image."""
# Do landmark detection first.
marks = md.detect_marks(image, md.MARK_SESS, md.MARK_GRAPH)
marks = marks * 128
# Draw marks
# md.draw_marks(image, marks)
# Get the target point location.
# IDX- TARGET
# 30 - Nose tip
# 8 - Chin
# 36 - Left eye left corner
# 45 - Right eye right corner
# 48 - Left Mouth corner
# 54 - Right mouth corner
target_point_idx = feature_index
target_point = marks[target_point_idx].astype(int)
local_x = target_point[0]
local_y = target_point[1]
# Try to make a 24x24 square box with target point as center.
local_box = [local_x - 12, local_y - 12,
local_x + 12, local_y + 12]
# Check if local box is in image.
if pt.box_in_image(local_box, image) is False:
return None
# Box ok, return it.
return local_box
def main():
"""The main entrance"""
# List all the image files.
img_list = []
for file_path, _, file_names in os.walk(DATA_DIR):
for file_name in file_names:
if file_name.split(".")[-1] in ["jpg"]:
img_list.append(os.path.join(file_path, file_name))
# Extract the image one by one. Use a dict to keep file count.
counter = {'invalid': 0}
for file_name in img_list:
print(file_name)
# Read in image file.
image = read_image(file_name)
# Read in label point.
json_path = file_name.split('.')[-2] + '.json'
feature_idx = 30
with open(json_path) as file:
label_marks = np.array(json.load(file), dtype=np.float32)
label_marks = np.reshape(label_marks, (-1, 2))
label_point = label_marks[feature_idx] * 128
# Extract face image and new points.
local_box = extract_local_img(image, feature_index=feature_idx)
# Check if target label point is in box.
if local_box is not None and pt.points_in_box([label_point], local_box):
# Get new image
local_img = image[local_box[1]:local_box[3],
local_box[0]:local_box[2]]
# New point value
point_normlized = [(label_point[0] - local_box[0]) / 24,
(label_point[1] - local_box[1]) / 24]
# cv2.circle(local_img, (int(point_normlized[0] * 24),
# int(point_normlized[1] * 24)), 1, (0, 255, 0), -1)
# # Preview the Image.
# preview_img = local_img.copy()
# preview_img = cv2.resize(
# preview_img, (512, 512), interpolation=cv2.INTER_AREA)
# cv2.imshow('preview', preview_img)
# if cv2.waitKey() == 27:
# break
# New file to be written.
_, tail = os.path.split(file_name)
common_file_name = tail.split('.')[-2]
common_url = os.path.join(
TARGET_DIR, 'feature30', common_file_name + '-' + str(feature_idx))
# Save the Image.
image_url = common_url + ".jpg"
cv2.imwrite(image_url, local_img)
# Save the new point location.
csv_url = common_url + ".json"
points_to_save = np.array(point_normlized).flatten()
with open(csv_url, mode='w') as file:
json.dump(list(points_to_save), file)
print("New file saved:", image_url, csv_url, sep='\n')
else:
counter['invalid'] += 1
# All done, output debug info.
print("All done! Total file: {}, invalid: {}, succeed: {}".format(
len(img_list), counter['invalid'],
len(img_list) - counter['invalid']))
if __name__ == '__main__':
main()