how I save variable values in a mysql database

Asked

Viewed 81 times

0

I’m trying to create a facial recognition system using Opencv to record users' access to the school’s lab but the problem is that I’m not being able to save the access time information of each user nor the image of it at the time it entered. I’m using this code

import MySQLdb
import cv2
from datetime import datetime
import numpy as np

classificador = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
reconhecedor = cv2.face.LBPHFaceRecognizer_create()
reconhecedor.read("classificadorLBPH.yml")
largura, altura = 220, 220
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
camera = cv2.VideoCapture(0)
banco_de_dados = MySQLdb.connect(user='root', host='127.0.0.1', 
database='acesso_usuarios')
cursor = banco_de_dados.cursor()
hora = datetime.now()
while (True):
    conectado, imagem = camera.read()
    imagemCinza = cv2.cvtColor(imagem, cv2.COLOR_BGR2GRAY)
    facesDetectadas = classificador.detectMultiScale(imagemCinza,
                                                 scaleFactor=1.5,
                                                 minSize=(150, 150))


    for (x, y, l, a) in facesDetectadas:
        imagemFace = cv2.resize(imagemCinza[y:y + a, x:x + l], (largura, altura))
        id, confianca = reconhecedor.predict(imagemFace)
        regiao = imagem[y:y + a, x:x + l]
        cv2.putText(imagem,
                str(hora.day) + '/' + str(hora.month) + '/' + str(hora.year) + '-' + str(hora.hour) + ':' + str(
                    hora.minute) + ':' + str(hora.second), (0, 20), font, 1.5, (0, 255, 0))

        if np.average(imagemCinza) > 110:

            chegada = str(hora.hour) + '-' + str(hora.minute) +'-'+ str(hora.second) +' '+ str(hora.day) +'/'+ str(hora.month) +'/'+ str(hora.year)
            nome = ''
            if id == -1:
                nome = 'desconhecido'
            elif id == 1:
                nome = 'William'
            elif id == 2:
                nome = 'Isaac'
            elif id == 3:
                nome = 'ivamberg'
            elif id == 4:
                nome = 'afonso'

            cursor.execute('SELECT * FROM acesso_usuarios.'+nome)
            arg = 'INSERT INTO' +nome+ '(hora_chegada) VALUES %s'
            val = chegada
            cursor.execute(arg, val)
            if np.average(imagemCinza) < 110 or (id != 1):
                hora_de_saida = datetime.now()

                saida = str(hora_de_saida.hour) + str(hora_de_saida.minute) + str(hora_de_saida.second) + str(
            hora_de_saida.day) + str(hora_de_saida.month) + str(hora_de_saida.year)
                arg = 'INSERT INTO' +nome+ '(hora_saida) VALUES %s'
                val = saida
                cursor.execute(arg, val)

            cv2.putText(imagem, nome, (x, y + (a + 30)), font, 2, (0, 0, 255)) 
            cv2.putText(imagem, str(confianca), (x, y + (a + 50)), font, 1, (0, 0, 255))  

        cv2.imshow("Face", imagem)
        if cv2.waitKey(1) == ord('q'):
            break

camera.release()
cv2.destroyAllWindows()

But every time I make that mistake

Traceback (Most recent call last): File "C: Users Gear Appdata Local Programs Python Python37-32 lib site-Packages Mysqldb Cursors.py", line 238, in execute query = query % args Typeerror: not all Arguments converted During string formatting During Handling of the above Exception, Another Exception occurred: Traceback (Most recent call last): File "C: Users Gear Appdata Local Programs Python Python37-32 teste.py", line 49, in cursor.execute(Arg, val) File "C: Users Gear Appdata Local Programs Python Python37-32 lib site-Packages Mysqldb Cursors.py", line 240, in execute self.errorhandler(self, Programmingerror, str(m)) File "C: Users Gear Appdata Local Programs Python Python37-32 lib site-Packages Mysqldb Connections.py", line 52, in defaulterrorhandler raise errorclass(errorvalue) _mysql_exceptions.Programmingerror: not all Arguments converted During string formatting

  • Is there a table for each username ? Because the way you are entering(INSERT INTO name (arrival time) VALLEYS %s) understand that each user is an entity itself and if you have not yet created this entity in the database is like trying to insert the data in the Table John without the table John exists. Could you show the table structure that should receive INSERT ? So it will be easier to help

  • yes each user has their own table

No answers

Browser other questions tagged

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