Skip to content

Commit a53ac64

Browse files
authored
Explicit rotated_rect, simplify example (#134)
1 parent 8e57d31 commit a53ac64

File tree

3 files changed

+29
-36
lines changed

3 files changed

+29
-36
lines changed

examples/cpp/synchronous_api/main.cpp

+23-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
// Copyright (C) 2018-2022 Intel Corporation
2+
// Copyright (C) 2018-2023 Intel Corporation
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -32,41 +32,32 @@
3232
#include <models/input_data.h>
3333
#include <models/results.h>
3434

35+
int main(int argc, char* argv[]) try {
36+
if (argc != 3) {
37+
throw std::runtime_error(std::string{"Usage: "} + argv[0] + " <path_to_model> <path_to_image>");
38+
}
3539

36-
int main(int argc, char* argv[]) {
37-
try {
38-
if (argc != 3) {
39-
std::cerr << "Usage : " << argv[0] << " <path_to_model> <path_to_image>"
40-
<< std::endl;
41-
return EXIT_FAILURE;
42-
}
43-
44-
cv::Mat image = cv::imread(argv[2]);
45-
if (!image.data) {
46-
throw std::runtime_error{"Failed to read the image"};
47-
}
40+
cv::Mat image = cv::imread(argv[2]);
41+
if (!image.data) {
42+
throw std::runtime_error{"Failed to read the image"};
43+
}
4844

49-
// Instantiate Object Detection model
50-
auto model = DetectionModel::create_model(argv[1]); // works with SSD models. Download it using Python Model API
45+
// Instantiate Object Detection model
46+
auto model = DetectionModel::create_model(argv[1]); // works with SSD models. Download it using Python Model API
5147

52-
// Run the inference
53-
auto result = model->infer(image);
48+
// Run the inference
49+
auto result = model->infer(image);
5450

55-
// Process detections
56-
for (auto& obj : result->objects) {
51+
// Process detections
52+
for (auto& obj : result->objects) {
5753
std::cout << " " << std::left << std::setw(9) << obj.label << " | " << std::setw(10) << obj.confidence
58-
<< " | " << std::setw(4) << int(obj.x) << " | " << std::setw(4) << int(obj.y) << " | "
59-
<< std::setw(4) << int(obj.x + obj.width) << " | " << std::setw(4) << int(obj.y + obj.height)
60-
<< std::endl;
61-
}
62-
63-
} catch (const std::exception& error) {
64-
std::cerr << error.what() << std::endl;
65-
return 1;
66-
} catch (...) {
67-
std::cerr << "Unknown/internal exception happened." << std::endl;
68-
return 1;
54+
<< " | " << std::setw(4) << int(obj.x) << " | " << std::setw(4) << int(obj.y) << " | "
55+
<< std::setw(4) << int(obj.x + obj.width) << " | " << std::setw(4) << int(obj.y + obj.height) << "\n";
6956
}
70-
71-
return 0;
57+
} catch (const std::exception& error) {
58+
std::cerr << error.what() << '\n';
59+
return 1;
60+
} catch (...) {
61+
std::cerr << "Non-exception object thrown\n";
62+
return 1;
7263
}

model_api/cpp/models/src/image_model.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ std::shared_ptr<InternalModelData> ImageModel::preprocess(const InputData& input
261261
auto img = inputTransform(origImg);
262262

263263
if (!useAutoResize && !embedded_processing) {
264-
// /* Resize and copy data from the image to the input tensor */
264+
// Resize and copy data from the image to the input tensor
265265
auto tensorShape = inferenceAdapter->getInputShape(inputNames[0]).get_max_shape(); // first input should be image
266266
const ov::Layout layout("NHWC");
267267
const size_t width = tensorShape[ov::layout::width_idx(layout)];

model_api/python/openvino/model_api/models/utils.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def __str__(self):
102102

103103

104104
class SegmentedObjectWithRects(SegmentedObject):
105-
def __init__(self, segmented_object):
105+
def __init__(self, segmented_object, rotated_rect):
106106
super().__init__(
107107
segmented_object.xmin,
108108
segmented_object.ymin,
@@ -113,6 +113,7 @@ def __init__(self, segmented_object):
113113
segmented_object.str_label,
114114
segmented_object.mask,
115115
)
116+
self.rotated_rect = rotated_rect
116117

117118
def __str__(self):
118119
res = super().__str__()
@@ -139,14 +140,15 @@ def __str__(self):
139140
def add_rotated_rects(segmented_objects):
140141
objects_with_rects = []
141142
for segmented_object in segmented_objects:
142-
objects_with_rects.append(SegmentedObjectWithRects(segmented_object))
143143
mask = segmented_object.mask.astype(np.uint8)
144144
contours, hierarchies = cv2.findContours(
145145
mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
146146
)
147147

148148
contour = np.vstack(contours)
149-
objects_with_rects[-1].rotated_rect = cv2.minAreaRect(contour)
149+
objects_with_rects.append(
150+
SegmentedObjectWithRects(segmented_object, cv2.minAreaRect(contour))
151+
)
150152
return objects_with_rects
151153

152154

0 commit comments

Comments
 (0)