yolo算法检测框上表现中文,yolo表现中文

[复制链接]
发表于 2026-1-12 17:50:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
 
检测效果可以看我b站视频【yolo检测框表现中文-哔哩哔哩】
https://b23.tv/iXZRt6m
  1. from ultralytics import YOLO
  2. import numpy as np
  3. import cv2
  4. from ultralytics import YOLO
  5. # Load a pretrained YOLOv8n model
  6. model = YOLO("yolov8n.pt")#我加载的是官方权重
  7. # Define path to video file
  8. video_path = r"D:\daye_input.mp4"
  9. cap = cv2.VideoCapture(video_path)
  10. if not cap.isOpened():
  11.     print("Cannot open camera")
  12.     exit()
  13. fps = cap.get(cv2.CAP_PROP_FPS)#输入视频帧率
  14. print(f"输入视频帧率为:Frames per second: {fps}")
  15. # 保存视频的一些设置
  16. width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
  17. height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
  18. # width = int(1920)
  19. # height = int(1080)
  20. # fourcc = cv2.VideoWriter_fourcc(*'mp4v')  # 保存视频的编码格式
  21. # output_video_path = 'daye.mp4'
  22. # fourcc = cv2.VideoWriter_fourcc(*'mp4v')  # 保存视频的编码格式
  23. # output_video_path = 'daye.mp4'
  24. fourcc = cv2.VideoWriter_fourcc(*'XVID')  # 保存视频的编码格式
  25. output_video_path = 'daye.avi'
  26. out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
  27. from PIL import Image, ImageDraw, ImageFont
  28. # 设置字体
  29. font = ImageFont.truetype(r'C:\Windows\Fonts\SimHei.ttf', 60)  # 替换为你的字体路径
  30. # 循环遍历视频帧
  31. while cap.isOpened():
  32.     # start_time = time.time() #记录开始时间
  33.     # 从视频读取一帧
  34.     success, frame = cap.read()
  35.     if success:
  36.         # 在帧上运行YOLOv8追踪,持续追踪帧间的物体
  37.         #     results = model(frame,  conf=0.3,iou=0.5,imgsz=(640,640))
  38.             results = model.track(frame, persist=True, conf=0.3, iou=0.5, tracker="ultralytics/cfg/trackers/bytetrack.yaml",
  39.                               imgsz=(1920,1080))
  40.             img_pil = Image.fromarray(frame)
  41.             draw = ImageDraw.Draw(img_pil)  # 创建Draw对象
  42.             if results[0].boxes and results[0].boxes.id is not None:
  43.                boxes = results[0].boxes.xyxy.cpu()
  44.                clss = results[0].boxes.cls
  45.                track_ids = results[0].boxes.id.int().cpu().tolist()
  46.                for id, cls, boxxyxy in zip( track_ids, clss, boxes ):
  47.                    class_id = cls.item()
  48.                    x1, y1, x2, y2 = boxxyxy
  49.                    tracker_id = id
  50.                    if class_id == 0.0:
  51.                        # class_id = 'DaYe'
  52.                        draw.text((int(x1), int(y1) - 50), str(tracker_id)+"号大爷", font=font, fill=(0, 0,255))# 在图片上绘制中文
  53.                        # cv2.rectangle(img_pil, (int(x1), int(y1)), (int(x2), int(y2)), (0, 69, 255), 2)  # 橙红
  54.                        draw.rectangle([(int(x1), int(y1)), (int(x2), int(y2))], outline="blue", width=4)  # 红色边框,宽度5
  55.                    # else:
  56.                    #     draw.text((int(x1), int(y1) - 50),  "不像大爷", font=font,
  57.                    #               fill=(255, 0, 0))  # 在图片上绘制中文
  58.                    #     # cv2.rectangle(img_pil, (int(x1), int(y1)), (int(x2), int(y2)), (0, 69, 255), 2)  # 橙红
  59.                    #
  60.                    #     draw.rectangle([(int(x1), int(y1)), (int(x2), int(y2))], outline="red", width=4)  # 红色边框,宽度5
  61.             # img = cv2.cvtColor(np.array(img_pil), cv2.COLOR_BGR2RGB)
  62.             img = np.array(img_pil)
  63.             # cv2.imshow('frame_pil', img)
  64.             # img = cv2.resize( img, (1920, 1080))
  65.             out.write(img)
  66.             cv2.imshow('frame_pil', img)
  67.             if cv2.waitKey(1) & 0xFF == ord('q'):
  68.                 break
  69.             # cv2.waitKey(0)
  70.             # cv2.destroyAllWindows()
  71.     else:
  72.         break
  73. cap.release()
  74. out.release()
  75. cv2.destroyAllWindows()
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表