Advertisement
kalin729

lab4 no extras

Jun 13th, 2024
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. # Importing libraries
  2. import numpy as np
  3. import cv2
  4. import matplotlib.pyplot as plt
  5. import argparse
  6. from datetime import datetime
  7.  
  8. # A function for plotting the images
  9.  
  10.  
  11. def plotImages(img):
  12.     plt.imshow(img, cmap="gray")
  13.     plt.axis('off')
  14.     plt.style.use('seaborn')
  15.     plt.gcf()
  16.  
  17.     # Shows the image
  18.     plt.show()
  19.    
  20. faces = []
  21.  
  22. # Reading an image using OpenCV
  23. # OpenCV reads images by default in BGR format
  24. image_src = "arctic.jpg"
  25. image = cv2.imread(image_src)
  26.  
  27. # Converting BGR image into a RGB image
  28. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  29.  
  30. # plotting the original image (remove comment to see the original image, then the image with the detected faces)
  31. # plotImages(image)
  32.  
  33. parser = argparse.ArgumentParser(description='Face detection')
  34. parser.add_argument('--face_cascade', help='Path to face cascade.', default='haarcascade_frontalface_alt.xml')
  35. parser.add_argument('--face_cascade2', help='Path to face cascade 2.', default='haarcascade_frontalface_alt_tree.xml')
  36. parser.add_argument('--face_cascade3', help='Path to face cascade 3.', default='haarcascade_frontalface_alt2.xml')
  37. parser.add_argument('--face_cascade4', help='Path to face cascade 4.', default='haarcascade_frontalface_default.xml')
  38. parser.add_argument('--face_cascade5', help='Path to face cascade 5.', default='haarcascade_frontalface_extended.xml')
  39. parser.add_argument('--face_cascade6', help='Path to face cascade 6.', default='haarcascade_profileface.xml')
  40. # parser.add_argument('--eyes_cascade', help='Path to eyes cascade.', default='data/haarcascades/haarcascade_eye_tree_eyeglasses.xml')
  41. parser.add_argument('--camera', help='Camera divide number.', type=int, default=0)
  42. args = parser.parse_args()
  43.  
  44. face_cascade_name = args.face_cascade
  45. # eyes_cascade_name = args.eyes_cascade
  46.  
  47. face_cascade = cv2.CascadeClassifier()
  48. # eyes_cascade = cv2.CascadeClassifier()
  49.  
  50. if not face_cascade.load(cv2.samples.findFile(face_cascade_name)):
  51.  print('--(!)Error loading face cascade')
  52.  exit(0)
  53.  
  54. faces = face_cascade.detectMultiScale(image, 1.1, 2)
  55.  
  56. # Draw rectangle around the faces which is our region of interest (ROI)
  57. for (x, y, w, h) in faces:
  58.     cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
  59.     roi = image[y:y+h, x:x+w]
  60.     # applying a gaussian blur over this new rectangle area
  61.     roi = cv2.blur(roi, (20, 20))
  62.     # impose this blurred image on original image to get final image
  63.  
  64.     image[y:y+roi.shape[0], x:x+roi.shape[1]] = roi
  65.  
  66. # print("Faces detected in the image:", len(faces))
  67. # Display the output
  68. plotImages(image)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement