![](/example/batch_predict.jpg)
# **YOLOv9 Plate Detection**
**Fine-tuned YOLOv9 weights are available in Kaggle!**
[Click to go weights!](https://www.kaggle.com/datasets/noepinefrin/yolov9-fine-tuned-plate-detection-model-weights)
and also Training Notebook are published in [Kaggle!](https://www.kaggle.com/code/noepinefrin/yolov9-fine-tuning-custom-dataset-plate-detection)
**Prediction Format**
```python
[*xyxy, confidence, predicted_class]
```
# How to Inference
###### After downloading fine-tuned weights you can continue with these steps.
**Clone Original YOLOv9 Repository**
```python
!git clone --recursive https://github.com/WongKinYiu/yolov9.git
# Change the directory
%cd yolov9/
```
**Install YOLOv9 Dependencies**
```python
!pip install -r requirements.txt -q
!pip install supervision datasets pyyaml -q
```
**Import Utility Functions**
```python
from models.common import DetectMultiBackend
from utils.general import check_img_size, non_max_suppression, scale_boxes
from utils.torch_utils import select_device, smart_inference_mode
from utils.augmentations import letterbox
from PIL import Image
import numpy as np
import supervision as sv
import torch
import os
HOME = os.getcwd()
%matplotlib inline
```
**Inference Function**
```python
@smart_inference_mode()
def predict(
image_path: str,
weights: str,
data: str,
imgsz: tuple,
conf_thres: float,
iou_thres: float,
device: str,
):
device = select_device(device)
model = DetectMultiBackend(weights, device=device, fp16=False, data=data)
stride, names, pt = model.stride, model.names, model.pt
# Load image
image = Image.open(image_path)
img0 = np.array(image)
assert img0 is not None, f'Image Not Found {image_path}'
img = letterbox(img0, imgsz, stride=stride, auto=True)[0]
img = img[:, :, ::-1].transpose(2, 0, 1)
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(device).float()
img /= 255.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
# Init bounding box annotator and label annotator
bounding_box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator(text_position=sv.Position.TOP_RIGHT, text_scale=0.4)
# Inference
pred = model(img, augment=False, visualize=False)
# Apply NMS
pred_t = non_max_suppression(pred[0], conf_thres, iou_thres, classes=None, max_det=1000)
# Process detections
for i, det in enumerate(pred_t):
if len(det):
det[:, :4] = scale_boxes(img.shape[2:], det[:, :4], img0.shape).round()
for *xyxy, conf, cls in reversed(det):
# Transform detections to supervisions detections
detections = sv.Detections(
xyxy=torch.stack(xyxy).cpu().numpy().reshape(1, -1),
class_id=np.array([int(cls)]),
confidence=np.array([float(conf)])
)
# Labels
labels = [
f"{names[int(class_id)]} {confidence:0.2f}"
for class_id, confidence
in zip(detections.class_id, detections.confidence)
]
img0 = bounding_box_annotator.annotate(img0, detections)
img0 = label_annotator.annotate(img0, detections, labels)
return img0
```
**Create Inference YAML file**
```python
def create_yaml(base_dir: str):
import yaml
data_yaml = {
'train': 'data/train/images',
'val': 'data/validation/images',
'test': 'data/test/images',
'nc': 1, # number of the classes, in our cases is just 1 because we only want to detect license plate,
'names': {
0: 'license_plate'
},
}
with open(f'{base_dir}/custom_data.yaml', 'w') as file:
yaml.dump(data_yaml, file)
create_yaml(base_dir=HOME)
```
**Initialize variables**
```python
FINETUNED_WEIGHTS_PATH = os.path.join(HOME, 'plate_detection.pt') # The fine-tuned weights must be under the HOME directory.
CUSTOM_DATA_YAML_PATH = os.path.join(HOME, 'custom_data.yaml') # Custom yaml file is necessary for inferencing
DEVICE = '0' if torch.cuda.is_available() else 'cpu' # IF GPU is available, its preferred.
```
**Get Inference**
```python
%matplotlib inline
image_path = '<paste-your-image-path-here>'
img = predict(
image_path=image_path,
weights=FINETUNED_WEIGHTS_PATH,
data=CUSTOM_DATA_YAML_PATH,
imgsz=640,
conf_thres=0.5,
iou_thres=0.2,
device=DEVICE,
)
sv.plot_image(img)
```
![](/example/863.jpg)
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
车牌检测_使用YOLOv9实现的车牌检测算法_支持训练自定义hf数据集_优质算法项目实战.zip (4个子文件)
车牌检测_使用YOLOv9实现的车牌检测算法_支持训练自定义hf数据集_优质算法项目实战
yolov9-fine-tuning-custom-dataset-plate-detection.ipynb 3.58MB
example
batch_predict.jpg 1.64MB
863.jpg 211KB
README.md 4KB
共 4 条
- 1
资源评论
极智视界
- 粉丝: 2w+
- 资源: 1468
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 抽取地下水对加州约塞米蒂国家公园山地湿地复合体可持续性的影响
- C++vector介绍及详细使用示例(源代码)
- java基于GUI的简单记事本
- 指纹识别-基于OpenCV实现的指纹识别算法-附项目源码.zip
- 206693250008_R01C02_Grn.idat
- 瞳孔跟踪-基于OpenCV+网络摄像头的瞳孔跟踪算法实现-附项目源码+流程教程-优质项目分享.zip
- ModStartCMS v8.4.0 框架稳定性持续迭代,修复部分已知问题
- bleder 教室学校学生教育室办公室考试
- 人脸检测-使用OpenCV实现的动漫+漫画人脸检测算法-附项目源码-优质项目实战.zip
- 道路贴图,材质材料免费
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功