shamangary / Keras-real-time-age-gender-estimation-demo

A demo for real-time age-gender estimation in keras

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Keras-real-time-age-gender-estimation-demo

I use the following code to train the keras model https://github.com/yu4u/age-gender-estimation (a very great github post)

  • Unlike the demo.py in the original github, I made several adjustments for the demo. The demo now supports both python2.7 and python3.5.

--

1. Small model

TYY_1stream:

        inputs = Input(shape=self._input_shape)

        x = Conv2D(32,(3,3),activation='relu')(inputs)
        x = MaxPooling2D(2,2)(x)
        x = Conv2D(32,(3,3),activation='relu')(x)
        x = MaxPooling2D(2,2)(x)
        x = Conv2D(64,(3,3),activation='relu')(x)
        x = MaxPooling2D(2,2)(x)
        x = Conv2D(64,(3,3),activation='relu')(x)
        x = BatchNormalization(axis=self._channel_axis)(x)

        # Classifier block
        pool = AveragePooling2D(pool_size=(4, 4), strides=(1, 1), padding="same")(x)
        flatten = Flatten()(pool)
        predictions_g = Dense(units=2, kernel_initializer=self._weight_init, use_bias=self._use_bias,
                              kernel_regularizer=l2(self._weight_decay), activation="softmax")(flatten)
        predictions_a = Dense(units=21, kernel_initializer=self._weight_init, use_bias=self._use_bias,
                              kernel_regularizer=l2(self._weight_decay), activation="softmax")(flatten)

        model = Model(inputs=inputs, outputs=[predictions_g, predictions_a])

2. Demo (Use "moviepy" instead of "cv2" for the frame of the video!!!)

There are a lot of issues of using cv2.VideoCapture() ContinuumIO/anaconda-issues#121

Anaconda will not install opencv3 with ffmpeg properly!

I have tried skvideo.io and pyav and something else...

[Replacement for cv2.VideoCapture] Using anaconda to install moviepy is the best option for python3.5/python2.7 with opencv3.1 or opencv3.2.

conda install -c conda-forge moviepy
conda install -c cogsci pygame

pygame is for showing the image with moviepy.

3. How to run?

  1. Put you video into the folder

  2. GPU with tensorflow backend (video name is the last term)

KERAS_BACKEND=tensorflow python TYY_demo.py mewtwo.mp4
  1. CPU with tensorflow backend (video name is the last term)
KERAS_BACKEND=tensorflow CUDA_VISIBLE_DEVICES='' python TYY_demo.py mewtwo.mp4

Python version for different display options

Since there are some problems in python2.7 for using cv2.imshow(), I set an option for python version choices.

  1. CPU with tensorflow backend with python2.7 or python3.5 (using img_clip.show())
KERAS_BACKEND=tensorflow CUDA_VISIBLE_DEVICES='' python TYY_demo.py mewtwo.mp4 '2'
  1. CPU with tensorflow backend with python3.5 (using cv2.imshow())
KERAS_BACKEND=tensorflow CUDA_VISIBLE_DEVICES='' python TYY_demo.py mewtwo.mp4 '3'

4. Dependencies

  1. Same as https://github.com/yu4u/age-gender-estimation
  2. moviepy
  3. pygame

5. Dependencies install guide (in Chinese)

http://shamangary.logdown.com/posts/3009851

About

A demo for real-time age-gender estimation in keras


Languages

Language:Python 100.0%