c# - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Emgu CV -
void framegrabber(object sender, eventargs e) { namepersons.clear(); namepersons.add(""); //get current frame form capture device currentframe = grabber.queryframe().resize(320, 240, emgu.cv.cvenum.inter.cv_inter_cubic); //convert grayscale gray = currentframe.convert<gray, byte>(); //face detector mcvavgcomp[][] facesdetected = gray.detecthaarcascade( face, 1.4, 4, emgu.cv.cvenum.haar_detection_type.do_canny_pruning, new size(20, 20)); //action each element detected foreach (mcvavgcomp f in facesdetected[0]) { t = t + 1; result = currentframe.copy(f.rect).convert<gray, byte>().resize(100, 100, emgu.cv.cvenum.inter.cv_inter_cubic); //draw face detected in 0th (gray) channel blue color currentframe.draw(f.rect, new bgr(color.red), 2); //if (trainingimages.toarray().length != 0) //{ //termcriteria face recognition numbers of trained images maxiteration mcvtermcriteria termcrit = new mcvtermcriteria(conttrain, 0.001); //eigen face recognizer eigenobjectrecognizer recognizer = new eigenobjectrecognizer( trainingimages.toarray(), labels.toarray(), 5000, ref termcrit); name = recognizer.recognize(result); groupbox1.visible = true; label12.text = name.trim(); sqlcommand cmd = new sqlcommand("select * visitortb name='"+name.trim()+"'",db.connect()); sqldatareader dr; dr=cmd.executereader(); if (dr.read()) { label13.text = dr[5].tostring().trim(); label14.text = dr[10].tostring().trim(); label16.text = dr[7].tostring().trim(); } //idcrd.showidcard(name); //draw label each face detected , recognized currentframe.draw(name, ref font, new point(f.rect.x - 2, f.rect.y - 2), new bgr(color.lightgreen)); } namepersons[t - 1] = name; namepersons.add(""); // } t = 0; //names concatenation of persons recognized //for (int nnn = 0; nnn < facesdetected[0].length; nnn++) //{ // //names = names + namepersons[nnn] + ", "; //} ////show faces procesed , recognized //imageboxframegrabber.image = currentframe; ////label4.text = names; //names = ""; ////clear list(vector) of names //namepersons.clear(); }
trying details of faces detected database, have been getting error "index out of range. must non-negative , less size of collection. parameter name: index" @ line
namepersons[t - 1] = name;
you commented out if block commented out wrong closing brace. because of that, these lines appear after foreach block, when inside it:
namepersons[t - 1] = name; namepersons.add("");
you increment t inside foreach block, if facesdetected
empty, t 0 , you'll index out of range exception.
if had stepped through code in debugger, have found problem in 2 minutes instead of hour.
another comment:
by "declare t globally" mean field of class? poor design decision. if variable used locally in method, should declared local variable of method.
Comments
Post a Comment