您好,
会员登录 快速注册
退出 ( 条未读消息)
关于本站 意见反馈 首页

公告:小宅博客网可以开发票了,需要发票的,去群里找群主哈!!
全部文章分类
  • 人工智能 >

  • 编程语言 >

  • WPF系列 >

  • ASP.NET系列 >

  • Linux >

  • 数据库 >

  • 嵌入式 >

  • WEB技术 >

  • PLC系列 >

  • 微服务与框架 >

  • 小宅DIY >

  • 学习资料 >

OpenCv基础 ANN车牌识别 yolov5车牌识别 指针式仪表识别 ROS系列 YOLO Halcon Detectron2 昇腾AI ChatGPT在线体验 英伟达JETSON ChatGLM ChatTTS FunASR 地平线 ByteTrack 魔搭社区 LangChain
C C# C++ Python Java Go
WPF
ASP.NET小功能 GPS定位系统-MVC GPS定位系统-VUE ASP.NET WebRTC
Linux Linux内核 Shell MakeFile
MySql SqlServer Oracle
STM8 STM32 51单片机
VUE入门 HTML JavaScript CSS layui镜像网站 ElementUi中文官网 element-plus 图标
三菱 欧姆龙 西门子 施耐德 松下 台达
IOTSharp IOTGateway ABP FRAMEWORK Docker
亚克力音响 编程仙途:智驭万法
面试题与技巧 Python入门技能树 微软C#教程
首页 编程之美 工具下载 全国就业 流量地图 文心一言
YOLO
YOLO 学习介绍 Labelme安装与运行 YOLOv5 安装开发环境 YOLOv5 官方数据集训练方法 YOLOv5 自定义数据集训练方法 YOLOv5 GPU训练方法 YOLOv5 调用USB摄像头识别 YOLOv5 在Ubuntu22.10安装运行环境 YOLOv5 实例分割-官方数据集训练方法 YOLOv5 实例分割-Labelme标注与json文件转txt YOLOv5 pt转onnx文件 YOLOv5 pt转engine文件 YOLOv5 C#中进行模型预测(.net版) YOLOv5 C#中进行模型预测(winform版) YOLOv5 C++进行模型预测 YOLOv5-7.0 安装开发环境 YOLOx 安装开发环境 YOLOx 官网数据集训练方法1(VOC) YOLOx 官网数据集训练方法2(COCO) YOLOx 自定义数据集训练方法1(VOC) YOLOx 自定义数据集训练方法2(COCO) YOLOx 调用USB摄像头识别 YOLOv8 安装开发环境 YOLOv8 官方数据集训练方法 YOLOv8 pt转onnx文件与测试 YOLOv11 安装开发环境 YOLOv11 自定义目标检测模型训练
YOLOx 自定义数据集训练方法2(COCO)
YOLOv8 安装开发环境
激萌の小宅 小宅博客网 YOLO

文章作者:激萌の小宅

促销:¥0

价格:¥0

配送方式: 购买后立即生效(如购买异常,请联系站长)
付款之后一定要等待自动跳转结束,否则购买可能会失败
  • 0 天

    有效期

  • 0

    总销量

  • 0

    累计评价

YOLOx 调用USB摄像头识别

yolox默认调用摄像头0的命令如下:

python tools/demo.py webcam -n yolox-s -c ./yolox_s.pth --conf 0.3 --nms 0.65 --tsize 640

如果加上“--save_result”,执行之后,不会有图像显示,但会在“YOLOX-main\YOLOX_outputs\yolox_s\vis_res”目录下,生成一个“camera.mp4”视频文件。


demo.py的精简代码:

执行命令:

python tools/my_demo.py
import argparse
import os
import time
from loguru import logger

import cv2

import torch

from yolox.data.data_augment import ValTransform
from yolox.data.datasets import COCO_CLASSES
from yolox.exp import get_exp
from yolox.utils import  postprocess, vis


class Predictor(object):
    def __init__(
            self,
            model,
            exp,
            cls_names=COCO_CLASSES,
            trt_file=None,
            decoder=None,
            device="cpu",
            fp16=False,
            legacy=False,
    ):
        self.model = model
        self.cls_names = cls_names
        self.decoder = decoder
        self.num_classes = exp.num_classes
        self.confthre = exp.test_conf
        self.nmsthre = exp.nmsthre
        self.test_size = exp.test_size
        self.device = device
        self.fp16 = fp16
        self.preproc = ValTransform(legacy=legacy)
        if trt_file is not None:
            from torch2trt import TRTModule

            model_trt = TRTModule()
            model_trt.load_state_dict(torch.load(trt_file))

            x = torch.ones(1, 3, exp.test_size[0], exp.test_size[1]).cuda()
            self.model(x)
            self.model = model_trt

    def inference(self, img):
        img_info = {"id": 0}
        if isinstance(img, str):
            img_info["file_name"] = os.path.basename(img)
            img = cv2.imread(img)
        else:
            img_info["file_name"] = None

        height, width = img.shape[:2]
        img_info["height"] = height
        img_info["width"] = width
        img_info["raw_img"] = img

        ratio = min(self.test_size[0] / img.shape[0], self.test_size[1] / img.shape[1])
        img_info["ratio"] = ratio

        img, _ = self.preproc(img, None, self.test_size)
        img = torch.from_numpy(img).unsqueeze(0)
        img = img.float()
        if self.device == "gpu":
            img = img.cuda()
            if self.fp16:
                img = img.half()  # to FP16

        with torch.no_grad():
            t0 = time.time()
            outputs = self.model(img)
            if self.decoder is not None:
                outputs = self.decoder(outputs, dtype=outputs.type())
            outputs = postprocess(
                outputs, self.num_classes, self.confthre,
                self.nmsthre, class_agnostic=True
            )
            # logger.info("Infer time: {:.4f}s".format(time.time() - t0))
        return outputs, img_info

    def visual(self, output, img_info, cls_conf=0.35):
        ratio = img_info["ratio"]
        img = img_info["raw_img"]
        if output is None:
            return img
        output = output.cpu()

        # 识别到的物体
        bboxes = output[:, 0:4]
        # preprocessing: resize
        bboxes /= ratio
        # 识别物体的名字下标
        cls = output[:, 6]
        # 每个物体的识别度
        scores = output[:, 4] * output[:, 5]

        logger.info(bboxes)
        logger.info(cls)
        logger.info(scores)

        # cls_conf 是过滤最小阈值
        vis_res = vis(img, bboxes, scores, cls, cls_conf, self.cls_names)
        return vis_res


def imageflow_demo(predictor):
    cap = cv2.VideoCapture(0)
    while True:
        ret_val, frame = cap.read()
        if ret_val:
            outputs, img_info = predictor.inference(frame)
            result_frame = predictor.visual(outputs[0], img_info, 0.35)
            cv2.imshow("yolox", result_frame)

            ch = cv2.waitKey(1)
            if ch == 27 or ch == ord("q") or ch == ord("Q"):
                break
        else:
            break


if __name__ == "__main__":
    exp = get_exp(None, "yolox-s")
    exp.nmsthre = 0.65
    exp.test_size = (640, 640)

    model = exp.get_model()
    # 使用GPU
    # model.cuda()
    # model.half()  # to FP16
    model.eval()

    ckpt = torch.load("yolox_s.pth", map_location="cpu")
    model.load_state_dict(ckpt["model"])

    predictor = Predictor(model, exp, COCO_CLASSES, None, None, "cpu", False, False)
    imageflow_demo(predictor)




YOLOx 自定义数据集训练方法2(COCO)
YOLOv8 安装开发环境

友情链接: CSDN激萌の小宅 95知识库 自考题库 罗分明个人网络博客 精益编程leanboot

小宅博客  www.bilibili996.com All Rights Reserved. 备案号: 闽ICP备2024034575号

网站经营许可证  福建省福州市 Copyright©2021-2025 版权所有

小宅博客
首页 智能家居 地图定位
公告:小宅博客网可以开发票了,需要发票的,去群里找群主哈!!

文章作者:激萌の小宅

促销:¥0

价格:¥0

配送方式: 购买后立即生效(如购买异常,请联系站长)
付款之后一定要等待自动跳转结束,否则购买可能会失败
  • 0 天

    有效期

  • 0

    总销量

  • 0

    累计评价

YOLOx 调用USB摄像头识别

yolox默认调用摄像头0的命令如下:

python tools/demo.py webcam -n yolox-s -c ./yolox_s.pth --conf 0.3 --nms 0.65 --tsize 640

如果加上“--save_result”,执行之后,不会有图像显示,但会在“YOLOX-main\YOLOX_outputs\yolox_s\vis_res”目录下,生成一个“camera.mp4”视频文件。


demo.py的精简代码:

执行命令:

python tools/my_demo.py
import argparse
import os
import time
from loguru import logger

import cv2

import torch

from yolox.data.data_augment import ValTransform
from yolox.data.datasets import COCO_CLASSES
from yolox.exp import get_exp
from yolox.utils import  postprocess, vis


class Predictor(object):
    def __init__(
            self,
            model,
            exp,
            cls_names=COCO_CLASSES,
            trt_file=None,
            decoder=None,
            device="cpu",
            fp16=False,
            legacy=False,
    ):
        self.model = model
        self.cls_names = cls_names
        self.decoder = decoder
        self.num_classes = exp.num_classes
        self.confthre = exp.test_conf
        self.nmsthre = exp.nmsthre
        self.test_size = exp.test_size
        self.device = device
        self.fp16 = fp16
        self.preproc = ValTransform(legacy=legacy)
        if trt_file is not None:
            from torch2trt import TRTModule

            model_trt = TRTModule()
            model_trt.load_state_dict(torch.load(trt_file))

            x = torch.ones(1, 3, exp.test_size[0], exp.test_size[1]).cuda()
            self.model(x)
            self.model = model_trt

    def inference(self, img):
        img_info = {"id": 0}
        if isinstance(img, str):
            img_info["file_name"] = os.path.basename(img)
            img = cv2.imread(img)
        else:
            img_info["file_name"] = None

        height, width = img.shape[:2]
        img_info["height"] = height
        img_info["width"] = width
        img_info["raw_img"] = img

        ratio = min(self.test_size[0] / img.shape[0], self.test_size[1] / img.shape[1])
        img_info["ratio"] = ratio

        img, _ = self.preproc(img, None, self.test_size)
        img = torch.from_numpy(img).unsqueeze(0)
        img = img.float()
        if self.device == "gpu":
            img = img.cuda()
            if self.fp16:
                img = img.half()  # to FP16

        with torch.no_grad():
            t0 = time.time()
            outputs = self.model(img)
            if self.decoder is not None:
                outputs = self.decoder(outputs, dtype=outputs.type())
            outputs = postprocess(
                outputs, self.num_classes, self.confthre,
                self.nmsthre, class_agnostic=True
            )
            # logger.info("Infer time: {:.4f}s".format(time.time() - t0))
        return outputs, img_info

    def visual(self, output, img_info, cls_conf=0.35):
        ratio = img_info["ratio"]
        img = img_info["raw_img"]
        if output is None:
            return img
        output = output.cpu()

        # 识别到的物体
        bboxes = output[:, 0:4]
        # preprocessing: resize
        bboxes /= ratio
        # 识别物体的名字下标
        cls = output[:, 6]
        # 每个物体的识别度
        scores = output[:, 4] * output[:, 5]

        logger.info(bboxes)
        logger.info(cls)
        logger.info(scores)

        # cls_conf 是过滤最小阈值
        vis_res = vis(img, bboxes, scores, cls, cls_conf, self.cls_names)
        return vis_res


def imageflow_demo(predictor):
    cap = cv2.VideoCapture(0)
    while True:
        ret_val, frame = cap.read()
        if ret_val:
            outputs, img_info = predictor.inference(frame)
            result_frame = predictor.visual(outputs[0], img_info, 0.35)
            cv2.imshow("yolox", result_frame)

            ch = cv2.waitKey(1)
            if ch == 27 or ch == ord("q") or ch == ord("Q"):
                break
        else:
            break


if __name__ == "__main__":
    exp = get_exp(None, "yolox-s")
    exp.nmsthre = 0.65
    exp.test_size = (640, 640)

    model = exp.get_model()
    # 使用GPU
    # model.cuda()
    # model.half()  # to FP16
    model.eval()

    ckpt = torch.load("yolox_s.pth", map_location="cpu")
    model.load_state_dict(ckpt["model"])

    predictor = Predictor(model, exp, COCO_CLASSES, None, None, "cpu", False, False)
    imageflow_demo(predictor)