Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Green on green #142

Closed
bassyboi opened this issue Aug 24, 2024 · 5 comments
Closed

Green on green #142

bassyboi opened this issue Aug 24, 2024 · 5 comments

Comments

@bassyboi
Copy link

Trying to figure out how to install coral on raspberrypi 4 using python 3.12!!! I tried back dating the python but run into trying to troubleshoot why the code not running with everything required and paths set up correctly!!! Any suggestions?? I have a data set here that seems to work well in pasture on laptop on my own script over a video!! Any suggestions

@geezacoleman
Copy link
Owner

Thanks for giving this a go! As you've probably found out unfortunately Google has not updated the PyCoral library for the Coral in some time. There are a few options you can try that have been developed by the community. What are your specific errors? Can you screenshot/copy paste them here? Which dataset are you using or is it one you've developed yourself?

There's a great video explaining the set up process by the YouTuber Jung. This thread on the issue (google-coral/pycoral#137) also provides some options to run it with post 3.9 versions of Python. The issue with backdating python is you'll run into compatibility issues with picamera2 I think and other packages.

I explain a bit more about the situation/plans in #141. When I set up GoG support for the OWL with the Coral and PyCoral I assumed there would be ongoing support for the hardware/software. Unfortunately it seems that isn't the case and the community is moving toward other options like the Hailo AI Kit. The Raspberry Pi 5 can run small models at moderate FPS, but having the additional hardware like the Hailo chip using the PCIe connection greatly improves speed. I'd recommend upgrading to a Pi 5 if you want to run GoG models in the future - it will be much better supported/faster.

@bassyboi
Copy link
Author

I have tried back dating the raspberry pi!!! I kinda wrote a script that sets up setting up python 3.9 in its own environment to run the coral (env-coral) in and use the rest of owl to run in the owl in (owl) I got it running once thought I got it right had camera up detecting weeds in my lawn thought I solved it then reset raspberry pi and kept saying I have to install open cv and a few and can’t get it to run again

@bassyboi
Copy link
Author

bassyboi commented Aug 26, 2024

Also yes I made a custom data set!! I working on a couple of data sets I’m finding that rather then naming each individual weed I just make a class that is one class which is (weed1) and make a dataset for the crop condition!!! Let’s say it is a pasture and wanting to remove broad leaf weeds!! I’d have the model for that!!! My theory is in the future is that maybe make data sets that are chemical related!!!

also going to add the way i sort of got GOG to work for the short time it did

this is the code to set up env for coral does need refining i have to install with sudo and i think my problems are permission related

#!/bin/bash

Function to check the exit status of the last executed command

check_status() {
if [ $? -ne 0 ]; then
echo "[ERROR] $1 failed."
exit 1
else
echo "[INFO] $1 completed successfully."
fi
}

Trap to catch errors and print a message before exiting

trap 'echo "[ERROR] An error occurred during the installation. Please check the logs above for more details."; exit 1' ERR

Create environment directory in the current script location

ENV_DIR=$(pwd)/env
echo "[INFO] Creating environment directory at $ENV_DIR"
mkdir -p $ENV_DIR || exit 1
check_status "Creating environment directory"

Set up Coral repository and install Edge TPU runtime using sudo

echo "[INFO] Setting up Coral repository and installing Edge TPU runtime..."
sudo sh -c 'echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" > /etc/apt/sources.list.d/coral-edgetpu.list'
sudo curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-get update || exit 1

Ask the user whether to install the standard or max operating frequency version of the Edge TPU runtime

echo "Do you want to install with MAX OPERATING FREQUENCY? This will increase framerate but also device temperature and power consumption."
echo "Check the official Google Coral documentation for full differences: https://coral.ai/docs/accelerator/get-started/"
read -r -p "Install MAX OPERATING FREQUENCY? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
echo "[INFO] Installing MAX OPERATING FREQUENCY runtime..."
sudo apt-get install -y libedgetpu1-max || exit 1
else
echo "[INFO] Installing STANDARD OPERATING FREQUENCY runtime..."
sudo apt-get install -y libedgetpu1-std || exit 1
fi
check_status "Installing Edge TPU runtime"

Install Python environment tools

echo "[INFO] Installing Python environment tools (curl, virtualenv)..."
sudo apt-get install -y curl virtualenv || exit 1
check_status "Installing Python environment tools"

Install pyenv for managing Python versions

echo "[INFO] Installing pyenv for Python version management..."
curl https://pyenv.run | bash || exit 1
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
check_status "Installing pyenv"

Install Python 3.9 using pyenv

echo "[INFO] Installing Python 3.9 via pyenv. This may take a while..."
pyenv install 3.9.0 || exit 1
pyenv global 3.9.0
check_status "Python 3.9 installation"

Create and activate a virtual environment using the specific Python version

echo "[INFO] Creating and activating the virtual environment..."
virtualenv -p $(pyenv which python) $ENV_DIR/coral_env || exit 1
source $ENV_DIR/coral_env/bin/activate || exit 1
check_status "Creating and activating virtual environment"

Install pycoral

echo "[INFO] Installing pycoral..."
pip install pycoral || {
echo "[WARNING] Failed to install pycoral from default repository, trying with Google's repository..."
pip install --extra-index-url https://google-coral.github.io/py-repo/ pycoral~=2.0 || exit 1
}
check_status "Installing pycoral"

echo "[INFO] Coral environment setup is complete."

this file is changed in the /utils green on green

#!/usr/bin/env python
from pycoral.adapters.common import input_size
from pycoral.adapters.detect import get_objects
from pycoral.utils.dataset import read_label_file
from pycoral.utils.edgetpu import make_interpreter
from pycoral.utils.edgetpu import run_inference
from pathlib import Path
import os
import cv2

class GreenOnGreen:
def init(self, model_path='models', label_file='models/labels.txt', env_path=None):
# Activate the virtual environment if specified
if env_path:
activate_script = Path(env_path) / 'bin' / 'activate_this.py'
if activate_script.is_file() and os.access(activate_script, os.X_OK):
with open(activate_script) as file:
exec(file.read(), {'file': str(activate_script)})
print("Environment activated successfully.")
else:
print("Activation script not found or not executable. Check the path and permissions.")

    if model_path is None:
        print('[WARNING] No model directory or path provided with --model-path flag. '
              'Attempting to load from default...')
        model_path = 'models'
    self.model_path = Path(model_path)

    if self.model_path.is_dir():
        model_files = list(self.model_path.glob('*.tflite'))
        if not model_files:
            raise FileNotFoundError('No .tflite model files found. Please provide a directory or .tflite file.')

        else:
            self.model_path = model_files[0]
            print(f'[INFO] Using {self.model_path.stem} model...')

    elif self.model_path.suffix == '.tflite':
        print(f'[INFO] Using {self.model_path.stem} model...')

    else:
        print(f'[WARNING] Specified model path {model_path} is unsupported, attempting to use default...')
        model_files = Path('models').glob('*.tflite')
        try:
            self.model_path = next(model_files)
            print(f'[INFO] Using {self.model_path.stem} model...')

        except StopIteration:
            print('[ERROR] No model files found.')

    self.labels = read_label_file(label_file)
    self.interpreter = make_interpreter(self.model_path.as_posix())
    self.interpreter.allocate_tensors()
    self.inference_size = input_size(self.interpreter)
    self.objects = None

def inference(self, image, confidence=0.5, filter_id=0):
    cv2_im_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    cv2_im_rgb = cv2.resize(cv2_im_rgb, self.inference_size)
    run_inference(self.interpreter, cv2_im_rgb.tobytes())
    self.objects = get_objects(self.interpreter, confidence)
    self.filter_id = filter_id

    height, width, channels = image.shape
    scale_x, scale_y = width / self.inference_size[0], height / self.inference_size[1]
    self.weed_centers = []
    self.boxes = []

    for det_object in self.objects:
        if det_object.id == self.filter_id:
            bbox = det_object.bbox.scale(scale_x, scale_y)

            startX, startY = int(bbox.xmin), int(bbox.ymin)
            endX, endY = int(bbox.xmax), int(bbox.ymax)
            boxW = endX - startX
            boxH = endY - startY

            # save the bounding box
            self.boxes.append([startX, startY, boxW, boxH])
            # compute box center
            centerX = int(startX + (boxW / 2))
            centerY = int(startY + (boxH / 2))
            self.weed_centers.append([centerX, centerY])

            percent = int(100 * det_object.score)
            label = f'{percent}% {self.labels.get(det_object.id, det_object.id)}'
            cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)
            cv2.putText(image, label, (startX, startY + 30),
                        cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 0), 2)
        else:
            pass
    return None, self.boxes, self.weed_centers, image

Example Usage:

Assuming the environment path is correct and points to your Coral setup

gog = GreenOnGreen(env_path='/home/bass/env/coral_env')

this code operates the normal python 3.11 and runs the env at 3.9 i know its a work around but there is a bit in the setup that works around the coral it wont work if you install the env manually.. i just having trouble with permissions now and trying to work around that

@geezacoleman
Copy link
Owner

This is really neat! Thank you for sharing the process and my apologies for not responding sooner or for being much help! It looks like you've got it sorted far better than I could have in any case. Your dataset sounds great - I have found that grouping very different weeds together may negatively impact model training, because of the diversity in appearance that the weeds present. Depends on the context and crop/background however. I have also found the opposite, where too many classes of very similar classes can result in poorer training, so I think there is a middle ground. Do you have some examples?

GoG will be a focus for me in the next 6 months starting from late September, so expect there to be a lot more support for it and updates. The idea is to update the openweedlocator-tools package and use that as the base for the main OWL repository. That way functions can be run independently and interact better with a GUI too.

Thanks again for sharing!

@bassyboi
Copy link
Author

yeah i trained and retrained the model with the likes clover mash mellow weed and that for a trial... just with weeds and that around the house in my area but had like 1500 photos and i hated sitting there trying to put a box over every weed haha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants