Skip to content

Instantly share code, notes, and snippets.

@juanpabloaj
Created September 10, 2024 02:33
Show Gist options
  • Save juanpabloaj/b811f0e0970567b20b2387788f28a2f3 to your computer and use it in GitHub Desktop.
Save juanpabloaj/b811f0e0970567b20b2387788f28a2f3 to your computer and use it in GitHub Desktop.
M5stack unitv2 object_recognition example
'''
it shows a log message when an object is detected.
references
https://medium.com/@sebastiaan.panasj/occupancy-counting-with-unitv2-by-m5stack-d35455037882
'''
import subprocess
import json
import os
import logging
LOGLEVEL = os.environ.get('LOGLEVEL', 'INFO').upper()
logging.basicConfig(level=LOGLEVEL, format="%(asctime)s %(message)s")
recognizer = subprocess.Popen(['/home/m5stack/payload/bin/object_recognition', '/home/m5stack/payload/uploads/models/yolo_20class'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
recognizer.stdin.write("_{\"stream\":1}\r\n".encode('utf-8'))
recognizer.stdin.flush()
while True:
line = recognizer.stdout.readline().decode('utf-8').strip()
if not line:
break
#logging.info(line)
try:
doc = json.loads(line)
if "obj" in doc:
logging.info(doc)
except json.JSONDecodeError as e:
logging.info(f"Error: Invalid JSON string\nJSONDecodeError: {str(e)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment