Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract text from the --save-crop images yolov5 #9839

Closed
1 task done
bachimanchiajay opened this issue Oct 18, 2022 · 11 comments
Closed
1 task done

extract text from the --save-crop images yolov5 #9839

bachimanchiajay opened this issue Oct 18, 2022 · 11 comments
Labels
question Further information is requested Stale

Comments

@bachimanchiajay
Copy link

Search before asking

Question

after executing the detect.py file --save-crop i can able to crop the class images but i need the text as well before image writing.
for eg : i tested with one image having text model predicted the text classes. i want to extract the text in the predicted coordinates before copping the class images

Additional

No response

@bachimanchiajay bachimanchiajay added the question Further information is requested label Oct 18, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Oct 18, 2022

👋 Hello @bachimanchiajay, 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 screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email support@ultralytics.com.

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.

@glenn-jocher
Copy link
Member

@bachimanchiajay 👋 Hello! Thanks for asking about cropping results with YOLOv5 🚀. Cropping bounding box detections can be useful for training classification models on box contents for example. This feature was added in PR #2827. You can crop detections using either detect.py or YOLOv5 PyTorch Hub:

detect.py

Crops will be saved under runs/detect/exp/crops, with a directory for each class detected.

python detect.py --save-crop

Original

Crop

YOLOv5 PyTorch Hub

Crops will be saved under runs/detect/exp/crops if save=True, and also returned as a dictionary with crops as numpy arrays.

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')  # or yolov5m, yolov5l, yolov5x, custom

# Images
img = 'https://ultralytics.com/images/zidane.jpg'  # or file, Path, PIL, OpenCV, numpy, list

# Inference
results = model(img)

# Results
crops = results.crop(save=True) 
# -- or --
crops = results.crop(save=True, save_dir='runs/detect/exp')  # specify save dir

Good luck 🍀 and let us know if you have any other questions!

@bachimanchiajay
Copy link
Author

i want to extract the text from the predicted results, have to pass the predicted bounding box coordinates to the textract can you help me in that

@bachimanchiajay
Copy link
Author

import cv2
import numpy as np
import os
import pytesseract as pt
import torch
from PIL import Image
import matplotlib.pyplot as plt
model = torch.hub.load('/home/pkumar/yolov5/', 'custom', path='/home/pkumar/Desktop/yolo_dataentry_models/best.pt',source='local')
def get_text(image_path, threshold):
img = cv2.imread(image_path)
Y, X,d = img.shape
result = model(img)
df = result.pandas().xyxy[0]
df = df[df['confidence']>=threshold]
df = df.drop(['class', 'name','confidence'], axis=1)
print(df.head())
output = []
i = 0
values = df.values.astype(int)
for xmin,ymin,xmax,ymax in df.values.astype(int):
xmin = max(0, xmin-3)
ymin = max(0, ymin-3)
xmax = min(X, xmax+3)
ymax = min(Y, ymax+3)
cv2.rectangle(img, (xmin,ymin),(xmax,ymax),(0,255,0),3)
crop_img = img[ymin:ymax, xmin:xmax]
crop_img = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
crop_img = cv2.resize(crop_img, None, fx=4,fy=4, interpolation=cv2.INTER_CUBIC)
text = pt.image_to_string(crop_img).strip()
output.append(text)
return output,img
path = '/home/pkumar/Downloads/UP_images/SAO1_0deblur.png'
text,image= get_text(path, 0.5)`

@bachimanchiajay
Copy link
Author

This one is not extracting all the classes only getting two class values @glenn-jocher can you cross check the above method once

@glenn-jocher
Copy link
Member

@bachimanchiajay I'm sorry we don't availability to debug custom code. If you find any issues with the official codebase please let us know though.

@Aayan37
Copy link

Aayan37 commented Oct 20, 2022

@glenn-jocher please can you let me know the code for YoloV5 with bounding boxes cropped, saved in some location. I have tried integrating method #2827! but its not working.. can you please share the latest code.

@glenn-jocher
Copy link
Member

👋 Hello! Thanks for asking about cropping results with YOLOv5 🚀. Cropping bounding box detections can be useful for training classification models on box contents for example. This feature was added in PR #2827. You can crop detections using either detect.py or YOLOv5 PyTorch Hub:

detect.py

Crops will be saved under runs/detect/exp/crops, with a directory for each class detected.

python detect.py --save-crop

Original

Crop

YOLOv5 PyTorch Hub

Crops will be saved under runs/detect/exp/crops if save=True, and also returned as a dictionary with crops as numpy arrays.

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')  # or yolov5m, yolov5l, yolov5x, custom

# Images
img = 'https://ultralytics.com/images/zidane.jpg'  # or file, Path, PIL, OpenCV, numpy, list

# Inference
results = model(img)

# Results
crops = results.crop(save=True) 
# -- or --
crops = results.crop(save=True, save_dir='runs/detect/exp')  # specify save dir

Good luck 🍀 and let us know if you have any other questions!

@Aayan37
Copy link

Aayan37 commented Oct 22, 2022

@glenn-jocher I have tried this but its not cropping the bounding boxes. can you please share the complete detect.py with cropping images code present in it. I have tried Abdurrahman, @burhr2 defined codes as well. May be I'm missing something, can you please share the detailed code for that. Thanks in Advance!!

@glenn-jocher
Copy link
Member

glenn-jocher commented Oct 22, 2022

@Aayan37 👋 hi, thanks for letting us know about this possible problem with YOLOv5 🚀. We've created a few short guidelines below to help users provide what we need in order to start investigating a possible problem.

How to create a Minimal, Reproducible Example

When asking a question, people will be better able to provide help if you provide code that they can easily understand and use to reproduce the problem. This is referred to by community members as creating a minimum reproducible example. Your code that reproduces the problem should be:

  • Minimal – Use as little code as possible to produce the problem
  • Complete – Provide all parts someone else needs to reproduce the problem
  • Reproducible – Test the code you're about to provide to make sure it reproduces the problem

For Ultralytics to provide assistance your code should also be:

  • Current – Verify that your code is up-to-date with GitHub master, and if necessary git pull or git clone a new copy to ensure your problem has not already been solved in master.
  • Unmodified – Your problem must be reproducible using official YOLOv5 code without changes. Ultralytics does not provide support for custom code ⚠️.

If you believe your problem meets all the above criteria, please close this issue and raise a new one using the 🐛 Bug Report template with a minimum reproducible example to help us better understand and diagnose your problem.

Thank you! 😃

@github-actions
Copy link
Contributor

github-actions bot commented Nov 22, 2022

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

Access additional Ultralytics ⚡ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐!

@github-actions github-actions bot added the Stale label Nov 22, 2022
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale
Projects
None yet
Development

No branches or pull requests

3 participants