File size: 4,034 Bytes
9e0f5c5
 
 
 
 
c463c33
 
 
9e0f5c5
 
6b368fb
 
9e0f5c5
 
 
 
6b368fb
9e0f5c5
 
 
 
6b368fb
 
9e0f5c5
939c610
9e0f5c5
939c610
6b368fb
 
9e0f5c5
 
 
 
6b368fb
 
 
9e0f5c5
 
 
 
 
 
6b368fb
9e0f5c5
 
 
6b368fb
 
9e0f5c5
6b368fb
 
 
 
 
 
 
9e0f5c5
 
6b368fb
 
 
9e0f5c5
6b368fb
 
9e0f5c5
6b368fb
 
9e0f5c5
 
6b368fb
9e0f5c5
6b368fb
 
9e0f5c5
6b368fb
9e0f5c5
 
6b368fb
9e0f5c5
72adf42
84e5b54
6b368fb
9e0f5c5
72adf42
f8889ec
1ff42e9
9e0f5c5
6b368fb
325c333
deeb0a3
6b368fb
 
 
 
 
325c333
6b368fb
 
 
 
9e0f5c5
 
 
939c610
deeb0a3
9e0f5c5
 
6b368fb
 
 
31ad5e9
9e0f5c5
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import gradio as gr
import cv2
import os
import boto3

aws_access_key_id = os.getenv('AWS_ACCESS_KEY_ID')
aws_secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY')

s3_client = boto3.client(
    's3',
    aws_access_key_id=aws_access_key_id,
    aws_secret_access_key=aws_secret_access_key,
    region_name='eu-central-1' 
)

def upload_to_s3(bucket_name, folder_name):
    image_paths = []
    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}")
            image_paths.append(file_path)
    return image_paths

def process_video(uploaded_video, name, surname, interval_ms):
    try:
        video_source = uploaded_video
        if video_source is None:
            return "No video file provided.", []

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

        # Video processing logic
        # Use video_source directly as it's a file path (string)
        temp_video_path = video_source
        face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
        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 / 1000))
        frame_count = 0
        saved_image_count = 0
        success, image = vidcap.read()
        image_paths = []

        while success and saved_image_count < 86:
          if frame_count % frame_interval == 0:
            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            faces = face_cascade.detectMultiScale(gray, 1.2, 4)
            for (x, y, w, h) in faces:
                # Additional checks for face region validation
                aspect_ratio = w / h
                if aspect_ratio > 0.75 and aspect_ratio < 1.33 and w * h > 4000:  # Example thresholds
                    face = image[y:y+h, x:x+w]
                    face_resized = cv2.resize(face, (160, 160))
                    image_filename = os.path.join(folder_name, f"{name}_{surname}_{saved_image_count:04d}.png")
                    cv2.imwrite(image_filename, face_resized)
                    image_paths.append(image_filename)
                    saved_image_count += 1
                if saved_image_count >= 86:
                    break

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


        vidcap.release()

        bucket_name = 'newimagesupload00'
        uploaded_images = upload_to_s3(bucket_name, folder_name)

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

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



# Gradio Interface
with gr.Blocks() as demo:
    gr.Markdown("### Video Uploader and Face Detector")
    gr.Markdown("Upload your own video to add your images to the dataset!")
    gr.Markdown("Make a short 10-15 seconds video of your front and side profiles, **slowly rotating your face**, with good lighting and visible face for best results.")
    with gr.Row():
        with gr.Column():
            video = gr.File(label="Upload Your Video!")
            
        with gr.Column():
            name = gr.Textbox(label="Name")
            surname = gr.Textbox(label="Surname")
            interval = gr.Number(label="Interval in milliseconds", value=100)
            submit_button = gr.Button("Submit")
            
        with gr.Column():
            gallery = gallery = gr.Gallery(
        label="Generated images", show_label=False, elem_id="gallery"
    , columns=[3], rows=[1], object_fit="contain", height="auto")

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

css = """
body { font-family: Arial, sans-serif; }
"""
# Demo Launching
demo.launch()