520.Detect Capital(String-Easy)

本文介绍了一种判断字符串中大写字母使用是否合法的方法,并提供了C++和Python两种语言的实现示例。合法条件包括全大写、全小写及仅首字母大写的情况。

转载请注明作者和出处: http://blog.csdn.net/c406495762

Given a word, you need to judge whether the usage of capitals in it is right or not.

We define the usage of capitals in a word to be right when one of the following cases holds:

  • All letters in this word are capitals, like “USA”.
  • All letters in this word are not capitals, like “leetcode”.
  • Only the first letter in this word is capital if it has more than one letter, like “Google”.

Otherwise, we define that this word doesn’t use capitals in a right way.

Example 1:

Input: "USA" 
Output: True

Example 2:

Input: "FlaG" 
Output: False

Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.

题目:判断字符串大写字母使用的是否合法。合法条件:(1)全为大写字母;(2)全为小写字母;(3)只有首字母大写,其余字母小写。

思路:

a) C++

    初始化一个记录字符串中含有大写字符数量的变量。遍历字符串的每一个字符,如果字符为大写字母,则大写字母计数变量计数一次(加一)。

    合法条件:

  • 大写字母计数变量为0(全为小写字母);
  • 大写字母计数变量为字符串长度(全为大写字母);
  • 大写字母计数变量为1且字符串首字符为大写(只有首字母大写,其余字母小写 )。

b) Python

    Python提供了可以直接调用的API接口:

  • isupper() 方法检测字符串中所有的字母是否都为大写;
  • islower() 方法检测字符串是否由小写字母组成;
  • istitle() 方法检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。

代码:

Language : cpp

class Solution {
public:
    bool detectCapitalUse(string word) {
        int cnt = 0;
        for(char c: word) {
            if('Z' - c >= 0){
                cnt++;
            }
        }
        return ((cnt == 0 || cnt == word.length() || (cnt == 1 && 'Z' - word[0] >= 0)));
    }
};

Language : python

class Solution(object):
    def detectCapitalUse(self, word):
        """
        :type word: str
        :rtype: bool
        """
        return word.isupper() or word.islower() or word.istitle()

代码获取:

Github的LeetCode项目

<think>我们正在使用YOLOv5的detect.py脚本进行推理,并希望通过`--benchmark`参数来测量推理时间。用户希望知道如何在该命令中替换自己的权重文件和图片地址(或视频/摄像头源)。 根据引用[1],`--source`参数用于指定输入源,可以是单张图片、图片目录、视频文件、视频目录或摄像头。`--weights`参数用于指定权重文件路径。 命令结构: ```bash python detect.py --weights [权重文件路径] --source [输入源路径] --benchmark ``` 替换方法: 1. **替换权重文件**:将`yolov5s.pt`替换为你自己的权重文件路径(例如:`my_weights.pt`或`runs/train/exp/weights/best.pt`)。 2. **替换输入源**:将`data/images`替换为你的输入源路径。输入源可以是: - 单张图片:`path/to/your/image.jpg` - 图片目录:`path/to/your/image_folder/` - 单个视频:`path/to/your/video.mp4` - 视频目录:`path/to/your/video_folder/` - 摄像头:`0`(代表默认摄像头)或`rtsp://192.168.1.64:554`(网络摄像头URL)等。 示例: 1. 使用自己的权重`my_model.pt`测试单张图片`test.jpg`: ```bash python detect.py --weights my_model.pt --source test.jpg --benchmark ``` 2. 使用自己的权重`custom.pt`测试图片目录`my_images`: ```bash python detect.py --weights custom.pt --source my_images/ --benchmark ``` 3. 测试摄像头(使用默认摄像头0)并用自己的权重`best.pt`: ```bash python detect.py --weights best.pt --source 0 --benchmark ``` 注意: - 确保权重文件路径和输入源路径正确。 - `--benchmark`参数会输出推理速度(包括预处理、推理、NMS时间)和FPS。 引用[2]提到detect.py脚本的可视化内容较多,但核心推理并不复杂,主要涉及非极大值抑制(NMS)函数(在general.py中)。替换权重和输入源后,运行命令即可得到推理结果和速度报告[^2]。 </think>### 在YOLOv5命令中替换自定义权重和图片地址的方法 要替换命令中的权重文件和图片地址,只需修改以下参数: ```bash python detect.py --weights [你的权重路径] --source [你的图片源] --benchmark ``` #### 具体替换步骤: 1. **替换权重文件路径**: - 将 `yolov5s.pt` 替换为你的自定义权重文件路径 - 示例(Linux/Mac): ```bash python detect.py --weights runs/train/exp/weights/best.pt --source data/images --benchmark ``` - 示例(Windows): ```cmd python detect.py --weights "C:\yolov5\runs\train\exp\weights\best.pt" --source data/images --benchmark ``` 2. **替换图片源路径**: - 支持多种输入类型(根据引用[1]): | 输入类型 | 示例命令 | |---------------|--------------------------------------------| | 单张图片 | `--source path/to/your_image.jpg` | | 图片目录 | `--source path/to/your_images_folder/` | | 视频文件 | `--source path/to/your_video.mp4` | | 摄像头 | `--source 0` (0表示默认摄像头) | | RTSP流 | `--source rtsp://192.168.1.64:554` | #### 完整示例: 测试自定义权重和单张图片: ```bash python detect.py --weights custom_weights.pt --source datasets/test_images/dog.jpg --benchmark ``` 测试自定义权重和整个图片目录: ```bash python detect.py --weights custom_weights.pt --source datasets/test_images/ --benchmark ``` #### 注意事项: 1. 权重文件必须是`.pt`格式的PyTorch模型文件 2. 图片/视频路径支持绝对路径和相对路径 3. 使用摄像头时需指定设备ID(如`--source 0`表示第一个摄像头)[^1] 4. 添加`--benchmark`参数会自动显示推理时间统计: ``` Speed: 8.2ms pre-process, 15.3ms inference, 1.8ms NMS per image FPS: 39.5 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jack-Cui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值