Face Detection - gideonSamzz/Empetror_gs_docx GitHub Wiki

import io import cv2 import numpy as np from dotenv import load_dotenv from google.cloud import videointelligence_v1 as videointelligence

def detect_faces(local_file_path="./resource/friends.mp4", output_path="output_faces.mp4"): """Detects faces in a video, displays live annotated window, and saves output video."""

client = videointelligence.VideoIntelligenceServiceClient()

with io.open(local_file_path, "rb") as f:
    input_content = f.read()

# Face Detection Config
config = videointelligence.FaceDetectionConfig(
    include_bounding_boxes=True, include_attributes=True
)
context = videointelligence.VideoContext(face_detection_config=config)

# Start async request
operation = client.annotate_video(
    request={
        "features": [videointelligence.Feature.FACE_DETECTION],
        "input_content": input_content,
        "video_context": context,
    }
)

print("\nProcessing video for face detection annotations...")
result = operation.result(timeout=300)
print("Finished processing.\n")

annotation_result = result.annotation_results[0]

# OpenCV video setup
cap = cv2.VideoCapture(local_file_path)
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

# VideoWriter to save output
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out_writer = cv2.VideoWriter(output_path, fourcc, fps, (width, height))

# Organize frame-based annotations
frame_annotations = {}

for annotation in annotation_result.face_detection_annotations:
    for track in annotation.tracks:
        for obj in track.timestamped_objects:
            timestamp = obj.time_offset.total_seconds()
            frame_index = int(timestamp * fps)

            if frame_index >= total_frames:
                continue

            if frame_index not in frame_annotations:
                frame_annotations[frame_index] = []

            frame_annotations[frame_index].append(obj)

# Display and Save
frame_id = 0
cv2.namedWindow("Live Face Detection", cv2.WINDOW_NORMAL)

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break

    original_frame = frame.copy()

    if frame_id in frame_annotations:
        for obj in frame_annotations[frame_id]:
            box = obj.normalized_bounding_box
            x1 = int(box.left * width)
            y1 = int(box.top * height)
            x2 = int(box.right * width)
            y2 = int(box.bottom * height)

            # Draw box
            cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
            cv2.rectangle(original_frame, (x1, y1), (x2, y2), (0, 255, 0), 2)

            # Draw attributes
            y_text = y1 - 10
            for attr in obj.attributes:
                label = f"{attr.name}: {attr.value} ({attr.confidence:.2f})"
                cv2.putText(frame, label, (x1, y_text),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 0, 0), 1)
                cv2.putText(original_frame, label, (x1, y_text),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 0, 0), 1)
                y_text -= 15

    # Resize display window
    max_display_width = 800
    if frame.shape[1] > max_display_width:
        scale_ratio = max_display_width / frame.shape[1]
        frame = cv2.resize(frame, (0, 0), fx=scale_ratio, fy=scale_ratio)

    # Show annotated frame
    cv2.imshow("Live Face Detection", frame)

    # Save original-size annotated frame
    out_writer.write(original_frame)

    if cv2.waitKey(int(1000 / fps)) & 0xFF == ord('q'):
        break

    frame_id += 1

cap.release()
out_writer.release()
cv2.destroyAllWindows()
print(f"Live window closed. Annotated video saved to: {output_path}")

if name == 'main': load_dotenv() detect_faces()