TenserFlow Issues

I have made a TensorFlow lite model recently (Which does work). I am having lots of trouble with errors in my code. My goal is to get an X Y point from the model, split the screen into thirds, find which third of the screen the point is in, and then return a number 1-3. Here is my code, do you have any advice (BTW I am using Andriod Studio).

package org.firstinspires.ftc.teamcode;

import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;

import org.firstinspires.ftc.robotcore.external.ClassFactory;
import org.firstinspires.ftc.robotcore.external.tfod.FrameGenerator;
import org.firstinspires.ftc.robotcore.external.tfod.Recognition;
import org.firstinspires.ftc.robotcore.external.tfod.TFObjectDetector;
import org.firstinspires.ftc.robotcore.external.tfod.TfodParameters;

import java.util.List;

@TeleOp()
public class Tenser extends LinearOpMode {

private TFObjectDetector tfod;

@Override
public void runOpMode() {
    // Initialize hardware and camera
    initTfod();

    // Wait for the game to start
    waitForStart();

    // Main loop
    while (opModeIsActive()) {
        // Get the updated recognition list
        List<Recognition> updatedRecognitions = tfod.getUpdatedRecognitions();

        if (updatedRecognitions != null) {
            // Process each recognition
            for (Recognition recognition : updatedRecognitions) {
                // Check if the object is on the left, center, or right third of the screen
                int screenThird = getScreenThird(recognition.getLeft());

                // Perform actions based on the detected position (e.g., drive towards the object)
                handleObjectPosition(screenThird);
            }
        }

        telemetry.update();
    }

    // Stop the TensorFlow Object Detection
    tfod.shutdown();
}

private void initTfod() {
    // Initialize TensorFlow Object Detection
    TfodParameters tfodParameters = new TfodParameters();

    tfod = ClassFactory.getInstance().createTFObjectDetector(tfodParameters, (FrameGenerator) hardwareMap);
    tfod.loadModelFromAsset("model_20231130_073441.tflite", "label.txt");
    tfod.activate();
}

private int getScreenThird(float objectLeft) {
    // Define your screen thirds based on the camera resolution
    // For example, if the camera resolution is 640x480, each third could be 213 pixels wide
    int screenThirdWidth = 213;

    if (objectLeft < screenThirdWidth) {
        return 1; // Left third
    } else if (objectLeft < 2 * screenThirdWidth) {
        return 2; // Center third
    } else {
        return 3; // Right third
    }
}

private void handleObjectPosition(int screenThird) {
    // Implement your logic based on the detected position (e.g., drive towards the object)
    // ...
}

}

My errors are the following:

tfod.loadModelFromAsset(“model_20231130_073441.tflite”, “label.txt”);

TfodParameters tfodParameters = new TfodParameters();

The first one has an error at “loadModelFromAsset”

The second one has an error at TfodParameters()

You say you have errors in your software, but you don’t express the actual errors here in this post, so your readers can only guess at what those errors are. You should edit this post to include the errors you are seeing. That said, you are calling initVuforia(). Vuforia no longer exists in the SDK, so you should remove that.

Take a look at the sample named ConceptTensorFlowObjectDetection. I’d base my initial OpMode off of that and then add functionality as desired. Note that it also uses the new VisionPortal.

A side observation is that the left edge of the bounding box may not actually be in the segment of the screen you think the object should be in. I would instead calculate the center of the object by adding the value of (recognition.getWidth() / 2) to recognition.getLeft().

1 Like

I agree with @cmacfarlane . If you are calling InitVuforia, it’s very likely you have not migrated your source base to the new 9.0.x source, and it will not be legal for use this season. See this article for more information about updating your Robot Controller source:

-Danny

First of all, I am adapting old code and I meant to remove initVuforia(), I am currently using Version 9 of the SDK. The errors I am having are 2 lines of code

tfod.loadModelFromAsset(“model_20231130_073441.tflite”, “label.txt”);

TfodParameters tfodParameters = new TfodParameters();

The first one has an error at “loadModelFromAsset”

The second one has an error at TfodParameters()

I would highly recommend you migrate your old software to the new sample. There was enough refactoring with the introduction of the VisionPortal, that the old interfaces don’t work any longer. Trying to migrate tfod software developed prior to the VisionPortal introduction in v8.2 (the offseason release) is going to be painful.

See in particular line 119 here: FtcRobotController/FtcRobotController/src/main/java/org/firstinspires/ftc/robotcontroller/external/samples/ConceptTensorFlowObjectDetection.java at master · FIRST-Tech-Challenge/FtcRobotController · GitHub along with the commented out lines that show example parameter usage.

How would that code work, the current code was made by a past programmer that left so I don’t know how to make this type of code

You will want to review how to use the Vision Portal. Here’s the best place to start (paying special attention to the TensorFlow Builder):

You can also take a look at the example sample that Craig pointed you to, which shows a simple usage of the Vision Portal with TensorFlow.

There is no “direct” way to convert existing code from the old Vuforia initialization to the Vision Portal, but if you look at the initTfod() function in the sample program that’s a pretty good all-in-one solution. You would essentially replace initVuforia() and InitTfod() in your existing code with the contents of initTfod() in the sample, and it requires very little if any changes to the rest of your code.

-Danny

How do I add recognition.getWidth() to recognition.getLeft()

int horizontalCenterOfObject  = recognition.getLeft() + (recognition.getWidth() / 2);

The parenthesis aren’t strictly necessary around the operands of the division operator, because of precedence, but I like to include them as they demonstrate intent of the author regardless.

I have another big issue, I have come up with this code but it is still having lots of issues:

package org.firstinspires.ftc.teamcode;

import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;

import org.firstinspires.ftc.robotcore.external.tfod.Recognition;
import org.firstinspires.ftc.robotcore.external.tfod.TFObjectDetector;
import org.firstinspires.ftc.vision.tfod.TfodProcessor;

import java.util.List;

@TeleOp()
public class Tenser extends LinearOpMode {

private static final String TFOD_MODEL_FILE = "/sdcard/FIRST/tflitemodels/model_20231130_073441.tflite";


private TfodProcessor tfodP;

private TFObjectDetector tfod;

@Override
public void runOpMode() {
    // Initialize hardware and camera
    initTfod();

    // Wait for the game to start
    waitForStart();

    // Main loop
    while (opModeIsActive()) {
        // Get the updated recognition list
        List<Recognition> updatedRecognitions = tfod.getUpdatedRecognitions();

        if (updatedRecognitions != null) {
            // Process each recognition
            for (Recognition recognition : updatedRecognitions) {
                // Check if the object is on the left, center, or right third of the screen
                int screenThird = getScreenThird(recognition.getLeft());


                // Perform actions based on the detected position (e.g., drive towards the object)
                handleObjectPosition(screenThird);
            }
        }

        telemetry.update();
    }

    // Stop the TensorFlow Object Detection
    tfod.shutdown();
}

private void initTfod() {
    // Initialize TensorFlow Object Detection

        // Create the TensorFlow processor by using a builder.
    tfodP = new TfodProcessor.Builder()

            .setModelFileName(TFOD_MODEL_FILE)

            .build();
}

private int getScreenThird(float objectLeft) {
    // Define your screen thirds based on the camera resolution
    // For example, if the camera resolution is 640x480, each third could be 213 pixels wide
    int screenThirdWidth = 213;

    if (objectLeft < screenThirdWidth) {
        return 1; // Left third
    } else if (objectLeft < 2 * screenThirdWidth) {
        return 2; // Center third
    } else {
        return 3; // Right third
    }
}

private void handleObjectPosition(int screenThird) {
    // Implement your logic based on the detected position (e.g., drive towards the object)
    // ...
}

}

I am getting hundreds of errors about duplicate classes related to JetBrains

Ex:

Duplicate class org.jetbrains.annotations.TestOnly found in modules jetified-annotations-12.0 (com.intellij:annotations:12.0) and jetified-annotations-13.0 (org.jetbrains:annotations:13.0)

@ddiaz

Can you provide a screenshot of the complete IDE you are using to attempt to compile, along with a close up screenshot of the actual errors you are encountering?