2025-03-26 11:55:56 +09:00
|
|
|
import os
|
|
|
|
|
import cv2
|
|
|
|
|
import time
|
|
|
|
|
import argparse
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
2025-07-02 18:52:28 +09:00
|
|
|
# UPDATED: Use the factory function and import from the new location
|
|
|
|
|
from uniface.detection import create_detector
|
|
|
|
|
from uniface.visualization import draw_detections
|
2025-03-26 11:55:56 +09:00
|
|
|
|
|
|
|
|
|
2025-07-02 18:52:28 +09:00
|
|
|
def run_inference(detector, image_path: str, vis_threshold: float = 0.6, save_dir: str = "outputs"):
|
2025-03-26 11:55:56 +09:00
|
|
|
"""
|
|
|
|
|
Run face detection on a single image.
|
|
|
|
|
|
|
|
|
|
Args:
|
2025-07-02 18:52:28 +09:00
|
|
|
detector: Initialized face detector.
|
2025-03-26 11:55:56 +09:00
|
|
|
image_path (str): Path to input image.
|
|
|
|
|
vis_threshold (float): Threshold for drawing detections.
|
|
|
|
|
save_dir (str): Directory to save output image.
|
|
|
|
|
"""
|
|
|
|
|
image = cv2.imread(image_path)
|
|
|
|
|
if image is None:
|
|
|
|
|
print(f"❌ Error: Failed to load image from '{image_path}'")
|
|
|
|
|
return
|
|
|
|
|
|
2025-07-02 18:52:28 +09:00
|
|
|
# 1. Get the list of face dictionaries from the detector
|
|
|
|
|
faces = detector.detect(image)
|
2025-11-08 01:15:25 +09:00
|
|
|
|
2025-07-02 18:52:28 +09:00
|
|
|
if faces:
|
|
|
|
|
# 2. Unpack the data into separate lists
|
|
|
|
|
bboxes = [face['bbox'] for face in faces]
|
|
|
|
|
scores = [face['confidence'] for face in faces]
|
|
|
|
|
landmarks = [face['landmarks'] for face in faces]
|
|
|
|
|
|
|
|
|
|
# 3. Pass the unpacked lists to the drawing function
|
|
|
|
|
draw_detections(image, bboxes, scores, landmarks, vis_threshold=0.6)
|
|
|
|
|
|
2025-03-26 11:55:56 +09:00
|
|
|
|
|
|
|
|
os.makedirs(save_dir, exist_ok=True)
|
|
|
|
|
output_path = os.path.join(save_dir, f"{os.path.splitext(os.path.basename(image_path))[0]}_out.jpg")
|
|
|
|
|
cv2.imwrite(output_path, image)
|
|
|
|
|
print(f"✅ Output saved at: {output_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2025-07-02 18:52:28 +09:00
|
|
|
parser = argparse.ArgumentParser(description="Run face detection on an image.")
|
2025-03-26 11:55:56 +09:00
|
|
|
parser.add_argument("--image", type=str, required=True, help="Path to the input image")
|
2025-07-02 18:52:28 +09:00
|
|
|
parser.add_argument(
|
|
|
|
|
"--method",
|
|
|
|
|
type=str,
|
|
|
|
|
default="retinaface",
|
|
|
|
|
choices=['retinaface', 'scrfd'],
|
|
|
|
|
help="Detection method to use."
|
|
|
|
|
)
|
2025-03-26 11:55:56 +09:00
|
|
|
parser.add_argument("--threshold", type=float, default=0.6, help="Visualization confidence threshold")
|
|
|
|
|
parser.add_argument("--iterations", type=int, default=1, help="Number of inference runs for benchmarking")
|
|
|
|
|
parser.add_argument("--save_dir", type=str, default="outputs", help="Directory to save output images")
|
2025-11-08 01:15:25 +09:00
|
|
|
parser.add_argument("--verbose", action="store_true", help="Enable verbose logging")
|
2025-03-26 11:55:56 +09:00
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
2025-11-08 01:15:25 +09:00
|
|
|
if args.verbose:
|
|
|
|
|
from uniface import enable_logging
|
|
|
|
|
enable_logging()
|
|
|
|
|
|
2025-07-02 18:52:28 +09:00
|
|
|
print(f"Initializing detector: {args.method}")
|
|
|
|
|
detector = create_detector(method=args.method)
|
2025-03-26 11:55:56 +09:00
|
|
|
|
|
|
|
|
avg_time = 0
|
|
|
|
|
for i in range(args.iterations):
|
|
|
|
|
start = time.time()
|
2025-07-02 18:52:28 +09:00
|
|
|
run_inference(detector, args.image, args.threshold, args.save_dir)
|
2025-03-26 11:55:56 +09:00
|
|
|
elapsed = time.time() - start
|
|
|
|
|
print(f"[{i + 1}/{args.iterations}] ⏱️ Inference time: {elapsed:.4f} seconds")
|
2025-07-02 18:52:28 +09:00
|
|
|
if i >= 0: # Avoid counting the first run if it includes model loading time
|
|
|
|
|
avg_time += elapsed
|
2025-03-26 11:55:56 +09:00
|
|
|
|
|
|
|
|
if args.iterations > 1:
|
2025-07-02 18:52:28 +09:00
|
|
|
# Adjust average calculation to exclude potential first-run overhead
|
|
|
|
|
effective_iterations = max(1, args.iterations)
|
|
|
|
|
print(
|
|
|
|
|
f"\n🔥 Average inference time over {effective_iterations} runs: {avg_time / effective_iterations:.4f} seconds")
|
2025-03-26 11:55:56 +09:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|