You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.8KB

  1. import random as rd
  2. import cv2
  3. import numpy as np
  4. from .elements import add_single_element, add_elements
  5. from . import mods
  6. def vaporize(image_path="testImgs/testface9.png"):
  7. face_cascade = cv2.CascadeClassifier('cascade/haarcascade_frontalface_default.xml')
  8. eye_cascade = cv2.CascadeClassifier('cascade/haarcascade_eye.xml')
  9. # load main image from local file
  10. img = cv2.imread(image_path)
  11. # height, width, depth = img.shape
  12. # turn image gray for detecting face and eyes
  13. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  14. # find all the faces in the image
  15. faces = face_cascade.detectMultiScale(gray, 1.3, 5)
  16. # go through each face
  17. for face in faces:
  18. y = face[1]
  19. x = face[0]
  20. w = face[2]
  21. h = face[3]
  22. roi_gray = gray[y:y + h, x:x + w]
  23. # roi_color = img[y:y + h, x:x + w]
  24. # find each eye. Modify second and third parameter if
  25. # feature detection is poor
  26. eyes = eye_cascade.detectMultiScale(roi_gray, 1.2, 6)
  27. for eye in eyes:
  28. eye[0] += face[0]
  29. eye[1] += face[1]
  30. # randomize which face modification will be performed
  31. eyes_present = len(eyes) >= 2
  32. mod_function, operates_on = mods.determine_face_mod(eyes_present)
  33. if operates_on == mods.EYES:
  34. mod_function(img, eyes)
  35. elif operates_on == mods.FACE:
  36. mod_function(img, face)
  37. # Add elements to image
  38. add_elements(img)
  39. # if there are no faces, just add more elements!
  40. if len(faces) < 1:
  41. add_elements(img)
  42. # randomize if high contrast is used
  43. choice = rd.randint(0, 1)
  44. if choice == 1:
  45. # edit alpha and beta to adjust contrast levels
  46. img = cv2.convertScaleAbs(img, alpha=1.2, beta=35)
  47. # randomize if high noise is used
  48. choice = rd.randint(0, 1)
  49. if choice == 1:
  50. row, col, ch = img.shape
  51. mean = 0
  52. # edit var to modify the amount of noise in the image
  53. var = 15
  54. sigma = var ** 1
  55. gauss = np.random.normal(mean, sigma, (row, col, ch))
  56. gauss = gauss.reshape(row, col, ch)
  57. noisy = (img + gauss)
  58. cv2.normalize(noisy, noisy, 0, 1, cv2.NORM_MINMAX)
  59. img = noisy
  60. # The following code is useful to determine if faces and eyes
  61. # are being read correctly. Uncommenting will draw boxes around
  62. # found features.
  63. # for (x,y,w,h) in faces:
  64. # cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
  65. # roi_gray = gray[y:y+h, x:x+w]
  66. # roi_color = img[y:y+h, x:x+w]
  67. # #edit the second and third parameter if feature detection is poor
  68. # eyes = eye_cascade.detectMultiScale(roi_gray, 1.2, 6)
  69. # for (ex,ey,ew,eh) in eyes:
  70. # cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
  71. return img