-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_tool.py
100 lines (85 loc) · 3.22 KB
/
face_tool.py
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
import argparse
import glob
import os
#from pose import pose # (Import only if needed)
def get_image_paths(input_path):
"""
Gets a list of image file paths from the given input path (image or directory).
Args:
input_path (str): Path to an image or directory.
Returns:
list: List of image file paths.
"""
if os.path.isfile(input_path):
return [input_path]
elif os.path.isdir(input_path):
# Use glob to search for image extensions (modify pattern if needed)
image_paths = glob.glob(os.path.join(input_path, "*.jpg"))
image_paths.extend(glob.glob(os.path.join(input_path, "*.jpeg")))
image_paths.extend(glob.glob(os.path.join(input_path, "*.png")))
return image_paths
else:
raise ValueError(f"Invalid input path: {input_path}")
def main():
parser = argparse.ArgumentParser(description="Face Processing App")
parser.add_argument(
"-i",
"--input",
type=str,
required=True,
help="Path to an image or directory containing images"
)
parser.add_argument(
"-d", "--detect", action="store_true", default=False, help="Perform face detection"
)
parser.add_argument(
"-l", "--landmark", action="store_true", default=False, help="Perform landmark recognition"
)
parser.add_argument("-p", "--parse", action="store_true", default=False, help="Perform face parsing")
parser.add_argument(
"-o",
"--output",
type=str,
default=None,
help="Output path to save processed images (optional)",
)
try:
args = parser.parse_args()
# Load modules only if their flags are set
if args.detect:
from detectors import detect
detector = detect.FaceDetector() # Initialize detector
else:
detector = None
if args.landmark: # Import only if needed and flag is set
from landmarks import landmark # (Import only if needed)
landmark_recognizer = landmark.LandmarkRecognizer()
else:
landmark_recognizer = None
if args.parse: # Import only if needed and flag is set
from face_parser import parserModel
face_parser = parserModel.FaceParser()
else:
face_parser = None
# Process images (modify logic based on your implementation)
for image_path in get_image_paths(args.input):
output_path = args.output
if detector:
if output_path:
detector.detect_and_draw_faces(image_path, output_path=output_path)
else:
detector.detect_and_draw_faces(image_path)
if landmark_recognizer: # Perform landmark recognition only if faces detected
if output_path:
landmark_recognizer.detect_and_draw_landmarks(image_path, output_path=output_path)
else:
landmark_recognizer.detect_and_draw_landmarks(image_path)
if face_parser: # Perform pose estimation only if faces detected
if output_path:
face_parser.parse_and_save_faces(image_path, output_path=output_path)
else:
face_parser.parse_and_save_faces(image_path)
except argparse.ArgumentError as e:
parser.error(f"{e}\nInput file or directory path must be provided.")
if __name__ == "__main__":
main()