首页 > 其他分享 >【亲测】解决使用super_gradients库预测图片无法获取预测结果图片

【亲测】解决使用super_gradients库预测图片无法获取预测结果图片

时间:2023-08-03 16:07:31浏览次数:29  
标签:confidence show color image mapping int gradients super 图片

问题

在使用super_gradients库中的Yolo-nas预测图片时,想要获取预测好的图片,但执行out = model.predict("camera01.png", conf=0.6, batch_size=None)之后只能out.show()和out.save(),无法返回预测结果图片。

解决方法

import cv2
import torch
from super_gradients.training import models

device = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
# yolo_nas_m,yolo_nas_l
model = models.get("yolo_nas_s", pretrained_weights="coco").to(device)
out = model.predict("camera01.png", conf=0.6)
# 关键在这
sigle_image_out = out._images_prediction_lst[0]
image = sigle_image_out.draw()
cv2.imshow('image',image)
cv2.waitKey()

原理

在out = model.predict("camera01.png", conf=0.6)处打断点, debug,进入到predict函数里:

def predict(
    self,
    images: ImageSource,
    iou: Optional[float] = None,
    conf: Optional[float] = None,
    batch_size: int = 32,
    fuse_model: bool = True,
) -> ImagesDetectionPrediction:
    """Predict an image or a list of images.

    :param images:      Images to predict.
    :param iou:         (Optional) IoU threshold for the nms algorithm. If None, the default value associated to the training is used.
    :param conf:        (Optional) Below the confidence threshold, prediction are discarded.
                        If None, the default value associated to the training is used.
    :param batch_size:  Maximum number of images to process at the same time.
    :param fuse_model:  If True, create a copy of the model, and fuse some of its layers to increase performance. This increases memory usage.
    """
    pipeline = self._get_pipeline(iou=iou, conf=conf, fuse_model=fuse_model)
    return pipeline(images, batch_size=batch_size)  # type: ignore

接着debug有函数的地方就进去,到最后发现,out是ImagesDetectionPrediction类

【亲测】解决使用super_gradients库预测图片无法获取预测结果图片_super_gradients

ImagesDetectionPrediction类的属性是_images_prediction_lst: List[ImageDetectionPrediction]

里面有多个ImageDetectionPrediction类。注意ImagesDetectionPrediction和ImageDetectionPrediction,一个是处理多个图片,一个是单个图片。其中ImageDetectionPrediction类有个draw方法,该方法画出了预测框并返回画出了预测框的结果。

所以如下两句代码,成功获取到了预测结果图片。sigle_image_out = out._images_prediction_lst[0] image = sigle_image_out.draw()

ImagesDetectionPrediction和ImageDetectionPrediction源码如下:

@dataclass
class ImagesDetectionPrediction(ImagesPredictions):
    """Object wrapping the list of image detection predictions.

    :attr _images_prediction_lst:  List of the predictions results
    """

    _images_prediction_lst: List[ImageDetectionPrediction]

    def show(self, box_thickness: int = 2, show_confidence: bool = True, color_mapping: Optional[List[Tuple[int, int, int]]] = None) -> None:
        """Display the predicted bboxes on the images.

        :param box_thickness:   Thickness of bounding boxes.
        :param show_confidence: Whether to show confidence scores on the image.
        :param color_mapping:   List of tuples representing the colors for each class.
                                Default is None, which generates a default color mapping based on the number of class names.
        """
        for prediction in self._images_prediction_lst:
            prediction.show(box_thickness=box_thickness, show_confidence=show_confidence, color_mapping=color_mapping)

    def save(
        self, output_folder: str, box_thickness: int = 2, show_confidence: bool = True, color_mapping: Optional[List[Tuple[int, int, int]]] = None
    ) -> None:
        """Save the predicted bboxes on the images.

        :param output_folder:     Folder path, where the images will be saved.
        :param box_thickness:   Thickness of bounding boxes.
        :param show_confidence: Whether to show confidence scores on the image.
        :param color_mapping:   List of tuples representing the colors for each class.
                                Default is None, which generates a default color mapping based on the number of class names.
        """
        if output_folder:
            os.makedirs(output_folder, exist_ok=True)

        for i, prediction in enumerate(self._images_prediction_lst):
            image_output_path = os.path.join(output_folder, f"pred_{i}.jpg")
            prediction.save(output_path=image_output_path, box_thickness=box_thickness, show_confidence=show_confidence, color_mapping=color_mapping)

_images_prediction_lst: List[ImageDetectionPrediction]

@dataclass
class ImageDetectionPrediction(ImagePrediction):
    """Object wrapping an image and a detection model's prediction.

    :attr image:        Input image
    :attr predictions:  Predictions of the model
    :attr class_names:  List of the class names to predict
    """

    image: np.ndarray
    prediction: DetectionPrediction
    class_names: List[str]

    def draw(self, box_thickness: int = 2, show_confidence: bool = True, color_mapping: Optional[List[Tuple[int, int, int]]] = None) -> np.ndarray:
        """Draw the predicted bboxes on the image.

        :param box_thickness:   Thickness of bounding boxes.
        :param show_confidence: Whether to show confidence scores on the image.
        :param color_mapping:   List of tuples representing the colors for each class.
                                Default is None, which generates a default color mapping based on the number of class names.
        :return:                Image with predicted bboxes. Note that this does not modify the original image.
        """
        image = self.image.copy()
        color_mapping = color_mapping or generate_color_mapping(len(self.class_names))

        for pred_i in np.argsort(self.prediction.confidence):
            class_id = int(self.prediction.labels[pred_i])
            score = "" if not show_confidence else str(round(self.prediction.confidence[pred_i], 2))

            image = draw_bbox(
                image=image,
                title=f"{self.class_names[class_id]} {score}",
                color=color_mapping[class_id],
                box_thickness=box_thickness,
                x1=int(self.prediction.bboxes_xyxy[pred_i, 0]),
                y1=int(self.prediction.bboxes_xyxy[pred_i, 1]),
                x2=int(self.prediction.bboxes_xyxy[pred_i, 2]),
                y2=int(self.prediction.bboxes_xyxy[pred_i, 3]),
            )

        return image

    def show(self, box_thickness: int = 2, show_confidence: bool = True, color_mapping: Optional[List[Tuple[int, int, int]]] = None) -> None:
        """Display the image with predicted bboxes.

        :param box_thickness:   Thickness of bounding boxes.
        :param show_confidence: Whether to show confidence scores on the image.
        :param color_mapping:   List of tuples representing the colors for each class.
                                Default is None, which generates a default color mapping based on the number of class names.
        """
        image = self.draw(box_thickness=box_thickness, show_confidence=show_confidence, color_mapping=color_mapping)
        show_image(image)

    def save(self, output_path: str, box_thickness: int = 2, show_confidence: bool = True, color_mapping: Optional[List[Tuple[int, int, int]]] = None) -> None:
        """Save the predicted bboxes on the images.

        :param output_path:     Path to the output video file.
        :param box_thickness:   Thickness of bounding boxes.
        :param show_confidence: Whether to show confidence scores on the image.
        :param color_mapping:   List of tuples representing the colors for each class.
                                Default is None, which generates a default color mapping based on the number of class names.
        """
        image = self.draw(box_thickness=box_thickness, show_confidence=show_confidence, color_mapping=color_mapping)
        save_image(image=image, path=output_path)

标签:confidence,show,color,image,mapping,int,gradients,super,图片
From: https://blog.51cto.com/u_16207976/6949702

相关文章

  • 富文本编辑器 图片粘贴上传,实现图文粘贴,图片自动上传
    ​ 由于工作需要必须将word文档内容粘贴到编辑器中使用但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直接解决这个问题考虑到自己除了工作其他时间基本上不使用windows,因此打算使用nodejs来解......
  • C# 获取图片缩略图
    ///<summary>///获取图片缩略图///</summary>///<paramname="iSource">原图片</param>///<paramname="destFile">保存路径</param>///<paramname="destHeight">目标高度</param>......
  • jQuery实现点击图片放大全屏预览效果
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=d......
  • HTML编辑器 图片粘贴上传,实现图文粘贴,图片自动上传
    ​ 如何做到ueditor批量上传word图片?1、前端引用代码<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>......
  • # yyds干货盘点 # 盘点一个可以一键免费下载图片的谷歌插件
    大家好,我是皮皮。一、前言前几天在Python知识星球里边看到【七年】大佬推荐的一个谷歌浏览器插件,可以一键下载浏览器中的图片或者PPT,这里也推荐给大家,一起来看看吧!二、实现过程这个插件是免费的,非常奈斯,但是在谷歌浏览器中下载的时候,需要借助ti子,在谷歌浏览器应用商店里边搜索【图......
  • node封装一个图片拼接插件
    说在前面平时我们拼接图片的时候一般都要通过ps或者其他图片处理工具来进行处理合成,这次有个需求就需要进行图片拼接,而且我希望是可以直接使用代码进行拼接,于是就有了这么一个工具包。插件效果通过该插件,我们可以将图片进行以下操作:1、横向拼接两张图片如下,我们有这么两张......
  • 论文解读(APCA)《Adaptive prototype and consistency alignment for semi-supervised d
    [Wechat:Y466551|付费咨询,非诚勿扰]论文信息论文标题:Adaptiveprototypeandconsistencyalignmentforsemi-superviseddomainadaptation论文作者:JihongOuyang、ZhengjieZhang、QingyiMeng论文来源:2023aRxiv论文地址:download 论文代码:download视屏讲解:click1介绍......
  • Topaz Photo AI - 图片智能降噪软件mac/win版
    TopazPhotoAI是一款由TopazLabs公司开发的人工智能图像处理软件。它基于先进的机器学习技术,提供了一系列强大的功能,可以帮助用户快速、简便地改善和优化照片。点击获取TopazPhotoAI AI增强功能:TopazPhotoAI提供了多种AI增强功能,包括智能增亮、细节增强、降......
  • 国标GB28181视频平台LntonGBS国标平台调用快照接口,未能正常返回快照图片的问题解决方
    LntonGBS国标视频云服务支持设备/平台通过国标GB28181协议注册接入,可实现视频的实时监控直播、录像、检索与回看、语音对讲、云存储、告警、平台级联等功能。LntonGBS平台便捷、丰富、灵活、可拓展的视频能力,已经使其成为当前安防市场的主流需求视频平台,并且已经在大量的项目中落地......
  • python3 压缩图片到合理范围
    importosfromPILimportImagefromPILimportImageFileimportimghdrdefcompress_image(outfile,mb=200,quality=85,k=0.9):#修改mb大小,就是想要设定的压缩后的大小。"""不改变图片尺寸压缩到指定大小:paramoutfile:压缩文件保存地址:parammb:压缩目标,KB:pa......