Spaces:
Sleeping
Sleeping
File size: 3,565 Bytes
b30e7a3 |
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 |
from __future__ import annotations
import difflib
import re
from typing import Dict, Tuple
COCO_CLASSES: Tuple[str, ...] = (
"person",
"bicycle",
"car",
"motorcycle",
"airplane",
"bus",
"train",
"truck",
"boat",
"traffic light",
"fire hydrant",
"stop sign",
"parking meter",
"bench",
"bird",
"cat",
"dog",
"horse",
"sheep",
"cow",
"elephant",
"bear",
"zebra",
"giraffe",
"backpack",
"umbrella",
"handbag",
"tie",
"suitcase",
"frisbee",
"skis",
"snowboard",
"sports ball",
"kite",
"baseball bat",
"baseball glove",
"skateboard",
"surfboard",
"tennis racket",
"bottle",
"wine glass",
"cup",
"fork",
"knife",
"spoon",
"bowl",
"banana",
"apple",
"sandwich",
"orange",
"broccoli",
"carrot",
"hot dog",
"pizza",
"donut",
"cake",
"chair",
"couch",
"potted plant",
"bed",
"dining table",
"toilet",
"tv",
"laptop",
"mouse",
"remote",
"keyboard",
"cell phone",
"microwave",
"oven",
"toaster",
"sink",
"refrigerator",
"book",
"clock",
"vase",
"scissors",
"teddy bear",
"hair drier",
"toothbrush",
)
def coco_class_catalog() -> str:
"""Return the COCO classes in a comma-separated catalog for prompts."""
return ", ".join(COCO_CLASSES)
def _normalize(label: str) -> str:
return re.sub(r"[^a-z0-9]+", " ", label.lower()).strip()
_CANONICAL_LOOKUP: Dict[str, str] = {_normalize(name): name for name in COCO_CLASSES}
_COCO_SYNONYMS: Dict[str, str] = {
"people": "person",
"man": "person",
"woman": "person",
"men": "person",
"women": "person",
"motorbike": "motorcycle",
"motor bike": "motorcycle",
"bike": "bicycle",
"aircraft": "airplane",
"plane": "airplane",
"jet": "airplane",
"aeroplane": "airplane",
"pickup": "truck",
"pickup truck": "truck",
"semi": "truck",
"lorry": "truck",
"tractor trailer": "truck",
"coach": "bus",
"television": "tv",
"tv monitor": "tv",
"mobile phone": "cell phone",
"smartphone": "cell phone",
"cellphone": "cell phone",
"dinner table": "dining table",
"sofa": "couch",
"cooker": "oven",
}
_ALIAS_LOOKUP: Dict[str, str] = {_normalize(alias): canonical for alias, canonical in _COCO_SYNONYMS.items()}
def canonicalize_coco_name(value: str | None) -> str | None:
"""Map an arbitrary string to the closest COCO class name if possible."""
if not value:
return None
normalized = _normalize(value)
if not normalized:
return None
if normalized in _CANONICAL_LOOKUP:
return _CANONICAL_LOOKUP[normalized]
if normalized in _ALIAS_LOOKUP:
return _ALIAS_LOOKUP[normalized]
for alias_norm, canonical in _ALIAS_LOOKUP.items():
if alias_norm and alias_norm in normalized:
return canonical
for canonical_norm, canonical in _CANONICAL_LOOKUP.items():
if canonical_norm and canonical_norm in normalized:
return canonical
tokens = normalized.split()
for token in tokens:
if token in _CANONICAL_LOOKUP:
return _CANONICAL_LOOKUP[token]
if token in _ALIAS_LOOKUP:
return _ALIAS_LOOKUP[token]
close = difflib.get_close_matches(normalized, list(_CANONICAL_LOOKUP.keys()), n=1, cutoff=0.82)
if close:
return _CANONICAL_LOOKUP[close[0]]
return None
|