YOLOv8 模型类通过视频帧进行预测

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

我需要一些帮助,因为我需要这个来为我的最终论文项目工作,因为模型有 2 类 inheat 和 non-inheat,我在下面做了代码,但是当它尝试通过视频帧进行预测或检测时,它检测到非inheat,但它只堆叠在ininheat帧中,而不是non_inheat帧中。我可能在这里做了一个错误的过程,因为通过帧将它们堆叠起来,即使它检测到非inheat它也堆叠到in_heat帧。

代码:

def process_video_with_second_model(video_path):
    cap = cv2.VideoCapture(video_path)
    class_counts = {'inheat': 0, 'non-inheat': 0}

    in_heat_frames = []
    non_in_heat_frames = []

    while True:
        ret, frame = cap.read()
        if frame is None:
            break # Break the loop when no more frames are available

        # Resize the frame to a smaller size (e.g., 400x400)
        frame_small = cv2.resize(frame, (400, 400))

        # Use the second model to detect in-heat behavior
        results_in_heat = yolov8_model_in_heat.predict(source=frame_small, show=True, conf=0.8)

        # Print results to inspect structure
        for results_in_heat_instance in results_in_heat:
            # Access bounding box coordinates
            boxes = results_in_heat_instance.boxes

            # CONFIDENCE 0.5
            if len(boxes) > 0:
                class_name = results_in_heat_instance.names[0]

                # Use a dictionary to store the counts for each class
                class_counts[class_name] += 1

                # Add the frame to the corresponding list based on the class name
                if class_name == 'non-inheat':
                    non_in_heat_frames.append(frame)
                elif class_name == 'inheat':
                    in_heat_frames.append(frame)

        print(f"Class Counts: {class_counts}")

        # Check if either condition is met (50 frames for inheat and 50 frames for non-inheat)
        if class_counts['inheat'] >= 50 and class_counts['non-inheat'] >= 50:
            break

    # Release resources for the second model
    cap.release()
    cv2.destroyAllWindows()

    # Stack the in-heat and non-in-heat frames vertically
    stacked_in_heat_frames = np.vstack(in_heat_frames)
    stacked_non_in_heat_frames = np.vstack(non_in_heat_frames)

    # Display the stacked in-heat and non-in-heat frames
    cv2.imshow('Stacked In-Heat Frames', stacked_in_heat_frames)
    cv2.imshow('Stacked Non-In-Heat Frames', stacked_non_in_heat_frames)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    # Compare the counts and return the label with the higher count
    if class_counts['inheat'] > class_counts['non-inheat']:
        return 'inheat'
    elif class_counts['non-inheat'] > class_counts['inheat']:
        return 'non-inheat'

我确实阅读了 YOLOv8 文档进行预测,但仍然无法做到。我是在 Pycharm IDE 中完成的

python opencv video pycharm yolov8
1个回答
0
投票

问题出在这一行:

class_name = results_in_heat_instance.names[0]
。查看结果的 names 对象:它是模型名称的完整字典,无论模型在帧中检测到什么,它都是相同的。要获取帧中每个检测到的对象的类名称,您需要迭代 boxes 并获取每个框对象的 cls 值,这将是上述 names 字典中检测到的类索引 。示例将如下所示:

results = model.predict(frame)
for result in results:
    for box in result.boxes:
        class_id = int(box.cls.item())
        class_name = result.names[class_id]
© www.soinside.com 2019 - 2024. All rights reserved.