Skip to content

Review inspection sessions and download a report ​

A session is one run of the line: a lot, a shift, a product change. The Sessions widget is a whole page of the panel to look back at them: the list of sessions, sortable by any column, and for each one its figures, a chart of the results, the errors with the images of their pieces, and downloads, among them a PDF report to hand over or file. The flow records the sessions and the widget shows them. In this guide you add a Sessions page to a biscuit line and download the report of a session.

What you will learn

  • What the Sessions widget needs from the flow: the list of sessions and the files of the saved pieces
  • How to set up the widget and its columns
  • How to sort the sessions, open one, look at the images of an error and download its PDF report

Before you start

You need a panel page with one group 12 columns wide for the widget; here the page «Line sessions» with the group «Sessions» (pages and groups are explained in Show inspection results on a panel). The flow already inspects the line and records its sessions:

  • «start session» and «stop session», two inject nodes, and «Session», a function node that opens a session with its counters at zero, starts the «camera», and at the end closes the session and adds it to the list. A real line starts and stops sessions from the panel or from the PLC, at a change of lot or of shift.
  • A «camera», here a from-dataset node that sends a pack of biscuits every 1.5 seconds, and a segmentation model that finds the biscuits.
  • «Check the pack», which judges the pack (four biscuits make a good pack, anything else is an error) and writes its figures and its boxes, and «Into the session», which counts the pack in the running session and files it as the widget expects (see the first step).
  • rp-resize and rp-save-file, which save a JPEG of 1024 pixels of each pack in the folder of the session, and «Metadata» and a «write file» node, which save the small JSON file of the pack next to it.
  • «Sessions list», which sends the list of sessions, here kept in the flow context; on a real line, the rows of a table of sessions in a database. «on deploy» sends it when the flow starts, and «Session» when a session ends.
  • Four nodes at the bottom that serve the saved images to the panel (see «Good to know»).

The code of each function node is at the end of this page. Two sessions, «Lot 01» and «Lot 02», were recorded before the video.

1. See how a pack is filed ​

Double-click «Into the session». The Sessions widget does not save anything: it reads what the flow keeps. For each pack, this node:

  • adds it to the counters of the running session: totalCounter, noClassCounter (the packs that failed), tagCounter (OK and NOK), errorCounter (the count of each error) and param (the minimum, maximum and mean of each figure);
  • names the folder of the pack, /opt/storage/images/sessions/<sessionId>/classes/<class>, where the class is «OK» or the name of the error, and the image, <time>_pack.jpg;
  • writes its metadata: the time, the result, the figures of the pack in resume and, for each image, the file and its boxes.

It reads the global saveImagesMode, which «Options» on the Sessions page sets, to keep the images of every pack, of the good ones, of the failed ones or of none; the counters always count every pack.

See how a pack is filed

2. Add the Sessions widget ​

Click «Cancel». Type ui-sessions in «filter nodes», drag the «sessions» node, in the «Renders» category, next to «Sessions list» and wire «Sessions list» to it. It is a whole Sessions page in one widget. Its first output sends the session the user deleted, after the widget has deleted its files: wire it to «Forget the session», which removes the row from the list and sends the list again.

Add the Sessions widget

3. Choose its group and its folder ​

Double-click it and choose the group, here «Sessions». «Sessions dir» is the folder with one folder per session, where the flow files the packs; leave the default, /opt/storage/images/sessions. «Images URL» is the address the panel loads those images from, under Node-RED's address; leave local/images/sessions.

Choose its group and its folder

4. Add a column ​

Open the «Table» tab. «Columns» are the columns of the list: a title and the path of the value in the row of the session. The defaults are the date, the configuration, the packs inspected and the errors. Click «add column» and add «Batch», with the path batch, the lot the flow wrote. «Details» chooses the fields shown for the selected session and the downloads offered, «Charts» and «Images» set up the rest of the page, and «Texts» translates every word of the page and of the report.

Add a column

5. Deploy and record a session ​

Click «Done» and «Deploy». Press «start session»: the packs go through the model and «Into the session» files them. Press «stop session» after a few packs: the session ends, joins the list and «Sessions list» sends the list to the widget.

Deploy and record a session

6. Open the Sessions page ​

Open «Panels» and the page «Line sessions». On the left, the list of sessions, with a search box above it and as many rows as fit; on the right, the details of the session you select. «Options», above the list, chooses which packs keep their image (see «Good to know»).

Open the Sessions page

7. Sort the list ​

Click the title of a column to sort the list by it, and again to sort the other way; a third click goes back to the order of the flow. Here «Errors», twice: the session with the most errors comes first. The dates sort by time.

Sort the list

8. Open a session ​

Click a row. On the right: the fields of the session (configuration, duration, packs inspected, start and finish, errors and batch) with the buttons «Configuration», which shows the settings the session ran with, «Download» and «Delete»; the line chart of the figures of the saved packs, here the biscuits found in each pack, with their average, minimum and maximum (each line is scaled from 0 to 100 % until «Normalize» is turned off in its settings, ⚙); the bar chart of the errors; and the list of errors, the most frequent first.

Open a session

9. See the images of an error ​

Click an error in the list. A dialog shows the saved packs of that error, the newest first, each with its boxes. Click one to see it larger, with its result, its date and its figures; the arrows, or the arrow keys, go to the next and the previous one.

See the images of an error

10. Download the report ​

Close the dialog and click «Download». «Report (PDF)» opens the report of the session in a new tab of the browser, to save or print. The menu also has «Summary (CSV)», the figures of the session; «Pieces (CSV)», one row per saved pack with the link to its image; and «Full data (JSON)», everything the session and its packs hold.

Download the report

The report ​

The report of «Lot 01», as the browser opened it:

First page of the PDF report of a session

  • The header: «Session report», when it was made, the configuration and the start of the session.
  • «Summary»: start, end, duration, configuration and batch, and the packs inspected, OK and NOK with their share, and the packs saved.
  • The first and the last saved packs, with their boxes, green when the region of the box passed and red when it failed: here the tag of each box, biscuit, names the entry biscuit of the pack's resume, whose result says OK or NOK.
  • «Error count»: one row per error, the most frequent first, with its count over the packs inspected and a saved pack of that error. When a box of the pack is tagged with the name of the error, the row shows that part of the image.

The code of the flow ​

«Session» starts and stops a session:

js
// «start session»: a new session, with its counters at zero
if (msg.payload === "start") {
  if (flow.get("session")) return null;
  const done = flow.get("sessions") ?? [];
  const now = Date.now();
  flow.set("session", {
    sessionId: now, startDate: now, endDate: null,
    configName: "Pack of 4", batch: "Lot " + String(done.length + 1).padStart(2, "0"),
    config: { name: "Pack of 4", model: "test_01", expected: 4, minConfidence: 0.5 },
    totalCounter: 0, noClassCounter: 0, tagCounter: { OK: 0, NOK: 0 }, errorCounter: {}, param: {},
  });
  return [{ payload: "start" }, null];
}
// «stop session»: the session ends and joins the list
const session = flow.get("session");
if (!session) return null;
session.endDate = Date.now();
flow.set("sessions", [session, ...(flow.get("sessions") ?? [])]);
flow.set("session", undefined);
return [{ payload: "stop" }, { payload: "list" }];
  • A session needs a sessionId made of digits (here the time it started) and startDate, in milliseconds; endDate is null while it runs. configName, batch, config and the counters are what the list, the details, the charts and the report show. Any other field of the row can be a column or a field of the details.

«Check the pack» judges each pack:

js
// Four biscuits the model is sure of make a good pack; anything else is an error
const biscuits = msg.inference.filter((b) => b.confidence >= 0.5);
const n = biscuits.length;
msg.error = n < 4 ? "Missing biscuit" : n > 4 ? "Extra biscuit" : null;
// The figures of the pack: the line chart plots the numbers
msg.resume = { biscuit: { found: n, expected: 4, result: msg.error ? "NOK" : "OK" } };
// The boxes, with positions from 0 to 1; the tag names the region of the resume
msg.bbox = biscuits.map((b) => ({ x_rel: b.xywhr.x, y_rel: b.xywhr.y, w_rel: b.xywhr.w, h_rel: b.xywhr.h, tag: "biscuit", score: b.confidence }));
return msg;

«Into the session» counts it and files it:

js
// Only while a session runs
const session = flow.get("session");
if (!session) return null;
// The counters of the session: the list, the charts and the report read them
const result = msg.error ?? "OK";
session.totalCounter += 1;
session.tagCounter[msg.error ? "NOK" : "OK"] += 1;
if (msg.error) {
  session.noClassCounter += 1;
  session.errorCounter[msg.error] = (session.errorCounter[msg.error] ?? 0) + 1;
}
const found = msg.resume.biscuit.found;
const p = session.param["biscuit.found"] ?? { min: found, max: found, mean: 0, count: 0 };
p.mean = (p.mean * p.count + found) / (p.count + 1);
p.count += 1;
p.min = Math.min(p.min, found);
p.max = Math.max(p.max, found);
session.param["biscuit.found"] = p;
flow.set("session", session);
node.status({ text: session.batch + ": " + session.totalCounter + " packs" });
// «Options» of the Sessions page: which packs keep their image
const mode = global.get("saveImagesMode") ?? "all";
if (mode === "none" || (mode === "ok" && msg.error) || (mode === "nok" && !msg.error)) return null;
// <Sessions dir>/<sessionId>/classes/<class>/: the image and its metadata
const ts = Date.now();
msg.folder = "/opt/storage/images/sessions/" + session.sessionId + "/classes/" + result;
msg.filename = ts + "_pack.jpg";
msg.meta = {
  ts, name: String(ts), date: ts, result, isAnomalous: msg.error !== null, resume: msg.resume,
  images: { pack: { file: msg.filename, bbox: msg.bbox } },
};
return msg;

«Metadata» turns the metadata into the file that «write file» saves next to the image:

js
msg.filename = msg.folder + "/" + msg.meta.ts + ".json";
msg.payload = JSON.stringify(msg.meta);
delete msg.image;
delete msg.jpg;
return msg;

«Sessions list» and «Forget the session»:

js
msg.payload = flow.get("sessions") ?? [];
return msg;

// The widget already deleted the files of the session: drop its row
flow.set("sessions", (flow.get("sessions") ?? []).filter((s) => s.sessionId !== msg.sessionId));
return msg;

Good to know ​

  • The files of a session. For each saved pack, /opt/storage/images/sessions/<sessionId>/classes/<class>/<time>.json with its metadata and, next to it, the images it names. The class is the name of the error, as in errorCounter, or «OK». The line chart plots every number of resume; the images of an error come from the folder of that class.

  • Images on the panel. The widget loads each image from /nodered/local/images/sessions/… and its thumbnail from /nodered/local/fit/400/images/sessions/…. The controller of this guide does not serve those addresses, so the flow does it with four nodes: two «http in» nodes for GET /local/images/sessions/:session/classes/:cls/:file and GET /local/fit/400/images/sessions/:session/classes/:cls/:file, a function node that turns the address into a file of the sessions, a «file in» node and an «http response» node with the header content-type: image/jpeg. The function node refuses any other file:

    js
    const { session, cls, file } = msg.req.params;
    // Only the files of a session folder: no "..", no other folder
    if (!/^\d+$/.test(session) || /[\/\\]|^\./.test(cls) || !/^[\w.-]+$/.test(file) || file.startsWith(".")) {
      msg.statusCode = 404;
      msg.payload = "";
      return [null, msg];
    }
    msg.filename = "/opt/storage/images/sessions/" + session + "/classes/" + cls + "/" + file;
    return [msg, null];
  • The running session. While a session runs, the inspection flow can keep the global status at true, sessionId at its id and the counters in the globals totalCounter, noClassCounter, tagCounter, errorCounter and param; then any message that is not a list, for example from an inject node every 5 seconds, updates its row on the page, and its «Delete» stays off. This flow sends only finished sessions, so it needs none of that.

  • Options. The button above the list chooses which packs keep their image: all of them, only the good ones, only the failed ones or none. The widget keeps the choice in the global saveImagesMode and sends it on its second output; the flow reads it, as «Into the session» does.

  • Deleting a session. «Delete» asks first, then deletes the folder of the session and sends it on the first output; the flow removes its row, here with «Forget the session», and sends the list again. A running session cannot be deleted.

  • Other languages. The «Texts» tab of the node holds every word of the page and of the report.

Next steps ​

Recorded on Rosepetal release 1.8.15, Node-RED 5.0.10.