Skip to content

Commit

Permalink
fix 1 part of remaining demos
Browse files Browse the repository at this point in the history
  • Loading branch information
ivikhrev committed Mar 31, 2022
1 parent a9bef2a commit 0af5e52
Show file tree
Hide file tree
Showing 53 changed files with 1,701 additions and 1,316 deletions.
66 changes: 38 additions & 28 deletions demos/crossroad_camera_demo/cpp/crossroad_camera_demo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,50 @@

///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <iostream>

#include "gflags/gflags.h"
#include "utils/default_flags.hpp"
#include <gflags/gflags.h>

#include <utils/default_flags.hpp>

DEFINE_INPUT_FLAGS
DEFINE_OUTPUT_FLAGS

static const char help_message[] = "Print a usage message.";
static const char person_vehicle_bike_detection_model_message[] = "Required. Path to the Person/Vehicle/Bike Detection Crossroad model (.xml) file.";
static const char person_attribs_model_message[] = "Optional. Path to the Person Attributes Recognition Crossroad model (.xml) file.";
static const char person_reid_model_message[] = "Optional. Path to the Person Reidentification Retail model (.xml) file.";
static const char target_device_message[] = "Optional. Specify the target device for Person/Vehicle/Bike Detection. "
"The list of available devices is shown below. Default value is CPU. "
"Use \"-d HETERO:<comma-separated_devices_list>\" format to specify HETERO plugin. "
"The application looks for a suitable plugin for the specified device.";
static const char target_device_message_person_attribs[] = "Optional. Specify the target device for Person Attributes Recognition. "
"The list of available devices is shown below. Default value is CPU. "
"Use \"-d HETERO:<comma-separated_devices_list>\" format to specify HETERO plugin. "
"The application looks for a suitable plugin for the specified device.";
static const char target_device_message_person_reid[] = "Optional. Specify the target device for Person Reidentification Retail. "
"The list of available devices is shown below. Default value is CPU. "
"Use \"-d HETERO:<comma-separated_devices_list>\" format to specify HETERO plugin. "
"The application looks for a suitable plugin for the specified device.";
static const char threshold_output_message[] = "Optional. Probability threshold for person/vehicle/bike crossroad detections.";
static const char threshold_output_message_person_reid[] = "Optional. Cosine similarity threshold between two vectors for person reidentification.";
static const char person_vehicle_bike_detection_model_message[] =
"Required. Path to the Person/Vehicle/Bike Detection Crossroad model (.xml) file.";
static const char person_attribs_model_message[] =
"Optional. Path to the Person Attributes Recognition Crossroad model (.xml) file.";
static const char person_reid_model_message[] =
"Optional. Path to the Person Reidentification Retail model (.xml) file.";
static const char target_device_message[] =
"Optional. Specify the target device for Person/Vehicle/Bike Detection. "
"The list of available devices is shown below. Default value is CPU. "
"Use \"-d HETERO:<comma-separated_devices_list>\" format to specify HETERO plugin. "
"The application looks for a suitable plugin for the specified device.";
static const char target_device_message_person_attribs[] =
"Optional. Specify the target device for Person Attributes Recognition. "
"The list of available devices is shown below. Default value is CPU. "
"Use \"-d HETERO:<comma-separated_devices_list>\" format to specify HETERO plugin. "
"The application looks for a suitable plugin for the specified device.";
static const char target_device_message_person_reid[] =
"Optional. Specify the target device for Person Reidentification Retail. "
"The list of available devices is shown below. Default value is CPU. "
"Use \"-d HETERO:<comma-separated_devices_list>\" format to specify HETERO plugin. "
"The application looks for a suitable plugin for the specified device.";
static const char threshold_output_message[] =
"Optional. Probability threshold for person/vehicle/bike crossroad detections.";
static const char threshold_output_message_person_reid[] =
"Optional. Cosine similarity threshold between two vectors for person reidentification.";
static const char raw_output_message[] = "Optional. Output Inference results as raw values.";
static const char no_show_message[] = "Optional. Don't show output.";
static const char input_resizable_message[] = "Optional. Enables resizable input with support of ROI crop & auto resize.";
static const char input_resizable_message[] =
"Optional. Enables resizable input with support of ROI crop & auto resize.";
static const char utilization_monitors_message[] = "Optional. List of monitors to show initially.";
static const char person_label_message[] = "Optional. The integer index of the objects' category corresponding to persons "
"(as it is returned from the detection network, may vary from one network to another). "
"The default value is 1.";

static const char person_label_message[] =
"Optional. The integer index of the objects' category corresponding to persons "
"(as it is returned from the detection network, may vary from one network to another). "
"The default value is 1.";

DEFINE_bool(h, false, help_message);
DEFINE_string(m, "", person_vehicle_bike_detection_model_message);
Expand All @@ -53,10 +64,9 @@ DEFINE_bool(auto_resize, false, input_resizable_message);
DEFINE_string(u, "", utilization_monitors_message);
DEFINE_int32(person_label, 1, person_label_message);


/**
* @brief This function show a help message
*/
* @brief This function show a help message
*/
static void showUsage() {
std::cout << std::endl;
std::cout << "crossroad_camera_demo [OPTION]" << std::endl;
Expand All @@ -67,7 +77,7 @@ static void showUsage() {
std::cout << " -loop " << loop_message << std::endl;
std::cout << " -o \"<path>\" " << output_message << std::endl;
std::cout << " -limit \"<num>\" " << limit_message << std::endl;
std::cout << " -m \"<path>\" " << person_vehicle_bike_detection_model_message<< std::endl;
std::cout << " -m \"<path>\" " << person_vehicle_bike_detection_model_message << std::endl;
std::cout << " -m_pa \"<path>\" " << person_attribs_model_message << std::endl;
std::cout << " -m_reid \"<path>\" " << person_reid_model_message << std::endl;
std::cout << " -d \"<device>\" " << target_device_message << std::endl;
Expand Down
23 changes: 16 additions & 7 deletions demos/crossroad_camera_demo/cpp/detection_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
// SPDX-License-Identifier: Apache-2.0
//

#include <memory>
#include <string>
#include "openvino/openvino.hpp"
#include "utils/slog.hpp"

#include <opencv2/core/types.hpp>
#include <openvino/openvino.hpp>

#include <utils/ocv_common.hpp>
#include <utils/slog.hpp>

#pragma once

Expand All @@ -17,8 +22,9 @@ struct BaseDetection {
std::string m_inputName;
std::string m_outputName;

BaseDetection(std::string& commandLineFlag, const std::string& detectorName) :
m_commandLineFlag(commandLineFlag), m_detectorName(detectorName) {}
BaseDetection(std::string& commandLineFlag, const std::string& detectorName)
: m_commandLineFlag(commandLineFlag),
m_detectorName(detectorName) {}

ov::CompiledModel* operator->() {
return &m_compiled_model;
Expand Down Expand Up @@ -53,7 +59,7 @@ struct BaseDetection {
}

virtual void wait() {
if (!enabled()|| !m_infer_request)
if (!enabled() || !m_infer_request)
return;

m_infer_request.wait();
Expand All @@ -62,7 +68,7 @@ struct BaseDetection {
mutable bool m_enablingChecked = false;
mutable bool m_enabled = false;

bool enabled() const {
bool enabled() const {
if (!m_enablingChecked) {
m_enabled = !m_commandLineFlag.empty();
if (!m_enabled) {
Expand All @@ -81,7 +87,10 @@ struct Load {
void into(ov::Core& core, const std::string& deviceName) const {
if (m_detector.enabled()) {
m_detector.m_compiled_model = core.compile_model(m_detector.read(core), deviceName);
logCompiledModelInfo(m_detector.m_compiled_model, m_detector.m_commandLineFlag, deviceName, m_detector.m_detectorName);
logCompiledModelInfo(m_detector.m_compiled_model,
m_detector.m_commandLineFlag,
deviceName,
m_detector.m_detectorName);
}
}
};
46 changes: 24 additions & 22 deletions demos/crossroad_camera_demo/cpp/detection_person.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
// SPDX-License-Identifier: Apache-2.0
//

#include <memory>
#include <string>
#include <vector>

#include "openvino/openvino.hpp"
#include <gflags/gflags.h>
#include <openvino/openvino.hpp>

#include <utils/slog.hpp>

#include "gflags/gflags.h"
#include "utils/slog.hpp"
#include "detection_base.hpp"
#include "crossroad_camera_demo.hpp"
#include "detection_base.hpp"

struct PersonDetection : BaseDetection {
size_t maxProposalCount;
Expand Down Expand Up @@ -86,25 +89,25 @@ struct PersonDetection : BaseDetection {
throw std::logic_error("Incorrect output dimensions for SSD");
}

const ov::Layout tensor_layout{ "NHWC" };
const ov::Layout tensor_layout{"NHWC"};

ov::preprocess::PrePostProcessor ppp = ov::preprocess::PrePostProcessor(model);

if (FLAGS_auto_resize) {
ppp.input().tensor().
set_element_type(ov::element::u8).
set_spatial_dynamic_shape().
set_layout(tensor_layout);
ppp.input().preprocess().
convert_element_type(ov::element::f32).
convert_layout("NCHW").
resize(ov::preprocess::ResizeAlgorithm::RESIZE_LINEAR);
ppp.input()
.tensor()
.set_element_type(ov::element::u8)
.set_spatial_dynamic_shape()
.set_layout(tensor_layout);
ppp.input()
.preprocess()
.convert_element_type(ov::element::f32)
.convert_layout("NCHW")
.resize(ov::preprocess::ResizeAlgorithm::RESIZE_LINEAR);
ppp.input().model().set_layout("NCHW");
ppp.output().tensor().set_element_type(ov::element::f32);
} else {
ppp.input().tensor().
set_element_type(ov::element::u8).
set_layout({ "NCHW" });
ppp.input().tensor().set_element_type(ov::element::u8).set_layout({"NCHW"});
}

model = ppp.build();
Expand All @@ -125,7 +128,7 @@ struct PersonDetection : BaseDetection {
const float* detections = m_infer_request.get_output_tensor().data<float>();
// pretty much regular SSD post-processing
for (size_t i = 0; i < maxProposalCount; i++) {
float image_id = detections[i * objectSize + 0]; // in case of batch
float image_id = detections[i * objectSize + 0]; // in case of batch
if (image_id < 0) {
// end of detections
break;
Expand All @@ -145,11 +148,10 @@ struct PersonDetection : BaseDetection {
}

if (FLAGS_r) {
slog::debug <<
"[" << i << "," << r.label << "] element, prob = " << r.confidence <<
" (" << r.location.x << "," << r.location.y <<
")-(" << r.location.width << "," << r.location.height << ")" <<
((r.confidence > FLAGS_t) ? " WILL BE RENDERED!" : "") << slog::endl;
slog::debug << "[" << i << "," << r.label << "] element, prob = " << r.confidence << " ("
<< r.location.x << "," << r.location.y << ")-(" << r.location.width << ","
<< r.location.height << ")" << ((r.confidence > FLAGS_t) ? " WILL BE RENDERED!" : "")
<< slog::endl;
}
results.push_back(r);
}
Expand Down
80 changes: 46 additions & 34 deletions demos/crossroad_camera_demo/cpp/detection_person_attr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
// SPDX-License-Identifier: Apache-2.0
//

#include <algorithm>
#include <memory>
#include <string>
#include <vector>

#include "openvino/openvino.hpp"
#include <gflags/gflags.h>
#include <openvino/openvino.hpp>

#include <utils/slog.hpp>

#include "gflags/gflags.h"
#include "utils/slog.hpp"
#include "detection_base.hpp"

struct PersonAttribsDetection : BaseDetection {
Expand All @@ -16,7 +20,6 @@ struct PersonAttribsDetection : BaseDetection {
std::string outputNameForBottomColorPoint;
bool hasTopBottomColor;


PersonAttribsDetection() : BaseDetection(FLAGS_m_pa, "Person Attributes Recognition"), hasTopBottomColor(false) {}

struct AttributesAndColorPoints {
Expand All @@ -37,8 +40,13 @@ struct PersonAttribsDetection : BaseDetection {
image.convertTo(image32f, CV_32F);
image32f = image32f.reshape(1, image32f.rows * image32f.cols);
clusterCount = std::min(clusterCount, image32f.rows);
cv::kmeans(image32f, clusterCount, labels, cv::TermCriteria(cv::TermCriteria::EPS+cv::TermCriteria::MAX_ITER, 10, 1.0),
10, cv::KMEANS_RANDOM_CENTERS, centers);
cv::kmeans(image32f,
clusterCount,
labels,
cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::MAX_ITER, 10, 1.0),
10,
cv::KMEANS_RANDOM_CENTERS,
centers);
centers.convertTo(centers, CV_8U);
centers = centers.reshape(3, clusterCount);
std::vector<int> freq(clusterCount);
Expand All @@ -53,12 +61,16 @@ struct PersonAttribsDetection : BaseDetection {
}

AttributesAndColorPoints GetPersonAttributes() {
static const char* const attributeStringsFor7Attributes[] = {
"is male", "has_bag", "has hat", "has longsleeves", "has longpants", "has longhair", "has coat_jacket"
};
static const char* const attributeStringsFor8Attributes[] = {
"is male", "has_bag", "has_backpack" , "has hat", "has longsleeves", "has longpants", "has longhair", "has coat_jacket"
};
static const char* const attributeStringsFor7Attributes[] =
{"is male", "has_bag", "has hat", "has longsleeves", "has longpants", "has longhair", "has coat_jacket"};
static const char* const attributeStringsFor8Attributes[] = {"is male",
"has_bag",
"has_backpack",
"has hat",
"has longsleeves",
"has longpants",
"has longhair",
"has coat_jacket"};

ov::Tensor attribsTensor = m_infer_request.get_tensor(outputNameForAttributes);
size_t numOfAttrChannels = attribsTensor.get_shape()[1];
Expand All @@ -69,12 +81,12 @@ struct PersonAttribsDetection : BaseDetection {
} else if (numOfAttrChannels == arraySize(attributeStringsFor8Attributes)) {
attributeStrings = attributeStringsFor8Attributes;
} else {
throw std::logic_error("Output size (" + std::to_string(numOfAttrChannels) + ") of the "
throw std::logic_error("Output size (" + std::to_string(numOfAttrChannels) +
") of the "
"Person Attributes Recognition network is not equal to expected "
"number of attributes ("
+ std::to_string(arraySize(attributeStringsFor7Attributes))
+ " or "
+ std::to_string(arraySize(attributeStringsFor7Attributes)) + ")");
"number of attributes (" +
std::to_string(arraySize(attributeStringsFor7Attributes)) + " or " +
std::to_string(arraySize(attributeStringsFor7Attributes)) + ")");
}

AttributesAndColorPoints returnValue;
Expand All @@ -92,11 +104,13 @@ struct PersonAttribsDetection : BaseDetection {
size_t numOfTCPointChannels = topColorPointTensor.get_shape()[1];
size_t numOfBCPointChannels = bottomColorPointTensor.get_shape()[1];
if (numOfTCPointChannels != 2) {
throw std::logic_error("Output size (" + std::to_string(numOfTCPointChannels) + ") of the "
throw std::logic_error("Output size (" + std::to_string(numOfTCPointChannels) +
") of the "
"Person Attributes Recognition network is not equal to point coordinates(2)");
}
if (numOfBCPointChannels != 2) {
throw std::logic_error("Output size (" + std::to_string(numOfBCPointChannels) + ") of the "
throw std::logic_error("Output size (" + std::to_string(numOfBCPointChannels) +
") of the "
"Person Attributes Recognition network is not equal to point coordinates (2)");
}

Expand All @@ -118,8 +132,9 @@ struct PersonAttribsDetection : BaseDetection {
}

bool CheckOutputNameExist(const ov::OutputVector& outputs, const std::string name) {
if (std::find_if(outputs.begin(), outputs.end(),
[&](const ov::Output<ov::Node>& output) {return output.get_any_name() == name; }) == outputs.end()) {
if (std::find_if(outputs.begin(), outputs.end(), [&](const ov::Output<ov::Node>& output) {
return output.get_any_name() == name;
}) == outputs.end()) {
return false;
}
return true;
Expand All @@ -144,19 +159,15 @@ struct PersonAttribsDetection : BaseDetection {
ov::preprocess::PrePostProcessor ppp = ov::preprocess::PrePostProcessor(model);

if (FLAGS_auto_resize) {
ppp.input().tensor().
set_element_type(ov::element::u8).
set_spatial_dynamic_shape().
set_layout({ "NHWC" });
ppp.input().preprocess().
convert_element_type(ov::element::f32).
convert_layout("NCHW").
resize(ov::preprocess::ResizeAlgorithm::RESIZE_LINEAR);
ppp.input().tensor().set_element_type(ov::element::u8).set_spatial_dynamic_shape().set_layout({"NHWC"});
ppp.input()
.preprocess()
.convert_element_type(ov::element::f32)
.convert_layout("NCHW")
.resize(ov::preprocess::ResizeAlgorithm::RESIZE_LINEAR);
ppp.input().model().set_layout("NCHW");
} else {
ppp.input().tensor().
set_element_type(ov::element::u8).
set_layout({ "NCHW" });
ppp.input().tensor().set_element_type(ov::element::u8).set_layout({"NCHW"});
}

model = ppp.build();
Expand Down Expand Up @@ -189,8 +200,9 @@ struct PersonAttribsDetection : BaseDetection {
}
hasTopBottomColor = true;
} else {
throw std::logic_error("Person Attribs Network expects either a network having one output (person attributes), "
"or a network having three outputs (person attributes, top color point, bottom color point)");
throw std::logic_error(
"Person Attribs Network expects either a network having one output (person attributes), "
"or a network having three outputs (person attributes, top color point, bottom color point)");
}

m_enabled = true;
Expand Down
Loading

0 comments on commit 0af5e52

Please sign in to comment.