{"name":"face-cropper: Advanced Face Cropping with Mediapipe and OpenCV","description":"The `face-cropper` repository offers a robust Python solution for extracting faces from images. It implements a custom pipeline leveraging Mediapipe's FaceDetection and FaceMesh networks to accurately crop, optionally remove backgrounds, and correct the roll of detected faces. This tool is ideal for pre-processing images in computer vision tasks or real-time applications.","github":"https://github.com/tsen-dev/face-cropper","url":"https://osrepos.com/repo/tsen-dev-face-cropper","source":"osrepos.com","sourceDescription":"This repository profile is provided by osrepos.com, an open source repository discovery platform.","repositoryProfile":"https://osrepos.com/repo/tsen-dev-face-cropper","generatedFor":"open source discovery and AI-assisted research","markdown":"https://osrepos.com/repo/tsen-dev-face-cropper.md","json":"https://osrepos.com/repo/tsen-dev-face-cropper.json","topics":["computer-vision","face-cropping","mediapipe","opencv","python","image-processing","face-detection","face-segmentation"],"keywords":["computer-vision","face-cropping","mediapipe","opencv","python","image-processing","face-detection","face-segmentation"],"stars":null,"summary":"The `face-cropper` repository offers a robust Python solution for extracting faces from images. It implements a custom pipeline leveraging Mediapipe's FaceDetection and FaceMesh networks to accurately crop, optionally remove backgrounds, and correct the roll of detected faces. This tool is ideal for pre-processing images in computer vision tasks or real-time applications.","content":"## Introduction\n\nThe `face-cropper` project by tsen-dev is a powerful Python library designed for precise face cropping from images. It implements a custom pipeline leveraging Mediapipe's FaceDetection and FaceMesh networks to accurately identify, segment, and extract faces. Key features include intelligent bounding box inflation to handle rotated faces, optional background removal, and automatic roll correction for perfectly aligned outputs. The repository includes several [demos](https://github.com/tsen-dev/face-cropper#demos) showcasing its capabilities across various 'in the wild' image conditions.\n\n## Installation\n\nTo integrate `face-cropper` into your project, simply add the `face_cropper.py` module to your Python environment. Ensure you have the necessary dependencies installed, primarily `mediapipe`, `opencv-python`, and `numpy`. You can install them via pip:\n\nbash\npip install mediapipe opencv-python numpy\n\n\n## Examples\n\nUsing `face-cropper` is straightforward. First, import the `FaceCropper` class and then instantiate it with your desired configuration. Finally, call the `get_faces()` method with your image.\n\npython\nimport cv2\nimport numpy as np\nfrom face_cropper import FaceCropper, LONG_RANGE, STATIC_MODE\n\n# Load an image (replace with your image path)\nimage = cv2.imread(\"path/to/your/image.jpg\")\nif image is None:\n    print(\"Error: Image not found.\")\n    exit()\n\n# Convert BGR to RGB (Mediapipe expects RGB)\nimage_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n# Create a FaceCropper object with custom configuration\nface_cropper = FaceCropper(\n    min_face_detector_confidence=0.5,\n    face_detector_model_selection=LONG_RANGE,\n    landmark_detector_static_image_mode=STATIC_MODE,\n    min_landmark_detector_confidence=0.5\n)\n\n# Get cropped faces, with background removal and roll correction\ncropped_faces = face_cropper.get_faces(\n    image_rgb,\n    remove_background=True,\n    correct_roll=True\n)\n\n# Process or save the cropped faces\nfor i, face in enumerate(cropped_faces):\n    # Convert back to BGR for OpenCV display/save\n    face_bgr = cv2.cvtColor(face, cv2.COLOR_RGB2BGR)\n    cv2.imwrite(f\"cropped_face_{i}.jpg\", face_bgr)\n    print(f\"Saved cropped_face_{i}.jpg\")\n\n# Example with default settings\n# face_cropper_default = FaceCropper()\n# cropped_faces_default = face_cropper_default.get_faces(image_rgb)\n\n\nThe `FaceCropper` constructor and `get_faces` method offer various parameters for fine-tuning behavior, such as confidence thresholds, model selection (short-range or long-range), and whether to enable background removal or roll correction.\n\n## Why Use\n\nThe `face-cropper` library offers several compelling reasons for its adoption in projects requiring face extraction:\n\n- **Accurate and Robust:** It handles 'in the wild' images effectively, including faces with in-plane rotation, thanks to its intelligent pipeline that inflates bounding boxes and corrects roll.\n- **Efficient:** Built upon light-weight Mediapipe networks, it is suitable for real-time applications, especially when processing a small number of faces.\n- **Customizable Pipeline:** The modular design allows for optional background removal and roll correction, providing flexibility to suit specific application needs.\n- **Pre-processing for ML:** It serves as an excellent pre-processing step to accelerate the learning of face features from uncontrolled image datasets, improving the quality of input for downstream machine learning models.\n\n## Links\n\nExplore the `face-cropper` project further using these links:\n\n- [GitHub Repository](https://github.com/tsen-dev/face-cropper)\n- [Mediapipe FaceDetection Documentation](https://google.github.io/mediapipe/solutions/face_detection.html)\n- [Mediapipe FaceMesh Documentation](https://google.github.io/mediapipe/solutions/face_mesh.html)","metrics":{"detailViews":5,"githubClicks":2},"dates":{"published":null,"modified":"2025-10-12T04:10:49.000Z"}}