For my project, I'm using Microsoft Visual Studio Ultimate 2013, opencv-3.3.1-vc14 and Microsoft Window 10. In my project, I'm going to load an .xml file but it didn't.
The directory of my project is: C:\Users\Muhammad_Anas_Hashmi\Desktop\Face_and_Eye_Detection. But the .xml file that I want to load has the directory: C:\opencv\sources\data\haarcascades\haarcascade_eye.xml.
The way that I use to load the file is:
CascadeClassifier eye_cascade;
eye_cascade.load("C:\\opencv\sources\data\haarcascades_cuda\haarcascade_eye.xml")
But I check through a message whether the file has been loaded or not through the following code.
if (!eye_cascade.load("C:\\opencv\sources\data\haarcascades_cuda\haarcascade_eye.xml"))
{
printf("Error loading cascade file for eye\n");
}
Every time it prints out the message. It means file has not been loaded.
But the whole code of my project is:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<opencv\cv.h>
#include<opencv\highgui.h>
#include<opencv2\objdetect\objdetect.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<vector>
using namespace cv;
using namespace std;
int main()
{
CascadeClassifier face_cascade, eye_cascade;
if (!face_cascade.load("C:\\Users\Muhammad Anas Hashmi\Desktop\Face_and_Eye_Detection\haarcascade_frontalface_alt2.xml"))
{
printf("Error loading cascade file for face\n");
/*return 1;*/
}
if (!eye_cascade.load("C:\\opencv\sources\data\haarcascades_cuda\haarcascade_eye.xml"))
{
printf("Error loading cascade file for eye\n");
/*return 1;*/
}
VideoCapture capture(0);
if (!capture.isOpened())
{
printf("Error to initialize camera");
return 1;
}
Mat cap_img, gray_img;
vector<Rect> faces, eyes;
while (1)
{
capture >> cap_img;
waitKey(10);
cvtColor(cap_img, gray_img, CV_BGR2GRAY);
cv::equalizeHist(gray_img, gray_img);
face_cascade.detectMultiScale(gray_img, faces, 1.1, 10, CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING, cvSize(0, 0), cvSize(300, 300));
for (int i = 0; i < faces.size(); i++)
{
Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
Point pt2(faces[i].x, faces[i].y);
Mat faceROI = gray_img(faces[i]);
eye_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
for (size_t j = 0; j < eyes.size(); j++)
{
Point center(faces[i].x + eyes[j].x + eyes[j].width * 0.5, faces[i].y + eyes[j].y + eyes[j].width * 0.5);
int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
circle(cap_img, center, radius, Scalar(255, 0, 0), 2, 8, 0);
}
rectangle(cap_img, pt1, pt2, cvScalar(0, 255, 0), 2, 8, 0);
}
imshow("Result", cap_img);
waitKey(3);
char c = waitKey(3);
if (c == 27)
{
break;
}
}
system("PAUSE");
return 0;
}
I found the problem at the start while loading the .xml file.
C:\\opencv\sources\data\haarcascades_cuda\haarcascade_eye.xm
You only escaped the first backslash. You need to escape all:
C:\\opencv\\sources\\data\\haarcascades_cuda\\haarcascade_eye.xml
Related
For some background, I am compiling in Visual Studio 2019 and running the code inside LabVIEW 2017. The reason I am doing it in LabVIEW is for research to control a robotic gantry. This is the vision system and it is supposed to detect rectangles (wirebond pads for silicon detectors).
I need it to atleast show me a picture or something but when I run it in LabVIEW, it just says it is not responding and makes me hard close the program. So frustrating! If theres no huge errors in my C++ code then I know I have to dig deeper into my LabVIEW code.
The following code is my problem. I am fairly new to C++ and programming in general. I have done the step each line inside LabVIEW and when it stops responding is when it starts to grab the nominalHeight, xfov etc... or just when it goes into the WBPdetection function in general.
Any help is much appreciated. Or if someone could just point me in the right direction.
#include "stdafx.h"
#include "utils.h"
#include "WBPdetection.h"
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <vector>
using namespace std;
void show3(cv::Mat img)
{
cv::namedWindow("MyWindow", cv::WINDOW_AUTOSIZE);
cv::imshow("MyWindow", img);
cv::waitKey(0);
cv::destroyWindow("MyWindow");
}
__declspec(dllexport) int __cdecl WBPdetection(
char* imgPtr,
int imgLineWidth,
int imgWidth,
int imgHeight,
double percent_size,
double nominalWidth,
double nominalHeight,
double tolerance,
double xfov,
double yfov)
{
cv::Mat img(imgHeight, imgWidth, CV_8U, (void*)imgPtr, imgLineWidth);
cv::resize(img, img, cv::Size(img.cols * percent_size, img.rows * percent_size), 0, 0);
//PREPPING IMAGE FOR DETECTION ALGORITHIM
cv::threshold(img, img, 125, 255, cv::THRESH_OTSU);
cv::GaussianBlur(img, img, cv::Size(5, 5), 0);
cv::erode(img, img, cv::Mat(), cv::Point(-1, -1), 2, 1, 1);
cv::dilate(img, img, cv::Mat(), cv::Point(-1, -1), 1, 1, 1);
//USE FIND CONTOURS ALGORITHIM
vector<vector<cv::Point>> contours;
vector<cv::Vec4i> hierarchy;
cv::findContours(img, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
for( int i = 0; i < contours.size(); i++ )
{ approxPolyDP( cv::Mat(contours[i]), contours_poly[i], 3, true );
boundRect[i] = cv::boundingRect( cv::Mat(contours_poly[i]) );
}
vector<vector<double>> dimRects; //ex [ [w1,h1], [w2,h2], [w3,h3], ...]
vector<cv::Point> centerRects; //ex [ [c1], [c2], [c3], ... ]
//PUTTING DIMENSIONS OF ALL RECTANGLES IN VECTORS
for (int i = 0; i < contours.size(); i++)
{
cv::Point center = ((boundRect[i].tl().x + boundRect[i].br().x) / 2, (boundRect[i].tl().y + boundRect[i].br().y) / 2); //what about even pixels
double rectWidth = (boundRect[i].br().x - boundRect[i].tl().x) * (xfov / img.cols); //might not matter tbh
double rectHeight = (boundRect[i].tl().y - boundRect[i].br().y) * (yfov / img.rows);
dimRects[i].push_back(rectWidth);
dimRects[i].push_back(rectHeight);
centerRects.push_back(center);
}
//DEFINING minWidth, etc... FROM tolerance AND nominalWidth
double minWidth = nominalWidth * (1 - tolerance);
double maxWidth = nominalWidth * (1 + tolerance);
double minHeight = nominalHeight * (1 - tolerance);
double maxHeight = nominalHeight * (1 + tolerance);
// DRAWING CONTOURS AND BOUNDING RECTANGLE + CENTER
for( int i = 0; i< dimRects.size(); i++ )
{
cv::Scalar color = cv::Scalar(255,255,255); //creates color
if ((dimRects[i][0] > minWidth && dimRects[i][0] < maxWidth) && (dimRects[i][1] > minHeight && dimRects[i][1] < maxHeight))
{
drawContours(img, contours_poly, i, color, 1, 8, vector<cv::Vec4i>(), 0, cv::Point());
rectangle(img, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);
circle(img, centerRects[i], 1, cv::Scalar(0, 0, 255), 1, cv::LINE_8);
}
}
show3(img);
return 0;
}
Well there is one error here
vector<vector<double>> dimRects;
...
for (int i = 0; i < contours.size(); i++)
{
...
dimRects[i].push_back(rectWidth);
dimRects[i].push_back(rectHeight);
dimRects has zero size but your code treats it as if it has the same size as contours.
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!
Hello everyone I have been having this issue with my opencv 3.1 program that I am programming in visual studio 2015 C++. It can use the camera just fine and display images just fine, but I try out a face detect example from the documentation my computer freezes up. Here is the code:
Here are the Two Functions where it gets hung up on: but mainly stops around the faceDetect function, I will do a break point to know specifically where.
void CoreVision::init()
{
face_cascade_name = "C:\\PAD\\opencv\\haarcascades\\haarcascade_frontalface_alt.xml";
eyes_cascade_name = "C:\\PAD\\opencv\\haarcascades\\haarcascade_eye_tree_eyeglasses.xml";
if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading face cascade\n"); };
if (!eyes_cascade.load(eyes_cascade_name)) { printf("--(!)Error loading eyes cascade\n"); };
std::cout << "face_dir: " << face_cascade_name << std::endl;
std::cout << "eye_dir: " << eyes_cascade_name << std::endl;
}
cv::Mat CoreVision::detectFace(cv::Mat main)
{
cv::Mat nuller;
try
{
faces.clear();
cv::Mat frame_gray;
cv::cvtColor(main, 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 / 2, faces[i].y + faces[i].height / 2);
cv::ellipse(main, center, cv::Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, cv::Scalar(255, 0, 255), 4, 8, 0);
cv::Mat faceROI = frame_gray(faces[i]);
std::vector<cv::Rect> eyes;
eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30));
for (size_t j = 0; j < eyes.size(); j++)
{
cv::Point eye_center(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2);
int radius = cvRound((eyes[j].width = eyes[j].height) * 0.25);
cv::circle(main, eye_center, radius, cv::Scalar(255, 0, 0), 4, 8, 0);
}
}
return main;
}
catch (...)
{
std::cout << "Exception: have issue with processing face detection.";
return nuller;
}
Here is the main function:
int main()
{
CoreVision _core;
_core.init();
while (true)
{
cv::Mat img = _core.captureImage(-1);
img = _core.detectFace(img);
_core.displayImage(img, "parker");
}
}
And finally the .h file for the two functions:
#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <opencv2\opencv.hpp>
#include <opencv2\world.hpp>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
class CoreVision
{
public:
CoreVision();
~CoreVision();
cv::Mat loadImage(const char* image_name, int type);
void saveImage(cv::Mat img, const char* filename);
cv::Mat captureImage(int cam);
cv::Mat detectFace(cv::Mat main);
int convertImage(cv::Mat img, int type);
void displayImage(cv::Mat, const char* window_name);
void init(void);
char cCurrentPath[FILENAME_MAX];
cv::String face_cascade_name;
cv::String eyes_cascade_name;
cv::CascadeClassifier face_cascade;
cv::CascadeClassifier eyes_cascade;
std::vector<cv::Rect> faces;
};
Thank you in advance for your help, I am not for sure if it is a driver issues, but I have tried it on multiple computers and the same freezing happens.
you don't seem to be storing the faces vector. You have it in public
std::vector faces; but where are you using it? eyes and faces are 2 different sets of data. Your also mixing two styles of code c style and c++ style opencv. You should stick with c++ since your using the latest opencv version.
faces.clear(); // under here
std::vector<Rect>faces; // see if this helps
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void saveImage(cv::Mat img, const char* filename); //should be
void imwrite(img, const char* filename);// etc.
cv::Mat img = _core.captureImage(-1);
img = _core.detectFace(img);
_core.displayImage(img, "parker"); // should be
_core.displayImage(Mat frame);
this proably isn't going to work asuming core.captureImage(-1);
is your camera. also what does int convertImage(cv::Mat img, int type); do? are you sure it takes an int value?
c++ convention is
Videocapture cap(0);
Mat frame:
cap >> frame;
namedWindow("video");
imshow(video, frame); //etc
I had exactly the same issue. I figured out that the line containing eyes_cascade.detectMultiScale was the one causing my driver to crash. I have the Intel HD graphics 4400.
The code started working perfectly after I completely uninstalled the Intel HD graphics drivers.
I am still pretty new to OpenCV and I've just recently come across the HoughLinesP function. First and foremost, my goal is to write code that will detect rectangles in a webcam. Currently, the code I have below only detect lines in general. However, I still have problems while I am debugging the program. Here is my code:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main() {
int erosion_size = 0;
VideoCapture cam(0);
if (!cam.isOpened()) {
cout << "cannot open camera";
}
while (true) {
Mat frame;
cam.read(frame);
Mat gray, edge, draw, die;
cvtColor(frame, gray, CV_BGR2GRAY);
Canny(gray, edge, 100, 150, 3);
edge.convertTo(draw, CV_8U);
dilate(draw, die, Mat(), Point(-1, -1), 2, 1, 1);
erode(die, die, Mat(), Point(-1, -1), 1, 1, 1);
#if 0
vector<Vec2f> lines;
HoughLines(die, lines, 1, CV_PI / 180, 100, 0, 0);
for (size_t i = 0; i < lines.size(); i++)
{
float rho = lines[i][0], theta = lines[i][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 1000 * (-b));
pt1.y = cvRound(y0 + 1000 * (a));
pt2.x = cvRound(x0 - 1000 * (-b));
pt2.y = cvRound(y0 - 1000 * (a));
line(frame, pt1, pt2, Scalar(0, 0, 255), 3, CV_AA);
}
#else
vector<Vec4i> lines;
HoughLinesP(die, lines, 1, CV_PI / 180, 200, 50, 10);
for (size_t i = 0; i < lines.size(); i++)
{
Vec4i l = lines[i];
line(frame, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, CV_AA);
}
#endif
imshow("Canny", die);
imshow("original", frame);
if (waitKey(30) >= 0)
break;
}
return 0;
}
When I debug the program, the webcam pops up okay, but when I show a rectangular object with lines(piece of paper) it stops the program with a break point error. I concluded that my program stopped every time it found a line. When I choose to continue instead of breaking, it gives me this error:
Debug Assertion failed!
Program: ....Studio
2013/Projects/TrialRectangle/Debug/TrialRectangle.exe
File: f:/dd/vctools/crt/crtw32/misc/dbgheap.c
Line: 1332
Expression: _CrtIsValidHeapPointer(pUserData)
I played around with the HoughLinesP function and found that a high threshold parameter(ex. 500) seems to make my program run fine BUT it does not show any HoughLines at all in my webcam. If someone could explain why that is, that would be helpful as well!
Does anyone have any ideas as to how to solve this breakpoint error?
I got into this Debug Assertion too.
In my case, It was because that my project was statically compiled, Yet I used OpenCV dynamically with it's dll. So I change my project into dynamically compile, Solved the Problem.
It's Because that OpenCV object is allocated in different heap. And When this object is destructed, current run-time can's find that heap, This is why the Assertion is hit.
I am developing some video analytic algorithms using openCV. However, after I process a frame and want to display it on a window, it hangs at imshow() function. I have searched for this issue online but still cannot find the problem! Here is the code where I am using multithread and openCV:
void CFeatureExtraction::extract(){
boost::thread OpFlowThread, BGSThread;
while (m_nRun){
ImgPtr frame_ptr;
m_pQueue->wait_and_pop(frame_ptr);
Mat frame1, frame2;
(*frame_ptr).copyTo(frame1);
(*frame_ptr).copyTo(frame2);
OpFlowThread = boost::thread(&COpFlow::op_flow, m_pAlgo,frame1);
BGSThread = boost::thread(&CBGSub::bgsub, m_pBGS, frame2);
OpFlowThread.join();
BGSThread.join();
}
}
And inside op_flow and bgsub function, I am using imshow() and cvWaitKey() together! But it keeps hanging the whole program.
If my question is still not clear for you, pls feel free to ask me for more detail.
Here is the detail code of calling imshow():
CFarnebackAlgo::CFarnebackAlgo() : COpFlow()
{
namedWindow("Optical Flow");
}
CFarnebackAlgo::~CFarnebackAlgo()
{
destroyWindow("Optical Flow");
}
int CFarnebackAlgo::op_flow(Mat frame)
{
Mat flow;
cvtColor(frame, gray, COLOR_BGR2GRAY);
if (prevgray.data)
{
calcOpticalFlowFarneback(prevgray, gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
for (int y = 0; y < frame.rows; y += 8)
for (int x = 0; x < frame.cols; x += 8){
const Point2f& fxy = flow.at<Point2f>(y, x);
line(frame, Point(x, y), Point(cvRound(x + fxy.x), cvRound(y + fxy.y)), Scalar(0, 255, 0));
circle(frame, Point(x, y), 2, Scalar(0, 255, 0), -1);
}
if (frame.data == NULL){
cout << "No Img!" << endl;
exit(0);
}
imshow("Optical Flow", frame);
waitKey(50);
}
std::swap(prevgray, gray);
return 0;
}
If I put the namedWindow() in the op_flow(), it is working. But if I put it in the constructor, it's not working and freezes. Anyone knows why?