Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. import random
  2. from _ast import mod
  3.  
  4. import tensorflow as tf
  5. import numpy as np
  6. from multiprocessing import Process
  7. import cv2
  8. import os
  9. import copy
  10.  
  11. import moviepy.editor as mpe
  12.  
  13. import cv2
  14. from time import sleep
  15.  
  16. minimum_probability = 0.6
  17. sizes = [10,20,50,100]
  18. IMG_SIZE = 96
  19.  
  20. faces = tf.keras.models.load_model("model/arctur8.h5")
  21. plates = tf.keras.models.load_model("model/letters2.h5")
  22.  
  23. video = mpe.VideoFileClip('video.mp4')
  24. frames = int(video.fps * video.duration)
  25. print(video.fps)
  26.  
  27. def prepare(image):
  28. IMG_SIZE = 136
  29. new_array = cv2.resize(image, (IMG_SIZE, IMG_SIZE))
  30. return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
  31.  
  32. def image_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
  33. dim = None
  34. (h, w) = image.shape[:2]
  35. if width is None and height is None:
  36. return image
  37. if width is None:
  38. r = height / float(h)
  39. dim = (int(w * r), height)
  40. else:
  41. r = width / float(w)
  42. dim = (width, int(h * r))
  43. resized = cv2.resize(image, dim, interpolation=inter)
  44. return resized
  45.  
  46. def get_face_probability(img):
  47. # img2 = cv2.resize(img,(136,136))
  48. prediction = faces.predict([prepare(img)])
  49. return 1 - prediction[0]
  50.  
  51. def get_plate_probability(img):
  52. # img2 = cv2.resize(img,(136,136))
  53. prediction = plates.predict([prepare(img)])
  54. return 1 - prediction[0]
  55.  
  56. def check_face(y, x, h, w, imgG, img, size):
  57. cropped = imgG[x: x + size, y: y + size]
  58. plate_probability = get_plate_probability(cropped)
  59. face_probability = get_face_probability(cropped)
  60. if face_probability > minimum_probability or plate_probability > minimum_probability:
  61. cv2.rectangle(img, (y, x), (y + size, x + size), (size * 1.6, 255, 255 - size * 1.2), cv2.FILLED)
  62. return img
  63.  
  64. def censor(img):
  65. h, w, a = img.shape
  66. # print(h,w,a)
  67. img = image_resize(img, height=480)
  68. imgG = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  69. h, w = imgG.shape
  70. # print(w,h)
  71. # num = gcd(w,h)
  72. # counter = 0
  73. print(h, w)
  74. for size in range((h + w) // 12, 25, -12):
  75. for x in range(0, h - size, size // 6):
  76. for y in range(0, w - size, size // 6):
  77. # counter += 1
  78. # print(x, y, size)
  79. img = check_face(y, x, h, w, imgG, img, size)
  80. #tmp_img = copy.deepcopy(img)
  81. #cv2.rectangle(tmp_img, (y, x), (y + size, x + size), (0, 0, 255))
  82. #cv2.imshow('image', tmp_img)
  83. #cv2.waitKey(1)
  84. # print(str(x)+"/"+str(h),str(y)+"/"+str(w),size)
  85. # imgc = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
  86. #cv2.imwrite(imagepath, img)
  87. #cv2.destroyAllWindows()
  88. return img
  89.  
  90. clip = mpe.VideoFileClip("video.mp4")
  91. new_frames = [frame for frame in clip.iter_frames()]
  92. new_clip = mpe.ImageSequenceClip(new_frames,fps=clip.fps)
  93. new_clip.write_videofile("test.mp4")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement