File size: 3,161 Bytes
7a6d8ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import gradio as gr
import cv2
import os
import boto3

s3_client = boto3.client(
    's3',
    aws_access_key_id='AKIAY5HVHYWVXRTEU6CB',
    aws_secret_access_key='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',
    region_name='eu-central-1' 
)

def upload_to_s3(bucket_name, folder_name):
    # Upload files in the folder to S3 bucket
    for filename in os.listdir(folder_name):
        if filename.endswith('.png'):
            file_path = os.path.join(folder_name, filename)
            s3_client.upload_file(file_path, bucket_name, f"{folder_name}/{filename}")

def process_video(uploaded_video, name, surname, interval_ms):
    try:
        if uploaded_video is None:
            return "No video file uploaded."

        folder_name = f"{name}_{surname}"
        os.makedirs(folder_name, exist_ok=True)

        # The uploaded_video is a NamedString object, extract the file path
        temp_video_path = uploaded_video.name

        # Initialize face detector
        face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

        # Open and process the video
        vidcap = cv2.VideoCapture(temp_video_path)
        if not vidcap.isOpened():
            raise Exception("Failed to open video file.")

        fps = vidcap.get(cv2.CAP_PROP_FPS)
        frame_interval = int(fps * (interval_ms / 10000))

        frame_count = 0
        saved_image_count = 0
        success, image = vidcap.read()
        while success and saved_image_count < 86:
            if frame_count % frame_interval == 0:
                # Apply face detection
                gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                faces = face_cascade.detectMultiScale(gray, 1.1, 4)
                for (x, y, w, h) in faces:
                    # Crop and resize face
                    face = image[y:y+h, x:x+w]
                    face_resized = cv2.resize(face, (160, 160))
                    cv2.imwrite(os.path.join(folder_name, f"{name}_{surname}_{saved_image_count:04d}.png"), face_resized)
                    saved_image_count += 1
                    if saved_image_count >= 86:
                        break

            success, image = vidcap.read()
            frame_count += 1

        vidcap.release()

        bucket_name = 'imagefilessml'  # Replace with your bucket name

        upload_to_s3(bucket_name, folder_name)

        return f"Saved and uploaded {saved_image_count} face images"


        return f"Saved {saved_image_count} face images in the folder: {folder_name}"

    except Exception as e:
        return f"An error occurred: {e}"

with gr.Blocks() as demo:
    with gr.Row():
        video = gr.File(label="Upload Your Video")
        name = gr.Textbox(label="Name")
        surname = gr.Textbox(label="Surname")
        interval = gr.Number(label="Interval in milliseconds", value=1000)
        submit_button = gr.Button("Submit")

    submit_button.click(
        fn=process_video,
        inputs=[video, name, surname, interval],
        outputs=[gr.Text(label="Result")]
    )

demo.launch()