深度学习篇---COCO数据集80个类别详解
·
一、80个类别完整列表
COCO(Common Objects in Context)数据集包含80个常见物体类别,分为8个大类:
1. 🚗 交通工具类(15个)
person(人) bicycle(自行车) car(汽车) motorcycle(摩托车) airplane(飞机) bus(公交车) train(火车) truck(卡车) boat(船) traffic light(交通灯) fire hydrant(消防栓) stop sign(停车标志) parking meter(停车计时器) bench(长椅)
2. 🏠 户外物品类(8个)
bird(鸟) cat(猫) dog(狗) horse(马) sheep(羊) cow(牛) elephant(大象) bear(熊) zebra(斑马) giraffe(长颈鹿)
3. 👜 日常物品类(26个)
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(牙刷)
4. 🍽️ 食物类(10个)
banana(香蕉) apple(苹果) sandwich(三明治) orange(橘子) broccoli(西兰花) carrot(胡萝卜) hot dog(热狗) pizza(披萨) donut(甜甜圈) cake(蛋糕)
5. 🏠 家具类(9个)
chair(椅子) couch(沙发) potted plant(盆栽植物) bed(床) dining table(餐桌) toilet(马桶) tv(电视) laptop(笔记本电脑)
6. 💻 电子产品类(5个)
tv(电视) laptop(笔记本电脑) mouse(鼠标) remote(遥控器) keyboard(键盘) cell phone(手机)
7. 🛠️ 家用电器类(5个)
microwave(微波炉) oven(烤箱) toaster(烤面包机) sink(水槽) refrigerator(冰箱)
8. 📚 文具杂物类(6个)
book(书) clock(钟) vase(花瓶) scissors(剪刀) teddy bear(泰迪熊) hair drier(吹风机) toothbrush(牙刷)
二、完整类别列表(按序号)
这是完整的80个类别,按COCO数据集的标准顺序:
序号 英文名称 中文名称 0 person 人 1 bicycle 自行车 2 car 汽车 3 motorcycle 摩托车 4 airplane 飞机 5 bus 公交车 6 train 火车 7 truck 卡车 8 boat 船 9 traffic light 交通灯 10 fire hydrant 消防栓 11 stop sign 停车标志 12 parking meter 停车计时器 13 bench 长椅 14 bird 鸟 15 cat 猫 16 dog 狗 17 horse 马 18 sheep 羊 19 cow 牛 20 elephant 大象 21 bear 熊 22 zebra 斑马 23 giraffe 长颈鹿 24 backpack 背包 25 umbrella 伞 26 handbag 手提包 27 tie 领带 28 suitcase 行李箱 29 frisbee 飞盘 30 skis 滑雪板 31 snowboard 滑雪板 32 sports ball 运动球 33 kite 风筝 34 baseball bat 棒球棒 35 baseball glove 棒球手套 36 skateboard 滑板 37 surfboard 冲浪板 38 tennis racket 网球拍 39 bottle 瓶子 40 wine glass 酒杯 41 cup 杯子 42 fork 叉子 43 knife 刀 44 spoon 勺子 45 bowl 碗 46 banana 香蕉 47 apple 苹果 48 sandwich 三明治 49 orange 橘子 50 broccoli 西兰花 51 carrot 胡萝卜 52 hot dog 热狗 53 pizza 披萨 54 donut 甜甜圈 55 cake 蛋糕 56 chair 椅子 57 couch 沙发 58 potted plant 盆栽植物 59 bed 床 60 dining table 餐桌 61 toilet 马桶 62 tv 电视 63 laptop 笔记本电脑 64 mouse 鼠标 65 remote 遥控器 66 keyboard 键盘 67 cell phone 手机 68 microwave 微波炉 69 oven 烤箱 70 toaster 烤面包机 71 sink 水槽 72 refrigerator 冰箱 73 book 书 74 clock 钟 75 vase 花瓶 76 scissors 剪刀 77 teddy bear 泰迪熊 78 hair drier 吹风机 79 toothbrush 牙刷
三、打印类别的代码
以下是几种打印类别的方法:
方法1:直接打印(最简单)
'''
Author: Ronin-Lotus
Description: 打印YOLOv8 COCO数据集的80个类别
'''
from ultralytics import YOLO
# 加载YOLOv8模型
model = YOLO("yolov8n.pt") # 或者 yolov8s.pt, yolov8m.pt 等
# 打印模型信息
print("=" * 60)
print("YOLOv8 模型类别信息")
print("=" * 60)
# 方法1:直接获取并打印类别
print("\n方法1:直接打印所有类别")
if hasattr(model, 'names') and model.names:
print(f"总共 {len(model.names)} 个类别:")
for idx, name in model.names.items():
print(f" {idx:2d}: {name}")
else:
print("无法获取类别名称")
# 方法2:按分组打印(更有条理)
print("\n" + "=" * 60)
print("方法2:按类别分组打印")
print("=" * 60)
# 定义类别分组
categories = {
"🚗 交通工具类": [0, 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类)
chinese_names = {
0: "人", 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: "牙刷"
}
# 按分组打印
for category_name, indices in categories.items():
print(f"\n{category_name} ({len(indices)}个):")
for idx in indices:
if idx in model.names and idx in chinese_names:
print(f" {idx:2d}: {model.names[idx]:15s} ({chinese_names[idx]})")
print("\n" + "=" * 60)
print("统计信息:")
print("=" * 60)
print(f"总类别数: {len(model.names)}")
print(f"模型类型: {model.__class__.__name__}")
print(f"模型文件: yolov8n.pt")
方法2:创建可视化函数
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
def visualize_coco_categories(model):
"""
可视化COCO数据集的所有类别
"""
# 创建图表
fig, axes = plt.subplots(8, 10, figsize=(20, 16))
fig.suptitle('COCO 数据集80个类别', fontsize=16, fontweight='bold')
# 平铺axes
axes = axes.flatten()
# 为每个类别创建一个小图
for idx in range(80):
ax = axes[idx]
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.axis('off')
# 显示类别编号和名称
if idx in model.names:
category_name = model.names[idx]
# 添加背景矩形
rect = Rectangle((0.1, 0.1), 0.8, 0.8,
linewidth=2, edgecolor='blue',
facecolor='lightblue', alpha=0.5)
ax.add_patch(rect)
# 添加文本
ax.text(0.5, 0.7, f"{idx}",
fontsize=10, fontweight='bold',
ha='center', va='center')
ax.text(0.5, 0.4, category_name,
fontsize=8, ha='center', va='center')
# 添加中文名称(如果存在)
chinese_names = {
0: "人", 1: "自行车", 2: "汽车", 3: "摩托车", 4: "飞机",
# ... 这里可以添加更多中文名称
}
if idx in chinese_names:
ax.text(0.5, 0.2, chinese_names[idx],
fontsize=7, ha='center', va='center', color='red')
# 隐藏多余的子图
for i in range(80, len(axes)):
axes[i].axis('off')
plt.tight_layout()
plt.savefig('coco_categories.png', dpi=150, bbox_inches='tight')
plt.show()
# 使用可视化函数
model = YOLO("yolov8n.pt")
visualize_coco_categories(model)
方法3:导出为HTML表格
def export_to_html(model, filename='coco_categories.html'):
"""
将类别信息导出为HTML表格
"""
html = """
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>COCO数据集80个类别</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
h1 { color: #333; text-align: center; }
table { width: 100%; border-collapse: collapse; margin: 20px 0; }
th { background-color: #4CAF50; color: white; padding: 12px; }
td { border: 1px solid #ddd; padding: 8px; text-align: center; }
tr:nth-child(even) { background-color: #f2f2f2; }
.category-group { background-color: #e8f4f8; font-weight: bold; }
.chinese { color: #d9534f; font-weight: bold; }
</style>
</head>
<body>
<h1>COCO数据集80个类别</h1>
<table>
<tr>
<th>序号</th>
<th>英文名称</th>
<th>中文名称</th>
<th>类别分组</th>
</tr>
"""
# 类别分组映射
group_map = {}
groups = {
"交通工具类": list(range(0, 14)),
"动物类": list(range(14, 24)),
"日常物品类": list(range(24, 39)),
"餐具类": list(range(39, 46)),
"食物类": list(range(46, 56)),
"家具类": list(range(56, 62)),
"电子产品类": list(range(62, 68)),
"家用电器类": list(range(68, 73)),
"文具杂物类": list(range(73, 80))
}
for group_name, indices in groups.items():
for idx in indices:
group_map[idx] = group_name
# 添加中英文对照
chinese_names = {
0: "人", 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: "牙刷"
}
# 生成表格行
for idx in range(80):
if idx in model.names:
english_name = model.names[idx]
chinese_name = chinese_names.get(idx, "")
category_group = group_map.get(idx, "其他")
html += f"""
<tr>
<td>{idx}</td>
<td><strong>{english_name}</strong></td>
<td class="chinese">{chinese_name}</td>
<td>{category_group}</td>
</tr>
"""
html += """
</table>
<p><strong>总计: 80个类别</strong></p>
<p>数据来源: COCO (Common Objects in Context) 数据集</p>
</body>
</html>
"""
with open(filename, 'w', encoding='utf-8') as f:
f.write(html)
print(f"HTML文件已保存为: {filename}")
# 使用导出函数
model = YOLO("yolov8n.pt")
export_to_html(model)
方法4:快速检查脚本
python
'''
快速检查YOLO模型类别的小脚本
'''
def quick_check_categories():
"""快速检查并打印类别"""
# 加载模型
model = YOLO("yolov8n.pt")
print("🔍 YOLOv8 类别快速检查")
print("=" * 50)
# 检查基本信息
print(f"模型: yolov8n.pt")
print(f"总类别数: {len(model.names)}")
# 随机显示几个类别作为示例
print("\n📋 类别示例(前10个和后10个):")
print("-" * 30)
# 前10个
print("前10个类别:")
for i in range(10):
if i in model.names:
print(f" {i:2d}: {model.names[i]}")
# 中间的一些重要类别
print("\n一些重要类别:")
important_indices = [0, 1, 2, 15, 16, 56, 62, 67]
for i in important_indices:
if i in model.names:
print(f" {i:2d}: {model.names[i]}")
# 后10个
print("\n后10个类别:")
for i in range(70, 80):
if i in model.names:
print(f" {i:2d}: {model.names[i]}")
print("\n✅ 检查完成!")
return model.names
# 运行快速检查
if __name__ == "__main__":
categories = quick_check_categories()
# 询问用户是否想查看特定类别
while True:
choice = input("\n输入类别编号查看详情,或输入 'q' 退出: ").strip()
if choice.lower() == 'q':
print("再见!👋")
break
try:
idx = int(choice)
if 0 <= idx < 80:
print(f"类别 {idx}: {categories.get(idx, '未知')}")
else:
print("请输入0-79之间的数字")
except ValueError:
print("请输入有效的数字或 'q' 退出")
四、使用技巧
4.1 在预测时使用类别信息
python
from ultralytics import YOLO
import cv2
# 加载模型
model = YOLO("yolov8n.pt")
# 进行预测
results = model.predict(source="your_image.jpg", conf=0.25)
# 处理结果
for result in results:
# 获取检测到的类别
if result.boxes is not None:
for box in result.boxes:
class_id = int(box.cls[0]) # 类别ID
class_name = result.names[class_id] # 类别名称
confidence = float(box.conf[0]) # 置信度
print(f"检测到: {class_name} (ID: {class_id}), 置信度: {confidence:.3f}")
4.2 过滤特定类别
def detect_specific_classes(image_path, class_ids=[0, 2, 56, 67]):
"""
只检测特定类别的物体
class_ids: 要检测的类别ID列表
"""
model = YOLO("yolov8n.pt")
# 进行预测
results = model.predict(source=image_path, conf=0.25)
print(f"只检测以下类别:")
for class_id in class_ids:
if class_id in model.names:
print(f" {model.names[class_id]} (ID: {class_id})")
# 过滤结果
filtered_results = []
for result in results:
if result.boxes is not None:
for box in result.boxes:
class_id = int(box.cls[0])
if class_id in class_ids:
class_name = result.names[class_id]
confidence = float(box.conf[0])
filtered_results.append({
'class_id': class_id,
'class_name': class_name,
'confidence': confidence,
'box': box.xyxy[0].tolist()
})
return filtered_results
# 使用示例:只检测人、车、椅子和手机
detections = detect_specific_classes("your_image.jpg", class_ids=[0, 2, 56, 67])
for det in detections:
print(f"{det['class_name']}: {det['confidence']:.3f}")
这些代码可以帮助你了解和使用YOLOv8的80个类别。最简单的就是使用方法1,直接加载模型并打印model.names即可看到所有类别!
更多推荐


所有评论(0)