File size: 8,968 Bytes
b020405
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import gradio as gr
import cv2
import numpy as np
from pathlib import Path
from ultralytics import YOLO

# ── Constants ─────────────────────────────────────────────────────────────────
MODEL_PATH = "yolov8_cattle_keypoints.pt"

KP_NAMES = [
    "left_ear_tip", "right_ear_tip", "left_ear_base", "right_ear_base",
    "left_eye", "right_eye", "nose_left", "nose_right", "nose_tip",
    "mouth_left", "mouth_right", "chin_left", "chin_right",
]

KP_COLORS = [
    (255,  69,   0),
    ( 30, 144, 255),
    (255, 165,   0),
    (  0, 191, 255),
    (154, 205,  50),
    (238, 130, 238),
    (  0, 255, 127),
    (255, 215,   0),
    (255, 255,   0),
    (255,  20, 147),
    (  0, 255, 255),
    (255, 140,   0),
    (147, 112, 219),
]

SKELETON = [
    (0, 2), (1, 3),
    (2, 4), (3, 5),
    (4, 5),
    (6, 8), (7, 8),
    (6, 9), (7, 10),
    (9, 10),
    (9, 11), (10, 12),
    (11, 12),
    (4, 6), (5, 7),
]

# ── Model loading ─────────────────────────────────────────────────────────────
_model = None

def load_model():
    global _model
    if _model is None:
        if not Path(MODEL_PATH).exists():
            raise FileNotFoundError(
                f"Model file '{MODEL_PATH}' not found. "
                "Upload yolov8_cattle_keypoints.pt to the Space root."
            )
        _model = YOLO(MODEL_PATH)
    return _model


# ── Inference ─────────────────────────────────────────────────────────────────
def run_inference(image: np.ndarray, conf_threshold: float, show_labels: bool):
    if image is None:
        return None, "Upload an image first."

    m = load_model()
    results = m.predict(source=image, conf=float(conf_threshold), verbose=False)

    annotated  = image.copy()
    table_rows = []

    for result in results:
        boxes          = result.boxes
        keypoints_data = result.keypoints
        if boxes is None or keypoints_data is None or len(boxes) == 0:
            continue

        for det_idx in range(len(boxes)):
            conf = float(boxes.conf[det_idx])
            kps  = keypoints_data.data[det_idx].cpu().numpy()  # (13, 3)

            # Bounding box
            x1, y1, x2, y2 = boxes.xyxy[det_idx].cpu().numpy().astype(int)
            cv2.rectangle(annotated, (x1, y1), (x2, y2), (255, 255, 255), 2)
            cv2.putText(
                annotated, f"cattle {conf:.2f}",
                (x1, max(y1 - 8, 0)),
                cv2.FONT_HERSHEY_SIMPLEX, 0.55, (255, 255, 255), 1, cv2.LINE_AA,
            )

            # Skeleton
            for (i, j) in SKELETON:
                if i >= len(kps) or j >= len(kps):
                    continue
                xi, yi, vi = kps[i]
                xj, yj, vj = kps[j]
                if vi < 0.5 or vj < 0.5:
                    continue
                cv2.line(
                    annotated,
                    (int(xi), int(yi)), (int(xj), int(yj)),
                    (180, 180, 180), 1, cv2.LINE_AA,
                )

            # Keypoints
            row = {"detection": det_idx + 1, "confidence": f"{conf:.3f}"}
            for kp_idx, (kx, ky, kv) in enumerate(kps):
                name  = KP_NAMES[kp_idx]
                color = KP_COLORS[kp_idx]
                if kv > 0.5:
                    cv2.circle(annotated, (int(kx), int(ky)), 6, color, -1)
                    cv2.circle(annotated, (int(kx), int(ky)), 7, (0, 0, 0), 1)
                    if show_labels:
                        cv2.putText(
                            annotated, name,
                            (int(kx) + 8, int(ky) - 4),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.38,
                            color, 1, cv2.LINE_AA,
                        )
                    row[name] = f"({int(kx)}, {int(ky)})  vis={kv:.2f}"
                else:
                    row[name] = "not visible"

            table_rows.append(row)

    if table_rows:
        lines = [f"### {len(table_rows)} detection(s) found\n"]
        for row in table_rows:
            lines.append(f"**Detection {row['detection']}** β€” conf {row['confidence']}")
            for name in KP_NAMES:
                lines.append(f"  - `{name}`: {row.get(name, 'n/a')}")
            lines.append("")
        results_md = "\n".join(lines)
    else:
        results_md = "### No cattle detected\nTry lowering the confidence threshold."

    return annotated, results_md


# ── CSS ───────────────────────────────────────────────────────────────────────
CSS = """
@import url('https://fonts.googleapis.com/css2?family=Bebas+Neue&family=DM+Mono:wght@400;500&family=DM+Sans:wght@300;400;500&display=swap');

:root {
    --bg:      #0d0f0e;
    --surface: #161a18;
    --border:  #2a332e;
    --accent:  #4ffe9a;
    --muted:   #7a8c80;
    --text:    #e8ede9;
    --radius:  4px;
}
body, .gradio-container {
    background: var(--bg) !important;
    font-family: 'DM Sans', sans-serif !important;
    color: var(--text) !important;
}
.hdr {
    padding: 2.5rem 0 1.5rem;
    text-align: center;
    border-bottom: 1px solid var(--border);
    margin-bottom: 2rem;
}
.hdr h1 {
    font-family: 'Bebas Neue', sans-serif;
    font-size: clamp(2.8rem, 7vw, 5.5rem);
    letter-spacing: 0.08em;
    color: var(--accent);
    margin: 0;
    line-height: 1;
    text-shadow: 0 0 40px rgba(79,254,154,0.25);
}
.hdr p {
    font-family: 'DM Mono', monospace;
    font-size: 0.78rem;
    color: var(--muted);
    letter-spacing: 0.15em;
    text-transform: uppercase;
    margin: 0.6rem 0 0;
}
.tags {
    display: flex; gap: 0.5rem; flex-wrap: wrap;
    justify-content: center; margin-top: 0.8rem;
}
.tag {
    font-family: 'DM Mono', monospace;
    font-size: 0.68rem; letter-spacing: 0.1em;
    text-transform: uppercase; padding: 0.25rem 0.65rem;
    border: 1px solid var(--border); border-radius: 2px; color: var(--muted);
}
.tag.hot { border-color: var(--accent); color: var(--accent); }
button.primary {
    background: var(--accent) !important;
    color: #0d0f0e !important;
    font-family: 'Bebas Neue', sans-serif !important;
    font-size: 1.1rem !important;
    letter-spacing: 0.12em !important;
    border: none !important;
    border-radius: var(--radius) !important;
    padding: 0.7rem 2rem !important;
    transition: opacity 0.15s, transform 0.1s !important;
}
button.primary:hover { opacity: 0.85 !important; transform: translateY(-1px) !important; }
input[type=range]    { accent-color: var(--accent) !important; }
input[type=checkbox] { accent-color: var(--accent) !important; }
"""

HEADER_HTML = """
<div class="hdr">
  <h1>CattleFace Β· Pose</h1>
  <p>YOLOv8 Β· 13-point facial landmark detection for bovines</p>
  <div class="tags">
    <span class="tag hot">13 keypoints</span>
    <span class="tag">ears Β· eyes Β· nose Β· mouth Β· chin</span>
    <span class="tag hot">real-time inference</span>
    <span class="tag">UARK-AICV benchmark</span>
  </div>
</div>
"""

FOOTER_HTML = """
<div style="text-align:center;padding:1.5rem 0 0.5rem;
            font-family:'DM Mono',monospace;font-size:0.7rem;
            color:#7a8c80;letter-spacing:0.08em;">
  MODEL Β· YOLOv8s-pose &nbsp;|&nbsp;
  DATASET Β· UARK-AICV/CattleFace-RGBT-benchmark &nbsp;|&nbsp;
  13 FACIAL LANDMARKS
</div>
"""

# ── Layout ────────────────────────────────────────────────────────────────────
with gr.Blocks(title="CattleFace Pose") as demo:
    gr.HTML(HEADER_HTML)

    with gr.Row():
        with gr.Column(scale=1):
            inp_image = gr.Image(
                label="Input Image",
                type="numpy",
                sources=["upload", "webcam", "clipboard"],
            )
            conf_slider = gr.Slider(
                minimum=0.05, maximum=0.95, value=0.25, step=0.05,
                label="Confidence Threshold",
            )
            show_labels = gr.Checkbox(value=True, label="Show keypoint labels")
            run_btn = gr.Button("Detect Landmarks", variant="primary")

        with gr.Column(scale=1):
            out_image = gr.Image(label="Annotated Output", type="numpy")
            out_text  = gr.Markdown(label="Keypoint Details")

    run_btn.click(
        fn=run_inference,
        inputs=[inp_image, conf_slider, show_labels],
        outputs=[out_image, out_text],
    )

    gr.HTML(FOOTER_HTML)

if __name__ == "__main__":
    demo.launch(css=CSS)