Code Monkey home page Code Monkey logo

background_removal's Introduction

Background removal with Python

All this repository is for learning purposes. I cover here how simple is to remove background from selfie view just like Zoom, Google Meets, Skype, and MS Teams.

Installation on Windows:

  • Clone this repository. (don't forget to Star it)
  • Install virtual environment: python -m venv venv
  • Activate virtual environment: venv\Scripts\activate
  • Install all the requirements: pip install -r requirements.txt
  • (Optional if have Nvidia GPU): install onnxruntime with GPU support: pip install onnxruntime-gpu

How to run basic background removal:

At this point, when you are looking at this project, I might be already updated this project with more features, but if you want only to run a quick test on your own webcam replace the main.py code with the following:

# main.py
from utils import FPSmetric
from selfieSegmentation import MPSegmentations
from engine import Engine

if __name__ == '__main__':
    fpsMetric = FPSmetric()
    segmentationModule = MPSegmentations(threshold=0.3, bg_images_path='', bg_blur_ratio=(45, 45))
    selfieSegmentation = Engine(webcam_id=0, show=True, custom_objects=[segmentationModule, fpsMetric])
    selfieSegmentation.run()

You can run it by typing python main.py in a terminal.

Run basic MediaPipe face detection:

# main.py
from utils import FPSmetric
from faceDetection import MPFaceDetection
from engine import Engine

if __name__ == '__main__':
    fpsMetric = FPSmetric()
    mpFaceDetector = MPFaceDetection() 
    selfieSegmentation = Engine(webcam_id=0, show=True, custom_objects=[mpFaceDetector, fpsMetric])
    selfieSegmentation.run()

You can run it by typing python main.py in a terminal.

Test "Pencil" sketch with Python on saved image:

# main.py
from pencilSketch import PencilSketch
from engine import Engine

if __name__ == '__main__': 
    pencilSketch = PencilSketch(blur_simga=5)
    selfieSegmentation = Engine(image_path='data/porche.jpg', show=True, custom_objects=[pencilSketch])
    selfieSegmentation.run()

You can run it by typing python main.py in a terminal.

Test facial recognition example on webcam stream

# main.py
from utils import FPSmetric
from engine import Engine
from faceDetection import MPFaceDetection
from faceNet.faceNet import FaceNet

if __name__ == '__main__':
    facenet = FaceNet(
        detector = MPFaceDetection(),
        onnx_model_path = "models/faceNet.onnx", 
        anchors = "faces",
        force_cpu = True,
    )
    engine = Engine(webcam_id=0, show=True, custom_objects=[facenet, FPSmetric()])

    # save first face crop as anchor, otherwise don't use
    while not facenet.detect_save_faces(engine.process_webcam(return_frame=True), output_dir="faces"):
        continue

    engine.run()

You can run it by typing python main.py in a terminal.

Test AnimeGAN effect with python on webcam stream:

from engine import Engine
from animegan import AnimeGAN

if __name__ == '__main__':
    animegan = AnimeGAN("models/Hayao_64.onnx")
    engine = Engine(webcam_id=0, show=True, custom_objects=[animegan])
    engine.run()

You can run it by typing python main.py in a terminal.

Detailed Tutorials:

background_removal's People

Contributors

pythonlessons avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

background_removal's Issues

AttributeError: module 'tensorflow' has no attribute '__version__'

To save the .onnx faceNet model in my models folder, I tried running convert_to_onnx.py and I get this error:

Traceback (most recent call last): File "C:\Users\Cando\Desktop\FaceRec_Denem99\faceNet\convert_to_onnx.py", line 3, in <module> import tf2onnx File "C:\Python310\lib\site-packages\tf2onnx\__init__.py", line 10, in <module> from . import verbose_logging as logging File "C:\Python310\lib\site-packages\tf2onnx\verbose_logging.py", line 15, in <module> TF2 = tf.__version__.startswith("2.") AttributeError: module 'tensorflow' has no attribute '__version__

I tried reinstalling tensorflow, tf2onnx but still doesn't work

MediaPipe API changed?

Error raised when I run main.py script using mediapipe 0.8.3

Traceback (most recent call last): File ".\main.py", line 7, in <module> mpFaceDetector = MPFaceDetection() File "C:\Users\xgx\Downloads\Compressed\background_removal-main\faceDetection.py", line 31, in __init__ self.face_detection = self.mp_face_detection.FaceDetection(model_selection=model_selection, min_detection_confidence=confidence) TypeError: __init__() got an unexpected keyword argument 'model_selection'

Is it right change code from

self.face_detection = self.mp_face_detection.FaceDetection(model_selection=model_selection, min_detection_confidence=confidence)

to

self.face_detection = self.mp_face_detection.FaceDetection(min_detection_confidence=confidence)

?

Thanks.

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.