''' 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)}")