Search code examples
pythonopencvcomputer-visionyolov8

yolov8 where is console output being printed from?


I have the following code

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()

I keep getting outputs to my console like

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

or

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)

Where are these messages getting printed from?

I tried looking at the YOLO model.py file but that too doesn't have any print statements in it. I want to do something like if there are no detections, print('no detect), and if something is detected, print the name of the object.


Solution

  • These logs are printed by the method stream_inference() of the predictor.py module: https://github.com/ultralytics/ultralytics/blob/main/ultralytics/engine/predictor.py#L243. Follow the LOGGER.info().

    0: 384x640 1 reese_pretzel, 75.0ms

    The first line of the output message comes from the code lines 317 - 319:

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

    s here comes from the write_results() method of the same module, if go deeper - from the verbose() method of the results.py module: https://github.com/ultralytics/ultralytics/blob/main/ultralytics/engine/results.py#L315.

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

    The second line of the output message comes from the code lines 325 - 330 of the stream_inference() method of the first module:

    # 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
        )