Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scrabbled face in the video #3

Closed
romasiugzdaite opened this issue Oct 19, 2023 · 5 comments · Fixed by #5
Closed

scrabbled face in the video #3

romasiugzdaite opened this issue Oct 19, 2023 · 5 comments · Fixed by #5

Comments

@romasiugzdaite
Copy link

hi Enes,

we are trying to scramble just a face in the video, could you help us with that please?

@altunenes
Copy link
Owner

hello @romasiugzdaite !
I realized I didn't focus on video scramble as much as static images, but we can still use it. 🙂 (I think I need to update scramblery soon... 😃)

For now, to fix your issue, I tried to create a simple code by commenting as much as possible so I hope it will be a roadmap for the next update🙂... just run the code by changing the video input name, and it should work... (but bump me if you got any errors/questions)

Note: This is designed for a single face in a video, if you have to apply scramble for multiple faces in a video let me know because the code might change significantly... 🙂

from scramblery import scramblery
import cv2 ## use pip install opencv-python if you don't have this library.
def process_video(input_video_path, output_video_path, scramble_settings):
    cap = cv2.VideoCapture(input_video_path)

    # Checking if the video opened successfully....
    if not cap.isOpened():
        print("Error: Could not open video.")
        return

    frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = cap.get(cv2.CAP_PROP_FPS)
    codec = cv2.VideoWriter_fourcc(*'XVID')


    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

    out = cv2.VideoWriter(output_video_path, codec, fps, (frame_width, frame_height))

    frame_number = 0  # Keep track of frame number
    while cap.isOpened():
        ret, frame = cap.read()

        # if frame is read correctly, ret is True
        if not ret:
            print("Can't receive frame. Exiting ...")
            break

        frame_number += 1  # Increment frame number

        # Printing the progress...
        print(f"Processing frame {frame_number} of {total_frames} ({(frame_number/total_frames)*100:.2f}%)")

        # Try processing the frame through scrambleface function
        try:
            scrambled_frame = scramblery.scrambleface(frame, **scramble_settings)
        except TypeError:  # Catch the error when no faces are detected
            scrambled_frame = frame  # If an error occurs, use the original frame

        # Write the frame into the output video file
        out.write(scrambled_frame)

        # Uncomment this section if you want to see the video while it's being processed
        # cv2.imshow('frame', scrambled_frame)
        # if cv2.waitKey(1) == ord('q'):
        #     break

    # Release everything when the job is finished
    cap.release()
    out.release()
    cv2.destroyAllWindows()

# Usage example
input_video_path = 'enes.mp4'  # Update with your video in your path
output_video_path = 'enes_scramble.mp4'  # output video name

scramble_settings = {
    'splits': 25,  #scramble ratio
    'type': 'pixel', ## For now only "stack" and "pixel" are available. 
    'seamless': False, ## Seamless Cloning also needs to be update for next patch. so keep it as "false" for now.
    'bg': True, ## note If you set it to "false" the background disappears, but the background is still visible at every frame where the face is not detected. So I don't recommend it for now...
    'seed': None, 
    'write': False  # It's important to set 'write' to False so the function returns the image instead of saving it
}

process_video(input_video_path, output_video_path, scramble_settings)

@romasiugzdaite
Copy link
Author

romasiugzdaite commented Oct 19, 2023 via email

@romasiugzdaite
Copy link
Author

romasiugzdaite commented Oct 19, 2023 via email

@altunenes
Copy link
Owner

glad that worked! 🙂 I will extend this project soon to prevent these kinds of problems...

@romasiugzdaite
Copy link
Author

romasiugzdaite commented Oct 19, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants