🛠 Diffuser des images JPEG depuis le Nicla Vision
Objectives
Transform QVGA captures from the Nicla Vision into usable JPEG images: either via embedded software encoding, or (recommended) by streaming raw RGB565 and encoding the JPEG on the host PC.
Hardware
- Arduino Nicla Vision + micro-USB cable
- ArduinoCore-mbed core (Camera library)
- PC with Python (Pillow) or the Processing CameraRawBytesVisualizer
- No hardware JPEG encoder: the board has neither a GPU nor a JPEG peripheral; embedded encoding is a software challenge on the 480 MHz Cortex-M7
Complete code
// Trame RGB565 + en-tete binaire pour que l'hote encode le JPEG
// Hote Python : numpy + Pillow -> Image.frombytes("RGB", (320,240), data)
#include "camera.h"
#include "gc2145.h"
GC2145 galaxyCore;
Camera cam(galaxyCore);
FrameBuffer fb;
const uint8_t MAGIC[4] = {0xAA, 0x55, 0x01, 0x01}; // magic+version
void setup {
Serial.begin(2000000);
if (!cam.begin(CAMERA_R320x240, CAMERA_RGB565, 15)) { // 15 FPS = marge USB
pinMode(LED_BUILTIN, OUTPUT);
while (true) { digitalWrite(LED_BUILTIN, LOW); delay(100);
digitalWrite(LED_BUILTIN, HIGH); delay(100); }
}
}
void loop {
if (cam.grabFrame(fb, 3000) != 0) return;
Serial.write(MAGIC, 4); // synchronisation trame
Serial.write(fb.getBuffer, cam.frameSize); // 320*240*2 = 153600 o
}
On the host side, read 4 bytes of magic, then 153600 bytes, convert RGB565 → RGB888, and use img.save("frame.jpg", quality=85) to obtain a JPEG.
Expected verification
The host script writes ~15 JPEG/s to a folder or serves them on an HTTP port; images are sharp in QVGA. If frames are missing, lower the camera FPS before touching the serial throughput.
Common errors
- 100% embedded JPEG encoding without measurement: a software encoder like TJpgEnc consumes CPU and RAM; prefer PC-side encoding for smooth streaming.
- Frame synchronization loss: without a magic header, a one-byte offset produces shifted images; always resynchronize on MAGIC.
- RGB565 Endianness: the GC2145 outputs RGB565 whose byte order may be inverted (bug #559); if colors appear blue/orange, swap the bytes on the host side.
- Unrealistic baudrate: 2 Mbaud CDC + 153600 bytes/frame ≈ 12 frames/s theoretical maximum; at 30 FPS the buffer overflows — hence 15 FPS.
Other tutorials for this board
Code examples for this board
led_rgb.inoArduinoimu_lsm6dsox.inoArduinotof_distance.inoArduinomicro_pdm.inoArduinocamera_capture.inoArduinowifi_scan.inoArduino