Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,11 +6,11 @@ import cv2
|
|
| 6 |
import base64
|
| 7 |
from ultralytics import YOLO
|
| 8 |
|
| 9 |
-
# βββ Load Models
|
| 10 |
person_model = YOLO("yolov8n.pt")
|
| 11 |
statue_model = YOLO("best.pt")
|
| 12 |
|
| 13 |
-
# βββ App Setup
|
| 14 |
app = FastAPI()
|
| 15 |
|
| 16 |
UPLOAD_FOLDER = "/tmp/uploads"
|
|
@@ -18,26 +18,26 @@ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
|
| 18 |
|
| 19 |
app.mount("/uploads", StaticFiles(directory=UPLOAD_FOLDER), name="uploads")
|
| 20 |
|
| 21 |
-
CONF_THRESHOLD = 0.
|
| 22 |
|
| 23 |
router = APIRouter()
|
| 24 |
|
| 25 |
|
| 26 |
def draw_label(image, label, x1, y1, x2, y2, box_color, text_color):
|
| 27 |
-
|
| 28 |
font = cv2.FONT_HERSHEY_SIMPLEX
|
| 29 |
font_scale = 0.6
|
| 30 |
thickness = 2
|
| 31 |
|
| 32 |
(tw, th), _ = cv2.getTextSize(label, font, font_scale, thickness)
|
| 33 |
|
| 34 |
-
|
| 35 |
if y1 - th - 8 >= 0:
|
| 36 |
label_y1 = y1 - th - 8
|
| 37 |
label_y2 = y1
|
| 38 |
text_y = y1 - 5
|
| 39 |
else:
|
| 40 |
-
|
| 41 |
label_y1 = y1
|
| 42 |
label_y2 = y1 + th + 8
|
| 43 |
text_y = y1 + th + 3
|
|
@@ -71,7 +71,7 @@ async def predict_image(
|
|
| 71 |
person_count = 0
|
| 72 |
statue_count = 0
|
| 73 |
|
| 74 |
-
# ββ Person Detection
|
| 75 |
person_results = person_model(file_path)
|
| 76 |
|
| 77 |
for box in person_results[0].boxes:
|
|
@@ -97,7 +97,7 @@ async def predict_image(
|
|
| 97 |
})
|
| 98 |
person_count += 1
|
| 99 |
|
| 100 |
-
# ββ Statue Detection
|
| 101 |
statue_results = statue_model(file_path)
|
| 102 |
|
| 103 |
for box in statue_results[0].boxes:
|
|
@@ -122,7 +122,7 @@ async def predict_image(
|
|
| 122 |
})
|
| 123 |
statue_count += 1
|
| 124 |
|
| 125 |
-
# ββ Save Output Image
|
| 126 |
output_filename = f"output_{safe_filename}"
|
| 127 |
output_path = os.path.join(UPLOAD_FOLDER, output_filename)
|
| 128 |
cv2.imwrite(output_path, image)
|
|
|
|
| 6 |
import base64
|
| 7 |
from ultralytics import YOLO
|
| 8 |
|
| 9 |
+
# βββ Load Models ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 10 |
person_model = YOLO("yolov8n.pt")
|
| 11 |
statue_model = YOLO("best.pt")
|
| 12 |
|
| 13 |
+
# βββ App Setup ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 14 |
app = FastAPI()
|
| 15 |
|
| 16 |
UPLOAD_FOLDER = "/tmp/uploads"
|
|
|
|
| 18 |
|
| 19 |
app.mount("/uploads", StaticFiles(directory=UPLOAD_FOLDER), name="uploads")
|
| 20 |
|
| 21 |
+
CONF_THRESHOLD = 0.25
|
| 22 |
|
| 23 |
router = APIRouter()
|
| 24 |
|
| 25 |
|
| 26 |
def draw_label(image, label, x1, y1, x2, y2, box_color, text_color):
|
| 27 |
+
"""Ψ§Ψ±Ψ³Ω
Ψ§ΩΩ label ΩΩΩ Ψ§ΩΩ boxΨ ΩΩΩ Ω
ΩΩΨ΄ Ω
ΩΨ§Ω ΩΩΩ ΨΨ·Ω ΨͺΨΨͺ"""
|
| 28 |
font = cv2.FONT_HERSHEY_SIMPLEX
|
| 29 |
font_scale = 0.6
|
| 30 |
thickness = 2
|
| 31 |
|
| 32 |
(tw, th), _ = cv2.getTextSize(label, font, font_scale, thickness)
|
| 33 |
|
| 34 |
+
# ΩΩ ΩΩΩ Ω
ΩΨ§Ω ΩΩΩ Ψ§ΩΩ box
|
| 35 |
if y1 - th - 8 >= 0:
|
| 36 |
label_y1 = y1 - th - 8
|
| 37 |
label_y2 = y1
|
| 38 |
text_y = y1 - 5
|
| 39 |
else:
|
| 40 |
+
# ΨΨ· Ψ§ΩΩ label Ψ¬ΩΩ Ψ§ΩΩ box Ω
Ω ΩΩΩ
|
| 41 |
label_y1 = y1
|
| 42 |
label_y2 = y1 + th + 8
|
| 43 |
text_y = y1 + th + 3
|
|
|
|
| 71 |
person_count = 0
|
| 72 |
statue_count = 0
|
| 73 |
|
| 74 |
+
# ββ Person Detection ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 75 |
person_results = person_model(file_path)
|
| 76 |
|
| 77 |
for box in person_results[0].boxes:
|
|
|
|
| 97 |
})
|
| 98 |
person_count += 1
|
| 99 |
|
| 100 |
+
# ββ Statue Detection ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 101 |
statue_results = statue_model(file_path)
|
| 102 |
|
| 103 |
for box in statue_results[0].boxes:
|
|
|
|
| 122 |
})
|
| 123 |
statue_count += 1
|
| 124 |
|
| 125 |
+
# ββ Save Output Image βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 126 |
output_filename = f"output_{safe_filename}"
|
| 127 |
output_path = os.path.join(UPLOAD_FOLDER, output_filename)
|
| 128 |
cv2.imwrite(output_path, image)
|