ONE AI Scripting: From Trained Model to Inspection Station
- 0:00Introduction
- 1:30ONE AI Project: Locate the label
- 2:38ONE AI Project: Defect detection
- 3:24Scripting Feature using agents
- 5:20Result
A trained model is only half of an inspection system. You also need a camera, a decision rule, a screen for the operator and a way to get results into your production systems. With ONE AI Scripting, you build all of that in a few lines of JavaScript, right inside ONE WARE Studio, without a separate application.
Packaging Control: Two Models, One Script
The video above shows our Packaging Control demo: quark cups move along a conveyor belt, and every sealed lid is checked for cuts in the foil. The inspection uses two small ONE AI segmentation models. The first finds the label on the belt, the second segments the cuts on it.
A single ONE AI script connects both models into a complete inspection station:
Grab a frame and crop it to the stop position on the belt.
The location model segments the lid.
Cut out a 600 × 400 pixel window centered on the label, without stretching.
The defect model segments cuts and the script rejects damaged packages.
The core of the script, simplified:
function inspect(frame) {
// 1. Find the label in the centered 717x1298 stop window
const roiX = Math.floor((frame.width - 717) / 2);
const roi = image.crop(frame, roiX, 0, 717, 1298);
const loc = onnx.predict('./Models/location.onnx', roi);
const regions = shape.components(mask.open(loc.mask, 1), { minArea: 200 });
if (!regions.length) return ui.label('result', 'No label found');
const r = regions.reduce((a, b) => (b.area > a.area ? b : a));
// 2. Cut out a 600x400 window centered on the label, without stretching
const sx = 717 / loc.mask.width, sy = 1298 / loc.mask.height;
const cx = roiX + (r.x + r.width / 2) * sx, cy = (r.y + r.height / 2) * sy;
const label = image.crop(frame, Math.round(cx - 300), Math.round(cy - 200), 600, 400);
// 3. Segment the cuts and decide
const def = onnx.predict('./Models/defect.onnx', label);
const cuts = shape.components(def.mask, { minArea: 30, connectivity: 8 });
const ok = cuts.length === 0;
ui.label('result', ok ? 'Good' : 'Defect', { color: ok ? '#22c55e' : '#ef4444', fontSize: 40 });
ui.image('labelCrop', mask.overlay(label, def.mask, { colors: { 1: '#ff2020' }, opacity: 0.5 }));
}
// Inspect once every time the belt stops
let prev = null, still = 0;
while (app.isRunning) {
const frame = await camera.capture(cam);
const cur = image.resize(frame, 232, 130, { interpolation: 'area' });
const moving = !prev || image.residual(prev, cur, { channels: 'gray' }).mean > 1.5;
still = moving ? 0 : still + 1;
if (still === 3) inspect(frame);
prev = cur;
await time.sleep(150);
}
The script also shows the live camera feed in the operator dashboard, counts inspected and rejected packages and logs every result. All of this happens inside ONE WARE Studio. You don't need a Python environment, an extra inference runtime or a separately built UI.
What You Can Build with Scripting
Packaging control is just one example. The same building blocks cover most tasks around an inspection line:
Operator Dashboards
Fullscreen dashboards with images and overlays, buttons, inputs, tables, gauges and multiple screens. Built with one line per widget.
Live Cameras
Capture frames from connected cameras and show a native live preview at up to 120 fps. Recorded videos work as a camera, so you can test without hardware.
Run and Chain Models
Run exported classification, object detection, segmentation and multi-image models. Chain several small models like in the packaging demo, or always pick the newest export automatically.
Count and Measure
Clean up masks, count objects, split touching parts and measure area, circularity, outlines and color. Turn every prediction into your own pass/fail rule.
Connect Your Systems
Send results to your MES or any REST endpoint, write CSV logs and keep counters that persist between runs.
PDF Reports
Generate multi-page inspection reports with a summary, overview images and one entry per finding.
Built for the Production Line
- Plain JavaScript: scripts are single
.oneai.jsfiles in your project. Full autocompletion and type checking come from the generatedoneai.d.ts. - Runs all day: set the run timeout to unlimited for permanent inspection stations. Stop and restart at any time from the dashboard.
- Sandboxed: scripts only write inside their own folder, and HTTP requests can be limited to an allow-list of hosts.
Let ONE WARE Agents Write the Script
You don't have to write scripts yourself. ONE WARE Agents know the complete scripting API. Describe what you need, for example:
Build an inspection dashboard with a live camera, a capture button and a PASS/FAIL verdict using my latest segmentation model.
For the packaging control dashboard in the video, we gave the agent a more detailed prompt with the inspection flow, the stop detection and the dashboard layout:
Show the full prompt from the video
# Task: Write `Quark_Blog-Scripting.oneai.js`, a ONE AI quality inspection dashboard
Project: `./Quark_Blog-Scripting.oneai`
Video: `./Assets/conveyor_inspection_hires.mp4`
Test the script and make sure it has no tsc errors.
## Goal
Quark cups stop on a conveyor belt under a camera. Detect cuts in the lid label
and show **Good** or **Defect** for every cup.
## AI flow
1. The camera frame is 2314x1298. Crop the centered 717x1298 stop window.
2. Run `./Models/location.onnx` (segmentation) and take the largest label region.
- Nothing found: show "No label found".
- Region touches the mask border: show "Label not in position".
3. Cut out the label at 600x400 without stretching the loose bounding box.
4. Run `./Models/defect.onnx` (segmentation) on the label crop and find the cuts.
- No cuts: **Good**.
- Otherwise: **Defect**. Draw a red rectangle around each cut on the frame
and on the label crop.
## Stop detection
In each loop:
- Capture a frame and resize it to 232x130.
- Compare it with the previous sample. If
`image.residual(prev, cur, { channels: 'gray' }).mean > 1.5`, the belt is moving.
- After 3 still samples, inspect once per stop.
- Wait 150 ms between checks.
## UI
- Row 1: camera select | conveyor status (Moving / Stopped)
- Row 2: last inspection | good count | defect count | result (large, colored)
- Row 3: live preview | last inspected frame with overlays | label crop
- Default input: the video above
The agent creates the script, tests it in a dashboard by operating the controls itself, and then hands it over to you. Before every run or test, it asks for your confirmation.
Get Started
Scripting is available in ONE WARE Studio with ONE AI extension 1.0.49 or newer:
- Open your ONE AI project in ONE WARE Studio.
- Create a file ending in
.oneai.jsin the project folder. - Click Start UI to run it.
Find everything you need in the Scripting documentation, including the Operator Dashboard, Image Analysis & Inference, ready-to-use Examples and the full API Reference.
Have an inspection task in mind? Book a demo or contact us and we will help you set it up.
