Skip to content

Commit

Permalink
add script to repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-idk committed Nov 1, 2024
1 parent 93cdcf9 commit 30fdc79
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/source/docs/objectDetection/about-object-detection.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ First, download airockchip's YOLOv5 fork along with the onnx to rknn conversion

```bash
git clone https://github.com/airockchip/yolov5.git airockchip-yolov5
curl -O link-to-onnx2rknn.py
wget https://raw.githubusercontent.com/PhotonVision/photonvision/refs/heads/master/scripts/onnx2rknn.py
```

#### Export Command
Expand Down
72 changes: 72 additions & 0 deletions scripts/onnx2rknn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import argparse
import cv2
import numpy as np
from rknn.api import RKNN
import os

def collect_images_from_directory(directory_path):
"""Collect all image paths from the specified directory."""
image_extensions = ['.jpg', '.jpeg', '.png']
image_paths = []

for root, dirs, files in os.walk(directory_path):
for file in files:
if any(file.lower().endswith(ext) for ext in image_extensions):
image_paths.append(os.path.join(root, file))

return image_paths

def convert(srcFileName, dstFilename, dataset):
platform = "rk3588"

print('--> Source file name: ' + srcFileName)
print('--> RKNN file name: ' + dstFilename)

rknn = RKNN()

rknn.config(mean_values=[[0, 0, 0]], std_values=[[255, 255, 255]], target_platform=platform)

# Load model
print('--> Loading model')
ret = rknn.load_onnx(srcFileName)
if ret != 0:
print('load model failed!')
exit(ret)
print('done')

# Build model with quantization
print('--> Building model')
ret = rknn.build(do_quantization=True, dataset=dataset)
if ret != 0:
print('build model failed.')
exit(ret)
print('done')

# Export model to rknn format for Rockchip NPU
print('--> Export rknn model')
ret = rknn.export_rknn(dstFilename)
if ret != 0:
print('Export rknn model failed!')
return ret

print('export done')

rknn.release()

def main():

parser = argparse.ArgumentParser(description='Transform to RKNN model')
parser.add_argument('source_file', help='Path to the ONNX model file')
parser.add_argument('description_file', help='Output path for the RKNN model file')
parser.add_argument('quant_dir', help='Directory containing images for quantization')
args = parser.parse_args()

dataset = collect_images_from_directory(args.quant_dir)
if not dataset:
print(f"No images found in directory: {args.quant_dir}")
exit(1)

convert(args.source_file, args.description_file, dataset)

if __name__ == '__main__':
main()

0 comments on commit 30fdc79

Please sign in to comment.