python-tensorflowHow can I use Python and TensorFlow to detect faces?
Using Python and TensorFlow, you can detect faces by first creating a Convolutional Neural Network (CNN) model and then using it to detect faces in images.
Example code
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
# Create a CNN model
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)))
model.add(MaxPooling2D(2, 2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile and fit the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10)
Output example
Epoch 1/10
240/240 [==============================] - 1s 6ms/step - loss: 0.6851 - accuracy: 0.6375
Epoch 2/10
240/240 [==============================] - 1s 5ms/step - loss: 0.6288 - accuracy: 0.7083
...
The code above creates a CNN model with two convolutional layers, two max pooling layers, and two dense layers. The model is then compiled and fit on the training data.
Code explanation
import tensorflow as tf
: This imports the TensorFlow library.from tensorflow.keras.models import Sequential
: This imports the Sequential model from the Keras library.from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
: This imports the convolutional, max pooling, flatten, and dense layers from the Keras library.model = Sequential()
: This creates an empty Sequential model.model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)))
: This adds a convolutional layer with 32 filters of size 3x3 and ReLU activation.model.add(MaxPooling2D(2, 2))
: This adds a max pooling layer with a pooling size of 2x2.model.add(Flatten())
: This flattens the output of the max pooling layer.model.add(Dense(128, activation='relu'))
: This adds a dense layer with 128 neurons and ReLU activation.model.add(Dense(1, activation='sigmoid'))
: This adds a dense layer with 1 neuron and sigmoid activation.model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
: This compiles the model with the Adam optimizer and binary cross-entropy loss function.model.fit(x_train, y_train, epochs=10)
: This fits the model on the training data for 10 epochs.
Helpful links
More of Python Tensorflow
- How do I resolve a SymbolAlreadyExposedError when the symbol "zeros" is already exposed as () in TensorFlow Python util tf_export?
- How can I use TensorFlow Lite with XNNPACK in Python?
- How do I install Python TensorFlow on Windows?
- How do I check the version of Python Tensorflow I'm using?
- How do I show the version of Python TensorFlow I am using?
- How can I use Python TensorFlow in W3Schools?
- How can I check the compatibility of different versions of Python and TensorFlow?
- How do I fix the error "unrecognized type class 'tensorflow.python.framework.ops.eagertensor'"?
- How do I upgrade my Python TensorFlow version?
- How can I use YOLOv3 with Python and TensorFlow?
See more codes...