Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- from _ast import mod
- import tensorflow as tf
- import numpy as np
- from multiprocessing import Process
- import cv2
- import os
- import copy
- import moviepy.editor as mpe
- import cv2
- from time import sleep
- minimum_probability = 0.6
- sizes = [10,20,50,100]
- IMG_SIZE = 96
- faces = tf.keras.models.load_model("model/arctur8.h5")
- plates = tf.keras.models.load_model("model/letters2.h5")
- video = mpe.VideoFileClip('video.mp4')
- frames = int(video.fps * video.duration)
- print(video.fps)
- def prepare(image):
- IMG_SIZE = 136
- new_array = cv2.resize(image, (IMG_SIZE, IMG_SIZE))
- return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
- def image_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
- dim = None
- (h, w) = image.shape[:2]
- if width is None and height is None:
- return image
- if width is None:
- r = height / float(h)
- dim = (int(w * r), height)
- else:
- r = width / float(w)
- dim = (width, int(h * r))
- resized = cv2.resize(image, dim, interpolation=inter)
- return resized
- def get_face_probability(img):
- # img2 = cv2.resize(img,(136,136))
- prediction = faces.predict([prepare(img)])
- return 1 - prediction[0]
- def get_plate_probability(img):
- # img2 = cv2.resize(img,(136,136))
- prediction = plates.predict([prepare(img)])
- return 1 - prediction[0]
- def check_face(y, x, h, w, imgG, img, size):
- cropped = imgG[x: x + size, y: y + size]
- plate_probability = get_plate_probability(cropped)
- face_probability = get_face_probability(cropped)
- if face_probability > minimum_probability or plate_probability > minimum_probability:
- cv2.rectangle(img, (y, x), (y + size, x + size), (size * 1.6, 255, 255 - size * 1.2), cv2.FILLED)
- return img
- def censor(img):
- h, w, a = img.shape
- # print(h,w,a)
- img = image_resize(img, height=480)
- imgG = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- h, w = imgG.shape
- # print(w,h)
- # num = gcd(w,h)
- # counter = 0
- print(h, w)
- for size in range((h + w) // 12, 25, -12):
- for x in range(0, h - size, size // 6):
- for y in range(0, w - size, size // 6):
- # counter += 1
- # print(x, y, size)
- img = check_face(y, x, h, w, imgG, img, size)
- #tmp_img = copy.deepcopy(img)
- #cv2.rectangle(tmp_img, (y, x), (y + size, x + size), (0, 0, 255))
- #cv2.imshow('image', tmp_img)
- #cv2.waitKey(1)
- # print(str(x)+"/"+str(h),str(y)+"/"+str(w),size)
- # imgc = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
- #cv2.imwrite(imagepath, img)
- #cv2.destroyAllWindows()
- return img
- clip = mpe.VideoFileClip("video.mp4")
- new_frames = [frame for frame in clip.iter_frames()]
- new_clip = mpe.ImageSequenceClip(new_frames,fps=clip.fps)
- new_clip.write_videofile("test.mp4")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement