YOLO Functions
Overview
- YOLO usage guide — see the Android version; training tutorials are the same
yolov8Api.newYolov8 Initialize YOLOv8 Instance
- Initialize a YOLOv8 instance
- @return
Yolov8Utilobject
function main() { // Initialize YOLO instance let yolov8s = yolov8Api.newYolov8(); // Initialize config options let config = yolov8s.getDefaultConfig("yolov8s-640", 640, 0.25, 0.35, "ALL", 0, [ "aixin", "pinglun" ]) // Set CPU thread count; lower values use less CPU. Default is 4; 1 or 2 is recommended config["num_thread"] = 1; logd("config : " + JSON.stringify(config)) // Initialize trained model let paramPath = "c:/model.ncnn.param"; let binPath = "c:/model.ncnn.bin"; let inted = yolov8s.initYoloModel(config, paramPath, binPath); if (inted) { logd("yolov8s init success"); } else { logd("yolov8s init failed: " + yolov8s.getErrorMsg()); return; } let bitmap = image.readBitmap("c:/a.png"); let result = yolov8s.detectBitmap(bitmap, []); // Or use: let img = image.readImage("c:/a.png") let result2 = yolov8s.detectImage("c:/a.png", []) image.recycle(img); // With parameters, only filter pinglun class data //let result = yolov8s.detectBitmap(bitmap, ["pinglun"]); if (result == null || result == "") { logd("yolov8s no result: " + yolov8s.getErrorMsg()); } else { logd("yolov8s result: " + result); } // Release when needed; do not release after every call yolov8s.release();}
main();yolov8Api.newYolov8Onxx Initialize YOLOv8 ONNX Instance (Multi-Instance Supported)
- Initialize a YOLOv8 ONNX instance
- @return
Yolov8Utilinstance object
function main() { // Initialize YOLO instance let yolov8s = yolov8Api.newYolov8Onxx(); // Initialize config options let config = yolov8s.getOnnxConfig([ "aixin", "pinglun" ], 640, 640, 0.35, 0.55, 2) logd("config : " + JSON.stringify(config)) // Initialize trained model let paramPath = "c:/best.onnx"; let inted = yolov8s.initYoloModel(config, paramPath, ""); if (inted) { logd("yolov8s init success"); } else { logd("yolov8s init failed: " + yolov8s.getErrorMsg()); return; } // Capture screen as img let img = image.captureFullScreen() // Read local image // let bitmap = image.readBitmap("/sdcard/a.png"); let result = yolov8s.detectImage(img, []); // With parameters, only filter pinglun class data //let result = yolov8s.detectBitmap(bitmap, ["pinglun"]); if (result == null || result == "") { logd("yolov8s no result: " + yolov8s.getErrorMsg()); } else { logd("yolov8s result: " + result); } if (img != null) { // Recycle image img.recycle(); } // Release when needed; do not release after every call yolov8s.release();}
main();Yolov8Util.getOnnxConfig ONNX Config Options
- ONNX configuration options
- @param obj_names JSON array of class names; if omitted, ONNX reads them from the model, e.g.
["star","common","face"] - @param input_width Training image width; 0 lets ONNX extract it automatically
- @param input_height Training image height; 0 lets ONNX extract it automatically
- @param confThreshold Minimum confidence threshold for detections during ONNX inference
- @param iouThreshold IoU threshold used in NMS during ONNX inference
- @param numThread Thread count; usually half the CPU count. If unknown, omit. Use 1 or 2 to reduce CPU usage
- @return
{JSON}
Yolov8Util.getDefaultConfig Get YOLOv8 Default Config
- Get the default YOLOv8 configuration
- @param model_name Model name; use
yolov8s-640by default - @param input_size YOLOv8 training imgsz parameter; use 640 by default
- @param box_thr Detection box coefficient; use 0.25 by default
- @param iou_thr Output coefficient; use 0.35 by default
- @param bind_cpu Whether to bind CPU; options are ALL, BIG, LITTLE; use ALL by default
- @param use_vulkan_compute Enable hardware acceleration: 1 yes, 0 no; use 0 by default
- @param obj_names JSON array of class names from training, e.g.
["star","common","face"] - @return JSON data
See the `Initialize YOLOv8 instance` exampleYolov8Util.initYoloModel Initialize YOLOv8 Model
- To generate param and bin files, see the YOLO usage chapter: convert YOLO pt to ncnn param/bin files
- For ONNX models, set binPath to null; paramPath is the ONNX file path
- @param map Parameter map; for ncnn use getDefaultConfig, for onnx use getOnnxConfig
- @param paramPath Path to the param file
- @param binPath Path to the bin file
- @return boolean true on success, false on failure
See the `Initialize YOLOv8 instance` exampleYolov8Util.detectBitmap Detect Image
- Detect objects in an image
- Example return data:
[{"name":"heart","confidence":0.92,"left":957,"top":986,"right":1050,"bottom":1078}]- name: class name; confidence: confidence score; left, top, right, bottom: bounding box coordinates
- @param bitmap Java BufferedImage object
- @param obj_names JSON array; omit to skip filtering, or provide class names to keep
- @return string
See the `Initialize YOLOv8 instance` exampleYolov8Util.detectImage Detect AutoImage
- Detect objects in an image
- Example return data:
[{"name":"heart","confidence":0.92,"left":957,"top":986,"right":1050,"bottom":1078}]- name: class name; confidence: confidence score; left, top, right, bottom: bounding box coordinates
- @param image AutoImage object
- @param obj_names JSON array; omit to skip filtering, or provide class names to keep
- @return string
See the `Initialize YOLOv8 instance` exampleYolov8Util.release Release Resources
- Release YOLOv8 resources
- @return boolean
See the `Initialize YOLOv8 instance` example Call release when the script ends; no need to release after every useYolov8Util.getErrorMsg Get YOLOv8 Error Message
- Get the YOLOv8 error message
- @return string
See the `Initialize YOLOv8 instance` example