GithubHelp home page GithubHelp logo

satoshirobatofujimoto / python-tf-bodypix Goto Github PK

View Code? Open in Web Editor NEW

This project forked from de-code/python-tf-bodypix

0.0 1.0 0.0 160 KB

A Python implementation of the bodypix model.

License: MIT License

Makefile 5.83% Python 92.92% Dockerfile 0.98% Shell 0.28%

python-tf-bodypix's Introduction

TensorFlow BodyPix (TF BodyPix)

PyPi version License: MIT

A Python implementation of body-pix.

Goals of this project is:

  • Python library, making it easy to integrate the BodyPix model
  • CLI with limited functionality, mostly for demonstration purpose

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, using tfjs-graph-converter
image Image loading via Pillow, required by the CLI.
video Video support via OpenCV
webcam Webcam support via OpenCV and pyfakewebcam
all All of the libraries

Python 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_part_mask(mask)
tf.keras.preprocessing.image.save_img(
    '/path/to/output-colored-mask.jpg',
    colored_mask
)

CLI

CLI Help

python -m tf_bodypix --help

or

python -m tf_bodypix <sub command> --help

List Available Models

python -m tf_bodypix list-models

The result will be a list of all of the bodypix TensorFlow JS models available in the tfjs-models bucket.

Those URLs can be passed as the --model-path arguments below, or to the download_model method of the Python API.

The CLI will download and cache the model from the provided path. If no --model-path is provided, it will use a default model (mobilenet).

Inputs and Outputs

Most commands will work with inputs (source) and outputs.

The source path can be specified via the --source parameter.

The following inputs are supported:

type description
image Static image (e.g. .png)
video Video (e.g. .mp4)
webcam Linux Webcam (/dev/videoN or webcam:0)

If the source path points to an external file (e.g. https://), then it will be downloaded and locally cached.

The output path can be specified via --output, unless --show-output is used.

The following outpus are supported:

type description
image_writer Write to a static image (e.g. .png)
v4l2 Linux Virtual Webcam (/dev/videoN)
window Display a window (by using --show-output)

Example commands

Creating a simple body mask

python -m tf_bodypix \
    draw-mask \
    --source \
    "https://www.dropbox.com/s/7tsaqgdp149d8aj/serious-black-businesswoman-sitting-at-desk-in-office-5669603.jpg?dl=1" \
    --show-output \
    --threshold=0.75

Image Source: Serious black businesswoman sitting at desk in office

Add the mask over the original image using --mask-alpha

python -m tf_bodypix \
    draw-mask \
    --source \
    "https://www.dropbox.com/s/7tsaqgdp149d8aj/serious-black-businesswoman-sitting-at-desk-in-office-5669603.jpg?dl=1" \
    --show-output \
    --threshold=0.75 \
    --mask-alpha=0.5

Image Source: Serious black businesswoman sitting at desk in office

Colorize the body mask depending on the body part

python -m tf_bodypix \
    draw-mask \
    --source \
    "https://www.dropbox.com/s/7tsaqgdp149d8aj/serious-black-businesswoman-sitting-at-desk-in-office-5669603.jpg?dl=1" \
    --show-output \
    --threshold=0.75 \
    --mask-alpha=0.5 \
    --colored

Image Source: Serious black businesswoman sitting at desk in office

Additionally select the body parts

python -m tf_bodypix \
    draw-mask \
    --source \
    "https://www.dropbox.com/s/7tsaqgdp149d8aj/serious-black-businesswoman-sitting-at-desk-in-office-5669603.jpg?dl=1" \
    --show-output \
    --threshold=0.75 \
    --mask-alpha=0.5 \
    --parts left_face right_face \
    --colored

Image Source: Serious black businesswoman sitting at desk in office

Add mask overlay to a video

python -m tf_bodypix \
    draw-mask \
    --source \
    "https://www.dropbox.com/s/s7jga3f0dreavlb/video-of-a-man-laughing-and-happy-1608393-360p.mp4?dl=1" \
    --show-output \
    --threshold=0.75 \
    --mask-alpha=0.5 \
    --colored

Video Source: Video Of A Man Laughing And Happy

Blur background of a video

python -m tf_bodypix \
    blur-background \
    --source \
    "https://www.dropbox.com/s/s7jga3f0dreavlb/video-of-a-man-laughing-and-happy-1608393-360p.mp4?dl=1" \
    --show-output \
    --threshold=0.75 \
    --mask-blur=5 \
    --background-blur=20

Video Source: Video Of A Man Laughing And Happy

Replace the background of a video

python -m tf_bodypix \
    replace-background \
    --source \
    "https://www.dropbox.com/s/s7jga3f0dreavlb/video-of-a-man-laughing-and-happy-1608393-360p.mp4?dl=1" \
    --background \
    "https://www.dropbox.com/s/b22ss59j6pp83zy/brown-landscape-under-grey-sky-3244513.jpg?dl=1" \
    --show-output \
    --threshold=0.75 \
    --mask-blur=5

Video Source: Video Of A Man Laughing And Happy

Background: Brown Landscape Under Grey Sky

Capture Webcam and adding mask overlay

python -m tf_bodypix \
    draw-mask \
    --source webcam:0 \
    --show-output \
    --threshold=0.75 \
    --mask-alpha=0.5 \
    --colored

Capture Webcam and adding mask overlay, writing to v4l2loopback device

(replace /dev/videoN with the actual virtual video device)

python -m tf_bodypix \
    draw-mask \
    --source webcam:0 \
    --output /dev/videoN \
    --threshold=0.75 \
    --mask-alpha=0.5 \
    --colored

Capture Webcam and blur background, writing to v4l2loopback device

(replace /dev/videoN with the actual virtual video device)

python -m tf_bodypix \
    blur-background \
    --source webcam:0 \
    --background-blur 20 \
    --output /dev/videoN \
    --threshold=0.75

Capture Webcam and replace background, writing to v4l2loopback device

(replace /dev/videoN with the actual virtual video device)

python -m tf_bodypix \
    replace-background \
    --source webcam:0 \
    --background \
    "https://www.dropbox.com/s/b22ss59j6pp83zy/brown-landscape-under-grey-sky-3244513.jpg?dl=1" \
    --threshold=0.75 \
    --output /dev/videoN

Background: Brown Landscape Under Grey Sky

TensorFlow Lite support (experimental)

The model path may also point to a TensorFlow Lite model (.tflite extension). Whether that actually improves performance may depend on the platform and available hardware.

You could convert one of the available TensorFlow JS models to TensorFlow Lite using the following command:

python -m tf_bodypix \
    convert-to-tflite \
    --model-path \
    "https://storage.googleapis.com/tfjs-models/savedmodel/bodypix/mobilenet/float/075/model-stride16.json" \
    --optimize \
    --quantization-type=float16 \
    --output-model-file "./mobilenet-float16-stride16.tflite"

The above command is provided for convenience. You may use alternative methods depending on your preference and requirements.

Relevant links:

Docker Usage

You could also use the Docker image if you prefer. The entrypoint will by default delegate to the CLI, except for python or bash commands.

# pull latest image (you may also use tags)
docker pull de4code/tf-bodypix
# mount real and virtual webcam devices on linux
docker run --rm \
    --device /dev/video0 \
    --device /dev/video2 \
    de4code/tf-bodypix \
    blur-background \
    --source /dev/video0 \
    --output /dev/video2 \
    --background-blur 20 \
    --threshold=0.75
# mount x11 display on linux
docker run --rm \
    --net=host \
    --volume /tmp/.X11-unix:/tmp/.X11-unix \
    --volume ${HOME}/.Xauthority:/root/.Xauthority \
    --env DISPLAY \
    de4code/tf-bodypix \
    replace-background \
    --source \
    "https://www.dropbox.com/s/s7jga3f0dreavlb/video-of-a-man-laughing-and-happy-1608393-360p.mp4?dl=1" \
    --background \
    "https://www.dropbox.com/s/b22ss59j6pp83zy/brown-landscape-under-grey-sky-3244513.jpg?dl=1" \
    --show-output \
    --threshold=0.75 \
    --mask-blur=5

Example Media

Here are a few example media files you could try.

Images:

Videos:

Background:

Experimental Downstream Projects

  • Layered Vision is an experimental project using the tf-bodypix Python API.

Acknowledgements

python-tf-bodypix's People

Contributors

de-code avatar dependabot-preview[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.