Computer Vision News - April 2022
11 Facial Recognition with Open CV2 from keras_vggface.vggface import VGGFace from tensorflow.keras import datasets, layers, models from keras.optimizers import RMSprop, SGD from keras_vggface.utils import preprocess_input from keras_vggface.utils import decode_predictions from numpy import asarray from keras.applications.vgg16 import VGG16 from keras.applications.vgg16 import preprocess_input from keras.preprocessing.image import load_img from keras.preprocessing.image import img_to_array from keras.models import Model from matplotlib import pyplot from numpy import expand_dims from keras.preprocessing import image Step 1.2: Load Data/Image We have various ways to load images e.g. OpenCV, MatplotLib, PIL, etc. Read and Write Images Example using OpenCV python cv2.imwrite(file_path (str), image (numpy.ndarray)) cv2.imread(file_path (str), read_mode (int)) Read Modes • 1 = cv2.IMREAD_COLOR • 0 = cv2.IMREAD_GRAYSCALE • -1 = cv2.IMREAD_UNCHANGED Load a Sample Smith image using OpenCV library # Read Image using OpenCV import cv2 img =cv2.imread("/content/Facial-Recognition-MMAI844-Tutorial/sample/Smith.jpg",1) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.imshow(img) plt.show() # Read Image using matplotlib import matplotlib.image as mpimg import matplotlib.pyplot as plt img = mpimg.imread('/content/Facial-Recognition-MMAI844-Tutorial/sample/Smith.jpg') plt.imshow(img) Step 1.3: Detecting Faces with OpenCV and front face detector xml detector = cv2.CascadeClassifier( xml_file_path) face_coord = detector.detectMultiScale(image, scale_factor, min_neighbors, min_size, flags) face_coord: Numpy array with rows equal to [x, y, width, height] Let’s import a facial image to detect the face frame= cv2.imread('/content/Facial-Recognition-MMAI844-Tutorial/sample/channing_tatum.jpg') name= "channing_tatum" plt_show(frame)
Made with FlippingBook
RkJQdWJsaXNoZXIy NTc3NzU=