Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Importing libraries
- import numpy as np
- import cv2
- import matplotlib.pyplot as plt
- import argparse
- from datetime import datetime
- # A function for plotting the images
- def plotImages(img):
- plt.imshow(img, cmap="gray")
- plt.axis('off')
- plt.style.use('seaborn')
- plt.gcf()
- # Shows the image
- plt.show()
- faces = []
- # Reading an image using OpenCV
- # OpenCV reads images by default in BGR format
- image_src = "arctic.jpg"
- image = cv2.imread(image_src)
- # Converting BGR image into a RGB image
- image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
- # plotting the original image (remove comment to see the original image, then the image with the detected faces)
- # plotImages(image)
- parser = argparse.ArgumentParser(description='Face detection')
- parser.add_argument('--face_cascade', help='Path to face cascade.', default='haarcascade_frontalface_alt.xml')
- parser.add_argument('--face_cascade2', help='Path to face cascade 2.', default='haarcascade_frontalface_alt_tree.xml')
- parser.add_argument('--face_cascade3', help='Path to face cascade 3.', default='haarcascade_frontalface_alt2.xml')
- parser.add_argument('--face_cascade4', help='Path to face cascade 4.', default='haarcascade_frontalface_default.xml')
- parser.add_argument('--face_cascade5', help='Path to face cascade 5.', default='haarcascade_frontalface_extended.xml')
- parser.add_argument('--face_cascade6', help='Path to face cascade 6.', default='haarcascade_profileface.xml')
- # parser.add_argument('--eyes_cascade', help='Path to eyes cascade.', default='data/haarcascades/haarcascade_eye_tree_eyeglasses.xml')
- parser.add_argument('--camera', help='Camera divide number.', type=int, default=0)
- args = parser.parse_args()
- face_cascade_name = args.face_cascade
- # eyes_cascade_name = args.eyes_cascade
- face_cascade = cv2.CascadeClassifier()
- # eyes_cascade = cv2.CascadeClassifier()
- if not face_cascade.load(cv2.samples.findFile(face_cascade_name)):
- print('--(!)Error loading face cascade')
- exit(0)
- faces = face_cascade.detectMultiScale(image, 1.1, 2)
- # Draw rectangle around the faces which is our region of interest (ROI)
- for (x, y, w, h) in faces:
- cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
- roi = image[y:y+h, x:x+w]
- # applying a gaussian blur over this new rectangle area
- roi = cv2.blur(roi, (20, 20))
- # impose this blurred image on original image to get final image
- image[y:y+roi.shape[0], x:x+roi.shape[1]] = roi
- # print("Faces detected in the image:", len(faces))
- # Display the output
- plotImages(image)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement