Detect an image area at the right time (python)

Asked

Viewed 74 times

0

I am developing a system for level detection in Ambar bottles (Beer/Soda), and I have the following problem:

Whenever the bottle appears in the image the script already performs the reading but with errors due to bottle not being 100% in front of the webcam.

I need to detect the image area only when the entire bottle is in front of the camera. I’m using Opencv and imutils to do the image processing. The following is an example of poor detection:

Falha na leitura Falha na leitura

The ideal would be for the bottle to be in this position to perform the reading:

Leitura correta

Sample video:

Gif

And for each poorly filled bottle I am calling a Thread to perform a certain function, the problem is that the Thread is called numerous times due to each frame that detects poorly-filled, as I do to call only one Thread for each misfilled bottle detected ?

Follows parts of the script:

    #Procura os contornos
contorno = cv2.findContours(garrafaEstrutura.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contorno = imutils.grab_contours(contorno)
#Classificar contornos
areas = [cv2.contourArea(contorno) for contorno in contorno]
if areas:
    (contorno, areas) = zip(*sorted(zip(contorno, areas), key=lambda a:a[1]))
    garrafaEstrutura = recortarImagem(frame.copy(), posicaox, posicaoy, largura, altura)
    (x, y, w, h) = cv2.boundingRect(contorno[-1])
    hh, ww, cc = garrafaEstrutura.shape
    valorParametro = int((ww + (hh/proporcao)) + (hh/proporcao))
    valorContorno = int((ww + y) + y)
    global tempoDistanciaAntecipada
    global tempoDistanciaPassada
    tempoDistanciaAntecipada = ww - (w+x)
    tempoDistanciaPassada = x
    #print("Distância antecipada:", tempoDistanciaAntecipada)
    #print("Distância Passada", tempoDistanciaPassada)
    #proporcaoGarrafa = w / float(h)
    #USAR O VALOR DO PARAMETRO X PARA DETECTAR A DISTANCIA QUE A CAMERA TIROU A FOTO
    if not modoTeste.get():
        if valorParametro > valorContorno:
            cv2.rectangle(garrafaEstrutura, (x, y), (x + w, y + h), (0, 255, 0), 2)
            cv2.putText(garrafaEstrutura, "Cheia", (x + 10, y + 30), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)
        else:
            cv2.rectangle(garrafaEstrutura, (x, y), (x + w, y + h), (0, 0, 255), 2)
            cv2.putText(garrafaEstrutura, "Mal-cheia", (x + 10, y + 30), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)     
            if not malcheiaUm:
                malUm = threading.Thread(name='threadMalcheiaUm', target=threadMalcheiaUm)
                if not malUm.is_alive():
                    try:
                        malUm.start()
                    except RuntimeError:
                        malUm = threading.Thread(name='threadMalcheiaUm', target=threadMalcheiaUm)
                        malUm.start()
            
  • separate this image at the top and bottom, at the top you should do some conditions to check 3 states, half end start, where 0 or 1: 000 would be without bottle(or a reset); 010 would be level reading point

  • Some small sketch ?

  • I can’t make code now, at the top it’s to use the neck to know when you have or not bottle, it would be like 3 presence sensors but using the white color and the tone of the bottle in the image, so in the initial area being white, half being dark and end being white you would do level reading

  • A good image quality prevents unnecessary treatments on the image. I suggest putting a "Backlight" and studying some of the basic knowledge of photography and industrial computer vision to perform a level measurement. Another point is the use of videos, where you would need unnecessary processing, use a Trigger sensor to get the photos at the right time and without the blur of the Blur motion.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.