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).
@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)
// ...
}
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().
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:
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
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.
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.
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.
@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)
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?