Skip to content

Commit c3a2b42

Browse files
krshrimalialalek
authored andcommitted
Check if faces detected or not.
Added if condition, whether faces are detected or not, will help to keep program running for some images. If imwrite used along with imshow, it may produce error for : No faces found.Aborting.. in function 'fit' (Bad Argument).
1 parent c818de9 commit c3a2b42

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

modules/face/samples/sampleDetectLandmarks.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,26 @@ int main(int argc,char** argv){
7575
resize(img,img,Size(460,460), 0, 0, INTER_LINEAR_EXACT);
7676
facemark->getFaces(img,faces);
7777
vector< vector<Point2f> > shapes;
78-
if(facemark->fit(img,faces,shapes))
79-
{
80-
for( size_t i = 0; i < faces.size(); i++ )
78+
79+
// Check if faces detected or not
80+
// Helps in proper exception handling when writing images to the directories.
81+
if(faces.size() != 0) {
82+
if(facemark->fit(img,faces,shapes))
8183
{
82-
cv::rectangle(img,faces[i],Scalar( 255, 0, 0 ));
83-
}
84-
for(unsigned long i=0;i<faces.size();i++){
85-
for(unsigned long k=0;k<shapes[i].size();k++)
86-
cv::circle(img,shapes[i][k],5,cv::Scalar(0,0,255),FILLED);
84+
for( size_t i = 0; i < faces.size(); i++ )
85+
{
86+
cv::rectangle(img,faces[i],Scalar( 255, 0, 0 ));
87+
}
88+
for(unsigned long i=0;i<faces.size();i++){
89+
for(unsigned long k=0;k<shapes[i].size();k++)
90+
cv::circle(img,shapes[i][k],5,cv::Scalar(0,0,255),FILLED);
91+
}
92+
namedWindow("Detected_shape");
93+
imshow("Detected_shape",img);
94+
waitKey(0);
8795
}
88-
namedWindow("Detected_shape");
89-
imshow("Detected_shape",img);
90-
waitKey(0);
96+
} else {
97+
cout << "Faces not detected." << endl;
9198
}
9299
return 0;
93-
}
100+
}

0 commit comments

Comments
 (0)