ARDUINO / PYTHON / SERIAL



我需要一些帮助才能使代码正常工作。以下将是arduino和python代码。

我需要做的是,当我戴上脸时,绿色LED亮起,这意味着它被释放了!如果出现未注册的人脸,红色led将亮起。但我认为我的代码不正确。

PYTHON代码

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代码

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);
}
}
}

我敢说它与这行有关。

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? 

当然,这只是魔术。您可能仍然需要调试代码的各个方面。1( Arduino命令2(Python到Arduino接口3(人脸识别功能4(转换和图像内容

最新更新