我在Kaggle笔记本上用YOLO做目标检测项目时遇到这个错误。这是我的代码和错误信息。

0 投票
0 回答
22 浏览
提问于 2025-04-12 05:01

我还是个新手,所以不知道该怎么做。
我希望它能打开一个新标签页,或者至少把输出保存成一个视频文件,这样我就能看到被检测到的物体。

我在 pycharm 上成功做到了这一点。因为我没有一个快的显卡,所以我在 Kaggle 上进行。

请帮帮我这个新手。

代码:

from ultralytics import YOLO
import cv2
import cvzone
import math
from IPython.display import HTML

model = YOLO('yolov8n.pt')

classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
              "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
              "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
              "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat",
              "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
              "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
              "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed",
              "diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone",
              "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
              "teddy bear", "hair drier", "toothbrush"]

# Open the video file
cap = cv2.VideoCapture('/kaggle/input/demo-videos/car2.mp4')

# Get video properties
fps = cap.get(cv2.CAP_PROP_FPS)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
output_video_path = '/kaggle/working/output.avi'
out = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))

while True:
    success, img = cap.read()
    if not success:
        break
    
    # Perform object detection
    results = model(img)
    
    # Print the results to understand its structure
    print(results)
    
    # Draw bounding boxes and annotations
    for pred in results:
        for det in pred:
            x1, y1, x2, y2 = map(int, det[:4])
            conf = det[4]
            cls = int(det[5])
            cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
            cv2.putText(img, f'{classNames[cls]} {conf:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
    
    # Write the annotated frame to the output video
    out.write(img)

# Release the video capture and writer objects
cap.release()
out.release()

# Display the video in a separate output cell
output_video_html = f'<video controls src="{output_video_path}" width="{frame_width}" height="{frame_height}" type="video/avi"></video>'
display(HTML(output_video_html))
Error: 



---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[25], line 47
     45 for pred in results:
     46     for det in pred:
---> 47         x1, y1, x2, y2 = map(int, det[:4])
     48         conf = det[4]
     49         cls = int(det[5])

TypeError: int() argument must be a string, a bytes-like object or a real number, not 'Results'

我正在尝试在 Kaggle 笔记本上使用 YOLO 做一个物体检测的项目,但我遇到了这个错误。这里是我的代码和错误信息。

0 个回答

暂无回答

撰写回答