yolov8 控制台输出是从哪里打印的?

问题描述 投票:0回答:1

我有以下代码

from ultralytics import YOLO
import cv2
import math
import os 
import time

# Start webcam
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)

# Load custom model
model_path = os.path.join('.', 'runs', 'detect', 'train', 'weights', 'last.pt')
model = YOLO(model_path)  # load a custom model

# Define your custom object classes
classNames = ["reese_pretzel"]  # Update with your custom classes

# Confidence threshold
confidence_threshold = 0.5

# Initialize variables for tracking time
start_time = None
end_time = None
object_detected = False

while True:
    success, img = cap.read()
    results = model(img, stream=True)

    # Process results
    for r in results:
        boxes = r.boxes

        for box in boxes:
            # Extract box coordinates
            x1, y1, x2, y2 = box.xyxy[0]
            x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)

            # Confidence
            confidence = math.ceil((box.conf[0]*100))/100

            # Check confidence threshold
            if confidence > confidence_threshold:
                # Class name
                cls = int(box.cls[0])
                class_name = classNames[cls]

                # Draw bounding box
                cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 3)

                # Object details
                org = [x1, y1]
                font = cv2.FONT_HERSHEY_SIMPLEX
                fontScale = 1
                color = (255, 0, 0)
                thickness = 2

                cv2.putText(img, class_name, org, font, fontScale, color, thickness)
                
                # Set start time when an object is first detected
                if not object_detected:
                    start_time = time.time()
                    object_detected = True
            else:
                # Reset start time when no object is detected
                start_time = None
                object_detected = False

    cv2.imshow('Webcam', img)
    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

我不断向控制台输出

0: 480x640 (no detections), 49.2ms
Speed: 0.9ms preprocess, 49.2ms inference, 0.2ms postprocess per image at shape (1, 3, 480, 640)

0: 384x640 1 reese_pretzel, 75.0ms
Speed: 4.8ms preprocess, 75.0ms inference, 0.3ms postprocess per image at shape (1, 3, 384, 640)

这些消息是从哪里打印的?

我尝试查看 YOLO model.py 文件,但其中也没有任何打印语句。我想做一些类似的事情,如果没有检测到,则打印('无检测),如果检测到某些东西,则打印对象的名称。

python opencv computer-vision yolov8
1个回答
0
投票

这些日志是通过

predictor.py
模块的方法 stream_inference() 打印的:https://github.com/ultralytics/ultralytics/blob/main/ultralytics/engine/predictor.py#L243。按照
LOGGER.info()

0: 384x640 1 reese_pretzel, 75.0ms

输出消息的第一行来自代码行 317 - 319:

# Print time (inference-only)
if self.args.verbose:
    LOGGER.info(f"{s}{profilers[1].dt * 1E3:.1f}ms")
这里的

s
来自同一模块的
write_results()
方法,如果深入的话-来自
results.py
模块的verbose()方法:https://github.com/ultralytics/ultralytics/blob /main/ultralytics/engine/results.py#L315.

速度:形状 (1, 3, 384, 640) 下每张图像的预处理时间为 4.8 毫秒,推理时间为 75.0 毫秒,后处理时间为 0.3 毫秒

输出消息的第二行来自第一个模块的

stream_inference()
方法的代码行325 - 330:

# Print results
if self.args.verbose and self.seen:
    t = tuple(x.t / self.seen * 1e3 for x in profilers)  # speeds per image
    LOGGER.info(
        f"Speed: %.1fms preprocess, %.1fms inference, %.1fms 
        postprocess per image at shape "f"{(1, 3, *im.shape[2:])}" % t
    )
© www.soinside.com 2019 - 2024. All rights reserved.