samedi 15 mars 2025

YOLO and Redbol

YOLO (You Only Look Once) is a wonderful tool for object detection and image segmentation (https://docs.ultralytics.com/).
Of course, this works very well with Python.  You'll find here a very clear documentation (https://pyimagesearch.com/2025/01/13/getting-started-with-yolo11/). 
But YOLO also offers a CLI mode that can be used with Rebol3 or Red.
With Rebol3 we use the opencv module made by Oldes.  The result with articulations detection in around 1 sec!


And for Red: just Red language functions.

#! /usr/local/bin/red-view
Red [
    Needs: View
    Author: "ldci"
]

appDir: system/options/path
change-dir appDir
imageFile: ""
iSize: 320x240
margins: 5x5
isFile?: false
tasks: ["segment" "pose" "detect"] ;--("obb" "classify" not yet)
modes: ["predict" "track" ];--("benchmark"  "train"  "export" "val" not yet)
models: ["yolo11n-seg.pt" "yolo11n-pose.pt" "yolo11n.pt" "yolov9c-seg.pt" "yolov8n-seg.pt"]
source: ""
task: tasks/1
mode: modes/1
model: rejoin ["models/" models/1]
loadImage:  does [
isFile?: false
tmpFile: request-file
unless none? tmpFile [
canvas1/image: load to-red-file tmpFile
canvas2/image: none
s: split-path tmpFile
imageFile: s/2
source: rejoin ["images/" imageFile]
sb/text: source
isFile?: true
]
]
runYOLO: does [
if isFile? [
canvas2/image: none
clear retStr/text
results: %results.txt
if exists? results [delete results]
prog: rejoin ["yolo " task " " mode " model=" model" " "source=" source]
sb/text: prog
do-events/no-wait
tt:  dt [ret: call/wait/shell/output prog results]
if ret = 0 [
retStr/text: f: read results
f: find f "runs" ;--get directory
s: split f "" ;--get complete directory
destination: rejoin [s/1 "/" imageFile]
canvas2/image: load to-red-file destination]
sb/text: rejoin ["Result: " destination "  in " round/to (tt/3) 0.01 " sec"]
]
]
mainWin: layout [
title "Red and YOLO"
origin margins space margins
base 40x22 snow "Model" 
dp1: drop-down 120 data models select 1
on-change [model: rejoin ["models/" pick face/data face/selected]]
base 40x22 snow "Tasks" 
dp2: drop-down 80 data tasks select 1
on-change [task: pick face/data face/selected]
base 40x22 snow "Mode"  
dp3: drop-down 80 data modes select 1 
on-change [mode: pick face/data face/selected]
button "Load Image" [loadImage]
button "Run YOLO" [runYOLO]
pad 280x0 button 50 "Quit" [quit]
return
canvas1: base iSize
canvas2: base iSize
retStr: area iSize wrap
return
sb: field 645
]
view mainWin


Nice job YOLO.





dimanche 9 mars 2025

Septimus: another real-world application with Red

My medical colleagues in the R2P2 unit are not all experienced developers. They've got other things to do, like saving lives. And they want immediate answers to their clinical questions. And they need easy-to-use tools.

That's why I like to use Red (or Rebol3) to develop tailor-made applications for my colleagues. No complexity (like Python), just Redbol simplicity!

That's what Septimus was designed for. We want to be able to follow the evolution of bacterial infections in our young patients according to the treatment applied. The big idea was to use an infra-red camera to detect points not visible to the naked eye.I've adapted some of redCV's functions to make it an independent module (see flir.red code). Actually, we use Septimus for following patients with acute tibial osteomyelitis.

Septimus is very simple. Once the IR image has been loaded, you can use a rectangle (of variable size or colour) to select the relevant body part in IR image. Then with a single button, you get the hottest point in that area.


Septimus is also a salute to the Blake and Mortimer comic strip, of which I've long been a fan. I think I own every album in the series.


mercredi 1 janvier 2025

Savitsky-Golay Filter

In 1964, A. Savitsky and M.J.E. Golay published an article in Analytical Chemistry describing a simple and effective smoothing technique: “Smoothing and Differentiation of Data by Simplified Least Squares Procedures”.

Their method makes it possible to smooth or derive a time series, with equidistant abscissa values, by a simple convolution with a series of coefficients corresponding to the degree of the chosen polynomial interpolation and to the desired operation: simple smoothing or derivation up to 5th order.

The convolution is performed by n multiplications, followed by the sum of the products and completed by dividing by the corresponding norm. The coefficients and norms are provided in the article. Savitzky and Golay's article is accompanied by 11 tables of coefficients suitable for smoothing or determining of the first 5 derivatives; convolutions are performed for different degrees of polynomials and over ranges from 5 to 25 points. The tables published by Savitzky and Golay contain different typo errors. They were corrected by J. Steiner, Y. Termonia and J. Deltour in 1972.

I really like this filter, as it preserves signal dynamics and effectively filters out background noise. We've used this technique a lot in recent years at R2P2 (https://uniter2p2.fr) to process  videos (of babies) who were shaking. This prevented our neural networks from correctly identifying the baby's body joints.  With this type of filter, everything is back to normal. The video images did not shake and the detection algorithms became perfect (see Taleb, A.,  Rambaud, P., Diop, S., Fauches, R., Tomasik, J., Jouen, F., Bergounioux, J.  "Spinal Muscular Amyotrophy detection using computer vision and artificial intelligence." in JAMA Pediatrics, Published online March 4, 2024.).

The main advantage of this process is that it's rather easy to program, allowing direct access to derivative values. On the other hand, abscissa values must be equidistant, and extreme points are ignored.  

You can find the filter code for Red: 

 (https://github.com/ldci/redCV/blob/master/samples/signal_processing/sgFilter.red

And for Rebol 3 here:

https://github.com/ldci/R3_tests/blob/main/signalProcessing/sgFilter.r3



Here's the SGFiltering function in Rebol 3. 


With Red, it's similar, but I used a Red/System routine to speed up the calculations. I'm not sure that rcvSGFiltering routine is any faster than code written in Red, as Red has come a long way since I wrote this routine.
You can find here:
https://github.com/ldci/Red_KIS/blob/main/Signal_Processing/sgFilter2.red a 100% Red pure version without Routines and Red/System.

These examples work with time series, but can easily be adapted to image processing. After all, an image is just a long vector of RGB values!

References:
A. Savitzky, M.J.E. Golay, ANAL. CHEM., 36,1627 (1964)
J. Steiner, Y. Termonia,  J. Deltour, ANAL. CHEM., 44,1909 (1972)