Advertisement
silver2row

i2c and useful sensors/face detection...

Oct 19th, 2023
646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1. # Example of accessing the Person Sensor from Useful Sensors on a Pi using
  2. # Python. See https://usfl.ink/ps_dev for the full developer guide.
  3.  
  4. import io
  5. import fcntl
  6. import struct
  7. import time
  8.  
  9. # The person sensor has the I2C ID of hex 62, or decimal 98.
  10. PERSON_SENSOR_I2C_ADDRESS = 0x62
  11.  
  12. # We will be reading raw bytes over I2C, and we'll need to decode them into
  13. # data structures. These strings define the format used for the decoding, and
  14. # are derived from the layouts defined in the developer guide.
  15. PERSON_SENSOR_I2C_HEADER_FORMAT = "BBH"
  16. PERSON_SENSOR_I2C_HEADER_BYTE_COUNT = struct.calcsize(
  17.     PERSON_SENSOR_I2C_HEADER_FORMAT)
  18.  
  19. PERSON_SENSOR_FACE_FORMAT = "BBBBBBbB"
  20. PERSON_SENSOR_FACE_BYTE_COUNT = struct.calcsize(PERSON_SENSOR_FACE_FORMAT)
  21.  
  22. PERSON_SENSOR_FACE_MAX = 4
  23. PERSON_SENSOR_RESULT_FORMAT = PERSON_SENSOR_I2C_HEADER_FORMAT + \
  24.     "B" + PERSON_SENSOR_FACE_FORMAT * PERSON_SENSOR_FACE_MAX + "H"
  25. PERSON_SENSOR_RESULT_BYTE_COUNT = struct.calcsize(PERSON_SENSOR_RESULT_FORMAT)
  26.  
  27. # I2C channel 1 is connected to the GPIO pins
  28. I2C_CHANNEL = 2
  29. I2C_PERIPHERAL = 0x703
  30.  
  31. # How long to pause between sensor polls.
  32. PERSON_SENSOR_DELAY = 0.2
  33.  
  34. i2c_handle = io.open("/dev/bone/i2c/" + str(I2C_CHANNEL), "rb", buffering=0)
  35. fcntl.ioctl(i2c_handle, I2C_PERIPHERAL, PERSON_SENSOR_I2C_ADDRESS)
  36.  
  37. while True:
  38.     try:
  39.         read_bytes = i2c_handle.read(PERSON_SENSOR_RESULT_BYTE_COUNT)
  40.     except OSError as error:
  41.         print("No person sensor data found")
  42.         print(error)
  43.         time.sleep(PERSON_SENSOR_DELAY)
  44.         continue
  45.     offset = 0
  46.     (pad1, pad2, payload_bytes) = struct.unpack_from(
  47.         PERSON_SENSOR_I2C_HEADER_FORMAT, read_bytes, offset)
  48.     offset = offset + PERSON_SENSOR_I2C_HEADER_BYTE_COUNT
  49.  
  50.     (num_faces) = struct.unpack_from("B", read_bytes, offset)
  51.     num_faces = int(num_faces[0])
  52.     offset = offset + 1
  53.  
  54.     faces = []
  55.     for i in range(num_faces):
  56.         (box_confidence, box_left, box_top, box_right, box_bottom, id_confidence, id,
  57.          is_facing) = struct.unpack_from(PERSON_SENSOR_FACE_FORMAT, read_bytes, offset)
  58.         offset = offset + PERSON_SENSOR_FACE_BYTE_COUNT
  59.         face = {
  60.             "box_confidence": box_confidence,
  61.             "box_left": box_left,
  62.             "box_top": box_top,
  63.             "box_right": box_right,
  64.             "box_bottom": box_bottom,
  65.             "id_confidence": id_confidence,
  66.             "id": id,
  67.             "is_facing": is_facing,
  68.         }
  69.         faces.append(face)
  70.     checksum = struct.unpack_from("H", read_bytes, offset)
  71.     print(num_faces, faces)
  72.     time.sleep(PERSON_SENSOR_DELAY)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement