Skip to content

Commit

Permalink
~
Browse files Browse the repository at this point in the history
  • Loading branch information
pabssen1 committed Jul 8, 2024
1 parent b90e2b1 commit c9e5a29
Show file tree
Hide file tree
Showing 50 changed files with 1,403 additions and 13,513 deletions.
12 changes: 12 additions & 0 deletions .eclipse/.bazelproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

# The project view file (.bazelproject) is used to import targets into the IDE.
#
# See: https://ij.bazel.build/docs/project-views.html
#
# This files provides a default experience for developers working with the project.
# You should customize it to suite your needs.

directories:
. # import everything (remove the dot if this is too much)

derive_targets_from_directories: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>mediapipe.graphs.object_detection_3d.obj_parser:obj_parser_lib</name>
<comment>Bazel project representing &apos;//mediapipe/graphs/object_detection_3d/obj_parser:obj_parser_lib&apos;</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.salesforce.bazel.eclipse.core.bazelprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>com.salesforce.bazel.eclipse.core.bazelnature</nature>
</natures>
<filteredResources>
<filter>
<id>1719489568429</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
line.separator=\n
34 changes: 34 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,39 @@
"source_location": "cpp",
"future": "cpp",
"__nullptr": "cpp"
},
"java.compile.nullAnalysis.mode": "automatic",
"java.completion.favoriteStaticMembers": [
"android.*",
"com.squareup.okhttp3.*",
"org.junit.Assert.*",
"org.junit.Assume.*",
"org.junit.jupiter.api.Assertions.*",
"org.junit.jupiter.api.Assumptions.*",
"org.junit.jupiter.api.DynamicContainer.*",
"org.junit.jupiter.api.DynamicTest.*",
"org.mockito.Mockito.*",
"org.mockito.ArgumentMatchers.*",
"org.mockito.Answers.*"
],
"bazel.projectview.open": false,
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/Thumbs.db": true,
".eclipse": false,
".git": false,
".github": false,
".vscode": false,
"docs": false,
"mediapipe": false,
"third_party": false,
"bazel-bin": false,
"bazel-mediapipe": false,
"bazel-out": false,
"bazel-testlogs": false
}
}
9 changes: 6 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
workspace(name = "mediapipe")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
android_sdk_repository(name = "androidsdk", path = "/root/Android/Sdk")
android_ndk_repository(name = "androidndk", api_level=21, path = "/root/Android/Sdk/ndk-bundle/android-ndk-r21")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
# Protobuf expects an //external:python_headers target
bind(
name = "python_headers",
Expand Down Expand Up @@ -406,7 +408,7 @@ http_archive(
build_file = "@//third_party:opencv_android.BUILD",
strip_prefix = "OpenCV-android-sdk",
type = "zip",
url = "https://github.com/opencv/opencv/releases/download/3.4.3/opencv-3.4.3-android-sdk.zip",
url = "https://github.com/opencv/opencv/releases/download/4.0.1/opencv-4.0.1-android-sdk.zip",
)

# After OpenCV 3.2.0, the pre-compiled opencv2.framework has google protobuf symbols, which will
Expand Down Expand Up @@ -507,6 +509,8 @@ maven_install(
"com.google.guava:listenablefuture:1.0",
"junit:junit:4.12",
"org.hamcrest:hamcrest-library:1.3",
"com.squareup.okhttp3:okhttp:4.9.3",
"com.android.volley:volley:1.1.0",
],
repositories = [
"https://maven.google.com",
Expand Down Expand Up @@ -679,4 +683,3 @@ http_archive(
urls = ["https://github.com/nlohmann/json/releases/download/v3.9.1/include.zip"],
build_file = "@//third_party:nlohmann.BUILD",
)

6,070 changes: 0 additions & 6,070 deletions hs_err_pid13675.log

This file was deleted.

4,598 changes: 0 additions & 4,598 deletions hs_err_pid60427.log

This file was deleted.

1,759 changes: 0 additions & 1,759 deletions hs_err_pid67833.log

This file was deleted.

25 changes: 25 additions & 0 deletions mediapipe/calculators/image/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,31 @@ cc_library(
alwayslink = 1,
)

cc_library(
name = "reranking_calculator",
srcs = ["reranking_calculator.cc"],
deps = [
"//mediapipe/framework:calculator_framework",
"//mediapipe/framework/formats:image_frame",
"//mediapipe/framework/formats:image_frame_opencv",
"//mediapipe/framework/formats:landmark_cc_proto",
"//mediapipe/framework/formats:video_stream_header",
"//mediapipe/framework/port:integral_types",
"//mediapipe/framework/port:logging",
"//mediapipe/framework/port:opencv_core",
"//mediapipe/framework/port:opencv_features2d",
"//mediapipe/framework/port:opencv_imgproc",
"//mediapipe/framework/port:ret_check",
"//mediapipe/framework/port:status",
"//mediapipe/framework/port:threadpool",
"//mediapipe/framework/tool:options_util",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/log",
"@com_google_absl//absl/synchronization",
],
alwayslink = 1,
)

cc_library(
name = "image_localization_calculator",
srcs = ["image_localization_calculator.cc"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace mediapipe
feature_detector_ = cv::ORB::create(
options_.max_features(), options_.scale_factor(),
options_.pyramid_level(), kPatchSize - 1, 0, 2, cv::ORB::FAST_SCORE);
cc->SetOffset(TimestampDiff(0));
return absl::OkStatus();
}

Expand Down Expand Up @@ -68,9 +69,8 @@ namespace mediapipe
descriptors.resize(keypoints.size());
}

// Output descriptors
auto descriptors_ptr = absl::make_unique<cv::Mat>(descriptors);
cc->Outputs().Tag("FEATURES").Add(descriptors_ptr.release(), timestamp);
auto features_ptr = absl::make_unique<cv::Mat>(descriptors);
cc->Outputs().Tag("FEATURES").Add(features_ptr.release(), cc->InputTimestamp());

return absl::OkStatus();
}
Expand Down
126 changes: 59 additions & 67 deletions mediapipe/calculators/image/image_localization_calculator.cc
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright 2023 mediapipe.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "mediapipe/framework/calculator_framework.h"
#include "mediapipe/framework/formats/image_frame.h"
#include "mediapipe/framework/formats/image_frame_opencv.h"
Expand All @@ -22,23 +8,6 @@
namespace mediapipe
{

// Finds the largest contour within a given image based on HSV color
// thresholds. The largest contour is assumed to be the picture. If a valid
// contour is found, the image will be cropped to the contour's bounding
// box.
//
// Inputs:
// IMAGE: An ImageFrame containing the input image.
//
// Outputs:
// IMAGE: An ImageFrame containing the cropped image.
//
// Example config:
// node {
// calculator: "PictureLocalizationCalculator"
// input_stream: "IMAGE:input_image"
// output_stream: "IMAGE:cropped_image"
// }
class PictureLocalizationCalculator : public CalculatorBase
{
public:
Expand All @@ -48,6 +17,7 @@ namespace mediapipe
cc->Outputs().Tag("IMAGE").Set<ImageFrame>();
return absl::OkStatus();
}

absl::Status Open(CalculatorContext *cc) override
{
cc->SetOffset(TimestampDiff(0));
Expand All @@ -60,63 +30,85 @@ namespace mediapipe
cv::Mat image = formats::MatView(&input_img);
cv::Mat cropped_image = LocalizePicture(image);

// Create output ImageFrame.
std::unique_ptr<ImageFrame> output_frame(
new ImageFrame(input_img.Format(), cropped_image.cols, cropped_image.rows));
cropped_image.copyTo(formats::MatView(output_frame.get()));
cc->Outputs().Tag("IMAGE").Add(output_frame.release(),
cc->InputTimestamp());
return absl::OkStatus();
}
absl::Status Close(CalculatorContext *cc)
{
cc->Outputs().Tag("IMAGE").Add(output_frame.release(), cc->InputTimestamp());
return absl::OkStatus();
}

private:
cv::Mat LocalizePicture(const cv::Mat &image)
{
// Convert to HSV color space
cv::Mat hsv;
cv::cvtColor(image, hsv, cv::COLOR_BGR2HSV);
cv::Mat gray, blur, edges;
cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);

// Define thresholds for picture colors
cv::Scalar lower_picture(10, 10, 10); // Lower HSV bound
cv::Scalar upper_picture(110, 110, 110); // Upper HSV bound
int min_bbox_size = 1000;
// Apply Gaussian blur to reduce noise
cv::GaussianBlur(gray, blur, cv::Size(5, 5), 0);

// Create a mask for the picture
cv::Mat mask;
cv::inRange(hsv, lower_picture, upper_picture, mask);
// Use Canny edge detection
cv::Canny(blur, edges, 50, 150);

// Dilate edges to close gaps
cv::Mat dilated_edges;
cv::dilate(edges, dilated_edges, cv::Mat(), cv::Point(-1, -1), 2);

// Find contours in the mask
std::vector<std::vector<cv::Point>> contours;
cv::findContours(mask, contours, cv::RETR_CCOMP,
cv::CHAIN_APPROX_SIMPLE);
cv::findContours(dilated_edges, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);

// Find the largest contour
int max_area = 0;
int max_index = -1;
for (int i = 0; i < contours.size(); ++i)
if (contours.empty())
{
int area = cv::contourArea(contours[i]);
if (area > max_area)
{
max_area = area;
max_index = i;
}
return image;
}

// Find the contour with the largest area
auto max_contour = std::max_element(contours.begin(), contours.end(),
[](const std::vector<cv::Point> &c1, const std::vector<cv::Point> &c2)
{
return cv::contourArea(c1) < cv::contourArea(c2);
});

// Check if the largest contour is significant enough
double max_area = cv::contourArea(*max_contour);
if (max_area < 0.10 * image.cols * image.rows)
{
return image; // Return original if no significant contour found
}

// Crop the bounding box
if (max_index >= 0)
// Approximate the contour to a polygon
std::vector<cv::Point> approx;
cv::approxPolyDP(*max_contour, approx, 0.02 * cv::arcLength(*max_contour, true), true);

// If we have a quadrilateral, perform perspective transform
if (approx.size() == 4)
{
cv::Rect rect = cv::boundingRect(contours[max_index]);
if (rect.width * rect.height > min_bbox_size)
cv::Point2f src_pts[4], dst_pts[4];
for (int i = 0; i < 4; i++)
{
return image(rect);
src_pts[i] = cv::Point2f(approx[i].x, approx[i].y);
}

// Sort points to ensure consistent ordering
std::sort(src_pts, src_pts + 4, [](const cv::Point2f &a, const cv::Point2f &b)
{ return a.x + a.y < b.x + b.y; });

// Define destination points for a rectangle
cv::Rect bounds = cv::boundingRect(approx);
dst_pts[0] = cv::Point2f(0, 0);
dst_pts[1] = cv::Point2f(bounds.width - 1, 0);
dst_pts[2] = cv::Point2f(bounds.width - 1, bounds.height - 1);
dst_pts[3] = cv::Point2f(0, bounds.height - 1);

cv::Mat transform = cv::getPerspectiveTransform(src_pts, dst_pts);
cv::Mat result;
cv::warpPerspective(image, result, transform, bounds.size());
return result;
}
else
{
// If not a quadrilateral, just return the bounding rectangle
return image(cv::boundingRect(approx));
}
return image; // Return original image if no suitable contour is found
}
};

Expand Down
Loading

0 comments on commit c9e5a29

Please sign in to comment.