YOLOv5 ONNX example by OpenCV DNN.
Project description
YOLOv5-ONNX read example by OpenCV DNN.
If you wanna export to onnx format, please refer: https://github.com/ultralytics/yolov5
Install package
pip install yolov5-onnx-cv
Code example:
import numpy as np
from yolov5_onnx_cv import YOLOv5_ONNX_CV
import cv2
# Just inherit YOLOv5_ONNX_CV and define your class_names and class_colors
class YOLOv5(YOLOv5_ONNX_CV):
# Define coco class names
class_names = [
'person', 'bicycle', 'car', 'motorcycle', 'airplane',
'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag',
'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork',
'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich',
'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut',
'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table',
'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book',
'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
]
# Random to generate colors
class_colors = [(int(i[0]), int(i[1]), int(i[2])) for i in np.random.randint(256, size=(len(class_names), 3))]
# Simple camera read
def load_capture():
capture = cv2.VideoCapture(1)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
return capture
if __name__ == '__main__':
# Load camera
capture = load_capture()
# Load checkpoint from any path. (onnx only)
model = YOLOv5('tests/yolov5s.onnx', (640, 640), 0.5)
while True:
# Get camera frame
success, frame = capture.read()
if not success:
print('Open camera fail.')
break
# Inference all of output result
preds = model(frame)
for pred in preds:
print(pred.id, pred.name, pred.conf, pred.box)
# Show labeled box result
model.show_label_boxes()
# or you can get labeled box result
# img = model.get_label_boxes_image()
# Any key to quit.
if cv2.waitKey(1) > -1:
print('finished by user')
break
capture.release()
Demo example:
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
yolov5_onnx_cv-0.0.5.tar.gz
(4.7 kB
view details)
Built Distribution
File details
Details for the file yolov5_onnx_cv-0.0.5.tar.gz
.
File metadata
- Download URL: yolov5_onnx_cv-0.0.5.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c108d7238a4ae2e2bc734c628bb64ef038071b25f51e5ed0b7f7ace1ef47d1b4 |
|
MD5 | d2e1e489b4423e0e698266fa54762f0e |
|
BLAKE2b-256 | db5824ff353cc5208c4be2fa5ab8d309116b97ec311d350b7de19a9c26ee52a8 |
Provenance
File details
Details for the file yolov5_onnx_cv-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: yolov5_onnx_cv-0.0.5-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1cee2374650884a4466436d7f1205afaac0c8e402d87c0edaacaab6b835e8008 |
|
MD5 | 8042cf78a25bbca909bdb3e1baf47b21 |
|
BLAKE2b-256 | 59afe21dfc2c0ba58089bbd87067d224b9455fb43d0a53e1be65b1443c323383 |