From debef7771a603823beb9b1ff1e1a239730095a29 Mon Sep 17 00:00:00 2001 From: se1exin Date: Fri, 13 May 2022 17:50:28 +1000 Subject: [PATCH] Initial commit --- .gitignore | 160 +++++++++++++++++++++++++++++++++++++++++++++++ main.py | 11 ++++ requirements.txt | 3 + tracker.py | 71 +++++++++++++++++++++ 4 files changed, 245 insertions(+) create mode 100644 .gitignore create mode 100644 main.py create mode 100644 requirements.txt create mode 100644 tracker.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2dc53ca --- /dev/null +++ b/.gitignore @@ -0,0 +1,160 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +.idea/ diff --git a/main.py b/main.py new file mode 100644 index 0000000..35e0440 --- /dev/null +++ b/main.py @@ -0,0 +1,11 @@ +from tracker import Tracker + +tracker = Tracker( + mqtt_address="10.1.1.100", + mqtt_client_id="cvzone_tracker_01", + show_img=False) + +while True: + tracker.loop() + +tracker.release() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b8cb76d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +cvzone==1.5.6 +mediapipe==0.8.10 +paho-mqtt==1.6.1 diff --git a/tracker.py b/tracker.py new file mode 100644 index 0000000..3d2fdc3 --- /dev/null +++ b/tracker.py @@ -0,0 +1,71 @@ +import cv2 + +import paho.mqtt.client as mqtt +from cvzone.FaceDetectionModule import FaceDetector + + +class Tracker(object): + def __init__(self, mqtt_address="", mqtt_port=1883, mqtt_client_id="", show_img=False): + self.show_img = show_img + self.min_face_score = 0.5 + self.cap = cv2.VideoCapture(0) + self.face_detector = FaceDetector() + self.img = None + self.face_found = False + + self.mqtt_address = mqtt_address + self.mqtt_port = mqtt_port + self.mqtt_client_id = mqtt_client_id + self.is_mqtt_connected = False + self.mqtt_client = mqtt.Client(mqtt_client_id) + self.mqtt_connect() + + def mqtt_connect(self): + self.mqtt_client.will_set("home/" + self.mqtt_client_id + "/status", "disconnected", 0, False) + self.mqtt_client.on_connect = self.mqtt_on_connect + self.mqtt_client.connect_async(self.mqtt_address, self.mqtt_port, 60) + self.mqtt_client.loop_start() + + def mqtt_on_connect(self, client, userdata, flags, rc): + print("Connected with result code " + str(rc)) + self.is_mqtt_connected = True + self.mqtt_client.publish("home/" + self.mqtt_client_id + "/status", "connected") + + def mqtt_publish(self, topic, payload): + self.mqtt_client.publish(topic, payload) + + def release(self): + self.cap.release() + cv2.destroyAllWindows() + + def read_img(self): + success, img = self.cap.read() + self.img = img + + def detect_face(self): + img, face_bboxs = self.face_detector.findFaces(self.img, draw=self.show_img) + if face_bboxs: + if self.show_img: + center = face_bboxs[0]["center"] + cv2.circle(self.img, center, 5, (255, 0, 255), cv2.FILLED) + + score = face_bboxs[0]["score"][0] + return score >= self.min_face_score + return False + + def loop(self): + self.read_img() + # Look for faces + face_detected = self.detect_face() + if face_detected: + if not self.face_found: + self.mqtt_publish("home/" + self.mqtt_client_id + "/face_detected", 1) + self.face_found = True + else: + if self.face_found: + self.mqtt_publish("home/" + self.mqtt_client_id + "/face_detected", 0) + self.face_found = False + + if self.show_img: + cv2.imshow("Image", self.img) + cv2.waitKey(1)