-
-
Notifications
You must be signed in to change notification settings - Fork 15.9k
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
its possible to get output like autoflip #10479
Comments
@akashAD98 👋 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.pyCrops will be saved under python detect.py --save-crop YOLOv5 PyTorch HubCrops will be saved under 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! |
@glenn-jocher i don't want to crop image, i want to crop all video which will focus on person only, like autoflip |
@glenn-jocher my question is using only object detection & code logic its possible to achieve performance like autofip? below image,show that they have done reframing based on person in frame, so using only bounding box can we achieve this ? i tried adding extra width & height for detected person bounding box . but each bonding box is having different size & its hard for matching w & h & saving that video. I'm looking for your suggetions |
@akashAD98 I'd just create a custom python workflow after loading a YOLOv5 model with PyTorch Hub. Some details are below, but basically you can handle the video cropping however you like based on the contents, i.e. detected people and their boxes. You might want some filtering to smooth transitions etc over time. YOLOv5 🚀 PyTorch Hub models allow for simple model loading and inference in a pure python environment without using Simple Inference ExampleThis example loads a pretrained YOLOv5s model from PyTorch Hub as import torch
# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # yolov5n - yolov5x6 official model
# 'custom', 'path/to/best.pt') # custom model
# Images
im = 'https://ultralytics.com/images/zidane.jpg' # or file, Path, URL, PIL, OpenCV, numpy, list
# Inference
results = model(im)
# Results
results.print() # or .show(), .save(), .crop(), .pandas(), etc.
results.xyxy[0] # im predictions (tensor)
results.pandas().xyxy[0] # im predictions (pandas)
# xmin ymin xmax ymax confidence class name
# 0 749.50 43.50 1148.0 704.5 0.874023 0 person
# 2 114.75 195.75 1095.0 708.0 0.624512 0 person
# 3 986.00 304.00 1028.0 420.0 0.286865 27 tie
results.pandas().xyxy[0].value_counts('name') # class counts (pandas)
# person 2
# tie 1 See YOLOv5 PyTorch Hub Tutorial for details. Good luck 🍀 and let us know if you have any other questions! |
@glenn-jocher i can crop images & save easily, but cropping in realtime video & focusing on person is i think different, how can I achieve this using pytorchHub |
@glenn-jocher im using torch.hub for model loading & other operations, this is my logic
I'm loading video using torch.hub & want to add this modifications
|
👋 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 ⭐! |
@akashAD98 You can certainly achieve real-time object detection and video cropping using YOLOv5 model loaded with PyTorch Hub. You can modify the detection results to perform custom cropping and resizing on the video frames as per your requirements. Below is a modified version of your logic that showcases how to accomplish this using YOLOv5 with some simple custom modifications: import cv2
import torch
# Load the YOLOv5 model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
model.classes = [0] # Set class to person
cap = cv2.VideoCapture('/content/withbackgroung_op.mp4')
while cap.isOpened():
ret, frame = cap.read()
if ret:
results = model(frame) # Perform object detection on the frame
# Processing the detection results
for det in results.pred[0]:
if det[5] == 0: # Check if the detection is a person
x, y, w, h = int(det[0]), int(det[1]), int(det[2]-det[0]), int(det[3]-det[1]) # Get x, y, w, h values from the detection
aspect_ratio = w / h
person_size = 200
new_width = int(person_size * aspect_ratio)
new_height = int(person_size)
resized_frame = cv2.resize(frame[y:y+h, x:x+w], (new_width, new_height)) # Crop and resize the frame based on the detected person
# Show the modified results or save them as required
cv2.imshow('frame', resized_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows() This snippet processes each video frame, performs YOLOv5 object detection on it, and then crops and resizes the frame based on the detected person. Feel free to further modify and customize the logic as per your specific use case. Let me know if you need any further assistance or modifications! |
Search before asking
Question
https://google.github.io/mediapipe/solutions/autoflip.html
i wan to use object detection code,it will detect person & crop according to it & save the result.
so my question is can only by detecting object person - ,can we achieve this kind of solution?
if we draw a bounding box for a person & then increase w & h,& it will get perfect aspect ratio?
@glenn-jocher @AyushExel
Additional
No response
The text was updated successfully, but these errors were encountered: