ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
51.11k stars 16.43k forks source link

How to set parameters for crop so that the cropped objects are not blocks, but only recognized objects #11548

Closed hktalent closed 1 year ago

hktalent commented 1 year ago

Search before asking

Question

python detect.py --weights weights/yolov5s.pt --save-txt  --save-crop  --source ./IMG_2061_w.jpg

thanks

Additional

No response

github-actions[bot] commented 1 year ago

👋 Hello @hktalent, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

YOLOv5 CI

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

Introducing YOLOv8 🚀

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 🚀!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics
glenn-jocher commented 1 year ago

@hktalent hello there! Thank you for reaching out. Regarding your questions:

  1. The --save-crop argument saves the cropped objects as a square image by default. To adjust the cropping box size based on the detected object, you can try setting --crop to true and --crop-auto to "threshold". This will crop only objects above a certain confidence threshold. You can also play around with the --crop-roi parameter to adjust the relative size of the cropped box.

  2. The numbers in the label files represent the class index of the detected objects. The class names can be found in the data.yaml file located in the same directory as your detection script. The index of each class corresponds to its position in the names list in data.yaml.

I hope that helps! Let me know if you have any additional questions.

hktalent commented 1 year ago

@glenn-jocher thanks

git pull
pip install ultralytics
$ git remote -v
origin  ssh://git@github.com/ultralytics/yolov5 (fetch)
origin  ssh://git@github.com/ultralytics/yolov5 (push)

$ python detect.py --weights weights/yolov5s.pt --save-txt --crop=true --crop-auto="threshold"  --source $HOME/Downloads/IMG_2061_w.jpg
usage: detect.py [-h] [--weights WEIGHTS [WEIGHTS ...]] [--source SOURCE]
                 [--data DATA] [--imgsz IMGSZ [IMGSZ ...]]
                 [--conf-thres CONF_THRES] [--iou-thres IOU_THRES]
                 [--max-det MAX_DET] [--device DEVICE] [--view-img]
                 [--save-txt] [--save-conf] [--save-crop] [--nosave]
                 [--classes CLASSES [CLASSES ...]] [--agnostic-nms]
                 [--augment] [--visualize] [--update] [--project PROJECT]
                 [--name NAME] [--exist-ok] [--line-thickness LINE_THICKNESS]
                 [--hide-labels] [--hide-conf] [--half] [--dnn]
                 [--vid-stride VID_STRIDE]
detect.py: error: unrecognized arguments: --crop=true --crop-auto=threshold

It doesn't seem to have the parameters you mentioned

I try checked the source code of detect.py No parameters: crop=true, crop_ auto="", Adding it can also cause errors

I don't know where the problem is

cannot find data.yaml

yolov5 $ find . -name "data.yaml"      
find $HOME/anaconda3/envs/yolov5 -name "data.yaml"                                                                                                              
(yolov5) 51pwn@123-2 yolov5 $ 
glenn-jocher commented 1 year ago

Hello @hktalent, glad to help!

It looks like the --crop and --crop-auto arguments were recently added to YOLOv5 and may not be available in your installed version. To use them, please run git pull to update your local repository to the latest version, then run pip install -r requirements.txt from the YOLOv5 directory to ensure all required packages are installed.

Regarding the missing data.yaml file, it's likely that you haven't specified the correct --data argument in your command. The --data argument expects a relative or absolute path to the data.yaml file of your project. For example, if your data.yaml file is located in the data folder, you can specify it like this: --data data/data.yaml.

I hope that helps! If you have any further questions, please let me know.

hktalent commented 1 year ago

@glenn-jocher in my macOS,i7 cpu 2.3

conda deactivate
conda remove --name yolov5 --all -y
conda create -n yolov5 -y python=3.7
conda activate yolov5
conda init zsh
export PATH="$HOME/anaconda3/envs/yolov5/bin:$PATH"
which pip python
pip install -U -r requirements.txt  -i https://mirror.baidu.com/pypi/simple

$ pip list
Package                 Version
----------------------- --------------------
absl-py                 1.4.0
appnope                 0.1.3
backcall                0.2.0
cachetools              5.3.0
certifi                 2023.5.7
charset-normalizer      3.1.0
coloredlogs             15.0.1
cycler                  0.11.0
decorator               5.1.1
flatbuffers             23.5.9
fonttools               4.38.0
gitdb                   4.0.10
GitPython               3.1.31
google-auth             2.18.0
google-auth-oauthlib    0.4.6
grpcio                  1.54.0
humanfriendly           10.0
idna                    3.4
importlib-metadata      6.6.0
ipython                 7.34.0
jedi                    0.18.2
kiwisolver              1.4.4
Markdown                3.4.3
MarkupSafe              2.1.2
matplotlib              3.5.3
matplotlib-inline       0.1.6
mpmath                  1.3.0
numpy                   1.21.6
oauthlib                3.2.2
onnx                    1.14.0
onnxruntime             1.14.1
opencv-python           4.7.0.72
packaging               23.1
pandas                  1.3.5
parso                   0.8.3
pexpect                 4.8.0
pickleshare             0.7.5
Pillow                  9.5.0
pip                     23.1.2
prompt-toolkit          3.0.38
protobuf                3.20.3
psutil                  5.9.5
ptyprocess              0.7.0
pyasn1                  0.5.0
pyasn1-modules          0.3.0
Pygments                2.15.1
pyparsing               3.0.9
python-dateutil         2.8.2
pytz                    2023.3
PyYAML                  6.0
requests                2.30.0
requests-oauthlib       1.3.1
rsa                     4.8
scipy                   1.7.3
seaborn                 0.12.2
sentry-sdk              1.22.2
setuptools              67.7.2
six                     1.16.0
smmap                   5.0.0
sympy                   1.10.1
tensorboard             2.11.2
tensorboard-data-server 0.6.1
tensorboard-plugin-wit  1.8.1
thop                    0.1.1.post2209072238
torch                   1.13.1
torchvision             0.14.1
tqdm                    4.65.0
traitlets               5.9.0
typing_extensions       4.5.0
ultralytics             8.0.98
urllib3                 1.26.15
wcwidth                 0.2.6
Werkzeug                2.2.3
wheel                   0.40.0
zipp                    3.15.0

I try:

wget -c 'https://github.com/ultralytics/yolov5/archive/refs/tags/v7.0.zip'
# or, from https://github.com/ultralytics/yolov5 master
# I'm not sure if the YOLOv5 you mentioned is the same?
gh repo clone ultralytics/yolov5
# same error
$ python detect.py --weights weights/yolov5s.pt --save-txt --crop --crop-auto="threshold"  --source $HOME/Downloads/IMG_2061_w.jpg
usage: detect.py [-h] [--weights WEIGHTS [WEIGHTS ...]] [--source SOURCE]
                 [--data DATA] [--imgsz IMGSZ [IMGSZ ...]]
                 [--conf-thres CONF_THRES] [--iou-thres IOU_THRES]
                 [--max-det MAX_DET] [--device DEVICE] [--view-img]
                 [--save-txt] [--save-conf] [--save-crop] [--nosave]
                 [--classes CLASSES [CLASSES ...]] [--agnostic-nms]
                 [--augment] [--visualize] [--update] [--project PROJECT]
                 [--name NAME] [--exist-ok] [--line-thickness LINE_THICKNESS]
                 [--hide-labels] [--hide-conf] [--half] [--dnn]
                 [--vid-stride VID_STRIDE]
detect.py: error: unrecognized arguments: --crop --crop-auto=threshold
hktalent commented 1 year ago

@glenn-jocher

https://github.com/search?q=repo%3Aultralytics%2Fyolov5+crop-auto&type=code I haven't found any reference code here I don't know if I found the wrong place

google cannot found, nothing。 Can you give me a try if it's an internal version? https://www.google.com/search?q=%22detect.py%22+%22--weights%22++%22--crop-auto%22&client=safari&rls=en&ei=eWVnZO6UDOTnkPIPucOXyAw&ved=0ahUKEwiutfKgq4H_AhXkM0QIHbnhBckQ4dUDCA8&uact=5&oq=%22detect.py%22+%22--weights%22++%22--crop-auto%22&gs_lcp=Cgxnd3Mtd2l6LXNlcnAQA0oECEEYAFAcWElg-AJoAHAAeACAAQCIAQCSAQCYAQCgAQHAAQE&sclient=gws-wiz-serp

I have read several of your articles and YOLOv5 is a very valuable contribution. I plan to use it to do some interesting things in the field of information security

https://medium.com/axinc-ai/mobilenetssd-a-machine-learning-model-for-fast-object-detection-37352ce6da7d

hktalent commented 1 year ago

@glenn-jocher Or did you forget to submit git commit and git push?

hktalent commented 1 year ago

@glenn-jocher thanks

git pull
pip install ultralytics
$ git remote -v
origin    ssh://git@github.com/ultralytics/yolov5 (fetch)
origin    ssh://git@github.com/ultralytics/yolov5 (push)

$ python detect.py --weights weights/yolov5s.pt --save-txt --crop=true --crop-auto="threshold"  --source $HOME/Downloads/IMG_2061_w.jpg
usage: detect.py [-h] [--weights WEIGHTS [WEIGHTS ...]] [--source SOURCE]
                 [--data DATA] [--imgsz IMGSZ [IMGSZ ...]]
                 [--conf-thres CONF_THRES] [--iou-thres IOU_THRES]
                 [--max-det MAX_DET] [--device DEVICE] [--view-img]
                 [--save-txt] [--save-conf] [--save-crop] [--nosave]
                 [--classes CLASSES [CLASSES ...]] [--agnostic-nms]
                 [--augment] [--visualize] [--update] [--project PROJECT]
                 [--name NAME] [--exist-ok] [--line-thickness LINE_THICKNESS]
                 [--hide-labels] [--hide-conf] [--half] [--dnn]
                 [--vid-stride VID_STRIDE]
detect.py: error: unrecognized arguments: --crop=true --crop-auto=threshold

It doesn't seem to have the parameters you mentioned

I try checked the source code of detect.py No parameters: crop=true, crop_ auto="", Adding it can also cause errors

I don't know where the problem is

cannot find data.yaml

yolov5 $ find . -name "data.yaml"      
find $HOME/anaconda3/envs/yolov5 -name "data.yaml"                                                                                                              
(yolov5) 51pwn@123-2 yolov5 $ 

I have already run git pull here

In fact, I did not find the keyword 'crop-auto' in the current project code on Github

 $ git status
位于分支 master
您的分支与上游分支 'origin/master' 一致。

无文件要提交,干净的工作区
(yolov5) 51pwn@123-2 yolov5 $ git pull
已经是最新的。
(yolov5) 51pwn@123-2 yolov5 $ git remote -v
origin  ssh://git@github.com/ultralytics/yolov5 (fetch)
origin  ssh://git@github.com/ultralytics/yolov5 (push)
glenn-jocher commented 1 year ago

@hktalent hey there,

Thank you for checking the source code of detect.py and confirming that --crop and --crop-auto parameters are not present in the current project code on GitHub. These parameters may have been added recently and have not been pushed to the public GitHub repository yet. I apologize for any confusion this may have caused.

Regarding the data.yaml file, please make sure that you have specified the correct --data argument in your command. The --data argument expects a relative or absolute path to the data.yaml file of your project. For example, if your data.yaml file is located in the data folder, you can specify it like this: --data data/data.yaml.

I hope that helps! If you have any further questions, please let me know.

hktalent commented 1 year ago

@glenn-jocher I found some reference codes here https://stackabuse.com/object-detection-inference-in-python-with-yolov5-and-pytorch/

import torch
import matplotlib.pyplot as plt

"""
py3 -m http.server 8088 --bind 0.0.0.0 --directory .
python lib/detectObj1.py
"""

# Loading in yolov5s - you can switch to larger models such as yolov5m or yolov5l, or smaller such as yolov5n
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
img = 'http://127.0.0.1:8088/IMG_2061_w.jpg'  # or file, Path, PIL, OpenCV, numpy, list
results = model(img)
results.save(save_dir='results')
imgs = results.crop()

# It looks like an edge node of the detected object here, but I found that each number is not (x, y) but 3 data, so I don't know what to do
print(imgs.im)

I have found the comparison between the number and label

here: https://github.com/ultralytics/yolov5/blob/master/data/coco128.yaml

Unfortunately, it is not a cross reference relationship between all code and tags

Additionally, the following code seems to not work properly

model("file:///xxxx/xxx.jpg")
model("/xxxx/xxx.jpg")
model("./xxx.jpg")
hktalent commented 1 year ago

@glenn-jocher https://github.com/WongKinYiu/yolov7 https://github.com/ultralytics/yolov5 What is their relationship?

glenn-jocher commented 1 year ago

@hktalent hi,

YOLOv7 is a fork of YOLOv5 that was created by a different developer. It is a separate project that includes modifications to the original YOLOv5 codebase.

On the other hand, YOLOv5 is an open-source object detection framework developed by Ultralytics. It is one of the most accurate and fastest object detection models available and has gained popularity among developers in the computer vision community.

So, they are two separate projects, and each project has its own codebase and community. I hope that helps! If you have any further questions, please let me know.