I'm working on a university project combining MQTT and Face recognition, using a raspberry pi. First of all I want to say that face recognition works normally when it's being run on its own but I have something that bugs me when applying it with MQTT.
Inside the Try section, when a person is being recognized or unknown it publishes really fine every 3 secs. The strange thing is that if I put my hand in front of the camera or if I get out of range, it keeps on sending the ID of the last person it had recognized.
I want it either do nothing or publish none.
Any idea or suggestion to fix this? Thanks in advance
EDIT 1: Basically as I figured out, it has to do more with the face recognition part. When using just a face recognition code using opencv it runs smoothly. But the thing complicates when I put my hand in front of the camera or go away, because the capture freezes at the last thing it sees so it keeps on printing/publishing the same thing. Haven't figured out a way to avoid this yet. Any help would be appreciated
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()
From the documentation on mqtt clients, you seem to be misusing the client.loop_forever()
method.
http://www.steves-internet-guide.com/loop-python-mqtt-client/
TLDR
client.loop(timeout)
in a loop to control processing of updates. client.loop_start()
starts a new thread which handles updates for you. client.loop_forever()
to block and process updates. Since you have already called client.loop_start()
, you don't need to call loop_forever()
. Removing those calls from your code should resolve the issue.
Side note: your time.sleep(3)
call could be moved to the end of the loop, since it should happen no matter which conditional path it takes.
By using if True: instead of while True: I fixed it. I also removed the subclient.loop_forever() and it works fine. Here is the sample code that is different from the code I posted to my question
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)
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.