t0mer / deepstack-trainer

deepstack trainer is a Flask powerd, easy to use web app, hepls us to train and test Deepstack AI
GNU General Public License v3.0
24 stars 3 forks source link

Creating the DeepStack Trainer addon #13

Open DivanX10 opened 2 years ago

DivanX10 commented 2 years ago

Thank you for your wonderful DeepStack Trainer client. DeepStack Trainer is an excellent and convenient program. Home Assistant is the most popular smart home platform today and I would like Home Assistant to have a DeepStack Trainer addon.

I have never done addons and decided to make a DeepStack Trainer addon for Home Assistant. I managed to create it, but not everything works as well as I wanted. The DeepStack Trainer addon works until you restart the addon. The problem is that the images database.db and photos in the /opt/trainer/photos/uploads folder are not saved and after restarting the DeepStack Trainer addon, they are erased. If you install DeepStack Trainer via Portainer, then the images and the database are not erased and you can still connect folders. But the problem is that the addon for Home Assistant is more convenient to use, installed, prescribed paths and you can use it.

That's how I specify folder paths in the Portainer 2022-09-04_02-48-21

I'm not that good at python, but I understand and use literature a bit. So that's what I did

This paragraph is responsible for copying files, and here I have embedded these lines so that files are copied to the Home Assistant during training

shutil.copy2(src_file_images_db, dest_file_db) #copy the database from docker to homeassistant
shutil.copytree(src_file_photos, dest_file_photos, dirs_exist_ok=True) #copy the photos from docker to homeassistant

This is the code from the file trainer.py

# from aiofiles import open
db_path='./db/images.db'
deepstack_host_address = os.getenv("DEEPSTACK_HOST_ADDRESS")
deepstack_api_key = os.getenv("DEEPSTACK_API_KEY")
min_confidence = os.getenv("MIN_CONFIDANCE")

#copy the database from docker to homeassistant
src_file_images_db='/opt/trainer/db/images.db'
src_file_db='/opt/trainer/db'
dest_file_db = os.getenv("HOMEASSISTANT_FOLDER_PATH_FOR_DATABASE")

#copy the photos from docker to homeassistant
src_file_photos= '/opt/trainer/photos/uploads'
dest_file_photos = os.getenv("HOMEASSISTANT_FOLDER_PATH_FOR_PHOTOS")

#copy the database from homeassistant to docker 
src_file_db_bkp = os.getenv("HOMEASSISTANT_FOLDER_PATH_FOR_DATABASE")
dest_file_db_bkp='/opt/trainer/db'

#copy the photos from homeassistant to docker 
src_file_photos_bkp = os.getenv("HOMEASSISTANT_FOLDER_PATH_FOR_PHOTOS")
dest_file_photos_bkp='/opt/trainer/photos/uploads'

def SaveImage(file, path):
    logger.info("Saving the image to the file system")
    try:
        with open(path, "wb") as buffer:
            shutil.copyfileobj(file, buffer)
            shutil.copy2(src_file_images_db, dest_file_db) #copy the database from docker to homeassistant
            shutil.copytree(src_file_photos, dest_file_photos, dirs_exist_ok=True) #copy the photos from docker to homeassistant
        logger.info("File saved")
    except Exception as e:
        logger.error("Unable to save file " + str(e))
        raise Exception(str(e))

You need to copy files to these folders and then restore them from them after launching the DeepStack Trainer addon

  "HOMEASSISTANT_FOLDER_PATH_FOR_DATABASE": "/config/deepstack/db",
  "HOMEASSISTANT_FOLDER_PATH_FOR_PHOTOS": "/config/deepstack/photos"

Trying to follow the documentation on building an addon for Home Assistant I found that on Ubuntu 20.04 it is not possible to copy run.sh , and in Debian it is possible. Your techblog/fastapi:latest build is built on Ubuntu 20.04. I rebuilt the image for docker, where Debian 11 slim-stable was chosen as the OS. My build divanx10/debian 11-facet api on Debian 11 slim-stable. The script is needed in order to overwrite the parameters in ENV. In ardon, in the configuration section, you can specify the paths to save and this data is written to the options file.json, and the set parameters are read from this file and written to ENV. But since ENTRYPOINT ["/usr/bin/python 3", "trainer.py "] is specified to run DeepStack Trainer, then I can't run the script run.sh . DeepStack Trainer just won't start.

I have two versions of the repository for addons, where one version is combat and I don't debug there, and in the test version I am working on the DeepStack Trainer addon. I would like to ask you to help me build the DeepStack Trainer addon or, if there is a desire, build it yourself. Thanks.

How to build an addon https://developers.home-assistant.io/docs/add-ons/tutorial

Combat version https://github.com/DivanX10/Home-Assistant-Add-on-Deepstack Test version https://github.com/DivanX10/Home-Assistant-Add-on-DeepStack-Edge