Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temp main #8

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ src/model/train/**
src/model/test/**
src/model/david/**
venv/**
build/**
Images/**

*.DS_Store
.vscode/
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.12)
project(edge-ml)

find_package(OpenCV REQUIRED)

# Add subdirectories
add_subdirectory(src)
add_subdirectory(utils)

# Define your executable and link libraries if needed
add_executable(camera src/simple_camera.cpp)

include_directories(${OpenCV_INCLUDE_DIRS})

target_link_libraries(camera PRIVATE ${OpenCV_LIBS} UtilityLib)
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@
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:

`cd build`

`cmake ..`

`make`

`./camera`
8 changes: 0 additions & 8 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +0,0 @@
cmake_minimum_required(VERSION 3.12)
project(OpenCVExample)

find_package(OpenCV REQUIRED)

add_executable(simple_camera simple_camera.cpp)

target_link_libraries(simple_camera PRIVATE ${OpenCV_LIBS})
142 changes: 39 additions & 103 deletions src/simple_camera.cpp
Original file line number Diff line number Diff line change
@@ -1,79 +1,4 @@
// // simple_camera.cpp
// // MIT License
// // Copyright (c) 2019-2022 JetsonHacks
// // See LICENSE for OpenCV license and additional information
// // Using a CSI camera (such as the Raspberry Pi Version 2) connected to a
// // NVIDIA Jetson Nano Developer Kit using OpenCV
// // Drivers for the camera and OpenCV are included in the base image

// #include <opencv2/opencv.hpp>

// std::string gstreamer_pipeline(int capture_width, int capture_height, int
// display_width, int display_height, int framerate, int flip_method)
// {
// return "nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)" +
// std::to_string(capture_width) + ", height=(int)" +
// std::to_string(capture_height) + ", framerate=(fraction)" +
// std::to_string(framerate) +
// "/1 ! nvvidconv flip-method=" + std::to_string(flip_method) + " !
// video/x-raw, width=(int)" + std::to_string(display_width) + ",
// height=(int)" + std::to_string(display_height) + ",
// format=(string)BGRx ! videoconvert ! video/x-raw,
// format=(string)BGR ! appsink";
// }

// int main()
// {
// int capture_width = 1280;
// int capture_height = 720;
// int display_width = 1280;
// int display_height = 720;
// int framerate = 5;
// int flip_method = 0;

// std::string pipeline = gstreamer_pipeline(capture_width,
// capture_height,
// display_width,
// display_height,
// framerate,
// flip_method);
// std::cout << "Using pipeline: \n\t" << pipeline << "\n";

// cv::VideoCapture cap(pipeline, cv::CAP_GSTREAMER);
// if (!cap.isOpened())
// {
// std::cout << "Failed to open camera." << std::endl;
// return (-1);
// }

// cv::namedWindow("CSI Camera", cv::WINDOW_AUTOSIZE);
// cv::Mat img;

// std::cout << "Hit ESC to exit"
// << "\n";

// int i = 0;
// while (true)
// {
// if (!cap.read(img))
// {
// std::cout << "Capture read error" << std::endl;
// break;
// }

// cv::imshow("CSI Camera", img);
// cv::imwrite("../Images/image" + std::to_string(i) + ".jpg", img);
// int keycode = cv::waitKey(10) & 0xff;
// if (keycode == 27)
// break;
// i++;
// }

// cap.release();
// cv::destroyAllWindows();
// return 0;
// }

#include "../utils/motion_detection.cpp"
#include <chrono>
#include <iomanip>
#include <opencv2/opencv.hpp>
Expand All @@ -92,7 +17,7 @@ std::string current_time_formatted() {
}

int main() {
// defualt webcam caputre - change for jetson
// default webcam caputre - change for jetson
int webcam_index = 0;

// open the default camera
Expand All @@ -106,7 +31,8 @@ int main() {
// load the face cascade
cv::CascadeClassifier face_cascade;
if (!face_cascade.load(
"./haarcascade_frontalface_default.xml")) // Update with correct path
"../utils/haarcascade_frontalface_default.xml")) // Update with
// correct path
{
// error handling
std::cout << "Error loading face cascade\n";
Expand All @@ -118,7 +44,8 @@ int main() {
cv::Mat img; // create a matrix to hold the image
std::cout << "Hit ESC to exit\n"; // print to console
auto start = std::chrono::steady_clock::now(); // start the timer
int i = 0; // image counter
int img_count = 0; // image counter
int bb_count = 0; // bounding box counter

// main loops
while (true) {
Expand All @@ -128,39 +55,49 @@ int main() {
break;
}

// detect faces
std::vector<cv::Rect> faces;
cv::Mat gray; // create a matrix to hold the gray image
cvtColor(img, gray, cv::COLOR_BGR2GRAY); // convert to gray
face_cascade.detectMultiScale(gray, faces); // detect faces

// draw the faces
for (const auto &face : faces) {
// draw a rectangle around the face
cv::rectangle(img, face, cv::Scalar(255, 0, 0), 2);
}
// fix paths when using jetson
std::string prevImPath =
"../Images/image" + std::to_string(img_count - 1) + ".jpg";
std::string currImPath =
"../Images/image" + std::to_string(img_count) + ".jpg";

// display realtime webcam feed
cv::imshow("Webcam", img);

// save the image every 5 seconds
auto end = std::chrono::steady_clock::now();
// calculate the elapsed time
std::chrono::duration<double> elapsed = end - start;

// save the image under time condition
if (elapsed.count() >= WAIT_TIME) // 5 seconds
{
if (elapsed.count() >= WAIT_TIME) {
// save the image
for (const auto &face : faces) {
// crop the face
cv::Mat face_img = img(face);
cv::imwrite(currImPath, img);

if (img_count > 0 && detect_motion(prevImPath, currImPath)) {
std::cout << "Motion detected!" << std::endl;

// detect faces
std::vector<cv::Rect> faces;
cv::Mat gray; // create a matrix to hold the gray image
cvtColor(img, gray, cv::COLOR_BGR2GRAY); // convert to gray
face_cascade.detectMultiScale(gray, faces); // detect faces

// draw the faces
for (const auto &face : faces) {
// draw a rectangle around the face
cv::rectangle(img, face, cv::Scalar(255, 0, 0), 2);
}
// save the image
std::string filename = "../Images/image_" + std::to_string(i) + "_" +
current_time_formatted() + ".jpg";
cv::imwrite(filename, face_img);
i++;
for (const auto &face : faces) {
// crop the face
cv::Mat face_img = img(face);
// save the image
std::string filename = "../Images/bounding_boxes/image" +
std::to_string(bb_count) + ".jpg";
cv::imwrite(filename, face_img);
bb_count++;
}
}
img_count++;
start = std::chrono::steady_clock::now(); // reset the timer
}

Expand All @@ -171,8 +108,7 @@ int main() {
}

// release the camera

cap.release();
cv::destroyAllWindows();
return EXIT_SUCCESS;
}
}
5 changes: 5 additions & 0 deletions utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Create a library from motion_detection.cpp
add_library(UtilityLib motion_detection.cpp)

# Link OpenCV libraries if needed
target_link_libraries(UtilityLib ${OpenCV_LIBS})
Loading