Skip to content

Commit

Permalink
Deep learning with OpenCV DNN module blog
Browse files Browse the repository at this point in the history
  • Loading branch information
sovit-123 committed Apr 9, 2021
1 parent e4af2fc commit 772d90a
Show file tree
Hide file tree
Showing 21 changed files with 11,049 additions and 0 deletions.
89 changes: 89 additions & 0 deletions Deep-Learning-with-OpenCV-DNN-Module/README.md
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.

[![img](https://camo.githubusercontent.com/18c5719ef10afe9607af3e87e990068c942ae4cba8bd4d72d21950d6213ea97e/68747470733a2f2f7777772e6c6561726e6f70656e63762e636f6d2f77702d636f6e74656e742f75706c6f6164732f323032302f30342f41492d436f75727365732d42792d4f70656e43562d4769746875622e706e67)](https://opencv.org/courses/)
21 changes: 21 additions & 0 deletions Deep-Learning-with-OpenCV-DNN-Module/cpp/classify/CMakeLists.txt
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 Deep-Learning-with-OpenCV-DNN-Module/cpp/classify/classify.cpp
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);
}
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})
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();

}
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})
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.
Loading

0 comments on commit 772d90a

Please sign in to comment.