Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cv2.error: OpenCV(4.1.0) /io/opencv/modules/core/src/persistence.cpp:2046: error: (-215:Assertion failed) isMap() in function 'operator[]' #14908

Open
leijuzi opened this issue Jun 27, 2019 · 10 comments
Labels
category: contrib Target patches to **opencv_contrib** repository: https://github.com/opencv/opencv_contrib needs investigation Collect and attach more details (build flags, stacktraces, input dumps, etc)

Comments

@leijuzi
Copy link

leijuzi commented Jun 27, 2019

image
i got this error

@pjckoch
Copy link

pjckoch commented Jul 12, 2019

I experience the same error with:

opencv-python==4.1.0.25
opencv-contrib-python==4.1.0.25

when running

fishface = cv2.face.FisherFaceRecognizer_create()
fishface.read("myxml.xml")

@mickey9801
Copy link

mickey9801 commented Sep 26, 2019

Same error with self compiled opencv 4.1.1 and opencv-contrib 4.1.1

The error:

(cv) pi@rpi401b:~/Documents/Python Projects/opencv/facelock $ python3 detector_webcam.py 
detector_webcam.py:23: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(relayPin, GPIO.OUT)
Traceback (most recent call last):
  File "detector_webcam.py", line 44, in <module>
    recognizer.read(fname) # load() for createLBPHFaceRecognizer()
cv2.error: OpenCV(4.1.1) /home/pi/opencv/modules/core/src/persistence.cpp:2046: error: (-215:Assertion failed) isMap() in function 'operator[]'

The code I ran:

import cv2
import os
import sqlite3
import RPi.GPIO as GPIO
import time

# Connect SQLite3 database
conn = sqlite3.connect('database.db')
db = conn.cursor()

# Assign the training data file
fname = "recognizer/trainingData.yml"
if not os.path.isfile(fname):
    print("Please train the data first")
    exit(0)

# Setup GPIO for unlock LED
relayPin = 26
GPIO.setmode(GPIO.BCM)
GPIO.setup(relayPin, GPIO.OUT)
GPIO.output(relayPin, 0)

lastDetectedAt = 0
detectInterval = 5
lastUnlockedAt = 0
unlockDuration = 5

# Font used for display
font = cv2.FONT_HERSHEY_SIMPLEX

# Connect to video source
vSource = 0 # first USB webcam
vStream = cv2.VideoCapture(vSource)

# Setup Classifier for detect face
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# Setup LBPH recognizer for face recognition
recognizer = cv2.face.LBPHFaceRecognizer_create()
# Load training data
recognizer.read(fname)

while vStream.isOpened():
    # Turn off unlock LED when timeout
    if time.time() - lastUnlockedAt > unlockDuration:
        GPIO.output(relayPin, 0)
    
    ok, frame = vStream.read() # Read frame
    if not ok: break
    
    timeElapsed = time.time() - lastDetectedAt
    if timeElapsed > 1./detectInterval:
        lastDetectedAt = time.time()
        
        # Detect face
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = faceCascade.detectMultiScale(gray, scaleFactor = 1.1, minNeighbors = 2)
        
        for (x, y, w, h) in faces:
            # Try to recognize the face a
            roiGray = gray[y:y+h, x:x+w]
            id_, conf = recognizer.predict(roiGray)
            print(id_, conf)
            
            # If recognized face has enough confident (<= 70),
            # retrieve the user name from database,
            # draw a rectangle around the face,
            # print the name of the user and
            # light up the unlock LED for 5 secord
            if conf <= 70:
                cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
                # retrieve user name from database
                db.execute("SELECT `name` FROM `users` WHERE `id` = (?);", (id_,))
                result = db.fetchall()
                name = result[0][0]
                
                GPIO.output(relayPin, 1) # Unlock
                lastUnlockedAt = time.time()
                print("[Unlock] " + str(id_) + ":" + name + " (" + str(conf) + ")")
                cv2.putText(frame, name, (x+2,y+h-5), font, 1, (150,255,0), 2)
            else:
                cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
                GPIO.output(relayPin, 0) # Lock the door if not enough confident
        
    cv2.imshow("Face Recognizer", frame)
    
    # Press ESC to quit the program
    key = cv2.waitKey(1) & 0xff
    if key == 27 or key == ord('q'):
        break

# Clean up
vStream.release()
conn.close()
cv2.destroyAllWindows()
GPIO.cleanup()
print("END")

@Futura9000
Copy link

Same error for me:

Ubuntu 18.04.3 LTS
Python 3.6.8
OpenCV 4.1.1
OpenCV Contrib: 4.1.1.26 (from sudo pip3 install opencv-contrib-python)

When using:

recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read(fname)

@mickey9801
Copy link

Finally I resolve the problem after retrain.

@Futura9000
Copy link

Finally I resolve the problem after retrain.

same for me!

@royalluckyboy
Copy link

how you can solve this problem? @mickey9801

@mickey9801
Copy link

mickey9801 commented Oct 6, 2019

how you can solve this problem? @mickey9801

Just retrain and it will work. @royalluckyboy

@mickey9801
Copy link

Retrain with the dataset and generate a new xml file. @royalluckyboy

@NitiKaur
Copy link

NitiKaur commented Dec 25, 2019

How to retrain it to generate the xml file? @mickey9801

@libni7erez
Copy link

libni7erez commented Jun 10, 2020

Perdón, como hago para Re entrenar, soy nuevo en esto. Pueden ayudarme?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: contrib Target patches to **opencv_contrib** repository: https://github.com/opencv/opencv_contrib needs investigation Collect and attach more details (build flags, stacktraces, input dumps, etc)
Projects
None yet
Development

No branches or pull requests

8 participants