taesiri commited on
Commit
584ce93
·
1 Parent(s): 6416175
Files changed (2) hide show
  1. app.py +48 -4
  2. requirements.txt +6 -0
app.py CHANGED
@@ -1,7 +1,51 @@
1
  import gradio as gr
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import face_alignment
3
+ import numpy as np
4
+ from PIL import Image
5
+ import cv2
6
+ import torch
7
+ import spaces
8
 
9
+ # Initialize the FaceAlignment model
10
+ fa = face_alignment.FaceAlignment(face_alignment.LandmarksType.TWO_D, device='cuda')
11
 
12
+ @spaces.GPU
13
+ def detect_landmarks(image):
14
+ # Convert PIL Image to numpy array
15
+ img_array = np.array(image)
16
+
17
+ # Detect landmarks
18
+ preds = fa.get_landmarks(img_array)
19
+
20
+ if preds is None:
21
+ return image, "No face detected in the image."
22
+
23
+ # Draw landmarks on the image
24
+ for facial_landmarks in preds:
25
+ for (x, y) in facial_landmarks:
26
+ cv2.circle(img_array, (int(x), int(y)), 2, (0, 255, 0), -1)
27
+
28
+ return Image.fromarray(img_array), f"Detected {len(preds)} face(s) in the image."
29
+
30
+ # Create Gradio Blocks
31
+ with gr.Blocks() as demo:
32
+ gr.Markdown("# Face Alignment Demo")
33
+ gr.Markdown("Upload an image to detect facial landmarks.")
34
+
35
+ with gr.Row():
36
+ with gr.Column():
37
+ input_image = gr.Image(type="pil", label="Input Image")
38
+ submit_btn = gr.Button("Detect Landmarks")
39
+
40
+ with gr.Column():
41
+ output_image = gr.Image(type="pil", label="Output Image")
42
+ output_text = gr.Textbox(label="Detection Result")
43
+
44
+ submit_btn.click(
45
+ fn=detect_landmarks,
46
+ inputs=input_image,
47
+ outputs=[output_image, output_text]
48
+ )
49
+
50
+ # Launch the interface
51
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ face_alignment
3
+ numpy
4
+ Pillow
5
+ opencv-python-headless
6
+ torch