Spaces:
Runtime error
Runtime error
Khunndev Cursor commited on
Commit ·
2b21a73
1
Parent(s): d16c4ed
Improve detection accuracy with tiling defaults, 1024 model, and tuned thresholds.
Browse files- detect.py +8 -13
- frontend/src/App.tsx +1 -1
- frontend/src/lib/constants.ts +4 -2
- frontend/src/lib/i18n.ts +2 -2
- frontend/src/lib/imageEnhance.ts +4 -4
- frontend/src/lib/inference.ts +2 -2
- frontend/src/lib/modelSelect.ts +2 -2
detect.py
CHANGED
|
@@ -16,10 +16,10 @@ MODEL_DIR = STATIC_PATH / "models"
|
|
| 16 |
|
| 17 |
INFERENCE_SIZE = 1024
|
| 18 |
INFERENCE_SIZE_SMALL = 640
|
| 19 |
-
CONF_THRESHOLD = 0.
|
| 20 |
-
IOU_THRESHOLD = 0.
|
| 21 |
MAX_DET = 4000
|
| 22 |
-
TILE_OVERLAP = 0.
|
| 23 |
|
| 24 |
_sessions: dict[str, Any] = {}
|
| 25 |
_session_lock = Lock()
|
|
@@ -74,29 +74,24 @@ def _megapixels(w: int, h: int) -> float:
|
|
| 74 |
|
| 75 |
def _suggest_tile_grid(w: int, h: int) -> int:
|
| 76 |
mp = _megapixels(w, h)
|
| 77 |
-
if mp >=
|
| 78 |
return 5
|
| 79 |
-
if mp >=
|
| 80 |
return 4
|
| 81 |
-
if mp >= 2:
|
| 82 |
return 3
|
| 83 |
return 2
|
| 84 |
|
| 85 |
|
| 86 |
def _should_auto_tile(w: int, h: int) -> bool:
|
| 87 |
-
return _megapixels(w, h) >=
|
| 88 |
|
| 89 |
|
| 90 |
def _select_model(w: int, h: int) -> tuple[Path, int]:
|
| 91 |
-
if max(w, h) <= 1200:
|
| 92 |
-
small = MODEL_DIR / "countmodel-640.onnx"
|
| 93 |
-
if small.is_file():
|
| 94 |
-
return small, INFERENCE_SIZE_SMALL
|
| 95 |
big = MODEL_DIR / "countmodel.onnx"
|
| 96 |
if big.is_file():
|
| 97 |
return big, INFERENCE_SIZE
|
| 98 |
-
|
| 99 |
-
return (MODEL_DIR / "countmodel-640.onnx", INFERENCE_SIZE_SMALL)
|
| 100 |
|
| 101 |
|
| 102 |
def _letterbox(img, size: int):
|
|
|
|
| 16 |
|
| 17 |
INFERENCE_SIZE = 1024
|
| 18 |
INFERENCE_SIZE_SMALL = 640
|
| 19 |
+
CONF_THRESHOLD = 0.65
|
| 20 |
+
IOU_THRESHOLD = 0.42
|
| 21 |
MAX_DET = 4000
|
| 22 |
+
TILE_OVERLAP = 0.28
|
| 23 |
|
| 24 |
_sessions: dict[str, Any] = {}
|
| 25 |
_session_lock = Lock()
|
|
|
|
| 74 |
|
| 75 |
def _suggest_tile_grid(w: int, h: int) -> int:
|
| 76 |
mp = _megapixels(w, h)
|
| 77 |
+
if mp >= 6:
|
| 78 |
return 5
|
| 79 |
+
if mp >= 3:
|
| 80 |
return 4
|
| 81 |
+
if mp >= 1.2:
|
| 82 |
return 3
|
| 83 |
return 2
|
| 84 |
|
| 85 |
|
| 86 |
def _should_auto_tile(w: int, h: int) -> bool:
|
| 87 |
+
return _megapixels(w, h) >= 0.75 or max(w, h) > 1400
|
| 88 |
|
| 89 |
|
| 90 |
def _select_model(w: int, h: int) -> tuple[Path, int]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
big = MODEL_DIR / "countmodel.onnx"
|
| 92 |
if big.is_file():
|
| 93 |
return big, INFERENCE_SIZE
|
| 94 |
+
return MODEL_DIR / "countmodel-640.onnx", INFERENCE_SIZE_SMALL
|
|
|
|
| 95 |
|
| 96 |
|
| 97 |
def _letterbox(img, size: int):
|
frontend/src/App.tsx
CHANGED
|
@@ -49,7 +49,7 @@ export default function App() {
|
|
| 49 |
// settings — server is the default counting mode
|
| 50 |
const [inferenceMode, setInferenceMode] = useState<InferenceMode>(loadInferenceMode)
|
| 51 |
const [groupCount, setGroupCount] = useState<number | 'auto'>(1)
|
| 52 |
-
const [useTiling, setUseTiling] = useState(
|
| 53 |
const [filterOutliers, setFilterOutliers] = useState(false)
|
| 54 |
|
| 55 |
// ui state
|
|
|
|
| 49 |
// settings — server is the default counting mode
|
| 50 |
const [inferenceMode, setInferenceMode] = useState<InferenceMode>(loadInferenceMode)
|
| 51 |
const [groupCount, setGroupCount] = useState<number | 'auto'>(1)
|
| 52 |
+
const [useTiling, setUseTiling] = useState(true)
|
| 53 |
const [filterOutliers, setFilterOutliers] = useState(false)
|
| 54 |
|
| 55 |
// ui state
|
frontend/src/lib/constants.ts
CHANGED
|
@@ -3,8 +3,10 @@ export const MODEL_640_URL = '/models/countmodel-640.onnx'
|
|
| 3 |
export const MODEL_INT8_URL = '/models/countmodel-int8.onnx'
|
| 4 |
export const INFERENCE_SIZE = 1024
|
| 5 |
export const INFERENCE_SIZE_SMALL = 640
|
| 6 |
-
|
| 7 |
-
export const
|
|
|
|
|
|
|
| 8 |
export const MAX_DETECTIONS = 4000
|
| 9 |
|
| 10 |
export const GROUP_COLORS = [
|
|
|
|
| 3 |
export const MODEL_INT8_URL = '/models/countmodel-int8.onnx'
|
| 4 |
export const INFERENCE_SIZE = 1024
|
| 5 |
export const INFERENCE_SIZE_SMALL = 640
|
| 6 |
+
/** Lower = more detections (recall); raised from 0.55 to reduce false positives. */
|
| 7 |
+
export const CONFIDENCE_THRESHOLD = 0.65
|
| 8 |
+
export const IOU_THRESHOLD = 0.42
|
| 9 |
+
export const TILE_OVERLAP = 0.28
|
| 10 |
export const MAX_DETECTIONS = 4000
|
| 11 |
|
| 12 |
export const GROUP_COLORS = [
|
frontend/src/lib/i18n.ts
CHANGED
|
@@ -56,7 +56,7 @@ const messages = {
|
|
| 56 |
exportCsv: 'Spreadsheet (CSV)',
|
| 57 |
export: 'Export results',
|
| 58 |
cancel: 'Cancel',
|
| 59 |
-
highAccuracy: 'High accuracy (multi-tile)',
|
| 60 |
filterOutliers: 'Filter outliers',
|
| 61 |
mismatch: 'Mismatch',
|
| 62 |
selectGroup: 'Tap a group, then use tools to correct detections.',
|
|
@@ -124,7 +124,7 @@ const messages = {
|
|
| 124 |
exportCsv: 'ตาราง (CSV)',
|
| 125 |
export: 'ส่งออกผลลัพธ์',
|
| 126 |
cancel: 'ยกเลิก',
|
| 127 |
-
highAccuracy: 'ความแม่นยำสูง (หลายส่วน)',
|
| 128 |
filterOutliers: 'กรองค่าผิดปกติ',
|
| 129 |
mismatch: 'ไม่ตรง',
|
| 130 |
selectGroup: 'แตะเลือกกลุ่ม แล้วใช้เครื่องมือแก้ไขจุดตรวจจับ',
|
|
|
|
| 56 |
exportCsv: 'Spreadsheet (CSV)',
|
| 57 |
export: 'Export results',
|
| 58 |
cancel: 'Cancel',
|
| 59 |
+
highAccuracy: 'High accuracy (multi-tile, recommended)',
|
| 60 |
filterOutliers: 'Filter outliers',
|
| 61 |
mismatch: 'Mismatch',
|
| 62 |
selectGroup: 'Tap a group, then use tools to correct detections.',
|
|
|
|
| 124 |
exportCsv: 'ตาราง (CSV)',
|
| 125 |
export: 'ส่งออกผลลัพธ์',
|
| 126 |
cancel: 'ยกเลิก',
|
| 127 |
+
highAccuracy: 'ความแม่นยำสูง (หลายส่วน, แนะนำ)',
|
| 128 |
filterOutliers: 'กรองค่าผิดปกติ',
|
| 129 |
mismatch: 'ไม่ตรง',
|
| 130 |
selectGroup: 'แตะเลือกกลุ่ม แล้วใช้เครื่องมือแก้ไขจุดตรวจจับ',
|
frontend/src/lib/imageEnhance.ts
CHANGED
|
@@ -195,18 +195,18 @@ export function megapixels(width: number, height: number): number {
|
|
| 195 |
|
| 196 |
export function suggestTileGrid(width: number, height: number): number {
|
| 197 |
const mp = megapixels(width, height)
|
| 198 |
-
if (mp >=
|
| 199 |
return 5
|
| 200 |
}
|
| 201 |
-
if (mp >=
|
| 202 |
return 4
|
| 203 |
}
|
| 204 |
-
if (mp >= 2) {
|
| 205 |
return 3
|
| 206 |
}
|
| 207 |
return 2
|
| 208 |
}
|
| 209 |
|
| 210 |
export function shouldAutoTile(width: number, height: number): boolean {
|
| 211 |
-
return megapixels(width, height) >=
|
| 212 |
}
|
|
|
|
| 195 |
|
| 196 |
export function suggestTileGrid(width: number, height: number): number {
|
| 197 |
const mp = megapixels(width, height)
|
| 198 |
+
if (mp >= 6) {
|
| 199 |
return 5
|
| 200 |
}
|
| 201 |
+
if (mp >= 3) {
|
| 202 |
return 4
|
| 203 |
}
|
| 204 |
+
if (mp >= 1.2) {
|
| 205 |
return 3
|
| 206 |
}
|
| 207 |
return 2
|
| 208 |
}
|
| 209 |
|
| 210 |
export function shouldAutoTile(width: number, height: number): boolean {
|
| 211 |
+
return megapixels(width, height) >= 0.75 || Math.max(width, height) > 1400
|
| 212 |
}
|
frontend/src/lib/inference.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import * as ort from 'onnxruntime-web'
|
| 2 |
-
import { CONFIDENCE_THRESHOLD, IOU_THRESHOLD, MAX_DETECTIONS, MODEL_640_URL, MODEL_URL } from './constants'
|
| 3 |
import {
|
| 4 |
enhanceImageElement,
|
| 5 |
shouldAutoTile,
|
|
@@ -298,7 +298,7 @@ async function runTiledDetection(
|
|
| 298 |
const w = image.naturalWidth
|
| 299 |
const h = image.naturalHeight
|
| 300 |
const grid = suggestTileGrid(w, h)
|
| 301 |
-
const overlap =
|
| 302 |
const tileW = w / grid
|
| 303 |
const tileH = h / grid
|
| 304 |
const all: Detection[] = []
|
|
|
|
| 1 |
import * as ort from 'onnxruntime-web'
|
| 2 |
+
import { CONFIDENCE_THRESHOLD, IOU_THRESHOLD, MAX_DETECTIONS, MODEL_640_URL, MODEL_URL, TILE_OVERLAP } from './constants'
|
| 3 |
import {
|
| 4 |
enhanceImageElement,
|
| 5 |
shouldAutoTile,
|
|
|
|
| 298 |
const w = image.naturalWidth
|
| 299 |
const h = image.naturalHeight
|
| 300 |
const grid = suggestTileGrid(w, h)
|
| 301 |
+
const overlap = TILE_OVERLAP
|
| 302 |
const tileW = w / grid
|
| 303 |
const tileH = h / grid
|
| 304 |
const all: Detection[] = []
|
frontend/src/lib/modelSelect.ts
CHANGED
|
@@ -10,8 +10,8 @@ export function selectModelForImage(width: number, height: number): {
|
|
| 10 |
inferenceSize: number
|
| 11 |
} {
|
| 12 |
const maxDim = Math.max(width, height)
|
| 13 |
-
//
|
| 14 |
-
if (maxDim <=
|
| 15 |
return { modelUrl: MODEL_640_URL, inferenceSize: INFERENCE_SIZE_SMALL }
|
| 16 |
}
|
| 17 |
return { modelUrl: MODEL_URL, inferenceSize: INFERENCE_SIZE }
|
|
|
|
| 10 |
inferenceSize: number
|
| 11 |
} {
|
| 12 |
const maxDim = Math.max(width, height)
|
| 13 |
+
// Prefer 1024 for pipe-end detail; 640 only for very small previews.
|
| 14 |
+
if (maxDim <= 900) {
|
| 15 |
return { modelUrl: MODEL_640_URL, inferenceSize: INFERENCE_SIZE_SMALL }
|
| 16 |
}
|
| 17 |
return { modelUrl: MODEL_URL, inferenceSize: INFERENCE_SIZE }
|