video_folder = "./videos/" filename = "103792584148574_1562618673.mp4" cap = cv2.VideoCapture(video_folder + filename) fps = cap.get(cv2.CAP_PROP_FPS) fps = int(fps + 0.5) size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) # video_writer = cv2.VideoWriter('./tmp/tmp_encrypted.avi', cv2.VideoWriter_fourcc('F', 'F', 'V', '1'), fps, size) video_writer = cv2.VideoWriter('./tmp/tmp_encrypted.avi', cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'), fps, size) video_writer.set(cv2.VIDEOWRITER_PROP_QUALITY, 1.0) key = Key.rand_init() master = Lorenz.from_key(key) slave = Lorenz.from_key(key) sender = Protocol(master) receiver = Protocol(slave) sender.skip_first_n(key.n) receiver.skip_first_n(key.n) if not cap.isOpened(): print("Error opening video stream or file") # Read until video is completed while cap.isOpened(): # Capture frame-by-frame ret, frame = cap.read() if frame is None: break start_time = time.time() # cv2.
def process(filename, video_folder="./videos/", dst_video_folder="./encrypted_video/"): """ pre deliver_key data 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 0 500 1000 :return: """ redis_host = "localhost" redis_port = 6379 redis_password = "" try: # The decode_responses flag here directs the client to convert the responses from Redis into Python strings r = redis.StrictRedis(host=redis_host, port=redis_port, password=redis_password, decode_responses=True) video_face_map = json.loads(r.hget("video_face_map", filename)) time_face_map = json.loads(r.hget('time_face_map', filename)) cap = cv2.VideoCapture(video_folder + filename) fps = cap.get(cv2.CAP_PROP_FPS) quad_fps = int((fps + 3) / 4) fps = int(fps + 0.5) size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) video_writer = cv2.VideoWriter(dst_video_folder + filename, cv2.VideoWriter_fourcc(*'mp4v'), fps, size) # define (Codec,CRF,preset) FFmpeg tweak parameters for writer # output_params = {"-vcodec": "mpeg4", # "-framerate": fps, "-crf": 5, # "-preset": "veryfast", "-r": fps, # "-qscale:v": 3} # video_writer = WriteGear(output_filename=dst_video_folder+filename, # compression_mode=True, logging=True, **output_params) # print("FPS: %d" % fps) # print("QUAD FPS: %d" % quad_fps) # setup the crypto system sender_dict = {} for fid in video_face_map.keys(): key_4d = get_key_from_redis(fid) master = HyperLu.from_key(key_4d) sender = Protocol(master) sender.skip_first_n(key_4d.n) sender_dict[fid] = sender # Check if camera opened successfully if not cap.isOpened(): print("Error opening video stream or file") # Read until video is completed while cap.isOpened(): # Capture frame-by-frame ret, frame = cap.read() if ret: start = time() # stamp = cap.get(cv2.CAP_PROP_POS_MSEC) cur_frame = cap.get(1) - 1 frame_width, frame_height = cap.get( cv2.CAP_PROP_FRAME_WIDTH), cap.get( cv2.CAP_PROP_FRAME_HEIGHT) # print(cur_frame) if cur_frame % fps < quad_fps: msec_idx = int(cur_frame // fps * 1000) else: msec_idx = int(cur_frame // quad_fps * 250) # print(msec_idx) for fid, idx in time_face_map[str(msec_idx)].items(): pos = video_face_map[fid][str(msec_idx)] # x1 = int(pos['x'] - 1 / 12 * pos['width']) # y1 = int(pos['y'] - 1 / 12 * pos['height']) # x2 = int(pos['x'] + 1.2 * pos['width']) # y2 = int(pos['y'] + 1.2 * pos['height']) # x1, y1 = max(x1, 0), max(y1, 0) # x2, y2 = min(x2, int(frame_width)), min(y2, int(frame_height)) x1 = int(pos['x']) y1 = int(pos['y']) x2 = int(pos['x'] + pos['width']) y2 = int(pos['y'] + pos['height']) x1, y1 = max(x1, 0), max(y1, 0) x2, y2 = min(x2, int(frame_width)), min(y2, int(frame_height)) frame[y1:y2, x1:x2] = sender_dict[fid].encrypt(frame[y1:y2, x1:x2]) # Display the resulting frame # cv2.imshow('Frame', frame) video_writer.write(frame) # end = time() # t = max(int(41 - 1000 * (end - start)), 1) # if t == 1: # print(t) # Press Q on keyboard to exit if cv2.waitKey(1) & 0xFF == ord('q'): break # Break the loop else: break cap.release() # video_writer.close() video_writer.release() # Closes all the frames cv2.destroyAllWindows() except Exception as e: print(e)
def key_revoke(faceid, filename, video_folder="./encrypted_video/"): # decrypt the video with old key redis_host = "localhost" redis_port = 6379 redis_password = "" # The decode_responses flag here directs the client to convert the responses from Redis into Python strings r = redis.StrictRedis(host=redis_host, port=redis_port, password=redis_password, decode_responses=True) video_face_map = json.loads(r.hget("video_face_map", filename)) time_face_map = json.loads(r.hget('time_face_map', filename)) cap = cv2.VideoCapture(video_folder + filename) fps = cap.get(cv2.CAP_PROP_FPS) quad_fps = int((fps + 3) / 4) fps = int(fps + 0.5) size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) video_writer = cv2.VideoWriter("./tmp/" + filename, cv2.VideoWriter_fourcc(*'mp4v'), fps, size) # setup the crypto system key_4d_old = get_key_from_redis(faceid) slaver = HyperLu.from_key(key_4d_old) receiver = Protocol(slaver) receiver.skip_first_n(key_4d_old.n) # Check if camera opened successfully if not cap.isOpened(): print("Error opening video stream or file") # Read until video is completed while cap.isOpened(): # Capture frame-by-frame ret, frame = cap.read() if ret: cur_frame = cap.get(1) - 1 frame_width, frame_height = cap.get( cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT) if cur_frame % fps < quad_fps: msec_idx = int(cur_frame // fps * 1000) else: msec_idx = int(cur_frame // quad_fps * 250) if faceid in time_face_map[str(msec_idx)]: pos = video_face_map[faceid][str(msec_idx)] x1 = int(pos['x']) y1 = int(pos['y']) x2 = int(pos['x'] + pos['width']) y2 = int(pos['y'] + pos['height']) x1, y1 = max(x1, 0), max(y1, 0) x2, y2 = min(x2, int(frame_width)), min(y2, int(frame_height)) frame[y1:y2, x1:x2] = receiver.decrypt(frame[y1:y2, x1:x2]) video_writer.write(frame) # Break the loop else: break # When everything done, release the video capture object cap.release() video_writer.release() # Closes all the frames cv2.destroyAllWindows() # generate new key key_4d_new = generate_new_key(faceid) # encrypt the face using new key cap = cv2.VideoCapture('./tmp/' + filename) video_writer = cv2.VideoWriter(video_folder + filename, cv2.VideoWriter_fourcc(*'mp4v'), fps, size) # setup the crypto system master = HyperLu.from_key(key_4d_new) sender = Protocol(master) sender.skip_first_n(key_4d_new.n) # Check if camera opened successfully if not cap.isOpened(): print("Error opening video stream or file") # Read until video is completed while cap.isOpened(): # Capture frame-by-frame ret, frame = cap.read() if ret: cur_frame = cap.get(1) - 1 frame_width, frame_height = cap.get( cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT) if cur_frame % fps < quad_fps: msec_idx = int(cur_frame // fps * 1000) else: msec_idx = int(cur_frame // quad_fps * 250) if faceid in time_face_map[str(msec_idx)]: pos = video_face_map[faceid][str(msec_idx)] x1 = int(pos['x']) y1 = int(pos['y']) x2 = int(pos['x'] + pos['width']) y2 = int(pos['y'] + pos['height']) x1, y1 = max(x1, 0), max(y1, 0) x2, y2 = min(x2, int(frame_width)), min(y2, int(frame_height)) frame[y1:y2, x1:x2] = sender.encrypt(frame[y1:y2, x1:x2]) video_writer.write(frame) # Break the loop else: break # When everything done, release the video capture object cap.release() # Closes all the frames cv2.destroyAllWindows()
def play(filename, json_data, video_folder="./encrypted_video/"): """ pre deliver_key data 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 0 500 1000 :return: """ try: # The decode_responses flag here directs the client to convert the responses from Redis into Python strings video_face_map = json_data['video_face_map'] # print(video_face_map) time_face_map = json_data['time_face_map'] # print(time_face_map) face_key = json_data['face_key'] # print(face_key) cap = cv2.VideoCapture(video_folder + filename) fps = cap.get(cv2.CAP_PROP_FPS) quad_fps = int((fps + 3) / 4) fps = int(fps + 0.5) interval = int((1 / fps) * 1000) # setup the crypto system receiver_dict = {} for fid in video_face_map.keys(): key_4d = Key4D.from_str(face_key[fid]) slaver = HyperLu.from_key(key_4d) receiver = Protocol(slaver) receiver.skip_first_n(key_4d.n) receiver_dict[fid] = receiver # Check if camera opened successfully if not cap.isOpened(): print("Error opening video stream or file") # Read until video is completed while cap.isOpened(): # Capture frame-by-frame ret, frame = cap.read() if ret: start = time() cur_frame = cap.get(1) - 1 if cur_frame == 59: a = 1 frame_width, frame_height = cap.get( cv2.CAP_PROP_FRAME_WIDTH), cap.get( cv2.CAP_PROP_FRAME_HEIGHT) if cur_frame % fps < quad_fps: msec_idx = int(cur_frame // fps * 1000) else: msec_idx = int(cur_frame // quad_fps * 250) for fid in time_face_map[str(msec_idx)]: pos = video_face_map[fid][str(msec_idx)] # x1 = int(pos['x'] - 1 / 12 * pos['width']) # y1 = int(pos['y'] - 1 / 12 * pos['height']) # x2 = int(pos['x'] + 1.2 * pos['width']) # y2 = int(pos['y'] + 1.2 * pos['height']) # x1, y1 = max(x1, 0), max(y1, 0) # x2, y2 = min(x2, int(frame_width)), min(y2, int(frame_height)) x1 = int(pos['x']) y1 = int(pos['y']) x2 = int(pos['x'] + pos['width']) y2 = int(pos['y'] + pos['height']) x1, y1 = max(x1, 0), max(y1, 0) x2, y2 = min(x2, int(frame_width)), min(y2, int(frame_height)) frame[y1:y2, x1:x2] = receiver_dict[fid].decrypt(frame[y1:y2, x1:x2]) # Display the resulting frame cv2.imshow('Frame', frame) end = time() t = max(int(interval - 1 - 1000 * (end - start)), 1) if t == 1: print(t) if cv2.waitKey(t) & 0xFF == ord('q'): break # Break the loop else: break # When everything done, release the video capture object cap.release() # Closes all the frames cv2.destroyAllWindows() except Exception as e: print(e)
if BLUR: image_folder = "./eval1_tmp/blur/" file_suffix = "blur_layer.png" for i in range(len(images)): blur_layer = images[i].copy() for _ in range(20): blur_layer = cv2.GaussianBlur(blur_layer, (11, 11), 0) cv2.imwrite(image_folder + str(i) + file_suffix, blur_layer) # permute once if PERMUTE_ONCE: image_folder = "./eval1_tmp/permute1/" file_suffix = "permute1.png" key_3d = Key3D.from_list(key_3d_list) master = Lorenz.from_key(key_3d) sender = Protocol(master) sender.skip_first_n(key_3d.n) for i in range(len(images)): print("permute once image %d" % i) img = sender.encrypt_permute_1(images[i].copy()) cv2.imwrite(image_folder + str(i) + file_suffix, img) # permute twice if PERMUTE_TWICE: image_folder = "./eval1_tmp/permute2/" file_suffix = "permute2.png" key_4d = Key4D.from_list(key_4d_list) master = HyperLu.from_key(key_4d) sender = Protocol(master) sender.skip_first_n(key_4d.n) for i in range(len(images)):
import numpy as np import cv2 from time import time import matplotlib.pyplot as plt image_folder = "./images/" filename = "loki.jpg" # key = np.random.uniform(-10, 10), np.random.uniform(-15, 15), np.random.uniform(0, 40) key_l = [11.381133082873731, 12.990637231862848, 8.822348261655519, 12.990637231862848, 1000] # init encoder key_m = Key.from_list(key_l) # master = Lorenz.from_key(key_m) master = HyperLu.from_key(key_m) sender = Protocol(master) sender.skip_first_n(key_m.n) # save key print(str(key_m)) key_str = str(key_m) # init decoder key_s = Key.from_str(key_str) # slave = Lorenz.from_key(key_s) slave = HyperLu.from_key(key_s) receiver = Protocol(slave) receiver.skip_first_n(key_s.n) print(master) print(slave) print(' ')