Skip to content

objdetect: Fix INT8 casting in FaceDetectorYN - #28806

Closed
Muhammad-Kamran-Khan wants to merge 1 commit into
opencv:4.xfrom
Muhammad-Kamran-Khan:fix-yunet-int8-parsing
Closed

Muhammad-Kamran-Khan wants to merge 1 commit into
opencv:4.xfrom
Muhammad-Kamran-Khan:fix-yunet-int8-parsing

Conversation

@Muhammad-Kamran-Khan

Copy link
Copy Markdown

Description

This PR fixes a bug where FaceDetectorYN fails to detect any faces when using INT8 quantized models (specifically face_detection_yunet_2023mar_int8.onnx).

Root Cause:
In modules/objdetect/src/face_detect.cpp, the postProcess function extracts output blobs and immediately casts their data to float*. For INT8 models, these blobs are of type CV_8S. Casting a 1-byte integer array directly to a 4-byte float pointer results in memory misinterpretation (garbage values), causing all bounding box scores to drop near zero and fail the scoreThreshold check.

Solution:
Added a step to safely cast the extracted tensors to CV_32F using cv::Mat::convertTo before applying the float* read. This acts as a safe cast for quantized models and is practically a no-op for models that already output CV_32F.

Related Issue

Fixes #28798

Checklist

  • I agree to contribute my code under OpenCV (Apache 2.0) license.
  • I have tested this code locally with both FP32 and INT8 models using the Python script.
  • The code matches the Coding Style Guide of the project.
  • I have checked for and removed any trailing whitespaces.
  • This PR contains a single, clean commit.

@asmorkalov

Copy link
Copy Markdown
Contributor
  1. Please revert not relevant changes.
  2. Please add a test for the fixed case. We already have a test with FP model. You can extend it or add alternative case.

@asmorkalov asmorkalov added incomplete pr: needs test New functionality requires minimal tests set labels Apr 14, 2026
Fixed CV_8S to float pointer bug using convertTo. Added INT8 regression test.
@Muhammad-Kamran-Khan

Copy link
Copy Markdown
Author

Hi @asmorkalov,

I have reverted the irrelevant imgcodecs files so the PR is clean now. I have also added the regression test for the INT8 model in modules/objdetect/test/test_face.cpp as requested. The test safely skips locally, I don't know why.

Please review the updated PR. Thank you!

@asmorkalov

Copy link
Copy Markdown
Contributor

You need to create PR with the same branch name in opencv_extra repo and add model file there.

@Muhammad-Kamran-Khan

Copy link
Copy Markdown
Author

Hi @asmorkalov, I have created the PR in the opencv_extra repo with the exact same branch name and added the model file. Here is the link:
opencv/opencv_extra#1341

@asmorkalov asmorkalov added this to the 4.14.0 milestone Apr 14, 2026
@asmorkalov

Copy link
Copy Markdown
Contributor

Tested branch locally. It does not fix model behaviour. Facedetect sample still does not detect faces with int8 model.

@asmorkalov asmorkalov closed this Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

incomplete pr: needs test New functionality requires minimal tests set

Projects

None yet

Development

Successfully merging this pull request may close these issues.

face_detection_yunet_2023mar_int8.onnx model from zoo detects nothing with 5.x

2 participants