Git Product home page Git Product logo

opencv-face-recognition-python's Introduction

Hi there 👋

I am Ramiz and I love mobile development. Here are a few things about me

  • 🔭 I’m currently working as a Sr. Android Developer at Plume, with Clean Code Architecture and Agile as the development model.
  • 🌱 I’m currently reading some cool books by Robert C. Martin (aka Uncle Bob), like Clean Code Architecture, Clean Coder and Clean Agile.
  • 👯 I’m looking to collaborate to some cool open-source projects.



My recommended freerlancing platform is Toptal so I would highly recommend you to join it if you are interested in high-quality remote jobs.


opencv-face-recognition-python's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

opencv-face-recognition-python's Issues

AttributeError: 'NoneType' object has no attribute 'copy'


AttributeError Traceback (most recent call last)
in ()
9 #perform a prediction
10 predicted_img1 = predict(test_img1)
---> 11 predicted_img2 = predict(test_img2)
12 print("Prediction complete")
13

in predict(test_img)
4 def predict(test_img):
5 #make a copy of the image as we don't want to chang original image
----> 6 img = test_img.copy()
7 #detect face from the image
8 face, rect = detect_face(img)

issues on opencv 3.3.0 version

hi Ramiz,

First of all, great tutorial, thank you.

When i (pip) installed the requirement for this tutorial, I've got OpenCV 3.3.0 and it had some issues with your code.

I've done few adjustments to make it work:

  1. Changed the creation of the recognizer from
    face_recognizer = cv2.face.createLBPHFaceRecognizer()
    to
    face_recognizer = cv2.face.LBPHFaceRecognizer_create()

  2. In the predict function changed this
    label = face_recognizer.predict(face)
    to
    label, confidence = face_recognizer.predict(face)
    The "predict" returns you a value of confidence also, and that addition fixed the TypeError.

OpenCV-Face-Recognition-Python.py, Line 223, in <Module>

OpenCV (3.4.1) Error: Assertion Failed <ssize.width> 0 && ssize.height > 0> in cv::resize, file resize.cpp, line 4044

OpenCV-Face-Recognition-Python.py, Line 223, in faces, labels = prepare_training_data("training-data")

File "OpenCV-Face-Recognition-Python.py, Line 178, in prepare_training_data cv2.imshow("Training on image...", cv2.resize<image, (400,500)))

cv2.imshow("Training on image...", cv2.resize(image, (400, 500)))
cv2.error: OpenCV(3.4.1) resize.cpp:4044: error <215> ssize.width > 0 && ssize height > 0 in function cv::resize

Please is there a solution to this?

Face extraction

  1. Assuming some of the faces in the image is tilted, does OpenCV extracts faces and un-rotate them? (i.e. giving coordinates for rectangles where the frame of the rectangle are not parallel to the frame of the image)
  2. Can OpenCV detect faces when the face is partially covered (glasses, hygienic masks)?

matrix.cpp:310: error: (-215) s >= 0 in function cv::setSize

matrix.cpp:310: error: (-215) s >= 0 in function cv::setSize
i am getting this error at:
-> face_recognizer.train(faces, np.array(labels))

why?

this is a labels np.array:

[0 0 0 1 2 2 2 3 3 3 3 4 4 4 5 5]

and this is a list of faces:

[array([ 36, 22, 244, 244]), array([ 20, 23, 249, 249]), array([ 32, 20, 245, 245]), array([ 39, 26, 239, 239]), array([ 19, 23, 248, 248]), array([ 28, 22, 235, 235]), array([ 28, 11, 243, 243]), array([ 39, 26, 238, 238]), array([ 27, 26, 243, 243]), array([ 21, 21, 257, 257]), array([ 35, 30, 245, 245]), array([ 33, 28, 246, 246]), array([ 24, 20, 257, 257]), array([ 32, 27, 250, 250]), array([ 35, 31, 232, 232]), array([ 35, 25, 236, 236])]

what is a problem?

I have this error

AttributeError: 'module' object has no attribute 'face'
on line 246 ?
screenshot from 2018-03-05 21-31-20

cannot detect face with beard

sir ,
You have written a nice code .
I have a doubt -
sg_1
In the attached pic , face cannot be found .

Please suggest .
Thanks.

AttributeError: 'NoneType' object has no attribute 'copy'

Hello!

Ive changed all dir paths to local ones because I want to be able to run the script only by terminal. However when I run it I get the following errors:

File "/home/kokot/Downloads/opencv-face-recognition-python-master/OpenCV-Face-Recognition-Python.py", line 326, in
predicted_img1 = predict(test_img1)
File "/home/kokot/Downloads/opencv-face-recognition-python-master/OpenCV-Face-Recognition-Python.py", line 298, in predict
img = test_img.copy()
AttributeError: 'NoneType' object has no attribute 'copy'

error: (-215:Assertion failed) s >= 0 in function 'cv::setSize'

I am getting this error while training my classifier -
face_recog.train(faces,np.array(faceID))

The full code is as follow:

[import cv2
import os
import numpy as np
%config IPCompleter.greedy=True

def facedetection(test_img):

gray_img = cv2.cvtColor(test_img,cv2.COLOR_BGR2GRAY)

face_cascade= cv2.CascadeClassifier("C:\\Users\\Abhijeet Debadwar\\AppData\\Local\\Programs\\Python\\Python38\\Lib\\site-packages\\cv2\\\data\\haarcascade_frontalface_default.xml")

faces = face_cascade.detectMultiScale(gray_img ,1.42,10)

return faces, gray_img

def labels_for_training_data(directory):

faces =[]
faceID= []

for maindirectory, subdirectory,filenames in os.walk(directory):
    
    for filename in filenames:
        
        if filename[0]==".":
            print("skipped")
            continue
            
        id = os.path.basename(maindirectory)
        
        img_path = os.path.join(maindirectory,filename)
        
        print("img_path ",img_path)
        print("image ID: ", id)
        
        test_img = cv2.imread(img_path)
        
        if test_img is None:
            print("IMAGE NOT LOADED PROPERLY")
            continue
                 
        faces_rect, gray_img = facedetection(test_img)
        
        if len(faces_rect)!=1:
            continue
            
        (x,y,w,h)= faces_rect[0]
        
        ROF_gray = gray_img[x:x+w,y:y+h]
        
        faces.append(ROF_gray)
        faceID.append(int(id))
        
        
return faces, faceID

def train_classifier(faces,faceID):

face_recog = cv2.face.LBPHFaceRecognizer_create()

face_recog.train(faces,np.array(faceID))

return face_recog

def draw_rect(test_img,faces):

(x,y,w,h) = faces

cv2.rectange(test_img,(x,y), (x+w,y+h),(255,0,0), 5)

def put_text(test_img,text,x,y):

cv2.putText(test_img,text, (x,y), cv2.FONT_HERSHEY_DUPLEX, 5, (0,255,0),5)

test_img = cv2.imread("D:\testimages\1\IMG_20190611_185155.jpg",1)

face_detected, gray_img = facedetection(test_img)

faces, faceID = labels_for_training_data("D:\testimages")

face_recog = train_classifier( faces, faceID)

name = { 1: "ABHIJEET", 0: "HERO"}

for face in face_detected:

(x,y,w,h)= face

roi_gray= gray_img[x:x+w,y:y+h]

label,confidencelevel = face_recog.predict(roi_gray)

print(" confidence ", confidencelevel)
print("label " , label)

draw_rect(test_img, face)

predicted_name = name[label]

put_text(test_img,predicted_name, x,y)


resized_img = cv2.resize(test_img, (500,500))

cv2.imshow("ABHIJEET S NICE", resized_img)

cv2.waitKey(0)

cv2.destroyAllWindows()                

Error:Int object is not Iterable

Gives the Following Error:
Preparing data...
Data prepared
('Total faces: ', 23)
('Total labels: ', 23)
Predicting images...
Traceback (most recent call last):
File "OpenCV-Face-Recognition-Python.py", line 328, in
predicted_img1 = predict(test_img1)
File "OpenCV-Face-Recognition-Python.py", line 306, in predict
label, confidence = face_recognizer.predict(face)
TypeError: 'int' object is not iterable

You can view my forked code because I made some Changes.

training on EigenFace and FisherFace failed.

by just commenting LBF and uncommenting FisherFace or EigenFace I am getting the following error

face_recognizer.train(faces, np.array(labels))
cv2.error: OpenCV(3.4.1) /home/majid/opencv_contrib/modules/face/src/eigen_faces.cpp:72: error: (-210) In the Eigenfaces method all input samples (training images) must be of equal size! Expected 143641 pixels, but was 6724 pixels. in function train

can't install opencv-contrib-python

I made a virtual environment and installed OpenCV but I found out that I need to install opencv-contrib-python and I'm getting this error message:
Could not install packages due to an EnvironmentError: [WinError 5] Access is denied: 'c:\users\asmah\envs\opencv\Lib\site-packages\cv2\cv2.cp36-win_amd64.pyd'
Consider using the --user option or check the permissions.
I tried using '--user' but I got this error message:
Can not perform a '--user' install. User site-packages are not visible in this virtualenv.
What should I do?

cv2.error: OpenCV(3.4.2) /opt/concourse/worker/volumes/live/9523d527-1b9e-48e0-7ed0-a36adde286f0/volume/opencv-suite_1535558719691/work/opencv_contrib-3.4.2/modules/face/src/eigen_faces.cpp:72: error: (-210:Unsupported format or combination of formats) In the Eigenfaces method all input samples (training images) must be of equal size! Expected 8100 pixels, but was 6561 pixels. in function 'train'

hello!
when I use
ace_recognizer = cv2.face.FisherFaceRecognizer_create()
or
face_recognizer = cv2.face.EigenFaceRecognizer_create()
There is an error:cv2.error: OpenCV(3.4.2) /opt/concourse/worker/volumes/live/9523d527-1b9e-48e0-7ed0-a36adde286f0/volume/opencv-suite_1535558719691/work/opencv_contrib-3.4.2/modules/face/src/fisher_faces.cpp:81: error: (-210:Unsupported format or combination of formats) In the Fisherfaces method all input samples (training images) must be of equal size! Expected 8836 pixels, but was 6724 pixels. in function 'train'

why ?
thank you !

Error no attribute 'face'

face_recognizer = cv2.face.LBPHFaceRecognizer_create()

AttributeError: module 'cv2.cv2' has no attribute 'face'

Why this happened?: SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set

Dear sir.
When I practice face detection by using the code as follows and an error happened as mentioned in Title.
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('opencv-files/lbpcascade_frontalface.xml')
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.0, minNeighbors=5)
I am sure that the the file "lbpcascade_frontalface.xml" has in the folder "opencv-files" which I created.
by the way, I got "lbpcascade_frontalface.xml" from my colleague because I don't know where to download this file on web.
I am looking forward for your answer, thank you very much.

At least two classes are needed to perform a LDA

OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-i1s8y2i1\opencv\modules\core\src\lda.cpp:1087: error: (-5:Bad argument) At least two classes are needed to perform a LDA. Reason: Only one class was given! in function 'cv::LDA::lda'

Detect multiple people but only RECOGNIZE one

Hi,

So, i have one question. In your datasets you used 2 classes... thus, is it possible to just overfit to a single one? We've been testing for a while and i'm not being able to recognize only the person that i want.

Thanks in advance.

Testing with other imagees

In the Fisherfaces method all input samples must be of equal size! Expected 12544 pixels, but was 48400 pixels. In function 'cv::face::Fisherfaces::train'

Model Training

Hi,

I have one question about the training. Shall I use one image or serveral images to train for one people ?

Same name (ID) on different faces

Hi Rameez, I am having a problem in face recognition.

I used LBPHFaceRecognizer() in training and pre trained HaarCascadeClassifier in face recognition. I used two samples faces and 30 images of each face.

Whenever I tested the model using a new face (that the model has not ben trained for) instead of showing un known it every time shows the name of the first face which I used for the training.

Can you help me in this regard?

Similarity scores

How to get similarity scores (by setting threshold??), can I use confidence as similarity score??

Want to know more about the teory

hi, thankyou, its really easy to understand your explaination about the theory of LBPH, but how about the classification, what theory that openCV use, can you explain that to? or do you have any reference that explaining the classification? Thankyou very much

TypeError: list indices must be integers or slices, not tuple

Preparing data...
Data prepared
Total faces: 23
Total labels: 23
Predicting images...
Traceback (most recent call last):
File "/Users/avadhesh/Downloads/opencv-face-recognition-python-master/OpenCV-Face-Recognition-Python.py", line 329, in
predicted_img1 = predict(test_img1)
File "/Users/avadhesh/Downloads/opencv-face-recognition-python-master/OpenCV-Face-Recognition-Python.py", line 309, in predict
label_text = subjects[label]
TypeError: list indices must be integers or slices, not tuple

Process finished with exit code 1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.