{"id":25164493,"url":"https://github.com/deepaksilaych/face_tracker","last_synced_at":"2025-04-03T15:48:06.045Z","repository":{"id":273946827,"uuid":"921403721","full_name":"DeepakSilaych/face_tracker","owner":"DeepakSilaych","description":"This is a face tracker that processes a video file instead of a live camera feed. The implementation is structured to ensure readability and scalability.","archived":false,"fork":false,"pushed_at":"2025-01-23T22:12:27.000Z","size":10252,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-09T04:41:12.162Z","etag":null,"topics":["face-detection","face-recognition","face-tracking","yolo"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DeepakSilaych.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-23T22:10:37.000Z","updated_at":"2025-01-23T22:13:21.000Z","dependencies_parsed_at":null,"dependency_job_id":"c00951d8-24d1-4a5a-8968-00c781ddd8c1","html_url":"https://github.com/DeepakSilaych/face_tracker","commit_stats":null,"previous_names":["deepaksilaych/face_tracker"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DeepakSilaych%2Fface_tracker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DeepakSilaych%2Fface_tracker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DeepakSilaych%2Fface_tracker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DeepakSilaych%2Fface_tracker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DeepakSilaych","download_url":"https://codeload.github.com/DeepakSilaych/face_tracker/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247033176,"owners_count":20872521,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["face-detection","face-recognition","face-tracking","yolo"],"created_at":"2025-02-09T04:33:26.067Z","updated_at":"2025-04-03T15:48:06.035Z","avatar_url":"https://github.com/DeepakSilaych.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"This is a face tracker that processes a video file instead of a live camera feed. The implementation is structured to ensure readability and scalability.\n\n---\n\n### **Modular File Structure**\n\n```\nface-tracker/\n├── src/\n│   ├── face_tracker.py       # Main script to run the tracker\n│   ├── modules/\n│   │   ├── detector.py       # Face detection logic\n│   │   ├── tracker.py        # Tracking logic\n│   │   ├── utils.py          # Utility functions (e.g., draw bounding boxes)\n├── data/\n│   ├── input_video.mp4       # Input video for testing\n│   ├── output_video.mp4      # Processed output video with tracking\n├── requirements.txt          # Dependencies\n├── README.md                 # Documentation\n```\n\n---\n\n### **Implementation**\n\n#### **Step 1: Create `detector.py`**\nThis module handles face detection using `dlib`.\n\n```python\nimport dlib\n\ndef detect_faces(frame, gray_frame):\n    \"\"\"\n    Detects faces in the given frame using dlib's face detector.\n    \n    Args:\n        frame (ndarray): Original color frame.\n        gray_frame (ndarray): Grayscale version of the frame.\n\n    Returns:\n        list: A list of bounding boxes [(x, y, w, h), ...].\n    \"\"\"\n    detector = dlib.get_frontal_face_detector()\n    faces = detector(gray_frame)\n    bboxes = [(face.left(), face.top(), face.width(), face.height()) for face in faces]\n    return bboxes\n```\n\n---\n\n#### **Step 2: Create `tracker.py`**\nThis module manages the tracking of detected faces using OpenCV’s trackers.\n\n```python\nimport cv2\n\ndef initialize_tracker(frame, bbox):\n    \"\"\"\n    Initializes a tracker for a given bounding box.\n\n    Args:\n        frame (ndarray): Initial frame where the object is located.\n        bbox (tuple): Bounding box (x, y, w, h).\n\n    Returns:\n        cv2.Tracker: Initialized tracker object.\n    \"\"\"\n    tracker = cv2.TrackerKCF_create()  # You can replace with other trackers\n    tracker.init(frame, bbox)\n    return tracker\n\ndef update_tracker(tracker, frame):\n    \"\"\"\n    Updates the tracker for the current frame.\n\n    Args:\n        tracker (cv2.Tracker): Initialized tracker object.\n        frame (ndarray): Current video frame.\n\n    Returns:\n        tuple: Success flag and updated bounding box (success, bbox).\n    \"\"\"\n    return tracker.update(frame)\n```\n\n---\n\n#### **Step 3: Create `utils.py`**\nUtility functions for drawing bounding boxes and video processing.\n\n```python\nimport cv2\n\ndef draw_bounding_box(frame, bbox, color=(0, 255, 0)):\n    \"\"\"\n    Draws a rectangle around the detected object.\n\n    Args:\n        frame (ndarray): Video frame.\n        bbox (tuple): Bounding box (x, y, w, h).\n        color (tuple): Rectangle color (default is green).\n    \"\"\"\n    x, y, w, h = map(int, bbox)\n    cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)\n\ndef save_video(output_path, frame_width, frame_height, fps):\n    \"\"\"\n    Initializes the video writer to save the processed video.\n\n    Args:\n        output_path (str): Path to save the output video.\n        frame_width (int): Width of the video frames.\n        frame_height (int): Height of the video frames.\n        fps (int): Frames per second of the video.\n\n    Returns:\n        cv2.VideoWriter: Video writer object.\n    \"\"\"\n    fourcc = cv2.VideoWriter_fourcc(*'mp4v')\n    return cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))\n```\n\n---\n\n#### **Step 4: Create `face_tracker.py`**\nThe main script to handle video processing and integrate the modules.\n\n```python\nimport cv2\nimport os\nfrom modules.detector import detect_faces\nfrom modules.tracker import initialize_tracker, update_tracker\nfrom modules.utils import draw_bounding_box, save_video\n\ndef main(input_video, output_video):\n    # Check if input video exists\n    if not os.path.exists(input_video):\n        raise FileNotFoundError(f\"Input video {input_video} not found.\")\n\n    # Open the input video\n    cap = cv2.VideoCapture(input_video)\n    fps = int(cap.get(cv2.CAP_PROP_FPS))\n    frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))\n    frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n\n    # Initialize video writer\n    writer = save_video(output_video, frame_width, frame_height, fps)\n\n    tracking = False\n    tracker = None\n\n    while cap.isOpened():\n        ret, frame = cap.read()\n        if not ret:\n            break\n\n        # Convert frame to grayscale\n        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n\n        if not tracking:\n            # Detect faces in the current frame\n            bboxes = detect_faces(frame, gray_frame)\n            if bboxes:\n                # Initialize tracker for the first detected face\n                tracker = initialize_tracker(frame, bboxes[0])\n                tracking = True\n        else:\n            # Update tracker\n            success, bbox = update_tracker(tracker, frame)\n            if success:\n                draw_bounding_box(frame, bbox)\n            else:\n                tracking = False  # Stop tracking if it fails\n\n        # Write the frame to the output video\n        writer.write(frame)\n\n        # Display the processed frame\n        cv2.imshow(\"Face Tracker\", frame)\n        if cv2.waitKey(1) \u0026 0xFF == ord('q'):\n            break\n\n    # Release resources\n    cap.release()\n    writer.release()\n    cv2.destroyAllWindows()\n\nif __name__ == \"__main__\":\n    input_video = \"data/input_video.mp4\"\n    output_video = \"data/output_video.mp4\"\n    main(input_video, output_video)\n```\n\n---\n\n### **Execution Steps**\n\n1. **Place Input Video**:\n   Save your test video as `data/input_video.mp4`.\n\n2. **Install Dependencies**:\n   Add these dependencies to `requirements.txt`:\n   ```text\n   opencv-python\n   dlib\n   imutils\n   numpy\n   ```\n   Install them:\n   ```bash\n   pip install -r requirements.txt\n   ```\n\n3. **Run the Script**:\n   Execute the face tracker:\n   ```bash\n   python src/face_tracker.py\n   ```\n\n4. **Output**:\n   - Processed video with bounding boxes is saved as `data/output_video.mp4`.\n\n---\n\nLet me know if you need help refining this implementation or adding additional features!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeepaksilaych%2Fface_tracker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeepaksilaych%2Fface_tracker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeepaksilaych%2Fface_tracker/lists"}