Python implemention of the TensorFlow BodyPix model.
Project description
TensorFlow BodyPix (TF BodyPix)
A Python implementation of body-pix.
Install
Install with all dependencies:
pip install tf-bodypix[all]
Install with minimal or no dependencies:
pip install tf-bodypix
Extras are provided to make it easier to provide or exclude dependencies when using this project as a library:
extra name | description |
---|---|
tf | TensorFlow (required). But you may use your own build. |
tfjs | TensorFlow JS Model support |
image | Image loading via Pillow, required by the CLI. |
webcam | Webcam support via OpenCV and pyfakewebcam |
all | All of the libraries |
CLI
Creating a simple body mask
TF_CPP_MIN_LOG_LEVEL=3 \
python -m tf_bodypix \
image-to-mask \
--source /path/to/input-image.jpg \
--output /path/to/output-mask.jpg \
--threshold=0.75
Colorize the body mask depending on the body part
TF_CPP_MIN_LOG_LEVEL=3 \
python -m tf_bodypix \
image-to-mask \
--source /path/to/input-image.jpg \
--output /path/to/output-colored-mask.jpg \
--threshold=0.75 \
--colored
Additionally select the body parts
TF_CPP_MIN_LOG_LEVEL=3 \
python -m tf_bodypix \
image-to-mask \
--source /path/to/input-image.jpg \
--output /path/to/output-colored-mask.jpg \
--threshold=0.75 \
--parts left_face right_face \
--colored
Capture Webcam and adding mask overlay, showing the result in an image
TF_CPP_MIN_LOG_LEVEL=3 \
python -m tf_bodypix \
image-to-mask \
--source webcam:0 \
--show-output \
--threshold=0.75 \
--add-overlay-alpha=0.5 \
--colored
Capture Webcam and adding mask overlay, writing to v4l2loopback device
(replace /dev/videoN
with the actual virtual video device)
TF_CPP_MIN_LOG_LEVEL=3 \
python -m tf_bodypix \
image-to-mask \
--source webcam:0 \
--output /dev/videoN \
--threshold=0.75 \
--add-overlay-alpha=0.5 \
--colored
Capture Webcam and replace background, writing to v4l2loopback device
(replace /dev/videoN
with the actual virtual video device)
TF_CPP_MIN_LOG_LEVEL=3 \
python -m tf_bodypix \
replace-background \
--source webcam:0 \
--background /path/to/background-image.jpg \
--output /dev/videoN \
--threshold=0.75
API
import tensorflow as tf
from tf_bodypix.api import download_model, load_model, BodyPixModelPaths
bodypix_model = load_model(download_model(
BodyPixModelPaths.MOBILENET_FLOAT_50_STRIDE_16
))
image = tf.keras.preprocessing.image.load_img(
'/path/to/input-image.jpg'
)
image_array = tf.keras.preprocessing.image.img_to_array(image)
result = bodypix_model.predict_single(image_array)
mask = result.get_mask(threshold=0.75)
tf.keras.preprocessing.image.save_img(
'/path/to/output-mask.jpg',
mask
)
colored_mask = result.get_colored_mask(mask)
tf.keras.preprocessing.image.save_img(
'/path/to/output-colored-mask.jpg',
colored_mask
)
Acknowledgements
- Original TensorFlow JS Implementation of BodyPix
- Linux-Fake-Background-Webcam, an implementation of the blog post describing using the TensorFlow JS implementation with Python via a Socket API.
- tfjs-to-tf for providing an easy way to convert TensorFlow JS models
- virtual_webcam_background for a great pure Python implementation
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
tf-bodypix-0.2.1.tar.gz
(18.3 kB
view hashes)
Built Distribution
tf_bodypix-0.2.1-py3-none-any.whl
(20.1 kB
view hashes)
Close
Hashes for tf_bodypix-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11c2afb29573d53ce88b490091b80de30930ca3bc3c72672c86062cd4a70b850 |
|
MD5 | 0c86e1f5248859df1aa19cc3a98618ba |
|
BLAKE2b-256 | 232a01dd49d1b22081429d6e8d855598d407ddfd1d09d494918d4f878375e944 |