Skip to content

Capture images on a panel and save them to a dataset ​

A new wrapper, a new product, a defect that the model misses: the line itself is the best source of images for the next training. The capture widget lets the operators collect them from the panel, without touching the flow: they start a capture session, stop it when they have enough, and upload it to a dataset of the project. In this guide you add it to the panel of a biscuit line and upload a session.

What you will learn

  • How the capture widget and the flow share the work: the widget decides, the flow saves
  • How to connect it to dataset-upload and list-dataset
  • How an operator captures a session and uploads it to a dataset

Before you start

You need a dataset to receive the images; here an empty segmentation dataset, «line_new_wrapper». You need a panel page with a group for the widget, 12 columns wide; here «Capture», on the page «Biscuit line» (see Show inspection results on a panel).

The flow already has the chain that saves the images, which the widget relies on:

  • A «camera», here a from-dataset node that sends a pack of biscuits every two seconds after «start».
  • A switch node, «capturing?», that only lets images through while the global trainingMode is true: the widget sets it when a capture starts.
  • «Into the session», which names each image <time>_pack.jpg and puts it in the folder of the session, /opt/storage/images/training/imageSaveDir/<session>; the name of the session is in the flow context, in captureName. Code below.
  • Two rp-save-file nodes: one saves the original, the other a light WebP copy of 400 pixels in imageShowDir/<session>, for the thumbnails.
  • «Saved», which tells the widget what was saved: msg.path, the folder, and msg.payload.imageSave, the files.
  • A dataset-upload node, with «Config» set to msg.configTraining, and a list-dataset node followed by «Only our datasets», which picks the datasets of this line out of all those of the project.

1. Add the capture widget ​

Type ui-capture in «filter nodes», drag the «capture» node, in the «Renders» category, below the chain and wire «Saved» to it. It is a whole capture page in one widget: the button to start and stop, the sessions with their latest images, and the upload to a dataset.

Add the capture widget

2. Set it up ​

Double-click it and choose the group, «Capture». The other fields match the chain of the flow:

  • «Images path»: «Training» keeps the sessions in /opt/storage/images/training, where «Into the session» saves them.
  • «Capture mode global» and «Capture name (flow context)»: the global flag and the flow variable the chain reads, trainingMode and captureName. Leave them.
  • «Thumbnail URL prefix»: the address the panel loads the thumbnails from. The default, /nodered/local/fit/400/images, is the image service of a controller; the controller of this guide does not have it, so the flow serves them itself with the four nodes at the bottom, at /nodered/line-thumbnails.
  • «Image sources»: one per camera, named as the end of the file names, here pack. The upload asks for a dataset per source.

Set it up

3. Connect the upload ​

Click «Done». The first output starts and stops uploads: wire it to dataset-upload, and the output of dataset-upload back to the widget, which follows the upload with it. The third output asks for the list of datasets when the operator opens the upload: wire it to list-dataset, and «Only our datasets» back to the widget. The second output sends what the operators do (start, stop, upload, delete), for an audit trail.

Connect the upload

4. Deploy and start the camera ​

Click «Deploy» and press the «start» inject node. The camera sends a pack every two seconds; «capturing?» stops them all until an operator starts a capture.

5. Start a capture ​

Open «Panels». The capture page shows the free space on the controller, the image sources and what is waiting to be uploaded. Press «START CAPTURE», name the session, here «new-wrapper», and press «START». The name becomes a folder, so it cannot contain /.

Start a capture

6. Watch the images arrive ​

Press «START». The widget sets trainingMode and captureName, «capturing?» lets the images through, and each one the chain saves appears in the session, marked «LIVE», with the number of images on the right.

Watch the images arrive

7. Stop the capture ​

Press «STOP CAPTURE». The images stay on the controller, in the session, and «Pending upload» counts them. The operator can start more sessions, one per batch or per product, and upload or delete each one later.

Stop the capture

8. Upload the session to a dataset ​

Press «Upload» on the session. For each image source, choose the dataset its images go to, here «line_new_wrapper»; «Suffix» adds a word to their names, such as the camera. The list only has the datasets that «Only our datasets» lets through. Press «UPLOAD».

Upload the session to a dataset

9. Follow the upload ​

The widget sends the session and the chosen dataset to dataset-upload, which uploads the images and reports each one back; the session counts them and shows «Uploaded» at the end. Each image uploaded is deleted from the controller, and the session with it when none is left. «Delete» removes a session without uploading it.

Follow the upload

10. Find the images in the dataset ​

Open the dataset in «Datasets». The images of the session are there, without labels, named as they were saved. Label them, and train again so that the model learns the new wrapper.

Find the images in the dataset

The code of the chain ​

«Into the session» puts each image in the folder of the session being captured:

js
// The session being captured, which the capture widget keeps in the flow context
const session = flow.get("captureName");
if (!session || flow.get("diskLow")) return null;
// <time>_<source>: dataset-upload finds the dataset of each image by its source, here «pack»
const name = Date.now() + "_pack";
msg.folder = "/opt/storage/images/training/imageSaveDir/" + session;
msg.filename = name + ".jpg";
msg.thumbFolder = "/opt/storage/images/training/imageShowDir/" + session;
msg.thumbName = name + ".webp";
return msg;

«Saved» tells the widget what was saved:

js
msg.path = msg.folder;
msg.payload = { imageSave: [{ filename: msg.filename }] };
delete msg.image;
delete msg.light;
return msg;

Serving the thumbnails from the flow ​

On a controller without the image service, four nodes serve the thumbnails: an «http in» node that answers GET /line-thumbnails/:images/:dir/:session/:file, a function node that turns the address into a file of /opt/storage/images, a «file in» node that reads it, and an «http response» node with the header content-type: image/webp. The function node refuses any other folder:

js
const p = msg.req.params;
const parts = [p.images, p.dir, p.session, p.file];
// Only files of the capture folders: no "..", no other folder
if (parts.some((part) => !part || part.includes("..") || part.includes("/"))) {
  msg.statusCode = 404;
  msg.payload = "";
  return [null, msg];
}
msg.filename = "/opt/storage/images/" + parts.join("/");
return [msg, null];

Good to know ​

  • Low disk. Below 10 GB free, the widget sets flow.diskLow and warns on the panel; «Into the session» then saves nothing.
  • Restarts. A redeploy keeps a capture running; a restart of Node-RED stops it. The sessions and their images stay on the controller until they are uploaded or deleted.
  • Several cameras. Add one image source per camera and end the file names with it (<time>_top.jpg, <time>_side.jpg): the upload asks for a dataset for each.
  • Who did what. The second output sends capture_start, capture_stop, capture_start_upload, capture_stop_upload and capture_remove_images, with the user of the panel in msg._client, for an audit trail.
  • Stopping from the flow. A message { action: "stopCapture" } to the widget stops the capture, for example after a number of images.

Next steps ​

Recorded on Rosepetal release 1.8.15, Node-RED 5.0.10.