yolo视频检测时,检测框上显示中文名字

python 复制代码
from ultralytics import YOLO
import numpy as np
import cv2
from ultralytics import YOLO

# Load a pretrained YOLOv8n model
model = YOLO("yolov8n.pt")#我加载的是官方权重

# Define path to video file
video_path = r"D:\daye_input.mp4"
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
    print("Cannot open camera")
    exit()

fps = cap.get(cv2.CAP_PROP_FPS)#输入视频帧率
print(f"输入视频帧率为:Frames per second: {fps}")

# 保存视频的一些设置
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# width = int(1920)
# height = int(1080)
# fourcc = cv2.VideoWriter_fourcc(*'mp4v')  # 保存视频的编码格式
# output_video_path = 'daye.mp4'
# fourcc = cv2.VideoWriter_fourcc(*'mp4v')  # 保存视频的编码格式
# output_video_path = 'daye.mp4'

fourcc = cv2.VideoWriter_fourcc(*'XVID')  # 保存视频的编码格式
output_video_path = 'daye.avi'


out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))

from PIL import Image, ImageDraw, ImageFont

# 设置字体
font = ImageFont.truetype(r'C:\Windows\Fonts\SimHei.ttf', 60)  # 替换为你的字体路径
# 循环遍历视频帧
while cap.isOpened():

    # start_time = time.time() #记录开始时间

    # 从视频读取一帧
    success, frame = cap.read()

    if success:
        # 在帧上运行YOLOv8追踪,持续追踪帧间的物体
        #     results = model(frame,  conf=0.3,iou=0.5,imgsz=(640,640))
            results = model.track(frame, persist=True, conf=0.3, iou=0.5, tracker="ultralytics/cfg/trackers/bytetrack.yaml",
                              imgsz=(1920,1080))
            img_pil = Image.fromarray(frame)
            draw = ImageDraw.Draw(img_pil)  # 创建Draw对象

            if results[0].boxes and results[0].boxes.id is not None:
               boxes = results[0].boxes.xyxy.cpu()
               clss = results[0].boxes.cls
               track_ids = results[0].boxes.id.int().cpu().tolist()
               for id, cls, boxxyxy in zip( track_ids, clss, boxes ):
                   class_id = cls.item()
                   x1, y1, x2, y2 = boxxyxy
                   tracker_id = id
                   if class_id == 0.0:
                       # class_id = 'DaYe'

                       draw.text((int(x1), int(y1) - 50), str(tracker_id)+"号大爷", font=font, fill=(0, 0,255))# 在图片上绘制中文
                       # cv2.rectangle(img_pil, (int(x1), int(y1)), (int(x2), int(y2)), (0, 69, 255), 2)  # 橙红

                       draw.rectangle([(int(x1), int(y1)), (int(x2), int(y2))], outline="blue", width=4)  # 红色边框,宽度5

                   # else:
                   #     draw.text((int(x1), int(y1) - 50),  "不像大爷", font=font,
                   #               fill=(255, 0, 0))  # 在图片上绘制中文
                   #     # cv2.rectangle(img_pil, (int(x1), int(y1)), (int(x2), int(y2)), (0, 69, 255), 2)  # 橙红
                   #
                   #     draw.rectangle([(int(x1), int(y1)), (int(x2), int(y2))], outline="red", width=4)  # 红色边框,宽度5

            # img = cv2.cvtColor(np.array(img_pil), cv2.COLOR_BGR2RGB)
            img = np.array(img_pil)
            # cv2.imshow('frame_pil', img)
            # img = cv2.resize( img, (1920, 1080))
            out.write(img)

            cv2.imshow('frame_pil', img)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            # cv2.waitKey(0)
            # cv2.destroyAllWindows()
    else:
        break
cap.release()
out.release()
cv2.destroyAllWindows()

检测效果见我b站视频【yolo检测框显示中文】 https://www.bilibili.com/video/BV1ih2wYWEcM/?share_source=copy_web\&vd_source=84543f4291e70cc3c31e5db4f6cabde8

相关推荐
前网易架构师-高司机25 分钟前
带标注的带标注的医用采血管分类和标签识别数据集,识别率80.7%,5524张图,支持yolo,coco json,voc xml,文末有模型训练代码
yolo·数据集·标签·医学·颜色·医院·采血管
西柚研究生1234561 小时前
论文分析10:基于改进YOLOv8模型的雨雾天气目标识别算法
yolo
前网易架构师-高司机21 小时前
带标注的山体滑坡塌方数据集数据集,识别率78.1%,974张图,支持yolo,coco json,voc xml,文末有模型训练代码
xml·yolo·json·数据集·自然灾害·山体滑坡
guo_xiao_xiao_1 天前
YOLOv11马匹皮肤病目标检测数据集
人工智能·yolo·目标检测
明月醉窗台1 天前
C++ tensorrt部署时打包需要的依赖库
yolo·nvidia
前网易架构师-高司机2 天前
带标注的SDD-SAR遥感船舶识别数据集,识别率92.9%,7000张图,支持yolo,coco json,voc xml,文末有模型训练代码
yolo·json·数据集·遥感·sar·船舶·sdd
一见已难忘3 天前
4类红外热成像目标检测数据集(轿车/行人/货车/摩托车)| 4300张YOLO红外夜视检测数据集 适用于自动驾驶、智能安防与红外目标检测研究
yolo·目标检测·自动驾驶
会飞锦鲤3 天前
基于YOLOv10的瓜果成熟度智能检测系统
人工智能·python·深度学习·yolo·flask
朱永博3 天前
Cpp使用Tensorrt10.8推理Yolo26
yolo
YOLO数据集集合3 天前
井下钻场智能监控系统:YOLOv8驱动的多目标检测全栈实战 | 打钻过程监测、安全行为识别、灾害防治辅助系统
yolo·目标检测·目标跟踪