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.

89 lines
2.9KB

  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. modded_img = mod_function(img, eyes)
  35. if modded_img is not None:
  36. img = modded_img
  37. elif operates_on == mods.FACE:
  38. modded_img = mod_function(img, face)
  39. if modded_img is not None:
  40. img = modded_img
  41. # Add elements to image
  42. add_elements(img)
  43. # if there are no faces, just add more elements!
  44. if len(faces) < 1:
  45. add_elements(img)
  46. # randomize if high contrast is used
  47. choice = rd.randint(0, 1)
  48. if choice == 1:
  49. # edit alpha and beta to adjust contrast levels
  50. img = cv2.convertScaleAbs(img, alpha=1.2, beta=35)
  51. # randomize if high noise is used
  52. choice = rd.randint(0, 1)
  53. if choice == 1:
  54. row, col, ch = img.shape
  55. mean = 0
  56. # edit var to modify the amount of noise in the image
  57. var = 15
  58. sigma = var ** 1
  59. gauss = np.random.normal(mean, sigma, (row, col, ch))
  60. gauss = gauss.reshape(row, col, ch)
  61. noisy = (img + gauss)
  62. cv2.normalize(noisy, noisy, 0, 1, cv2.NORM_MINMAX)
  63. img = noisy
  64. # The following code is useful to determine if faces and eyes
  65. # are being read correctly. Uncommenting will draw boxes around
  66. # found features.
  67. # for (x,y,w,h) in faces:
  68. # cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
  69. # roi_gray = gray[y:y+h, x:x+w]
  70. # roi_color = img[y:y+h, x:x+w]
  71. # #edit the second and third parameter if feature detection is poor
  72. # eyes = eye_cascade.detectMultiScale(roi_gray, 1.2, 6)
  73. # for (ex,ey,ew,eh) in eyes:
  74. # cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
  75. return img