简体   繁体   English

如何使用自定义 yolov5 更改标签字体、边界框颜色和厚度

[英]how to change label font, bounding box color and thickness with custom yolov5

I need to change the label font and bounding box color, in the inference我需要在推理中更改标签字体和边界框颜色

i have assigned a variable for torch hub model and pass it to running the inference but coudnt change the bounding box color and and text font我已经为 torch hub 模型分配了一个变量并将其传递给运行推理但无法更改边界框颜色和文本字体

model = torch.hub.load('ultralytics/yolov5', 'custom', path=r'C:\Users\rohin\Desktop\NewData\yolo training\last.pt', force_reload=True)
cap = cv2.VideoCapture(0)
while cap.isOpened():
    ret, frame = cap.read()

    # Make detections
    results = model(frame)

    cv2.imshow('YOLO', np.squeeze(results.render()))

    if cv2.waitKey(10) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()



i think, it will help you.我想,它会对你有所帮助。

import cv2
import numpy as np
import torch
import imutils


def draw_text(img, text,
          font=cv2.FONT_HERSHEY_COMPLEX_SMALL ,
          pos=(0, 0),
          font_scale=1,
          font_thickness=1,
          text_color=(255, 255, 255),
          text_color_bg=(0, 0, 0)
          ):

    x, y = pos
    text_size, _ = cv2.getTextSize(text, font, font_scale, font_thickness)
    text_w, text_h = text_size
    cv2.rectangle(img, pos, (x + text_w + 5, y + text_h+ 5), text_color_bg, -1)
    cv2.putText(img, text, (x, y + text_h + font_scale - 1), font, font_scale, text_color, font_thickness)



model = torch.hub.load('ultralytics/yolov5', 'custom', path=r'C:\Users\rohin\Desktop\NewData\yolo training\last.pt', force_reload=True)
# Create a VideoCapture object
cap = cv2.VideoCapture(0)

# Check if camera opened successfully
if (cap.isOpened() == False):
  print("Unable to read camera feed")


# Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.
out = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 20.0, (640, 360))

while(True):
  ret, frame = cap.read()

  if not ret:
    print("Can't receive frame (stream end?). Exiting ...")
    break
  color = [(240, 248, 255)]

  frame = imutils.resize(frame, width=640)
  results = model([frame], size=640)

  if results.pandas().xyxy[0].empty is not True:
    bbox = results.pandas().xyxy[0]

    for box in zip(bbox.xmin, bbox.ymin, bbox.xmax, bbox.ymax, bbox.confidence, bbox.name):
      if(box[4] >= 0.7):

        draw_text(frame, str(box[5]), font_scale=1, pos=(int(box[0]), int(box[1])), text_color_bg=(0, 0, 0))
        cv2.rectangle(frame, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (79, 255, 0), 2)

  # Write the frame into the file 'output.avi'
  out.write(frame)

  # Display the resulting frame
  cv2.imshow('frame',frame)

  # Press Q on keyboard to stop recording
  if cv2.waitKey(100) & 0xFF == ord('q'):
    break


# When everything done, release the video capture and video write objects
cap.release()
out.release()


# frame_width
# Closes all the frames
cv2.destroyAllWindows()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM