Code Monkey home page Code Monkey logo

face-recognition-server's Introduction

Face recognition with webrtc, websockets and opencv

This is a webrtc, websockets and opencv experiment developed during a athega hackday.

How does it work?

Frames are captured from the web camera via webrtc and sent to the server over websockets. On the server the frames are processed with opencv and a json response is sent back to the client.

Sample json response:

  {
    "face": {
      "distance": 428.53381034802453,
      "coords": {
        "y": "39",
        "x": "121",
        "height": "137",
        "width": "137"
      },
      "name": "mike"
    }
  }

Everything except distance is pretty self explanatory.

  • name is the predicted name of the person in front of the camera.

  • coords is where the face is found in the image.

  • distance is a measurement on how accurate the prediction is, lower is better.

If we can't get a reliable prediction (10 consecutive frames that contains a face and with a distance lower than 1000) we switch over to training mode. In training mode we capture 10 images and send them together with a name back to the server for retraining. After the training has been completed we switch back to recognition mode and hopefully we'll get a more accurate result.

Running it

Make sure the dependencies are met.

Create the database by issuing the following in the data folder sqlite3 images.db < ../db/create_db.sql.

Download the AT&T face database and extract it to data/images before the server is started. This is needed to build the initial prediction model.

cd data
wget http://www.cl.cam.ac.uk/Research/DTG/attarchive/pub/data/att_faces.tar.Z
tar zxvf att_faces.tar.Z
mv att_faces images

Copy haarcascade_frontalface_alt.xml from <path to opencv source>/data/haarcascades/ to the data folder.

Run with python server.py and browse to http://localhost:8888 when the model has been trained.

face-recognition-server's People

Contributors

dentarg avatar ragulin avatar

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

face-recognition-server's Issues

WebSocket is already in CLOSING or CLOSED state.

image

everytime i try to start training, save a new name, it keeps saving, always "saving label" and when i checked the console, it said the websocket is already in closing. how do i fix it? thank you.

Socket Connection Closed

I am not sure if this is an issue with my install or not but my server is unable to access ws://localhost:8888/predict. It looks like there is no socket server listening. Do you know what I may have missed or how to fix this? From what I have read it looks like Tornado is handling the sockets.

From Google Chrome's error console

WebSocket connection to 'ws://localhost:8888/predict' failed: Connection closed before receiving a handshake response 

I am also seeing this in the console

WebSocket is already in CLOSING or CLOSED state. 

Resize error

OpenCV Error: Assertion failed (ssize.area() > 0) in resize, file /usr/local/opencv/modules/imgproc/src/imgwarp.cpp, line 1824
Traceback (most recent call last):
File "server.py", line 121, in
main()
File "server.py", line 114, in main
opencv.train()
File "/home/petaoctet/Desktop/facerec/opencv.py", line 86, in train
images, labels = load_images_from_db()
File "/home/petaoctet/Desktop/facerec/opencv.py", line 78, in load_images_from_db
cv_image = cv2.resize(cv_image, (100,100))
cv2.error: /usr/local/opencv/modules/imgproc/src/imgwarp.cpp:1824: error: (-215) ssize.area() > 0 in function resize

Getting it running

I have a small issue with setting up the system. I think the database is the issue. I don't seem to be getting anything on the DB. I'm constantly getting an error saying "sqlite3.OperationalError: no such table: label"

Please help me.

Training failed

When i set up the system and then load localhost:8888 on the broser, the page opens and it notifies me saying it doesn't recognize me. So i enter my name and click ok. I get a few exceptions caught in the terminal.

The error is in the line gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) //opencv.py", line 23.
The directory is created but there is no images in the directory in the images folder.

Please look into this matter

harvesting error

[E 140101 23:45:19 websocket:306] Uncaught exception in /harvesting
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tornado/websocket.py", line 303, in wrapper
return callback(_args, *_kwargs)
File "server.py", line 55, in on_message
self.process(image)
File "server.py", line 94, in process
result = opencv.Image(label=label).persist(cv_image)
File "/root/School/Face/face-recognition-server-master/opencv.py", line 159, in persist
cropped = to_grayscale(crop_faces(cv_image, faces))
File "/root/School/Face/face-recognition-server-master/opencv.py", line 41, in crop_faces
return img[y:y+h,x:x+w]
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 512, in getattr
raise AttributeError(name)
AttributeError: getitem

After load_images_from_db, lists 'images' and 'labels' are returned empty

When I ran the program, it showed me all the info including 'saving paths..' then I encountered this error,

 OpenCV Error: Bad argument (Empty training data was given. You'll need more than one sample to learn a model.) in train, file /io/opencv_contrib/modules/face/src/fisher_faces.cpp, line 71
Traceback (most recent call last):
  File "server.py", line 121, in <module>
    main()
  File "server.py", line 114, in main
    opencv.train()
  File "/home/sashank/face-recognition-server/opencv.py", line 89, in train
    model.train(images,labels)
cv2.error: /io/opencv_contrib/modules/face/src/fisher_faces.cpp:71: error: (-5) Empty training data was given. You'll need more than one sample to learn a model. in function train

Turns out, the images and labels lists are empty.
Checked the 'load_images_from_db()' function and I figured that this code, doesn't even run -

try:
        cv_image = cv2.imread(image.path, cv2.IMREAD_GRAYSCALE)
        cv_image = cv2.resize(cv_image, (100,100))
        images.append(np.asarray(cv_image, dtype=np.uint8))
        labels.append(label.id)
      except IOError, (errno, strerror):
       print "IOError({0}): {1}".format(errno, strerror)

That's because it isn't entering the for image in label.image_set: loop!

I have no idea whats wrong and I'm struggling with this from a long time. The only changes I made to the original code are,
Changed label.save() to label[0].save() and image.save() to image[0].save() since I was encountering the

image.save()
AttributeError: 'tuple' object has no attribute 'save'

and image = Image.get_or_create(path=path, label = label) to image = Image.get_or_create(path=path) since i was having this error -

cursor.execute(sql, params or ())
peewee.OperationalError: near ",": syntax error

Really require help with this. Thanks a lot.

How to add new face?

Once a face is added, it keeps showing that particular face. How to add a new face?

predict error

[E 140101 23:41:21 websocket:306] Uncaught exception in /predict
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tornado/websocket.py", line 303, in wrapper
return callback(_args, *_kwargs)
File "server.py", line 55, in on_message
self.process(image)
File "server.py", line 104, in process
result = opencv.predict(cv_image)
File "/root/School/Face/face-recognition-server-master/opencv.py", line 100, in predict
cropped = to_grayscale(crop_faces(cv_image, faces))
File "/root/School/Face/face-recognition-server-master/opencv.py", line 41, in crop_faces
return img[y:y+h,x:x+w]
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 512, in getattr
raise AttributeError(name)
AttributeError: getitem

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.