OpenCV 4 TypeError: Expected cv::UMat for argument 'labels'












1















I am writing a facial recognition program and I keep getting this error when I try to train my recognizer



TypeError: Expected cv::UMat for argument 'labels'


my code is



def detect_face(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);
if (len(faces)==0):
return None, None
(x, y, w, h) = faces[0]
return gray[y:y+w, x:x+h], faces[0]

def prepare_training_data():
faces =
labels =
for img in photo_name_list: #a collection of file locations as strings
image = cv2.imread(img)
face, rect = detect_face(image)
if face is not None:
faces.append(face)
labels.append("me")
return faces, labels

def test_photos():
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
faces, labels = prepare_training_data()
face_recognizer.train(faces, np.ndarray(labels))


labels is list of labels for each photo in the image list returned from prepare_training_data, and I convert it to a numpy array because I read that is what train() needs it to be.










share|improve this question



























    1















    I am writing a facial recognition program and I keep getting this error when I try to train my recognizer



    TypeError: Expected cv::UMat for argument 'labels'


    my code is



    def detect_face(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);
    if (len(faces)==0):
    return None, None
    (x, y, w, h) = faces[0]
    return gray[y:y+w, x:x+h], faces[0]

    def prepare_training_data():
    faces =
    labels =
    for img in photo_name_list: #a collection of file locations as strings
    image = cv2.imread(img)
    face, rect = detect_face(image)
    if face is not None:
    faces.append(face)
    labels.append("me")
    return faces, labels

    def test_photos():
    face_recognizer = cv2.face.LBPHFaceRecognizer_create()
    faces, labels = prepare_training_data()
    face_recognizer.train(faces, np.ndarray(labels))


    labels is list of labels for each photo in the image list returned from prepare_training_data, and I convert it to a numpy array because I read that is what train() needs it to be.










    share|improve this question

























      1












      1








      1


      1






      I am writing a facial recognition program and I keep getting this error when I try to train my recognizer



      TypeError: Expected cv::UMat for argument 'labels'


      my code is



      def detect_face(img):
      gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
      faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);
      if (len(faces)==0):
      return None, None
      (x, y, w, h) = faces[0]
      return gray[y:y+w, x:x+h], faces[0]

      def prepare_training_data():
      faces =
      labels =
      for img in photo_name_list: #a collection of file locations as strings
      image = cv2.imread(img)
      face, rect = detect_face(image)
      if face is not None:
      faces.append(face)
      labels.append("me")
      return faces, labels

      def test_photos():
      face_recognizer = cv2.face.LBPHFaceRecognizer_create()
      faces, labels = prepare_training_data()
      face_recognizer.train(faces, np.ndarray(labels))


      labels is list of labels for each photo in the image list returned from prepare_training_data, and I convert it to a numpy array because I read that is what train() needs it to be.










      share|improve this question














      I am writing a facial recognition program and I keep getting this error when I try to train my recognizer



      TypeError: Expected cv::UMat for argument 'labels'


      my code is



      def detect_face(img):
      gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
      faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);
      if (len(faces)==0):
      return None, None
      (x, y, w, h) = faces[0]
      return gray[y:y+w, x:x+h], faces[0]

      def prepare_training_data():
      faces =
      labels =
      for img in photo_name_list: #a collection of file locations as strings
      image = cv2.imread(img)
      face, rect = detect_face(image)
      if face is not None:
      faces.append(face)
      labels.append("me")
      return faces, labels

      def test_photos():
      face_recognizer = cv2.face.LBPHFaceRecognizer_create()
      faces, labels = prepare_training_data()
      face_recognizer.train(faces, np.ndarray(labels))


      labels is list of labels for each photo in the image list returned from prepare_training_data, and I convert it to a numpy array because I read that is what train() needs it to be.







      python python-3.x numpy opencv facial-identification






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 20 at 7:02









      Tyler StrouthTyler Strouth

      62




      62
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Solution - labels should be list of integers, and you should use numpy.array(labels) (or np.array(labels)).



          Dummy example to check an error absence:



          labels=[0]*len(faces)
          face_recognizer.train(faces, np.array(labels))


          I haven't found any documentation for openCV face recodnisers on python, so i've started to look over c++ documentation and examples. And due to documentation this library uses labels input for train as a std::vector<int>. A cpp example, provided by openCV docs, also uses vector<int> labels. And so on, library even have an error for not an integer input.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54274298%2fopencv-4-typeerror-expected-cvumat-for-argument-labels%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Solution - labels should be list of integers, and you should use numpy.array(labels) (or np.array(labels)).



            Dummy example to check an error absence:



            labels=[0]*len(faces)
            face_recognizer.train(faces, np.array(labels))


            I haven't found any documentation for openCV face recodnisers on python, so i've started to look over c++ documentation and examples. And due to documentation this library uses labels input for train as a std::vector<int>. A cpp example, provided by openCV docs, also uses vector<int> labels. And so on, library even have an error for not an integer input.






            share|improve this answer




























              0














              Solution - labels should be list of integers, and you should use numpy.array(labels) (or np.array(labels)).



              Dummy example to check an error absence:



              labels=[0]*len(faces)
              face_recognizer.train(faces, np.array(labels))


              I haven't found any documentation for openCV face recodnisers on python, so i've started to look over c++ documentation and examples. And due to documentation this library uses labels input for train as a std::vector<int>. A cpp example, provided by openCV docs, also uses vector<int> labels. And so on, library even have an error for not an integer input.






              share|improve this answer


























                0












                0








                0







                Solution - labels should be list of integers, and you should use numpy.array(labels) (or np.array(labels)).



                Dummy example to check an error absence:



                labels=[0]*len(faces)
                face_recognizer.train(faces, np.array(labels))


                I haven't found any documentation for openCV face recodnisers on python, so i've started to look over c++ documentation and examples. And due to documentation this library uses labels input for train as a std::vector<int>. A cpp example, provided by openCV docs, also uses vector<int> labels. And so on, library even have an error for not an integer input.






                share|improve this answer













                Solution - labels should be list of integers, and you should use numpy.array(labels) (or np.array(labels)).



                Dummy example to check an error absence:



                labels=[0]*len(faces)
                face_recognizer.train(faces, np.array(labels))


                I haven't found any documentation for openCV face recodnisers on python, so i've started to look over c++ documentation and examples. And due to documentation this library uses labels input for train as a std::vector<int>. A cpp example, provided by openCV docs, also uses vector<int> labels. And so on, library even have an error for not an integer input.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 21 at 13:53









                PolyGlotPolyGlot

                215




                215
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54274298%2fopencv-4-typeerror-expected-cvumat-for-argument-labels%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Homophylophilia

                    Updating UILabel text programmatically using a function

                    Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage