Exemple #1
0
# 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.
    # imgsplit = cv2.split(frame)
    # cv2.cvtColor(frame, cv2.COLOR_BGR2YCrCb, frame)
    # for i in range(0, 90, 10):
    #     frame[i:i+10, i:i+10] = sender.encrypt(frame[i:i+10, i:i+10])
    #
    # for i in range(0, 90, 10):
    #     frame[i:i+10, i:i+10] = receiver.decrypt(frame[i:i+10, i:i+10])
    frame[10:100, 10:100] = sender.encrypt(frame[10:100, 10:100])
    # cv2.cvtColor(frame, cv2.COLOR_YCR_CB2BGR, frame)
    # frame[10:100, 10:100] = receiver.decrypt(frame[10:100, 10:100])
    # image = receiver.decrypt(encrypt_image)
    print("--- Encrypt %s seconds ---" % (time.time() - start_time))
    # cv2.imshow('frame', image)

    video_writer.write(frame)
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# When everything done, release the capture


video_writer.release()
cap.release()
Exemple #2
0
        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)):
            print("permute twice image %d" % i)
            img = sender.encrypt(images[i].copy())
            cv2.imwrite(image_folder + str(i) + file_suffix, img)
    # cv2.imshow('tmp', images[5])
    # cv2.waitKey()

    # reorder
    if REORDER:
        image_folder = "./eval1_tmp/reorder/"
        file_suffix = "reorder.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)):
            print("reorder image %d" % i)
            img = sender.reorder(images[i].copy(), ENCRYPT)
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()
#
# plt.show()

# encrypt with reorder
start_time = time()
# sender.reorder(img1[70:70+bs, 70:70+bs], ENCRYPT)
# img2[70:200, 40:160] = sender.reorder(img1[70:200, 40:160], ENCRYPT)
t1 = time() - start_time
print("--- Encrypt with reorder %s seconds ---" % t1)

cv2.imshow('encrypt_reorder', img2)

# encrypt with permute
start_time = time()
# sender.encrypt(img1[70:70+bs, 70:70+bs])
img3[70:200, 40:160] = sender.encrypt(img3[70:200, 40:160])
t2 = time() - start_time
print("--- Encrypt with permute %s seconds ---" % t2)
print(" ")
cv2.imshow('encrypt_permute', img3)
cv2.imwrite(image_folder+"permute_lorenz_encrypted.jpg", img3)

# decrypt
img2 = cv2.imread(image_folder + "permute_lorenz_encrypted.jpg", cv2.IMREAD_UNCHANGED)
start_time = time()
# img2[70:200, 40:160] = receiver.reorder(img2[70:200, 40:160], DECRYPT)
img2[70:200, 40:160] = receiver.decrypt(img2[70:200, 40:160])
print("--- decrypt %s seconds ---" % (time() - start_time))
cv2.imshow('decrypt', img2)

cv2.waitKey()