Ask Your Question

Revision history [back]

Face Prediction is invalid in Opencv Python

faces are showing Wrong Information in cv2.putText

Following is my code

def start_webcam(self): try: face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml'); capture = cv2.VideoCapture(1); while(True): ret, img = capture.read(); gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5); for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2) cv2.waitKey(10) cv2.imshow('img', img); #cv2.waitKey(100) #self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT,480) #self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640) #self.timer=QTimer(self) #self.timer.timeout.connect(self.update_frame) #self.timer.start(5) capture.release() cv2.destroyAllWindows() except Exception: logging.exception("Connection issue")

def stop_webcam(self): try: if self.studentid==0: self.displayMessageBox("Enter Student Information first") return; while(True): capture = cv2.VideoCapture(1); return_value, img=capture.read() gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) for i in range(5): cv2.imwrite("dataSet/User." + str(self.studentid) + "." + str(i) + '.jpg',gray) cv2.waitKey(100) del (capture) cv2.imshow("face",img) capture.release()

def Traniner_Button(self): recognizer=cv2.face.LBPHFaceRecognizer_create() path='dataSet'

    def getImagesWithID(path):
        imagePaths=[os.path.join(path, f) for f in os.listdir(path)]
        faces=[]
        IDs=[]
        for imagePath in imagePaths:
            faceImg=Image.open(imagePath).convert('L')
            faceNp=np.array(faceImg, 'uint8')
            ID=int(os.path.split(imagePath)[-1].split('.')[1])
            faces.append(faceNp)
            IDs.append(ID)
            cv2.imshow("training", faceNp)
            cv2.waitKey(10)
        return np.array(IDs), faces

    def train():
        Ids, faces=getImagesWithID(path)
        recognizer.train(faces, Ids)
        recognizer.save('recognizer/trainingData.yml')
        cv2.destroyAllWindows()
    try:
        train()
    except Exception:
        logging.exception("Trainer Issue")

recognizer=cv2.face.LBPHFaceRecognizer_create() recognizer.read('recognizer/trainingData.yml') cascadePath=('haarcascade_frontalface_default.xml') faceCascade=cv2.CascadeClassifier(cascadePath); font=cv2.FONT_HERSHEY_SIMPLEX cam=cv2.VideoCapture(1) studentProf=None try: while True: ret, img=cam.read(); gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces=faceCascade.detectMultiScale(gray, 1.2, 5) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (225, 0, 0), 2) imgId, confidence=recognizer.predict(gray[y:y + h, x:x + w]) studentProf = self.getStudentProfile(str(imgId)) if (studentProf != None): cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) cv2.putText(img, str(studentProf), (x, y + h), font, 1, (255, 255, 255), 3) cv2.waitKey(10) cv2.imshow('img', img)

click to hide/show revision 2
None

updated 2018-05-01 05:28:04 -0600

berak gravatar image

Face Prediction is invalid in Opencv Python

faces are showing Wrong Information in cv2.putText

Following is my code

 def start_webcam(self):
        try:
            face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml');
            capture = cv2.VideoCapture(1);
            while(True):
                ret, img = capture.read();
                gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                faces = face_cascade.detectMultiScale(gray, 1.3, 5);
                for (x, y, w, h) in faces:
                    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
                    cv2.waitKey(10)
                cv2.imshow('img', img);
                #cv2.waitKey(100)
            #self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
            #self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
            #self.timer=QTimer(self)
            #self.timer.timeout.connect(self.update_frame)
            #self.timer.start(5)
            capture.release()
            cv2.destroyAllWindows()
        except Exception:
            logging.exception("Connection issue")

issue")

def stop_webcam(self): try: if self.studentid==0: self.displayMessageBox("Enter Student Information first") return; while(True): capture = cv2.VideoCapture(1); return_value, img=capture.read() gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) for i in range(5): cv2.imwrite("dataSet/User." + str(self.studentid) + "." + str(i) + '.jpg',gray) cv2.waitKey(100) del (capture) cv2.imshow("face",img) capture.release()

capture.release()

def Traniner_Button(self): recognizer=cv2.face.LBPHFaceRecognizer_create() path='dataSet'

path='dataSet'
     def getImagesWithID(path):
         imagePaths=[os.path.join(path, f) for f in os.listdir(path)]
         faces=[]
         IDs=[]
         for imagePath in imagePaths:
             faceImg=Image.open(imagePath).convert('L')
             faceNp=np.array(faceImg, 'uint8')
             ID=int(os.path.split(imagePath)[-1].split('.')[1])
             faces.append(faceNp)
             IDs.append(ID)
             cv2.imshow("training", faceNp)
             cv2.waitKey(10)
         return np.array(IDs), faces

     def train():
         Ids, faces=getImagesWithID(path)
         recognizer.train(faces, Ids)
         recognizer.save('recognizer/trainingData.yml')
         cv2.destroyAllWindows()
     try:
         train()
     except Exception:
         logging.exception("Trainer Issue")

recognizer=cv2.face.LBPHFaceRecognizer_create() recognizer.read('recognizer/trainingData.yml') cascadePath=('haarcascade_frontalface_default.xml') faceCascade=cv2.CascadeClassifier(cascadePath); font=cv2.FONT_HERSHEY_SIMPLEX cam=cv2.VideoCapture(1) studentProf=None try: while True: ret, img=cam.read(); gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces=faceCascade.detectMultiScale(gray, 1.2, 5) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (225, 0, 0), 2) imgId, confidence=recognizer.predict(gray[y:y + h, x:x + w]) studentProf = self.getStudentProfile(str(imgId)) if (studentProf != None): cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) cv2.putText(img, str(studentProf), (x, y + h), font, 1, (255, 255, 255), 3) cv2.waitKey(10) cv2.imshow('img', img)

img)