简体   繁体   中英

ARDUINO / PYTHON / SERIAL

I need a little help to make my code work correctly. The following will be the arduino and python codes.

What I need to happen is that when I put my face on, the green LED lights up, meaning it's released, If an unregistered face appears. the red led will light up. But I think my code is not correct.

PYTHON CODE

import face_recognition as fr
import os
import serial

porta_serial = serial.Serial('COM3',9600)

encoders = []
nomes = []

def criarEncoders():
    lista = os.listdir('Pessoas')
    for arquivo in lista:
        imAtual = fr.load_image_file(f'Pessoas/{arquivo}')
        imAtual = cv2.cvtColor(imAtual,cv2.COLOR_BGR2RGB)
        encoders.append(fr.face_encodings(imAtual)[0])
        nomes.append(os.path.splitext(arquivo)[0])

def compararWebcam():
    video = cv2.VideoCapture(0)

    while True:
        check,img = video.read()
        imgP = cv2.resize(img, (0, 0), None, 0.25, 0.25)

        imgP = cv2.cvtColor(imgP,cv2.COLOR_BGR2RGB)

        try:
            faceLoc = fr.face_locations(imgP)[0]
        except:
            faceLoc = []

        if faceLoc:
            y1,x2,y2,x1 = faceLoc
            y1, x2, y2, x1 = y1*4, x2*4, y2*4, x1*4
            cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0),2)  # criando o retângulo em volta da face na webcam com a cor verde
            porta_serial.write(b'o')
            encodeImg = fr.face_encodings(imgP)[0]

            for id,enc in enumerate(encoders):             # loop para comparar imagem com o banco de dados
                comp = fr.compare_faces([encodeImg],enc)
                if comp[0]:
                    cv2.rectangle(img,(x1,y2-35),(x2,y2),(0,255,0),-1)
                    cv2.putText(img,nomes[id],(x1+6,y2-6),cv2.FONT_HERSHEY_COMPLEX,1,(255,255,255),2)
                    porta_serial.write(b'f')
                    print(nomes[id])

        cv2.imshow('Webcam', img)
        cv2.waitKey(1)

criarEncoders()
compararWebcam()

ARDUINO CODE

int led_liberado = 8;
int led_nao_liberado = 7;
char var;

void setup() {
  Serial.begin(9600);
  pinMode(led_liberado, OUTPUT);
  pinMode(led_nao_liberado, OUTPUT);
  digitalWrite(led_nao_liberado, HIGH);
}

void loop() {
  while (Serial.available() > 0) {
    if (var=='o') {
      digitalWrite(led_liberado, HIGH);
      digitalWrite(led_nao_liberado, LOW);
    } else if(var=='f') {
      digitalWrite(led_nao_liberado,HIGH);
      digitalWrite(led_liberado, LOW);
    } else if (var=='q') {
      digitalWrite(led_nao_liberado, HIGH);
      digitalWrite(led_liberado, LOW);
    }
  }
}

I would venture it has something to do with this line.

y1,x2,y2,x1 = faceLoc
y1, x2, y2, x1 = y1*4, x2*4, y2*4, x1*4
#If (x1,y1),(x2,y2) indicate some coordinate of where a face is which seems like it, multiplying them by a scalar loses that information?
#Consider your ROI is at (2,2) and your function grids it at (1,1),(3,3).
#Your code would then change the rectangle to (4,4),(12,12) 
#which doesnt even contain your point of interest. Meaning...no face? 

Of course this is just conjencture. You would probably still need to debug every aspect of your code. 1) Arduino commands 2) Python to Arduino interface 3) Face recognition function 4) Transforms and image stuff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM