hello, now the program no longer gives errors; I did the machine learning with edge impulse: on the object detection section 50 cycles because otherwise it didn’t work, I used FOMO mobilnetV2 0.1, this thing gave me an f1 score of 99.7%; in the image section: there are numbers under raw features which are all 0x0, 0x0, 0x0, etc., all so the same I don’t know what this means, the color depth I put graysvcale since I only use black and white, even under the processed features entry is all 0.0000, 0.0000, etc.; on create impulse I put 96x96 images with fit longer axis, then I chose an object detection learning block but I don’t know if it’s as good as all the other parameters, I remind you that I want to recognize the black letters h, s and u on a white background with openmv h7 plus, on model testing it gave an accuracy greater than 97%, now my problem is that whatever I put in front of the camera detects the blob but identifies it as the background at 100%; I use a wide angle since my robot could be very close to the walls with letters, this is my code:
import sensor, image, time, ml, os
# Inizializza il sensore
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE) # Usa scala di grigi
sensor.set_framesize(sensor.QQVGA) # Risoluzione QQVGA (160x120)
sensor.skip_frames(time=2000) # Aspetta che il sensore si stabilizzi
clock = time.clock()
# Percorsi dei file
model_file = "/model/trained.tflite"
labels_file = "/model/labels.txt"
# Verifica l'esistenza dei file
def file_exists(filepath):
directory = "/".join(filepath.split("/")[:-1])
filename = filepath.split("/")[-1]
try:
return filename in os.listdir(directory)
except OSError as e:
print(f"Errore: {e}")
return False
if not file_exists(model_file):
raise OSError(f"File non trovato: {model_file}")
if not file_exists(labels_file):
raise OSError(f"File non trovato: {labels_file}")
# Carica il modello e le etichette
model = ml.Model(model_file, load_to_fb=True)
with open(labels_file, 'r') as f:
labels = f.read().splitlines()
if not labels:
raise ValueError("Il file delle etichette è vuoto.")
# Ciclo principale
while True:
clock.tick()
img = sensor.snapshot()
# Disegna rettangoli bianchi per "rimpicciolire" l'area visibile
img.draw_rectangle(0, 0, 160, 20, (255), fill=True) # Bordo superiore
img.draw_rectangle(0, 100, 160, 20, (255), fill=True) # Bordo inferiore (modificato da 120 a 100)
img.draw_rectangle(0, 20, 21, 120, (255), fill=True) # Bordo sinistro
img.draw_rectangle(139, 20, 21, 120, (255), fill=True) # Bordo destro
img.gamma_corr(contrast=1.5, brightness=-0.2)
# Trova blob
blobs = img.find_blobs([(0, 50, -50, 50, -50, 50)], pixels_threshold=20, area_threshold=20, merge=False)
if blobs:
largest_blob = max(blobs, key=lambda b: b.pixels())
x, y, w, h = largest_blob.rect()
# Disegna il blob
img.draw_rectangle(largest_blob.rect())
img.draw_cross(largest_blob.cx(), largest_blob.cy())
# Ritaglia ROI
roi = (x, y, w, h)
cropped_img = img.copy(roi=roi)
# Previsione
prediction_result = model.predict([cropped_img])
if prediction_result:
# Combina etichette e punteggi
scores = sorted(
zip(labels, prediction_result[0].flatten().tolist()),
key=lambda x: x[1],
reverse=True
)
label, confidence = scores[0]
print(f"Predizione: {label} (Confidenza: {confidence:.2f})")
else:
print("Errore: Il modello non ha prodotto alcun risultato.")
else:
print("Nessun blob trovato.")