How to stop a for inside a while (Python)

Asked

Viewed 55 times

0

I’m doing a facial recognition so I’m using a while to process the video and inside the while has 3 for and inside one of them where I left a comment "#STOP HERE" and where I need a stop because this is processes the information coming from the database and displays but keeps repeating until the while stop but need to display 1x but while can not stop Code:

import cv2
from database import *
cursor.execute("SELECT id FROM users")
resultado = cursor.fetchall()
ids = list(resultado)
ids_user = []
for x in resultado:
    ids_user.append(str(x).strip('()[].,'))

camera = cv2.VideoCapture('01.mp4')
detectorFace = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
reconhecedor = cv2.face.EigenFaceRecognizer_create()
reconhecedor.read("classificadorEigen.yml")
largura,altura = 220,220
font = cv2.FONT_HERSHEY_COMPLEX_SMALL

while (True):
    conectado, imagem = camera.read()
    imagemCinza = cv2.cvtColor(imagem,cv2.COLOR_BGR2GRAY)
    facesDetectadas = detectorFace.detectMultiScale(imagemCinza, minNeighbors=20, minSize=(30, 30), maxSize=(400, 400))
    for (x,y,l,a) in facesDetectadas:
        imagemFace = cv2.resize(imagemCinza[y:y+a,x:x+l],(largura,altura))
        cv2.rectangle(imagem,(x,y),(x+l,y+a),(0,0,255),2)
        id,confianca = reconhecedor.predict(imagemFace)
        id_img = [1]
        for id_db in ids_user:
            for id_img in str(id):
                if(id_db == id_img):
                    sql = "SELECT * FROM users WHERE id = " + id_db
                    cursor.execute(sql)
                    result = cursor.fetchall()
                    for row in result:
                        print("ID: " + str(row[0]))
                        print("Nome: " + str(row[1]))
                        print("Cargo: " + str(row[2]))
                        #PARAR AQUI
                else:
                    resultado_2 = "Searching"
                cv2.putText(imagem, str(row[1]), (x, y + (a + 30)), font, 2, (0, 0, 255))


    cv2.imshow("Face",imagem)

    if cv2.waitKey(1) == ord('q'):
        break

camera.release()
cv2.destroyAllWindows()
  • If you only want the first query result, use LIMIT 1 in the same... I mean, sql = f"SELECT * FROM users WHERE id = {id_db} limit 1. In this case, you could use the cursor.fetchone(). Anyway, your code is in trouble, because you initialize id_img = [1] and in a go below uses for id_img in str(id):

No answers

Browser other questions tagged

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