-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7246aa0
commit c044c9b
Showing
62 changed files
with
199 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,127 @@ | ||
#include <opencv2/opencv.hpp> | ||
#include <iostream> | ||
|
||
#include "modules/speed-estimator/speed_estimator.h" | ||
#include "modules/lane-detector/lane_detector.h" | ||
#include "modules/pedestrian-detector/pedestrian_detector.h" | ||
#include "modules/stopping-distance-calculator/stopping_distance_calculator.h" | ||
#include "modules/traffic-sign-detector/traffic_sign_detector.h" | ||
|
||
//#define VIBRATION_MOTOR | ||
//#define DETECT_PEDESTRIANS | ||
|
||
#ifdef VIBRATION_MOTOR | ||
#include "rpi3.h" | ||
#endif | ||
|
||
#define RED Scalar(0, 0, 255) | ||
#define GREEN Scalar(0, 255, 0) | ||
#define BLUE Scalar(255, 0, 0) | ||
|
||
#define VIDEO_RATIO 0.4 | ||
|
||
using namespace std; | ||
using namespace cv; | ||
|
||
int main(int argc, const char** argv) | ||
{ | ||
Mat in, out; | ||
Speed speed, currSpeed; | ||
vector<Vec4i> lanes; | ||
vector<Rect> pedestrians; | ||
LaneDetector* lane_detector; | ||
PedestrianDetector* pedestrian_detector; | ||
TrafficSignDetector* sign_detector; | ||
int curr_frame = 0; | ||
bool is_out_of_lane = false, | ||
is_distance_safe = true, | ||
contains_pedestrian = false; | ||
|
||
// Vibration motor | ||
Mat in, out; | ||
Speed speed, currSpeed; | ||
vector<Rect> pedestrians; | ||
LaneDetector* lane_detector; | ||
PedestrianDetector* pedestrian_detector; | ||
TrafficSignDetector* sign_detector; | ||
SpeedEstimator* speed_estimator; | ||
StoppingDistanceCalculator* distance_calculator; | ||
int curr_frame = 0, | ||
running_mode = atoi(argv[1]); | ||
bool is_distance_safe = true, | ||
contains_pedestrian = false; | ||
|
||
#ifdef VIBRATION_MOTOR | ||
// Vibration motor initialization | ||
hardwareSetup(); | ||
|
||
#endif | ||
|
||
VideoCapture cap(0); | ||
cap >> in; | ||
resize(in, in, Size(), VIDEO_RATIO, VIDEO_RATIO, INTER_AREA); | ||
|
||
lane_detector = new LaneDetector(in, atoi(argv[1])); | ||
resize(in, in, Size(480, 360)); | ||
lane_detector = new LaneDetector(in, running_mode); | ||
sign_detector = new TrafficSignDetector(in); | ||
pedestrian_detector = new PedestrianDetector(); | ||
namedWindow("PROJECT", WINDOW_KEEPRATIO); | ||
speed_estimator = new SpeedEstimator(); | ||
distance_calculator = new StoppingDistanceCalculator(); | ||
|
||
while (true) { | ||
|
||
cap >> in; | ||
resize(in, in, Size(), VIDEO_RATIO, VIDEO_RATIO, INTER_AREA); | ||
resize(in, in, Size(480, 360)); | ||
in.copyTo(out); | ||
|
||
// Speed Estimator | ||
currSpeed = SpeedEstimator::GetSpeed(in, curr_frame++, atoi(argv[1])); | ||
currSpeed = speed_estimator->GetSpeed(in, curr_frame++, running_mode); | ||
if (currSpeed != SPD_CURR) { | ||
speed = currSpeed; | ||
} | ||
switch (speed) | ||
{ | ||
case SPD_SLOW: | ||
putText(out, "SPD: SLOW", Point(40, in.rows - 50), 1, 2, RED, 2); | ||
break; | ||
case SPD_NORMAL: | ||
putText(out, "SPD: NORMAL", Point(40, in.rows - 50), 1, 2, RED, 2); | ||
break; | ||
case SPD_FAST: | ||
putText(out, "SPD: FAST", Point(40, in.rows - 50), 1, 2, RED, 2); | ||
vibrationStateChange(1); | ||
break; | ||
case SPD_SLOW: | ||
putText(out, "SPD: SLOW", | ||
Point(40, in.rows - 50), 1, 2, RED, 2); | ||
break; | ||
case SPD_NORMAL: | ||
putText(out, "SPD: NORMAL", | ||
Point(40, in.rows - 50), 1, 2, RED, 2); | ||
break; | ||
case SPD_FAST: | ||
putText(out, "SPD: FAST", | ||
Point(40, in.rows - 50), 1, 2, RED, 2); | ||
|
||
#ifdef VIBRATION_MOTOR | ||
vibrationStateChange(1); | ||
#endif | ||
break; | ||
} | ||
#if 0 | ||
|
||
#ifdef DETECT_PEDESTRIANS | ||
// Pedestrian Detector | ||
if (currSpeed == SPD_FAST) { | ||
contains_pedestrian = pedestrian_detector->DetectPedestrians(in, out, BLUE, 2); | ||
contains_pedestrian = | ||
pedestrian_detector->DetectPedestrians(in, out, BLUE, 2); | ||
if (contains_pedestrian) { | ||
|
||
#ifdef VIBRATION_MOTOR | ||
vibrationStateChange(1); | ||
#endif | ||
} | ||
} | ||
#endif | ||
|
||
// Lane Detector | ||
lanes = lane_detector->GetLanes(in); | ||
is_out_of_lane = lane_detector->IsOutOfLane(in); | ||
lane_detector->DrawLanes(out, lanes, GREEN, 7); | ||
if (is_out_of_lane) { | ||
vibrationStateChange(1); | ||
} | ||
lane_detector->DrawLanes(out, lane_detector->GetLanes(in), GREEN, 5); | ||
|
||
// Stopping Distance Calculator | ||
if (curr_frame % 20 == 0) { | ||
is_distance_safe = StoppingDistanceCalculator::IsSafe(in); | ||
is_distance_safe = distance_calculator->IsSafe(in); | ||
} | ||
if (!is_distance_safe) { | ||
putText(out, "DIST: NOT SAFE", | ||
Point(in.cols / 2, in.rows - 50), 1, 2, RED, 2); | ||
|
||
#ifdef VIBRATION_MOTOR | ||
vibrationStateChange(1); | ||
#endif | ||
} | ||
|
||
// Traffic Sign Detector | ||
sign_detector->DetectTrafficSigns(in, out, CLR_BLUE, BLUE, 2); | ||
sign_detector->DetectTrafficSigns(in, out, BLUE, 2); | ||
|
||
imshow("PROJECT", out); | ||
imshow("DRIVING ASSISTANT", out); | ||
waitKey(1); | ||
reset(); | ||
} | ||
|
||
#ifdef VIBRATION_MOTOR | ||
//reset(); | ||
#endif | ||
|
||
} // Infinite loop | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,59 @@ | ||
#pragma once | ||
|
||
#include <opencv2/opencv.hpp> | ||
#include <iostream> | ||
|
||
using namespace std; | ||
using namespace cv; | ||
|
||
class LaneDetector | ||
{ | ||
class LaneDetector { | ||
|
||
public: | ||
// Initialize with a sample image | ||
// Mode: 0 -> If the video quality is low | ||
// Mode: 1 -> If the video quality is middle | ||
// Mode: 2 -> If the video quality is high | ||
/* | ||
* Initialize with a sample image | ||
* | ||
* Mode: 0 -> If the video quality is low | ||
* Mode: 1 -> If the video quality is middle | ||
* Mode: 2 -> If the video quality is high | ||
*/ | ||
LaneDetector(Mat image, int mode); | ||
static bool IsOutOfLane(Mat image); | ||
static vector<Vec4i> GetLanes(Mat image); | ||
static void DrawLanes(Mat image, vector<Vec4i> lanes, Scalar color, int thickness); | ||
|
||
/* | ||
* Returns true if the vehicle is out of lane | ||
*/ | ||
bool IsOutOfLane(Mat image); | ||
|
||
vector<Vec4i> GetLanes(Mat image); | ||
void DrawLanes(Mat image, vector<Vec4i> lanes, Scalar color, int thickness); | ||
|
||
private: | ||
// Returns a binary image in which | ||
// 1 corresponds to white area, 0 corresponds to other colors | ||
// apply_mask flag myst be true if we are detecting lanes, false o/w | ||
static Mat GetWhiteMask(Mat image, bool apply_mask); | ||
static Mat mask_for_elim; | ||
/* | ||
* Returns a binary image in which 1 corresponds to white area, 0 corresponds | ||
* to other colors apply_mask flag must be true if we are detecting lanes, | ||
* false o/w | ||
*/ | ||
Mat GetWhiteMask(Mat image, bool apply_mask); | ||
|
||
// Treshold value for checking if the vehice is in the middle of lanes | ||
/* | ||
* Running mode | ||
* | ||
* Mode: 0 -> If the video quality is low | ||
* Mode: 1 -> If the video quality is middle | ||
* Mode: 2 -> If the video quality is high | ||
*/ | ||
int mode_; | ||
|
||
Mat mask_for_elim_; | ||
|
||
/* | ||
* Treshold value for checking if the vehice is in the middle of lanes | ||
* (Same as checking if the vehicle is out of lane) | ||
*/ | ||
const static double kLaneTresh; | ||
|
||
// If the angle of a line is greater than kAngleTresh or less than | ||
// -kAngleTresh, then the line is considered to be vertical | ||
/* | ||
* If the angle of a line is greater than kAngleTresh or less than | ||
* -kAngleTresh, then the line is considered to be vertical (they are | ||
* more likely to denote a lane) | ||
*/ | ||
const static double kAngleTresh; | ||
|
||
static int kMode; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.