forked from spmallick/learnopencv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deep learning with OpenCV DNN module blog
- Loading branch information
Showing
21 changed files
with
11,049 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Deep Learning with OpenCV's DNN Module | ||
|
||
|
||
|
||
## Directory Structure | ||
|
||
**All the code files and folders follow the following structure.** | ||
|
||
``` | ||
├── cpp | ||
│ ├── classify | ||
│ │ ├── classify.cpp | ||
│ │ └── CMakeLists.txt | ||
│ └── detection | ||
│ ├── detect_img | ||
│ │ ├── CMakeLists.txt | ||
│ │ └── detect_img.cpp | ||
│ └── detect_vid | ||
│ ├── CMakeLists.txt | ||
│ └── detect_vid.cpp | ||
├── input | ||
│ ├── classification_classes_ILSVRC2012.txt | ||
│ ├── DenseNet_121.caffemodel | ||
│ ├── DenseNet_121.prototxt | ||
│ ├── frozen_inference_graph.pb | ||
│ ├── image_1.jpg | ||
│ ├── image_2.jpg | ||
│ ├── object_detection_classes_coco.txt | ||
│ ├── ssd_mobilenet_v2_coco_2018_03_29.pbtxt.txt | ||
│ └── video_1.mp4 | ||
├── python | ||
│ ├── classification | ||
│ │ ├── classify.py | ||
│ │ └── README.md | ||
│ └── detection | ||
│ ├── detect_img.py | ||
│ ├── detect_vid.py | ||
│ ├── image_result.jpg | ||
│ ├── README.md | ||
│ └── video_result.mp4 | ||
└── README.md | ||
``` | ||
|
||
|
||
|
||
## Instructions | ||
|
||
### Python | ||
|
||
To run the code in Python, please go into the `python` folder and execute the Python scripts in each of the respective sub-folders. | ||
|
||
### C++ | ||
|
||
To run the code in C++, please go into the `cpp` folder, then go into each of the respective sub-folders and follow the steps below: | ||
|
||
``` | ||
mkdir build | ||
cd build | ||
cmake .. | ||
cmake --build . --config Release | ||
cd .. | ||
./build/classify | ||
``` | ||
|
||
``` | ||
mkdir build | ||
cd build | ||
cmake .. | ||
cmake --build . --config Release | ||
cd .. | ||
./build/detect_img | ||
``` | ||
|
||
``` | ||
mkdir build | ||
cd build | ||
cmake .. | ||
cmake --build . --config Release | ||
cd .. | ||
./build/detect_vid | ||
``` | ||
|
||
|
||
|
||
# AI Courses by OpenCV | ||
|
||
Want to become an expert in AI? [AI Courses by OpenCV](https://opencv.org/courses/) is a great place to start. | ||
|
||
[](https://opencv.org/courses/) |
21 changes: 21 additions & 0 deletions
21
Deep-Learning-with-OpenCV-DNN-Module/cpp/classify/CMakeLists.txt
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
cmake_minimum_required(VERSION 3.0.0) | ||
project(classify VERSION 0.1.0) | ||
|
||
include(CTest) | ||
enable_testing() | ||
|
||
|
||
find_package(OpenCV REQUIRED) | ||
find_package(Threads REQUIRED) | ||
|
||
add_executable(classify classify.cpp) | ||
|
||
set(CPACK_PROJECT_NAME ${PROJECT_NAME}) | ||
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) | ||
include(CPack) | ||
|
||
set(CMAKE_CXX_STANDARD 11) | ||
set(CMAKE_CXX__STANDARD_REQUIRED ON) | ||
|
||
include_directories(${OpenCV_INCLUDE_DIRS}) | ||
target_link_libraries(classify ${OpenCV_LIBS} ${CMAKE_THREAD_LIBS_INIT}) |
48 changes: 48 additions & 0 deletions
48
Deep-Learning-with-OpenCV-DNN-Module/cpp/classify/classify.cpp
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <opencv2/opencv.hpp> | ||
#include <opencv2/dnn.hpp> | ||
#include <opencv2/dnn/all_layers.hpp> | ||
|
||
using namespace std; | ||
using namespace cv; | ||
using namespace dnn; | ||
|
||
int main(int, char**) { | ||
std::vector<std::string> class_names; | ||
ifstream ifs(string("../../input/classification_classes_ILSVRC2012.txt").c_str()); | ||
string line; | ||
while (getline(ifs, line)) | ||
{ | ||
class_names.push_back(line); | ||
} | ||
|
||
// load the neural network model | ||
auto model = readNet("../../input/DenseNet_121.prototxt", | ||
"../../input/DenseNet_121.caffemodel", | ||
"Caffe"); | ||
|
||
// load the image from disk | ||
Mat image = imread("../../input/image_1.jpg"); | ||
// create blob from image | ||
Mat blob = blobFromImage(image, 0.01, Size(224, 224), Scalar(104, 117, 123)); | ||
|
||
// set the input blob for the neural network | ||
model.setInput(blob); | ||
// forward pass the image blob through the model | ||
Mat outputs = model.forward(); | ||
|
||
Point classIdPoint; | ||
double final_prob; | ||
minMaxLoc(outputs.reshape(1, 1), 0, &final_prob, 0, &classIdPoint); | ||
int label_id = classIdPoint.x; | ||
|
||
// Print predicted class. | ||
string out_text = format("%s, %.3f", (class_names[label_id].c_str()), final_prob); | ||
// put the class name text on top of the image | ||
putText(image, out_text, Point(25, 50), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), | ||
2); | ||
|
||
imshow("Image", image); | ||
imwrite("result_image.jpg", image); | ||
} |
21 changes: 21 additions & 0 deletions
21
Deep-Learning-with-OpenCV-DNN-Module/cpp/detection/detect_img/CMakeLists.txt
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
cmake_minimum_required(VERSION 3.0.0) | ||
project(detect_img VERSION 0.1.0) | ||
|
||
include(CTest) | ||
enable_testing() | ||
|
||
|
||
find_package(OpenCV REQUIRED) | ||
find_package(Threads REQUIRED) | ||
|
||
add_executable(detect_img detect_img.cpp) | ||
|
||
set(CPACK_PROJECT_NAME ${PROJECT_NAME}) | ||
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) | ||
include(CPack) | ||
|
||
set(CMAKE_CXX_STANDARD 11) | ||
set(CMAKE_CXX__STANDARD_REQUIRED ON) | ||
|
||
include_directories(${OpenCV_INCLUDE_DIRS}) | ||
target_link_libraries(detect_img ${OpenCV_LIBS} ${CMAKE_THREAD_LIBS_INIT}) |
60 changes: 60 additions & 0 deletions
60
Deep-Learning-with-OpenCV-DNN-Module/cpp/detection/detect_img/detect_img.cpp
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <opencv2/opencv.hpp> | ||
#include <opencv2/dnn.hpp> | ||
#include <opencv2/dnn/all_layers.hpp> | ||
|
||
using namespace std; | ||
using namespace cv; | ||
using namespace dnn; | ||
|
||
|
||
int main(int, char**) { | ||
std::vector<std::string> class_names; | ||
ifstream ifs(string("../../../input/object_detection_classes_coco.txt").c_str()); | ||
string line; | ||
while (getline(ifs, line)) | ||
{ | ||
class_names.push_back(line); | ||
} | ||
|
||
// load the neural network model | ||
auto model = readNet("../../../input/frozen_inference_graph.pb", | ||
"../../../input/ssd_mobilenet_v2_coco_2018_03_29.pbtxt.txt", | ||
"TensorFlow"); | ||
|
||
// read the image from disk | ||
Mat image = imread("../../../input/image_2.jpg"); | ||
int image_height = image.cols; | ||
int image_width = image.rows; | ||
//create blob from image | ||
Mat blob = blobFromImage(image, 1.0, Size(300, 300), Scalar(127.5, 127.5, 127.5), | ||
true, false); | ||
//create blob from image | ||
model.setInput(blob); | ||
//forward pass through the model to carry out the detection | ||
Mat output = model.forward(); | ||
|
||
Mat detectionMat(output.size[2], output.size[3], CV_32F, output.ptr<float>()); | ||
|
||
for (int i = 0; i < detectionMat.rows; i++){ | ||
int class_id = detectionMat.at<float>(i, 1); | ||
float confidence = detectionMat.at<float>(i, 2); | ||
|
||
// Check if the detection is of good quality | ||
if (confidence > 0.4){ | ||
int box_x = static_cast<int>(detectionMat.at<float>(i, 3) * image.cols); | ||
int box_y = static_cast<int>(detectionMat.at<float>(i, 4) * image.rows); | ||
int box_width = static_cast<int>(detectionMat.at<float>(i, 5) * image.cols - box_x); | ||
int box_height = static_cast<int>(detectionMat.at<float>(i, 6) * image.rows - box_y); | ||
rectangle(image, Point(box_x, box_y), Point(box_x+box_width, box_y+box_height), Scalar(255,255,255), 2); | ||
putText(image, class_names[class_id-1].c_str(), Point(box_x, box_y-5), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,255,255), 1); | ||
} | ||
} | ||
|
||
imshow("image", image); | ||
imwrite("image_result.jpg", image); | ||
waitKey(0); | ||
destroyAllWindows(); | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
Deep-Learning-with-OpenCV-DNN-Module/cpp/detection/detect_vid/CMakeLists.txt
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
cmake_minimum_required(VERSION 3.0.0) | ||
project(detect_vid VERSION 0.1.0) | ||
|
||
include(CTest) | ||
enable_testing() | ||
|
||
|
||
find_package(OpenCV REQUIRED) | ||
find_package(Threads REQUIRED) | ||
|
||
add_executable(detect_vid detect_vid.cpp) | ||
|
||
set(CPACK_PROJECT_NAME ${PROJECT_NAME}) | ||
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) | ||
include(CPack) | ||
|
||
set(CMAKE_CXX_STANDARD 11) | ||
set(CMAKE_CXX__STANDARD_REQUIRED ON) | ||
|
||
include_directories(${OpenCV_INCLUDE_DIRS}) | ||
target_link_libraries(detect_vid ${OpenCV_LIBS} ${CMAKE_THREAD_LIBS_INIT}) |
76 changes: 76 additions & 0 deletions
76
Deep-Learning-with-OpenCV-DNN-Module/cpp/detection/detect_vid/detect_vid.cpp
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <opencv2/opencv.hpp> | ||
#include <opencv2/dnn.hpp> | ||
#include <opencv2/dnn/all_layers.hpp> | ||
|
||
using namespace std; | ||
using namespace cv; | ||
using namespace dnn; | ||
|
||
|
||
int main(int, char**) { | ||
std::vector<std::string> class_names; | ||
ifstream ifs(string("../../../input/object_detection_classes_coco.txt").c_str()); | ||
string line; | ||
while (getline(ifs, line)) | ||
{ | ||
class_names.push_back(line); | ||
} | ||
|
||
// load the neural network model | ||
auto model = readNet("../../../input/frozen_inference_graph.pb", | ||
"../../../input/ssd_mobilenet_v2_coco_2018_03_29.pbtxt.txt", | ||
"TensorFlow"); | ||
|
||
// capture the video | ||
VideoCapture cap("../../../input/video_1.mp4"); | ||
// get the video frames' width and height for proper saving of videos | ||
int frame_width = static_cast<int>(cap.get(3)); | ||
int frame_height = static_cast<int>(cap.get(4)); | ||
// create the `VideoWriter()` object | ||
VideoWriter out("video_result.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'), 30, | ||
Size(frame_width, frame_height)); | ||
|
||
while (cap.isOpened()) { | ||
Mat image; | ||
bool isSuccess = cap.read(image); | ||
|
||
int image_height = image.cols; | ||
int image_width = image.rows; | ||
//create blob from image | ||
Mat blob = blobFromImage(image, 1.0, Size(300, 300), Scalar(127.5, 127.5, 127.5), | ||
true, false); | ||
//create blob from image | ||
model.setInput(blob); | ||
//forward pass through the model to carry out the detection | ||
Mat output = model.forward(); | ||
|
||
Mat detectionMat(output.size[2], output.size[3], CV_32F, output.ptr<float>()); | ||
|
||
for (int i = 0; i < detectionMat.rows; i++){ | ||
int class_id = detectionMat.at<float>(i, 1); | ||
float confidence = detectionMat.at<float>(i, 2); | ||
|
||
// Check if the detection is of good quality | ||
if (confidence > 0.4){ | ||
int box_x = static_cast<int>(detectionMat.at<float>(i, 3) * image.cols); | ||
int box_y = static_cast<int>(detectionMat.at<float>(i, 4) * image.rows); | ||
int box_width = static_cast<int>(detectionMat.at<float>(i, 5) * image.cols - box_x); | ||
int box_height = static_cast<int>(detectionMat.at<float>(i, 6) * image.rows - box_y); | ||
rectangle(image, Point(box_x, box_y), Point(box_x+box_width, box_y+box_height), Scalar(255,255,255), 2); | ||
putText(image, class_names[class_id-1].c_str(), Point(box_x, box_y-5), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,255,255), 1); | ||
} | ||
} | ||
|
||
// imshow("image", image); | ||
out.write(image); | ||
int k = waitKey(10); | ||
if (k == 113){ | ||
break; | ||
} | ||
} | ||
|
||
cap.release(); | ||
destroyAllWindows(); | ||
} |
Binary file not shown.
Oops, something went wrong.