I am trying to create an OpenCV plugin for Unreal Engine 4 to detect faces, but when the code reaches the detectMultiScale function I get this error:
Exception thrown at 0x00007FFD4F34FF1B (UE4Editor-Core.dll) in
UE4Editor-Win64-DebugGame.exe: 0xC0000005: Access violation writing
location 0x0000000000000010.
I am using OpenCV version 4.1.0 with VS2017. I also tried with version 3.4.7 of OpenCV, but having the same issue.
This is my function that gets the webcam input and renders it in UE4:
In the constructor:
if (!face_cascade.load("C:\\models\\haarcascade_frontalface_alt.xml")) { printf("--(!)Error loading\n"); }
And the function:
void AWebcamReader::UpdateTexture()
{
std::vector<cv::Rect> faces;
cv::Mat frame_gray;
//cvtColor(frame, frame_gray, cv::COLOR_BGR2GRAY);
if (isStreamOpen && frame.data)
{
cv::cvtColor(frame, frame_gray, cv::COLOR_BGR2GRAY);
cv::equalizeHist(frame_gray, frame_gray);
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30));
for (size_t i = 0; i < faces.size(); i++)
{
cv::Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
ellipse(frame, center, cv::Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, cv::Scalar(255, 0, 255), 4, 8, 0);
cv::Mat faceROI = frame_gray(faces[i]);
}
// Copy Mat data to Data array
for (int y = 0; y < VideoSize.Y; y++)
{
for (int x = 0; x < VideoSize.X; x++)
{
int i = x + (y * VideoSize.X);
Data[i].B = frame.data[i * 3 + 0];
Data[i].G = frame.data[i * 3 + 1];
Data[i].R = frame.data[i * 3 + 2];
}
}
// Update texture 2D
UpdateTextureRegions(VideoTexture, (int32)0, (uint32)1, VideoUpdateTextureRegion, (uint32)(4 * VideoSize.X), (uint32)4, (uint8*)Data.GetData(), false);
}
}
If I comment out the detectMultiScale function, the plugin runs without a problem and I see the webcam input rendered correctly.
The Exception gets called on this line in VT2.cpp (VT2 is the project name)
IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, VT2, "VT2" );
Any suggestions would be greatly appreciated!