Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,63 @@ import gradio as gr
|
|
2 |
import spaces
|
3 |
import torch
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
zero = torch.Tensor([0]).cuda()
|
6 |
print(zero.device) # <-- 'cpu' 🤔
|
7 |
|
|
|
2 |
import spaces
|
3 |
import torch
|
4 |
|
5 |
+
from ultralytics import YOLO
|
6 |
+
from PIL import Image
|
7 |
+
import supervision as sv
|
8 |
+
|
9 |
+
IOU_THRESHOLD = 0.3
|
10 |
+
CONFIDENCE_THRESHOLD = 0.2
|
11 |
+
|
12 |
+
pretrained_path = "erax_nsfw_yolo11m.pt"
|
13 |
+
image_path_list = ["img_0.jpg", "img_1.jpg"]
|
14 |
+
|
15 |
+
model = YOLO(pretrained_path)
|
16 |
+
results = model(image_path_list,
|
17 |
+
conf=CONFIDENCE_THRESHOLD,
|
18 |
+
iou=IOU_THRESHOLD
|
19 |
+
)
|
20 |
+
|
21 |
+
|
22 |
+
for result in results:
|
23 |
+
annotated_image = result.orig_img.copy()
|
24 |
+
h, w = annotated_image.shape[:2]
|
25 |
+
anchor = h if h > w else w
|
26 |
+
|
27 |
+
# make_love class will cover entire context !!!
|
28 |
+
# selected_classes = [0, 1, 2, 3, 4, 5] # all classes
|
29 |
+
selected_classes = [0, 2, 3, 4, 5] # hidden make_love class
|
30 |
+
detections = sv.Detections.from_ultralytics(result)
|
31 |
+
detections = detections[np.isin(detections.class_id, selected_classes)]
|
32 |
+
|
33 |
+
# box_annotator = sv.BoxAnnotator()
|
34 |
+
# annotated_image = box_annotator.annotate(
|
35 |
+
# annotated_image,
|
36 |
+
# detections=detections
|
37 |
+
# )
|
38 |
+
|
39 |
+
# blur_annotator = sv.BlurAnnotator(kernel_size=anchor/50)
|
40 |
+
# annotated_image = blur_annotator.annotate(
|
41 |
+
# annotated_image.copy(),
|
42 |
+
# detections=detections
|
43 |
+
# )
|
44 |
+
|
45 |
+
label_annotator = sv.LabelAnnotator(text_color=sv.Color.BLACK,
|
46 |
+
text_scale=anchor/1700)
|
47 |
+
annotated_image = label_annotator.annotate(
|
48 |
+
annotated_image,
|
49 |
+
detections=detections
|
50 |
+
)
|
51 |
+
|
52 |
+
pixelate_annotator = sv.PixelateAnnotator(pixel_size=anchor/50)
|
53 |
+
annotated_image = pixelate_annotator.annotate(
|
54 |
+
scene=annotated_image.copy(),
|
55 |
+
detections=detections
|
56 |
+
)
|
57 |
+
|
58 |
+
sv.plot_image(annotated_image, size=(10, 10))
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
zero = torch.Tensor([0]).cuda()
|
63 |
print(zero.device) # <-- 'cpu' 🤔
|
64 |
|