usefulsensors / person_sensor_docs

Documentation for the Person Sensor
55 stars 11 forks source link

Persist ID not working for me #18

Open baljo opened 1 month ago

baljo commented 1 month ago

Not sure if I'm doing something wrong, but even if I'm setting persist IDs to true, the program below always returns recognition ID 0, regardless of person. First face it sees is mine, returns ID 0 which is fine, next it sees a portrait of a family member done by pencil, also then it returns ID 0 for any of the family member portraits it sees. I was surprised - not necessarily positively - that it does not distinguish between real persons and portraits. I do understand that the sensor should not be used as identification device for any critical purpose.

Edit 1: Hmm, I've left out the calibration step, will try it before closing this issue.

Edit 2: Have added the calibration, and face recognition of several faces is working alright until I recycle power to the device. After that faces are lost. This is the relevant code from the setup function:

    // Enable ID persistence
    Wire.beginTransmission(PERSON_SENSOR_I2C_ADDRESS);
    Wire.write(MODE_REGISTER);
    Wire.write(0x01);
    Wire.write(ENABLE_ID_REGISTER);
    Wire.write(0x01);
    Wire.write(PERSIST_ID_REGISTER); // Register for persisting IDs
    Wire.write(0x01); // Set persist IDs to true
    Wire.endTransmission();
// Code for detecting faces with Person Sensor, storing IDs, and blinking user LED when a face is found
// Date: 2024-10-04 14:30:00 | Thomas Vikström

#include <Wire.h>
#include "person_sensor.h"

// How long to wait between reading the sensor. The sensor can be read as
// frequently as you like, but the results only change at about 5FPS, so
// waiting for 200ms is reasonable.
const int32_t SAMPLE_DELAY_MS = 200;

#define USER_LED_PIN D7  // User LED pin
#define PERSIST_ID_REGISTER 0x05

void setup() {
  // You need to make sure you call Wire.begin() in setup, or the I2C access
  // below will fail.
  Wire.begin();
  Serial.begin(9600);
  pinMode(USER_LED_PIN, OUTPUT);  // Set user LED pin as output

  // Enable ID persistence by writing to the appropriate register
  Wire.beginTransmission(PERSON_SENSOR_I2C_ADDRESS);
  Wire.write(PERSIST_ID_REGISTER);  // Register address for persisting IDs
  Wire.write(0x01);  // Set persist IDs to true
  Wire.endTransmission();

  Serial.println("Person Sensor setup complete. ID persistence enabled.");
}

void loop() {
  person_sensor_results_t results = {};
  // Perform a read action on the I2C address of the sensor to get the
  // current face information detected.
  if (!person_sensor_read(&results)) {
    Serial.println("No person sensor results found on the i2c bus");
    delay(SAMPLE_DELAY_MS);
    return;
  }

  Serial.println("********");
  Serial.print(results.num_faces);
  Serial.println(" faces found");

  if (results.num_faces > 0) {
    // Blink user LED if a face is found
    digitalWrite(USER_LED_PIN, HIGH);
    delay(100);  // LED on for 100ms
    digitalWrite(USER_LED_PIN, LOW);
  }

  for (int i = 0; i < results.num_faces; ++i) {
    const person_sensor_face_t* face = &results.faces[i];
    Serial.print("Face #");
    Serial.print(i);
    Serial.print(": ");
    Serial.print(face->box_confidence);
    Serial.print(" confidence, (");
    Serial.print(face->box_left);
    Serial.print(", ");
    Serial.print(face->box_top);
    Serial.print("), (");
    Serial.print(face->box_right);
    Serial.print(", ");
    Serial.print(face->box_bottom);
    Serial.print("), ");
    if (face->is_facing) {
      Serial.println("facing");
    } else {
      Serial.println("not facing");
    }
    Serial.print("Recognition ID: ");
    Serial.println(face->id);

    // Publish an event if face ID 0 is detected
    if (face->id == 0) {
      Particle.publish("face_0_detected", "Face ID 0 has been detected", PRIVATE);
      delay(2000);   // This is to avoid duplicates
    }
  }
  delay(SAMPLE_DELAY_MS);
}