-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlock.cpp
121 lines (91 loc) · 3.21 KB
/
lock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Grayson Pike, 2018
/*
Facial Recognition Lock Software (Main program)
Runs continuously looking for specific faces, then triggers a servo
when an authenticated face is recognized.
To prepare a model for this program, follow the steps in README.md
*/
#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <unistd.h>
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/face.hpp>
#include <opencv2/imgcodecs.hpp>
#include "face_detect.hpp"
#include "hardware.hpp"
#include "config.hpp"
/*
Returns model name from command line arguments or exits on failure
*/
std::string get_model_name(int argc, char *argv[]) {
std::string model_name;
if (argc != 2) {
std::cout << "Error: Correct usage: ./train <model_name>" << std::endl;
exit(1);
} else {
return argv[1];
}
}
int main(int argc, char *argv[]) {
//GPIO input_pin("17", "in");
//std::cout << input_pin.read_value() << std::endl;
std::string model_name = get_model_name(argc, argv);
// Load model
std::cout << "Loading model..." << std::flush;
cv::Ptr<cv::face::FaceRecognizer> model = cv::face::LBPHFaceRecognizer::create();
model->read(std::string(MODEL_DIR) + model_name + ".xml");
std::cout << "[DONE]" << std::endl;
cv::VideoCapture camera = get_camera();
// Loop to detect faces
bool loop = true;
while (loop) {
cv::Mat image;
// Capture an image
flush_capture_buffer(camera);
camera >> image;
// Convert image to greyscale
cv::cvtColor(image, image, cv::COLOR_RGB2GRAY);
// Detect coordinates of a face, if any
std::vector<cv::Rect> face_regions = detect_faces(image);
std::cout << "Deteced " << face_regions.size() << " faces." << std::endl;
// TODO: Make this work with more than one face in the picture
if (face_regions.size() == 1) {
// Crop image to face, then resize
image = image(face_regions[0]);
cv::resize(image, image, cv::Size(FACE_WIDTH, FACE_HEIGHT), 0, 0, cv::INTER_LANCZOS4);
// Determine if face is allowed
int label;
double confidence;
model->predict(image, label, confidence);
std::cout << "Results: " << label << ", " << confidence << std::endl;
// If a face is recognized and authorized
if (label != 0 && confidence <= POSITIVE_THRESHOLD) {
std::cout << "****** Detected allowed face with label: " << label << std::endl;
// Move the lock to the unlocked position
system("sudo python servo.py 250");
// Sleep for 1 second (or 1000000 microseconds) to allow the servo to move
usleep(1500000u);
// Move back to the resting position
system("sudo python servo.py 175");
// Sleep for WAIT_TIME seconds before relocking the door
usleep(1500000u * WAIT_TIME);
// Move the lock to the locked position
system("sudo python servo.py 100");
// Sleep for 1 second (or 1000000 microseconds) to allow the servo to move
usleep(1000000u);
// Move back to the resting position
system("sudo python servo.py 175");
}
}
//std::string input;
//std::cout << "Enter 'q' to quit, or any other key to take another picture: " << std::endl;
//std::cin >> input;
//if (input == "q" || input == "Q") {
// loop = false;
//}
}
}