ageitgey / face_recognition

The world's simplest facial recognition api for Python and the command line
MIT License
52.86k stars 13.43k forks source link

how to load opencv object to face_recognition. load_image_file() api does not work for this. #441

Open axilaris opened 6 years ago

axilaris commented 6 years ago

Description

I would like to load an opencv image object into face_recognition library. I am using Django Python, I have used opencv to crop and do some face quality checks.

Here is what something I did:

im_cv = cv2.imread(im_path) result, checked_cv_image = process(im_cv)

unknown_image = face_recognition.load_image_file(checked_cv_image) <-- this doesnt work

it complaints: AttributeError: 'numpy.ndarray' object has no attribute 'read'

What I Did

unknown_image = face_recognition.load_image_file(checked_cv_image) <-- this doesnt work, checked_cv_image is an opencv object

I would like to know is it possible to figure a way how to load an opencv object into face_recognition object.

Thanks, really appreciate there is a way for this.

axilaris commented 6 years ago

I tried to use, this:

ret, buf_checked_cv_image = cv2.imencode( '.png', checked_cv_image ) unknown_image = face_recognition.load_image_file(buf_checked_cv_image)

this doesnt work as well, same error: AttributeError: 'numpy.ndarray' object has no attribute 'read'

MLDSBigGuy commented 6 years ago

Never use cv2 for simple tasks. use PIL. cv2 will create pbs in future.

ageitgey commented 6 years ago

@axilaris You don't need to load the image. You already did that with cv2.imread(). Since you already have a numpy array, you don't need to call load_image_file(). You can just pass your numpy array directly to other python functions.

The one issue you will hit is that OpenCV stores images in Blue/Green/Red order but face_recognition uses Red/Green/Blue order. So you do need to convert that before using it.

    # convert your BGR image to RGB
    rgb_image = checked_cv_image[:, :, ::-1] 

    # then use the image however you want
    encodings = face_recognition.face_encodings(rgb_image)
axilaris commented 6 years ago

thank you very much @ageitgey

I tried all 3 this methods with this png image https://goo.gl/vTLCzH:

face_location_unknown = face_recognition.face_locations(rgb_image)
face_unknown = face_recognition.face_encodings(rgb_image)[0]
face_unknown = face_recognition.face_encodings(rgb_image)

but got this error:

    RuntimeError: Unsupported image type, must be 8bit gray or RGB image.

Do I need to solve with with opencv + ffmpeg, the instructions seems to be for ubuntu, i am using mac, looking at how to do it for mac. ? https://github.com/ageitgey/face_recognition#common-issues

axilaris commented 6 years ago

I should have ffmpeg:

import cv2 cv2.getBuildInformation()

Version control: 3.4.0-dirty Platform: Timestamp: 2017-12-28T20:44:14Z Host: Darwin 16.7.0 x86_64 Target: Darwin 16 x86_64 CMake: 3.9.4 CMake generator: Unix Makefiles CMake build tool: /usr/bin/make ........................................ NO GUI: QT: YES (ver 4.8.7 EDITION = OpenSource) QT OpenGL support: NO Cocoa: YES Media I/O: ZLib: build (ver 1.2.11) JPEG: build (ver 90) WEBP: build (ver encoder: 0x020e) PNG: build (ver 1.6.34) TIFF: build (ver 42 - 4.0.9) JPEG 2000: build (ver 1.900.1) OpenEXR: build (ver 1.7.1) Video I/O: DC1394: NO FFMPEG: YES avcodec: YES (ver 57.107.100) avformat: YES (ver 57.83.100) avutil: YES (ver 55.78.100) swscale: YES (ver 4.8.100) avresample: YES (ver 3.7.0) GStreamer: NO AVFoundation: YES gPhoto2: NO Parallel framework: GCD Trace: YES (with Intel ITT) Other third-party libraries: Intel IPP: 2017.0.3 [2017.0.3] at: /Users/travis/build/skvark/opencv- .........

laldeo commented 6 years ago

@axilaris change "read" to "load".