Skip to content

fix: face_detector_yn needs to accept NO_OBJDETECT#1830

Closed
ctacke wants to merge 1 commit intoshimat:mainfrom
ctacke:main
Closed

fix: face_detector_yn needs to accept NO_OBJDETECT#1830
ctacke wants to merge 1 commit intoshimat:mainfrom
ctacke:main

Conversation

@ctacke
Copy link
Contributor

@ctacke ctacke commented Mar 1, 2026

If you're trying to build a slimmed-down version and have NO_OBJDETECT, the new face_detector_yn regressed the build

Summary by CodeRabbit

  • Bug Fixes
    • Increased robustness of face detection to work with diverse OpenCV configurations. The library now gracefully handles scenarios where optional object detection components may be disabled or unavailable in your OpenCV installation. This prevents build failures and runtime errors, ensuring stable operation across different system environments.

@coderabbitai
Copy link

coderabbitai bot commented Mar 1, 2026

📝 Walkthrough

Walkthrough

This PR adds preprocessor guards around FaceDetectorYN API implementations and declarations, providing no-op fallback implementations when OBJDETECT support is unavailable in OpenCV, replacing previous runtime/compile-time error behavior.

Changes

Cohort / File(s) Summary
Header Guards & Declarations
src/OpenCvSharpExtern/face_detector_yn.h
Conditionally protects includes (opencv2/objdetect/face.hpp) and public API declarations (cveFaceDetectorYNCreate, cveFaceDetectorYNRelease, cveFaceDetectorYNDetect) behind HAVE_OPENCV_OBJDETECT macro, replacing unconditional core includes.
Implementation Fallback Stubs
src/OpenCvSharpExtern/face_detector_yn.cpp
Wraps FaceDetectorYN API calls with NO_OBJDETECT preprocessor guards, providing no-op fallback implementations (returning nullptr/0) when object detection is unavailable, replacing previous throw_no_objdetect() error behavior.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • shimat/opencvsharp#1827: Introduces the FaceDetectorYN native bindings that this PR now guards with conditional compilation.
  • shimat/opencvsharp#1828: Adds tests for the FaceDetectorYN API that will be affected by these conditional compilation changes.
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: adding support for the NO_OBJDETECT macro to the face_detector_yn component to fix a build issue.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/OpenCvSharpExtern/face_detector_yn.cpp`:
- Around line 43-67: The stub implementations under the `#else` branch incorrectly
reference OpenCV types that only exist when HAVE_OPENCV_OBJDETECT is defined;
either remove these stub definitions so the .cpp matches the header guards, or
replace their signatures to use opaque types (e.g., void* or
conditionally-compiled typedefs) to avoid referring to cv::FaceDetectorYN and
cv::Ptr<cv::FaceDetectorYN> when HAVE_OPENCV_OBJDETECT is not set. Update the
implementations for cveFaceDetectorYNCreate, cveFaceDetectorYNRelease, and
cveFaceDetectorYNDetect accordingly and ensure the `#ifdef/HAVE_OPENCV_OBJDETECT`
guards in the .cpp mirror the header declarations exactly.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 95f438e and 6c39dca.

📒 Files selected for processing (2)
  • src/OpenCvSharpExtern/face_detector_yn.cpp
  • src/OpenCvSharpExtern/face_detector_yn.h

Comment on lines +43 to +67
#else

cv::FaceDetectorYN* cveFaceDetectorYNCreate(
cv::String* model,
cv::String* config,
CvSize* inputSize,
float scoreThreshold,
float nmsThreshold,
int topK,
int backendId,
int targetId,
cv::Ptr<cv::FaceDetectorYN>** sharedPtr)
{
return nullptr;
}

void cveFaceDetectorYNRelease(cv::Ptr<cv::FaceDetectorYN>** faceDetector)
{
}

int cveFaceDetectorYNDetect(cv::FaceDetectorYN* faceDetector, cv::_InputArray* image, cv::_OutputArray* faces)
{
return 0;
}
#endif
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Stub implementations use undefined types when HAVE_OPENCV_OBJDETECT is not defined.

The #else branch (stub implementations) references types like cv::FaceDetectorYN*, cv::Ptr<cv::FaceDetectorYN>** which are only defined when opencv2/objdetect/face.hpp is included. This branch executes precisely when that header is NOT included, causing compilation failures.

Additionally, the header file (lines 17-32) only declares these functions when HAVE_OPENCV_OBJDETECT is defined, creating an inconsistency where the .cpp defines functions that aren't declared in the header.

🔧 Proposed fix: Remove stub implementations and align with header guards

The header doesn't declare these functions when HAVE_OPENCV_OBJDETECT is not defined, so the .cpp shouldn't define them either:

 `#endif`
-
-#else
-
-cv::FaceDetectorYN* cveFaceDetectorYNCreate(
-	cv::String* model,
-	cv::String* config,
-	CvSize* inputSize,
-	float scoreThreshold,
-	float nmsThreshold,
-	int topK,
-	int backendId,
-	int targetId,
-	cv::Ptr<cv::FaceDetectorYN>** sharedPtr)
-{
-	return nullptr;
-}
-
-void cveFaceDetectorYNRelease(cv::Ptr<cv::FaceDetectorYN>** faceDetector)
-{
-}
-
-int cveFaceDetectorYNDetect(cv::FaceDetectorYN* faceDetector, cv::_InputArray* image, cv::_OutputArray* faces)
-{
-	return 0;
-}
-#endif

 `#endif`

Alternatively, if stub implementations are required for linkage, use opaque void* pointers instead of the concrete OpenCV types.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/OpenCvSharpExtern/face_detector_yn.cpp` around lines 43 - 67, The stub
implementations under the `#else` branch incorrectly reference OpenCV types that
only exist when HAVE_OPENCV_OBJDETECT is defined; either remove these stub
definitions so the .cpp matches the header guards, or replace their signatures
to use opaque types (e.g., void* or conditionally-compiled typedefs) to avoid
referring to cv::FaceDetectorYN and cv::Ptr<cv::FaceDetectorYN> when
HAVE_OPENCV_OBJDETECT is not set. Update the implementations for
cveFaceDetectorYNCreate, cveFaceDetectorYNRelease, and cveFaceDetectorYNDetect
accordingly and ensure the `#ifdef/HAVE_OPENCV_OBJDETECT` guards in the .cpp
mirror the header declarations exactly.

shimat added a commit that referenced this pull request Mar 1, 2026
@shimat shimat mentioned this pull request Mar 1, 2026
@shimat shimat closed this in #1832 Mar 1, 2026
shimat added a commit that referenced this pull request Mar 1, 2026
@shimat
Copy link
Owner

shimat commented Mar 1, 2026

Thank you for identifying this and submitting a fix!

I've opened a separate PR that addresses the same root cause while aligning the implementation with the conventions used in the rest of the codebase. Closing this in favor of that PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants