我正在使用树莓派将MQTT和人脸识别结合在一起的大学项目。 首先,我想说的是,人脸识别在单独运行时可以正常工作,但是在将其与MQTT结合使用时,我遇到了一些麻烦。
在“尝试”部分中,当一个人被识别或未知时,每3秒发布一次就很好。 奇怪的是,如果我将手放在相机前面或超出范围,它会继续发送已识别出的最后一个人的ID。
我希望它什么都不做或不发布。
是否有解决此问题的想法或建议? 预先感谢
编辑1:基本上,正如我所知道的,它必须与面部识别部分做更多的工作。当仅使用使用opencv的人脸识别代码时,它可以平稳运行。但是当我将手放在相机前或离开相机时,事情变得复杂了,因为捕获的图像冻结在最后看到的东西上,因此它会继续打印/发布相同的东西。尚未找到避免这种情况的方法。任何帮助将不胜感激
import cv2
import json
import time
import datetime as dt
import paho.mqtt.client as mqtt
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
recognizer = cv2.face.LBPHFaceRecognizer_create()
broker = "*****"
port = *****
username = '*****'
password = '*****'
def on_connect(client, userdata, flags, rc):
print("Connected with code: ", rc)
client.subscribe('test/#')
def on_message(client, userdata, msg):
print(str(msg.payload))
client = mqtt.Client("Python1")
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(username, password)
client.connect(broker, port, 60)
client.loop_start()
time.sleep(1)
def facerecognizer():
recognizer.read("trainerdir/training.yml")
font = cv2.FONT_HERSHEY_SIMPLEX
# Loading data
with open('data.json', 'r') as f:
names = json.load(f)
# reverse the data
# NOTE: for k, v !!
# else it raises error !
names = {v: k for k, v in names.items()}
# print(names)
print("[INFO] Face recognition is starting..")
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 360)
try:
while True:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray,
scaleFactor=1.3,
minNeighbors=5
# minSize = (20,20)
)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
roi_gray = gray[y:y + h, x:x + w]
roi_color = img[y:y + h, x:x + w]
ID, confidence = recognizer.predict(roi_gray)
if (confidence < 100):
ID = names[ID]
confidence = "{}%".format(round(100 - confidence))
while True:
client.publish("Tutorial/", ID + " " + str(dt.datetime.now()))
print('ID sent')
time.sleep(3)
else:
client.publish("Tutorial/", 'None')
print('ID sent')
time.sleep(3)
client.loop_forever()
else:
ID = "Unkown"
confidence = "{}%".format(round(100 - confidence))
print(ID, dt.datetime.now())
while True:
client.publish("Tutorial/", ID + " " + str(dt.datetime.now()))
print('ID sent')
time.sleep(3)
else:
client.publish("Tutorial/", 'None')
print('ID sent')
time.sleep(3)
client.loop_forever()
# except UnboundLocalError:
# print("Error occured. Exitting..")
except KeyboardInterrupt:
pass
except KeyError as K:
print(K)
print('[INFO] Name Value is a string and not an integer')
print("[INFO] Exiting program..")
cap.release()
答案 0 :(得分:1)
从mqtt客户端的文档中,您似乎在滥用client.loop_forever()
方法。
http://www.steves-internet-guide.com/loop-python-mqtt-client/
TLDR
client.loop(timeout)
来控制更新的处理。client.loop_start()
启动一个新线程来为您处理更新。client.loop_forever()
阻止和处理更新。 由于您已经致电client.loop_start()
,因此无需致电loop_forever()
。从代码中删除这些调用应该可以解决问题。
侧面说明:您的time.sleep(3)
调用可以移至循环的末尾,因为无论采用哪种条件路径都应该发生。
答案 1 :(得分:1)
通过使用if True:而不是True:我修复了它。我还删除了subclient.loop_forever(),它工作正常。 这是与我发布到问题中的代码不同的示例代码
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
roi_gray = gray[y:y + h, x:x + w]
# roi_color = img[y:y + h, x:x + w]
ID, confidence = recognizer.predict(roi_gray)
# 0 is 100% accuracy , and 100 is 0% accuracy
if (confidence < 100):
ID = names[ID]
if True:
subclient.publish("project/IDReceiver", ID + " " + str(dt.datetime.now()))
print('ID sent')
time.sleep(3)
# subclient.loop_forever()
else:
ID = "Unknown"
if True:
subclient.publish("project/IDReceiver", "Unknown " + str(dt.datetime.now()))
print('ID sent')
time.sleep(3)