pycharm YOLOv8模型类通过视频帧进行预测[已关闭]

bqujaahr  于 5个月前  发布在  PyCharm
关注(0)|答案(1)|浏览(71)

已关闭。此问题需要details or clarity。目前不接受回答。
**要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

29天前关闭。
Improve this question
我需要一些帮助,因为我将需要这个工作,我的最后论文项目的模型有2类inheat和非inheat,我做了下面的代码,但当它试图预测或检测的视频帧,它检测到非inheat,但它只堆叠在 *inheat帧,而不是 * 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文档来预测,但还是做不到。

1rhkuytd

1rhkuytd1#

问题出在这一行:class_name = results_in_heat_instance.names[0],看结果的names对象:它是一个完整的模型名称字典,无论模型在一个帧中检测到什么,它都是一样的。要获得帧中每个检测到的对象的类名,您需要遍历boxes并获得每个box对象的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]

字符串

相关问题