From 6fd230c98136ba9d4a928a900dcc0ab2a456aabc Mon Sep 17 00:00:00 2001 From: Bryant Park Date: Sun, 11 Feb 2024 17:55:01 -0500 Subject: [PATCH 1/8] Remove __pycache__ from repo --- src/model/__pycache__/utils.cpython-38.pyc | Bin 856 -> 0 bytes .../message/__pycache__/__init__.cpython-38.pyc | Bin 145 -> 0 bytes .../__pycache__/sms_message.cpython-38.pyc | Bin 697 -> 0 bytes 3 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/model/__pycache__/utils.cpython-38.pyc delete mode 100644 src/model/message/__pycache__/__init__.cpython-38.pyc delete mode 100644 src/model/message/__pycache__/sms_message.cpython-38.pyc diff --git a/src/model/__pycache__/utils.cpython-38.pyc b/src/model/__pycache__/utils.cpython-38.pyc deleted file mode 100644 index 0a34ceb671ced56ebe4b3feb285791315fc65594..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 856 zcmYjPPm2>V6i+hkbhfryUG$vm;O-XOt1N=Zf`|y>r3XuxyiTV~lOf5jZ0VlXt9OsP zN56^q5pwm^uOJA%WNf={$orS%yD@N&VVs3%&9GmxD*GJeofeY2IdI!RQOzold{))Kh2?_3 z{qW|TS4&aC5pN7MMi*c$uN)Uj#_I-bwStgwI+k(5l;F6Xiv~{OkxhV}Q=b)cp%m0M zKk&@WJgc-nFd*bKH1yrr$3KuzXbICr1EoJKw7LZ2rg3j7-Bkm=H>!viIFccEg`kf<+8DsUZ3>h(HF6K#l_t7qb9~6oz01O-8?!3`HPe1o6vEKO;XkRX;hU zST`>*FJISKKQ$#iRW~pMfawsqmQFWc03$TX5(pgGI~qfce&a0eKwtZcsE`(pY;0u{>y&v z@aWmVMS~-Vdk2RH&kO8E?97L=20jGWPXX3Qg$!4OkqtJ;U_)-mhOEhjcj2$GshNOz z-@J{-5UBOXtB0e?s3qg-@v-2_bXMUh7lkZ&C-JO*CfT*EeNwW_EHN^vD_AJ9ab6U1 zS(!{Pn2qwKInRu| z+tj>bcZrr;)dK*8n>fM&Chq&E_8^nQmPk!w^JO6xa$j+6wl}81?ei)3@7Q1tf~zZ4 rhd_l6Vh2qJuFF>MJ!nJDhOa>4Qt;RA`Lw%H2=kjHAQ6s;kA3nVm4UFk From 34d0f95a48032f9c9c845e16bfd15dcf126cbda8 Mon Sep 17 00:00:00 2001 From: Bryant Park Date: Mon, 12 Feb 2024 00:35:41 -0500 Subject: [PATCH 2/8] Add pre-commit config with clang-format --- .pre-commit-config.yaml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..2f34e07 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,15 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + - id: check-xml +- repo: https://github.com/pre-commit/mirrors-clang-format + rev: 'v17.0.6' # Use the sha / tag you want to point at + hooks: + - id: clang-format From 6e41b335f4fb2e73ff4c9b366ff9ccb1f6515c7b Mon Sep 17 00:00:00 2001 From: Bryant Park Date: Mon, 12 Feb 2024 00:36:07 -0500 Subject: [PATCH 3/8] Run pre-commit hooks on all files --- .github/workflows/clang-format.yml | 2 +- README.md | 6 +- ci/clang-format-checker.sh | 2 +- src/haarcascade_frontalface_default.xml | 2 +- src/model/EdgeTraining.py | 2 +- src/model/message/sms_message.py | 5 +- src/model/realtime/splits/train.txt | 2 +- src/model/utils.py | 2 +- utils/haarcascade_frontalface_default.xml | 2 +- utils/motion_detection.cpp | 73 +++++++++++------------ 10 files changed, 47 insertions(+), 51 deletions(-) diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml index 567bca2..5557aff 100644 --- a/.github/workflows/clang-format.yml +++ b/.github/workflows/clang-format.yml @@ -19,4 +19,4 @@ jobs: exit 1 else echo "Merging is allowed." - fi \ No newline at end of file + fi diff --git a/README.md b/README.md index 2026649..3e20960 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,9 @@ Make sure git-clang-format is installed with `npm install -g clang-format` 1. Call `clang-format.sh` under ci/ 2. `git add` and `git commit`! -## To run +## To run -`cd build` +`cd build` `cmake ..` @@ -21,7 +21,7 @@ Make sure git-clang-format is installed with `npm install -g clang-format` - `cd` to `src/message/` -- Make a `.env` file with `TWILIO_ACCOUNT_SID` and `TWILIO_AUTH_TOKEN` set to your account SID and Auth Token +- Make a `.env` file with `TWILIO_ACCOUNT_SID` and `TWILIO_AUTH_TOKEN` set to your account SID and Auth Token - macOS: `brew tap twilio/brew && brew install twilio` diff --git a/ci/clang-format-checker.sh b/ci/clang-format-checker.sh index cea4b3c..3cfed15 100755 --- a/ci/clang-format-checker.sh +++ b/ci/clang-format-checker.sh @@ -15,4 +15,4 @@ modified_files = [f.decode("utf-8") for f in modified_files] for f in modified_files: result = subprocess.run(["clang-format", "--dry-run", "--Werror", f]) if result.returncode: - exit(1) \ No newline at end of file + exit(1) diff --git a/src/haarcascade_frontalface_default.xml b/src/haarcascade_frontalface_default.xml index bc2aa3c..cbd1aa8 100644 --- a/src/haarcascade_frontalface_default.xml +++ b/src/haarcascade_frontalface_default.xml @@ -33311,4 +33311,4 @@ 12 1 3 11 2. <_> 9 12 3 11 2. - \ No newline at end of file + diff --git a/src/model/EdgeTraining.py b/src/model/EdgeTraining.py index 55ff228..b9de8f4 100644 --- a/src/model/EdgeTraining.py +++ b/src/model/EdgeTraining.py @@ -44,4 +44,4 @@ early_stopping=True, patience=5, min_delta=0.1, - ) \ No newline at end of file + ) diff --git a/src/model/message/sms_message.py b/src/model/message/sms_message.py index 5504b71..4382f31 100644 --- a/src/model/message/sms_message.py +++ b/src/model/message/sms_message.py @@ -1,5 +1,5 @@ -import os -from twilio.rest import Client +import os +from twilio.rest import Client from dotenv import load_dotenv load_dotenv() @@ -17,4 +17,3 @@ def send_message(intruder_detected): body=msg, from_=from_number, to=to_number) - diff --git a/src/model/realtime/splits/train.txt b/src/model/realtime/splits/train.txt index 75b4312..11c78de 100644 --- a/src/model/realtime/splits/train.txt +++ b/src/model/realtime/splits/train.txt @@ -2,4 +2,4 @@ David_Han 1 5 David_Han 2 6 David_Han 3 7 David_Han 4 8 -David_Han 5 10 \ No newline at end of file +David_Han 5 10 diff --git a/src/model/utils.py b/src/model/utils.py index e9dd156..7c015e8 100644 --- a/src/model/utils.py +++ b/src/model/utils.py @@ -25,4 +25,4 @@ def preprocess_triplets(anchor, positive, negative): preprocess_image(anchor), preprocess_image(positive), preprocess_image(negative), - ) \ No newline at end of file + ) diff --git a/utils/haarcascade_frontalface_default.xml b/utils/haarcascade_frontalface_default.xml index bc2aa3c..cbd1aa8 100644 --- a/utils/haarcascade_frontalface_default.xml +++ b/utils/haarcascade_frontalface_default.xml @@ -33311,4 +33311,4 @@ 12 1 3 11 2. <_> 9 12 3 11 2. - \ No newline at end of file + diff --git a/utils/motion_detection.cpp b/utils/motion_detection.cpp index 15f2cfa..23de50b 100644 --- a/utils/motion_detection.cpp +++ b/utils/motion_detection.cpp @@ -1,45 +1,42 @@ -#include #include +#include const double MIN_CONTOUR_SIZE = 3000; // Adjust this threshold as needed -bool detect_motion(std::string im_path1, std::string im_path2) -{ - - // load images - cv::Mat image1 = cv::imread(im_path1); - cv::Mat image2 = cv::imread(im_path2); - - // preprocess - cv::Mat gray1, gray2; - - cv::cvtColor(image1, gray1, cv::COLOR_BGR2GRAY); - cv::cvtColor(image2, gray2, cv::COLOR_BGR2GRAY); - cv::GaussianBlur(gray1, gray1, cv::Size(21, 21), 0); - cv::GaussianBlur(gray2, gray2, cv::Size(21, 21), 0); - - cv::Mat diff; - cv::absdiff(gray1, gray2, diff); - - cv::Mat thresh; - cv::threshold(diff, thresh, 25, 255, cv::THRESH_BINARY); - - std::vector> contours; - cv::findContours(thresh, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE); - - for (int i = 0; i < contours.size(); i++) - { - cv::drawContours(image2, contours, i, cv::Scalar(0, 255, 0), 2); - } +bool detect_motion(std::string im_path1, std::string im_path2) { + + // load images + cv::Mat image1 = cv::imread(im_path1); + cv::Mat image2 = cv::imread(im_path2); + + // preprocess + cv::Mat gray1, gray2; + + cv::cvtColor(image1, gray1, cv::COLOR_BGR2GRAY); + cv::cvtColor(image2, gray2, cv::COLOR_BGR2GRAY); + cv::GaussianBlur(gray1, gray1, cv::Size(21, 21), 0); + cv::GaussianBlur(gray2, gray2, cv::Size(21, 21), 0); + + cv::Mat diff; + cv::absdiff(gray1, gray2, diff); + + cv::Mat thresh; + cv::threshold(diff, thresh, 25, 255, cv::THRESH_BINARY); + + std::vector> contours; + cv::findContours(thresh, contours, cv::RETR_EXTERNAL, + cv::CHAIN_APPROX_SIMPLE); + + for (int i = 0; i < contours.size(); i++) { + cv::drawContours(image2, contours, i, cv::Scalar(0, 255, 0), 2); + } - bool motionDetected = false; - for (int i = 0; i < contours.size(); i++) - { - if (cv::contourArea(contours[i]) > MIN_CONTOUR_SIZE) - { - motionDetected = true; - break; - } + bool motionDetected = false; + for (int i = 0; i < contours.size(); i++) { + if (cv::contourArea(contours[i]) > MIN_CONTOUR_SIZE) { + motionDetected = true; + break; } - return motionDetected; + } + return motionDetected; } From 7a26440f7b3f864a5a2509a66d5f25aaffb94b36 Mon Sep 17 00:00:00 2001 From: Bryant Park Date: Mon, 12 Feb 2024 00:39:03 -0500 Subject: [PATCH 4/8] Add GitHub Action for pre-commit --- .github/workflows/pre-commit.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .github/workflows/pre-commit.yml diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 0000000..2b11178 --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,14 @@ +name: pre-commit + +on: + pull_request: + push: + branches: [main] + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v3 + - uses: pre-commit/action@v3.0.1 From 93839e8b5cae07c47f33acded102af77086f6720 Mon Sep 17 00:00:00 2001 From: Bryant Park Date: Mon, 12 Feb 2024 00:40:34 -0500 Subject: [PATCH 5/8] Remove old clang-format scripts and GH Action --- .github/workflows/clang-format.yml | 22 ---------------------- ci/clang-format-checker.sh | 18 ------------------ ci/clang-format.sh | 23 ----------------------- 3 files changed, 63 deletions(-) delete mode 100644 .github/workflows/clang-format.yml delete mode 100755 ci/clang-format-checker.sh delete mode 100755 ci/clang-format.sh diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml deleted file mode 100644 index 5557aff..0000000 --- a/.github/workflows/clang-format.yml +++ /dev/null @@ -1,22 +0,0 @@ -name: clang-format-checker - -on: push - -jobs: - check_merge: - runs-on: ubuntu-latest - - steps: - - name: Checkout - uses: actions/checkout@v3 - - name: Check if script returns 1 - run: | - set +x - ./ci/clang-format-checker.sh - exit_code=$? - - if [ $exit_code -eq 1 ]; then - exit 1 - else - echo "Merging is allowed." - fi diff --git a/ci/clang-format-checker.sh b/ci/clang-format-checker.sh deleted file mode 100755 index 3cfed15..0000000 --- a/ci/clang-format-checker.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env python - -## Open all modified files in the current branch and run clang-format on them. -import subprocess - -# Get the list of modified files. from git -modified_files = subprocess.check_output( - "find src/ -iname *.h -o -iname *.cpp", - shell=True, -).splitlines() - -# convert to list of strings -modified_files = [f.decode("utf-8") for f in modified_files] - -for f in modified_files: - result = subprocess.run(["clang-format", "--dry-run", "--Werror", f]) - if result.returncode: - exit(1) diff --git a/ci/clang-format.sh b/ci/clang-format.sh deleted file mode 100755 index d545317..0000000 --- a/ci/clang-format.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env python3 - -## Open all modified files in the current branch and run clang-format on them. -import subprocess - -# Get the list of modified files. from git -#modified_files = subprocess.check_output( -# "{ git diff --name-only ; git diff --name-only --staged ; } | sort | uniq", -# shell=True, -#).splitlines() - -modified_files = subprocess.check_output( - "find ../src/ -iname *.h -o -iname *.cpp", - shell=True, -).splitlines() - -# convert to list of strings -modified_files = [f.decode("utf-8") for f in modified_files] - -for f in modified_files: - if f.endswith(".h") or f.endswith(".cpp"): - print("Formatting: " + f) - subprocess.check_call(["clang-format", "-i", f]) From 58bdb1dbb63360b78e97a80b098b309ce07cd309 Mon Sep 17 00:00:00 2001 From: Bryant Park Date: Mon, 12 Feb 2024 00:44:34 -0500 Subject: [PATCH 6/8] Update README --- README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3e20960..5010821 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,13 @@ [![cpp-linter](https://github.com/cpp-linter/cpp-linter-action/actions/workflows/cpp-linter.yml/badge.svg)](https://github.com/cpp-linter/cpp-linter-action/actions/workflows/cpp-linter.yml) -# To Lint CPP -Make sure git-clang-format is installed with `npm install -g clang-format` -1. Call `clang-format.sh` under ci/ -2. `git add` and `git commit`! +## Installing pre-commit hooks + +Install pre-commmit following the directions from [here](https://pre-commit.com/#install). If you're on a Mac with `homebrew`, the easiest way is `brew install pre-commit`. + +Then, run `pre-commit install`. You only need to do this once. + +Now, pre-commit will run automatically when you make a commit, and will fix files for you. Make sure to add the changed files after pre-commit runs! ## To run From 27278774145650e5b90e3121fc05fef085e07484 Mon Sep 17 00:00:00 2001 From: Bryant Park Date: Mon, 12 Feb 2024 00:58:28 -0500 Subject: [PATCH 7/8] Add black to pre-commit --- .pre-commit-config.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2f34e07..5c54a65 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,3 +13,7 @@ repos: rev: 'v17.0.6' # Use the sha / tag you want to point at hooks: - id: clang-format +- repo: https://github.com/psf/black + rev: 22.10.0 + hooks: + - id: black From d368843211d7a3a72c857bbd9032e3c75b5d337a Mon Sep 17 00:00:00 2001 From: Bryant Park Date: Mon, 12 Feb 2024 00:59:02 -0500 Subject: [PATCH 8/8] Run black via pre-commit --- src/model/EdgeTraining.py | 34 +++++++++++++++----------------- src/model/Siamese_Predictor.py | 1 + src/model/message/sms_message.py | 14 ++++++------- src/model/utils.py | 1 + 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/model/EdgeTraining.py b/src/model/EdgeTraining.py index b9de8f4..31c03b7 100644 --- a/src/model/EdgeTraining.py +++ b/src/model/EdgeTraining.py @@ -11,9 +11,7 @@ data_path = "realtime" -train_path = os.path.join( - data_path, "train.pickle" - ) # A path for the train file +train_path = os.path.join(data_path, "train.pickle") # A path for the train file siamese = SiameseNetwork( seed=seed, @@ -27,21 +25,21 @@ ) loader = DataLoader( - width=WIDTH, - height=HEIGHT, - cells=CEELS, - data_path=data_path, - output_path=train_path, - ) + width=WIDTH, + height=HEIGHT, + cells=CEELS, + data_path=data_path, + output_path=train_path, +) loader.load(set_name="train") siamese.fit( - weights_file="weights/weights.h5", - train_path=train_path, - validation_size=0.2, - batch_size=32, - epochs=2, - early_stopping=True, - patience=5, - min_delta=0.1, - ) + weights_file="weights/weights.h5", + train_path=train_path, + validation_size=0.2, + batch_size=32, + epochs=2, + early_stopping=True, + patience=5, + min_delta=0.1, +) diff --git a/src/model/Siamese_Predictor.py b/src/model/Siamese_Predictor.py index f0d5998..813bb0f 100644 --- a/src/model/Siamese_Predictor.py +++ b/src/model/Siamese_Predictor.py @@ -41,6 +41,7 @@ def preprocess_image(filename): return img + # load the model siamese = SiameseNetwork( seed=seed, diff --git a/src/model/message/sms_message.py b/src/model/message/sms_message.py index 4382f31..7074a66 100644 --- a/src/model/message/sms_message.py +++ b/src/model/message/sms_message.py @@ -4,16 +4,14 @@ load_dotenv() -account_sid = os.getenv('TWILIO_ACCOUNT_SID') -auth_token = os.getenv('TWILIO_AUTH_TOKEN') +account_sid = os.getenv("TWILIO_ACCOUNT_SID") +auth_token = os.getenv("TWILIO_AUTH_TOKEN") + +from_number = "+18886815709" +to_number = "+19788814542" -from_number = '+18886815709' -to_number = '+19788814542' def send_message(intruder_detected): msg = "Intruder Alert!" if intruder_detected else "Welcome!" client = Client(account_sid, auth_token) - message = client.messages.create( - body=msg, - from_=from_number, - to=to_number) + message = client.messages.create(body=msg, from_=from_number, to=to_number) diff --git a/src/model/utils.py b/src/model/utils.py index 7c015e8..cfa8524 100644 --- a/src/model/utils.py +++ b/src/model/utils.py @@ -2,6 +2,7 @@ target_shape = (200, 200) + def preprocess_image(filename): """ Load the specified file as a JPEG image, preprocess it and