Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
|
|
3 |
import torch
|
4 |
from PIL import Image, ImageFilter, ImageDraw
|
5 |
from ultralytics import YOLO
|
|
|
6 |
|
7 |
MODEL_PATH = "./models/face_yolov8n_v2.pt"
|
8 |
MODEL = YOLO(MODEL_PATH)
|
@@ -37,7 +38,8 @@ def create_rounded_rectangle_mask(image:Image.Image, radius, alpha=255):
|
|
37 |
|
38 |
def max_rounding_radius(rect_coords):
|
39 |
# Extract the coordinates of the rectangle [x0, y0, x1, y1]
|
40 |
-
rect_coords = [coord
|
|
|
41 |
x0, y0, x1, y1 = rect_coords
|
42 |
|
43 |
# Calculate the width and height of the rectangle
|
@@ -50,11 +52,12 @@ def max_rounding_radius(rect_coords):
|
|
50 |
# Calculate the maximum radius as half of the smallest dimension
|
51 |
return min_dimension // 2
|
52 |
|
|
|
53 |
def generate_image(source_image:Image.Image, confidence=0.3, radius=50, blur_amount=10, margin=0):
|
54 |
if source_image is None:
|
55 |
return source_image
|
56 |
|
57 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
58 |
pred = MODEL(source_image, conf=confidence, device=device)
|
59 |
bboxes = pred[0].boxes.xyxy.cpu().numpy()
|
60 |
if bboxes.size == 0:
|
|
|
3 |
import torch
|
4 |
from PIL import Image, ImageFilter, ImageDraw
|
5 |
from ultralytics import YOLO
|
6 |
+
import spaces
|
7 |
|
8 |
MODEL_PATH = "./models/face_yolov8n_v2.pt"
|
9 |
MODEL = YOLO(MODEL_PATH)
|
|
|
38 |
|
39 |
def max_rounding_radius(rect_coords):
|
40 |
# Extract the coordinates of the rectangle [x0, y0, x1, y1]
|
41 |
+
rect_coords = [coord
|
42 |
+
-1 for coord in rect_coords]
|
43 |
x0, y0, x1, y1 = rect_coords
|
44 |
|
45 |
# Calculate the width and height of the rectangle
|
|
|
52 |
# Calculate the maximum radius as half of the smallest dimension
|
53 |
return min_dimension // 2
|
54 |
|
55 |
+
@spaces.GPU(enable_queue=True)
|
56 |
def generate_image(source_image:Image.Image, confidence=0.3, radius=50, blur_amount=10, margin=0):
|
57 |
if source_image is None:
|
58 |
return source_image
|
59 |
|
60 |
+
device = "cuda"# if torch.cuda.is_available() else "cpu"
|
61 |
pred = MODEL(source_image, conf=confidence, device=device)
|
62 |
bboxes = pred[0].boxes.xyxy.cpu().numpy()
|
63 |
if bboxes.size == 0:
|