Skip to content

kirbycope/godot-python-opencv

Repository files navigation

Godot Python OpenCV

godot-python-opencv

Use OpenCV and Python to display the webcam video in a Godot Scene.

Technologies Used

Godot is a cross-platform, free and open-source game engine released under the permissive MIT license.
OpenCV (Open Source Computer Vision Library) is a library of programming functions mainly for real-time computer vision.
PyInstaller bundles a Python application and all its dependencies into a single package.

Running the Godot Application

  1. Clone this repo.
  2. Import project.godot using Godot
  3. Edit the project
    1. Or, skip the next step and press "Run" instead
  4. Press "Run Project (F5)"

Testing the Webcam Server

When you make changes to the Python script, you'll need to tell the Godot application not to start the webcam server.

  1. To activate the virtual environment, run
    • [macOS] source venv/bin/activate
    • [Windows] venv\Scripts\activate
  2. Run the webcam software, python webcam_server.py
  3. Edit the GDScript so that it doesn't try to start the server by commenting out webcam_server_start()
  4. Start the scene (see "Testing the Webcam Server", above)
    • The scene should connect automatically; when _ready() calls webcam_server_connect()
  5. To exit the server, press [Ctrl]+[C]
  6. To exit the virtual environment, run deactivate

Packaging the Python Webcam Server for Godot

PyInstaller bundles a Python application and all its dependencies into a single package. The user can run the packaged app without installing a Python interpreter or any modules. PyInstaller is tested against Windows, MacOS X, and Linux. However, it is not a cross-compiler; to make a Windows app you run PyInstaller on Windows, and to make a Linux app you run it on Linux. etc. NOTE: Whenever you make changes to the Python code, you must regenerate this executable.

  1. Run pyinstaller webcam_server.spec
    • This will create a dist folder containing the executable.
    • The executeable is called in texture_rect.gd by webcam_server_start()

Project Creation (Historical)

  1. Open the root folder using VS Code
    • If you use GitHub Desktop, select the "Open in Visual Studio" button
  2. Open the integrated terminal
  3. To create a virtual environment, run python -m venv venv
    1. This creates a folder called "venv" that contains a local copy of Python and its package manager, "pip".
  4. To activate the virtual environment, run
    • [macOS] source .venv/bin/activate
    • [Windows] .venv\Scripts\activate
  5. To install required packages, run pip install opencv-python websockets numpy pyinstaller
  6. Verify package installation by running pip list
  7. To exit the venv, run deactivate

About

Godot Python OpenCV

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published