SaladSlayer00 commited on
Commit
9e0f5c5
1 Parent(s): f4d2144

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -90
app.py CHANGED
@@ -1,90 +1,90 @@
1
- import gradio as gr
2
- import cv2
3
- import os
4
- import boto3
5
-
6
- s3_client = boto3.client(
7
- 's3',
8
- aws_access_key_id='AKIAY5HVHYWVXRTEU6CB',
9
- aws_secret_access_key='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',
10
- region_name='eu-central-1'
11
- )
12
-
13
- def upload_to_s3(bucket_name, folder_name):
14
- # Upload files in the folder to S3 bucket
15
- for filename in os.listdir(folder_name):
16
- if filename.endswith('.png'):
17
- file_path = os.path.join(folder_name, filename)
18
- s3_client.upload_file(file_path, bucket_name, f"{folder_name}/{filename}")
19
-
20
- def process_video(uploaded_video, name, surname, interval_ms):
21
- try:
22
- if uploaded_video is None:
23
- return "No video file uploaded."
24
-
25
- folder_name = f"{name}_{surname}"
26
- os.makedirs(folder_name, exist_ok=True)
27
-
28
- # The uploaded_video is a NamedString object, extract the file path
29
- temp_video_path = uploaded_video.name
30
-
31
- # Initialize face detector
32
- face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
33
-
34
- # Open and process the video
35
- vidcap = cv2.VideoCapture(temp_video_path)
36
- if not vidcap.isOpened():
37
- raise Exception("Failed to open video file.")
38
-
39
- fps = vidcap.get(cv2.CAP_PROP_FPS)
40
- frame_interval = int(fps * (interval_ms / 10000))
41
-
42
- frame_count = 0
43
- saved_image_count = 0
44
- success, image = vidcap.read()
45
- while success and saved_image_count < 86:
46
- if frame_count % frame_interval == 0:
47
- # Apply face detection
48
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
49
- faces = face_cascade.detectMultiScale(gray, 1.1, 4)
50
- for (x, y, w, h) in faces:
51
- # Crop and resize face
52
- face = image[y:y+h, x:x+w]
53
- face_resized = cv2.resize(face, (160, 160))
54
- cv2.imwrite(os.path.join(folder_name, f"{name}_{surname}_{saved_image_count:04d}.png"), face_resized)
55
- saved_image_count += 1
56
- if saved_image_count >= 86:
57
- break
58
-
59
- success, image = vidcap.read()
60
- frame_count += 1
61
-
62
- vidcap.release()
63
-
64
- bucket_name = 'imagefilessml' # Replace with your bucket name
65
-
66
- upload_to_s3(bucket_name, folder_name)
67
-
68
- return f"Saved and uploaded {saved_image_count} face images"
69
-
70
-
71
- return f"Saved {saved_image_count} face images in the folder: {folder_name}"
72
-
73
- except Exception as e:
74
- return f"An error occurred: {e}"
75
-
76
- with gr.Blocks() as demo:
77
- with gr.Row():
78
- video = gr.File(label="Upload Your Video")
79
- name = gr.Textbox(label="Name")
80
- surname = gr.Textbox(label="Surname")
81
- interval = gr.Number(label="Interval in milliseconds", value=1000)
82
- submit_button = gr.Button("Submit")
83
-
84
- submit_button.click(
85
- fn=process_video,
86
- inputs=[video, name, surname, interval],
87
- outputs=[gr.Text(label="Result")]
88
- )
89
-
90
- demo.launch()
 
1
+ import gradio as gr
2
+ import cv2
3
+ import os
4
+ import boto3
5
+
6
+ s3_client = boto3.client(
7
+ 's3',
8
+ aws_access_key_id='AWS_ACCESS_KEY_ID',
9
+ aws_secret_access_key='CKxcJhYPNQHBmnVKrcK6wjxD3QV0gdj7HvVw7JWl',
10
+ region_name='eu-central-1'
11
+ )
12
+
13
+ def upload_to_s3(bucket_name, folder_name):
14
+ # Upload files in the folder to S3 bucket
15
+ for filename in os.listdir(folder_name):
16
+ if filename.endswith('.png'):
17
+ file_path = os.path.join(folder_name, filename)
18
+ s3_client.upload_file(file_path, bucket_name, f"{folder_name}/{filename}")
19
+
20
+ def process_video(uploaded_video, name, surname, interval_ms):
21
+ try:
22
+ if uploaded_video is None:
23
+ return "No video file uploaded."
24
+
25
+ folder_name = f"{name}_{surname}"
26
+ os.makedirs(folder_name, exist_ok=True)
27
+
28
+ # The uploaded_video is a NamedString object, extract the file path
29
+ temp_video_path = uploaded_video.name
30
+
31
+ # Initialize face detector
32
+ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
33
+
34
+ # Open and process the video
35
+ vidcap = cv2.VideoCapture(temp_video_path)
36
+ if not vidcap.isOpened():
37
+ raise Exception("Failed to open video file.")
38
+
39
+ fps = vidcap.get(cv2.CAP_PROP_FPS)
40
+ frame_interval = int(fps * (interval_ms / 10000))
41
+
42
+ frame_count = 0
43
+ saved_image_count = 0
44
+ success, image = vidcap.read()
45
+ while success and saved_image_count < 86:
46
+ if frame_count % frame_interval == 0:
47
+ # Apply face detection
48
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
49
+ faces = face_cascade.detectMultiScale(gray, 1.1, 4)
50
+ for (x, y, w, h) in faces:
51
+ # Crop and resize face
52
+ face = image[y:y+h, x:x+w]
53
+ face_resized = cv2.resize(face, (160, 160))
54
+ cv2.imwrite(os.path.join(folder_name, f"{name}_{surname}_{saved_image_count:04d}.png"), face_resized)
55
+ saved_image_count += 1
56
+ if saved_image_count >= 86:
57
+ break
58
+
59
+ success, image = vidcap.read()
60
+ frame_count += 1
61
+
62
+ vidcap.release()
63
+
64
+ bucket_name = 'imagefilessml' # Replace with your bucket name
65
+
66
+ upload_to_s3(bucket_name, folder_name)
67
+
68
+ return f"Saved and uploaded {saved_image_count} face images"
69
+
70
+
71
+ return f"Saved {saved_image_count} face images in the folder: {folder_name}"
72
+
73
+ except Exception as e:
74
+ return f"An error occurred: {e}"
75
+
76
+ with gr.Blocks() as demo:
77
+ with gr.Row():
78
+ video = gr.File(label="Upload Your Video")
79
+ name = gr.Textbox(label="Name")
80
+ surname = gr.Textbox(label="Surname")
81
+ interval = gr.Number(label="Interval in milliseconds", value=1000)
82
+ submit_button = gr.Button("Submit")
83
+
84
+ submit_button.click(
85
+ fn=process_video,
86
+ inputs=[video, name, surname, interval],
87
+ outputs=[gr.Text(label="Result")]
88
+ )
89
+
90
+ demo.launch()