Caméra Nicla Vision

import sensor, image, time

# Fonction pour calculer la distance à partir de la taille de l'objet détecté
def calculate_distance(size):
    # La taille de l'objet dépend de sa distance à la caméra et de la focale de la caméra
    focal_length = 650  # Focale de la caméra en pixels (à adapter en fonction de votre caméra)
    known_diameter = 2.0  # Diamètre de la boule en mètres (à adapter en fonction de la taille réelle de votre boule)
    known_distance = 50  # Distance de référence à laquelle le diamètre est connu en centimètres

    distance = (known_diameter * focal_length) / size
    return distance

# Initialisation de la caméra
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

while True:
    # Capture d'une image
    img = sensor.snapshot()

    # Convertir l'image en niveaux de gris
    img_gray = img.to_grayscale()

    # Appliquer un seuillage pour détecter les bords
    img_thresh = img_gray.binary([(200, 255)])  # Vous pouvez ajuster ces valeurs selon votre environnement d'éclairage

    # Trouver les régions d'intérêt (blobs) dans l'image
    blobs = img_thresh.find_blobs()

    for blob in blobs:
        # Si le blob a une taille suffisante, considérer qu'il s'agit d'une boule
        if blob.pixels() >= 8:
            # Trouver le centre du blob
            cx = blob.cx()
            cy = blob.cy()

            # Calculer la taille du blob en pixels
            size = blob.w() * blob.h()  # On calcule la taille du blob en multipliant sa largeur par sa hauteur

            # Calculer la distance à partir de la taille de l'objet
            distance = calculate_distance(size)

            # Dessiner le contour de l'objet et afficher la distance
            img.draw_rectangle(blob.rect(), color=(255, 0, 0))  # Dessiner un rectangle autour du blob

            # Afficher la distance
            text_x = cx - 20  # Décalage en x pour positionner le texte
            text_y = cy + 10  # Décalage en y pour positionner le texte
            img.draw_string(text_x, text_y, "%.2f cm" % distance, color=(255, 255, 255))

    # Afficher l'image
    img.show()

dans ce programme j’ai un problème ligne 30 avec la fonction blob j’aimerais savoir si quelqu’un a une idée le but étant de calculer la distance entre la boule et l

Hi, you have to do a lookup table. Just manually measure the distance given a size and then generate a lookup table for it. You can then do interpolation the size value to the closest measurement in the lookup table to get the distance.

good evening I would like to know why line 30 there is a problem. I didn’t quite understand your answer.
Why doesn’t the blob function work?
Is it possible to give me the program with the diameter of the ball and the focal length of the nicla vision pro.
The goal of our project is to be able to measure the distance between the camera and the ball using the focal length and the diameter of the ball
in our program we have the camera which takes screenshots of the image then draws the outline of the ball and calculates its diameter then the distance with the focal length but our program does not work

You are passing the size of the bounding box into a function that expects a diameter?

The issue you are having here is pure math setup. This is not something you need help or support for. You just need to actually determine how to solve your problem. This is generic python code at this point.

Our code base gives you a bounding box around the ball. This is accurate for what the camera sees. Determining the diameter is then just a math problem given that.