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()