mirror of
https://github.com/espressif/esp-idf.git
synced 2026-09-22 13:01:16 +03:00
feat(jpeg): simplify encode example and add pytest coverage
Embed a 720p BGR raw asset and stream the encoded JPEG over UART so this example no longer depends on SD card setup. Add pytest coverage that reconstructs the JPEG from base64 output and compares it against a checked-in golden image.
This commit is contained in:
@@ -5,25 +5,23 @@
|
||||
|
||||
## Overview
|
||||
|
||||
This example demonstrates how to use the JPEG hardware [encoder](https://docs.espressif.com/projects/esp-idf/en/latest/esp32p4/api-reference/peripherals/jpeg.html) to encode a 1080p picture:
|
||||
This example demonstrates how to use the JPEG hardware [encoder](https://docs.espressif.com/projects/esp-idf/en/latest/esp32p4/api-reference/peripherals/jpeg.html) to encode a 720p raw image.
|
||||
|
||||
This example makes use of the hardware-based JPEG encoder. If you have multiple pictures that need to be decoded, such as *.rgb -> *.jpg, you can use this example to accelerate encoding.
|
||||
The example performs:
|
||||
|
||||
## How to use example
|
||||
- Embedding `main/assets/esp720p.rgb` into the final firmware image
|
||||
- Letting the JPEG encoder read one 1280x720 `bgr24` frame directly from flash
|
||||
- Encoding the frame into JPEG with the hardware encoder
|
||||
- Base64-encoding the resulting JPEG bitstream and printing it with machine-parseable markers
|
||||
- Letting pytest rebuild `jpeg_encode_result.jpeg` and compare it against `golden_output.jpeg`
|
||||
|
||||
### Hardware Required
|
||||
## Hardware Required
|
||||
|
||||
* An Espressif development board based on a chip listed in supported targets
|
||||
* A USB cable for power supply and serial communication
|
||||
* Computer with ESP-IDF installed and configured
|
||||
* The raw picture is the only source that you need to prepare (We have an [esp1080p.rgb](https://github.com/espressif/esp-idf/tree/master/examples/peripherals/jpeg/jpeg_encode/resources/esp1080.rgb) in resources folder, you can also get it from [jpeg_decode](https://github.com/espressif/esp-idf/tree/master/examples/peripherals/jpeg/jpeg_decode) example).
|
||||
* ffmpeg can also be used to produce rgb picture. For example `ffmpeg -i input.jpg -pix_fmt rgb24 output.rgb`
|
||||
Any board based on a supported target can be used, provided it has enough flash to hold the embedded 720p raw asset and the application image. The example defaults are configured for a 4 MB flash layout and PSRAM-enabled builds.
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Before you start build and flash this example, please put the image `esp1080.rgb` in your sdcard.
|
||||
|
||||
Enter `idf.py -p PORT flash monitor` to build, flash and monitor the project.
|
||||
Run `idf.py -p PORT flash monitor` to build, flash and monitor the project.
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
@@ -31,27 +29,37 @@ See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/l
|
||||
|
||||
## Example Output
|
||||
|
||||
```bash
|
||||
I (1114) jpeg.example: Initializing SD card
|
||||
I (1114) gpio: GPIO[43]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
||||
I (1124) gpio: GPIO[44]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
||||
I (1134) gpio: GPIO[39]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
||||
I (1144) gpio: GPIO[40]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
||||
I (1154) gpio: GPIO[41]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
||||
I (1164) gpio: GPIO[42]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
|
||||
I (1414) gpio: GPIO[42]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
||||
Name: SD64G
|
||||
Type: SDHC/SDXC
|
||||
Speed: 40.00 MHz (limit: 40.00 MHz)
|
||||
Size: 60906MB
|
||||
CSD: ver=2, sector_size=512, capacity=124735488 read_bl_len=9
|
||||
SSR: bus_width=4
|
||||
I (1434) jpeg.example: infile_1080p:/sdcard/esp1080.rgb
|
||||
I (5174) jpeg.example: outfile:/sdcard/outjpg.jpg
|
||||
I (5284) jpeg.example: Card unmounted
|
||||
I (5284) main_task: Returned from app_main()
|
||||
```text
|
||||
Loading embedded BGR24 image from flash...
|
||||
Embedded raw image size: 2764800 bytes
|
||||
Encoding BGR24(raw) -> JPEG...
|
||||
Encoded JPEG size: 30795 bytes
|
||||
JPEG_META width=1280 height=720 format=JPEG encoding=base64 size=30795
|
||||
JPEG_BASE64_BEGIN
|
||||
JPEG_BASE64 ...
|
||||
JPEG_BASE64 ...
|
||||
JPEG_BASE64_END
|
||||
JPEG encode demo done.
|
||||
```
|
||||
|
||||
## Pytest Visual Check
|
||||
|
||||
The accompanying `pytest_jpeg_encode.py` script captures the `JPEG_META` and `JPEG_BASE64` output, reconstructs the encoded JPEG, and saves it as:
|
||||
|
||||
- `dut.logdir/jpeg_encode_result.jpeg`
|
||||
|
||||
It also compares the generated JPEG with `golden_output.jpeg`. This turns the example into both a functional regression test and a host-side artifact generator that makes the encoded result easy to inspect.
|
||||
|
||||
## Replacing The Embedded RGB Asset
|
||||
|
||||
If you want to regenerate a compatible raw frame from another input image, one simple workflow is:
|
||||
|
||||
```bash
|
||||
ffmpeg -y -i input.jpg -vf scale=1280:720 -pix_fmt bgr24 -f rawvideo main/assets/esp720p.rgb
|
||||
```
|
||||
|
||||
After replacing the raw asset, rebuild and flash the example. The firmware will emit the encoded JPEG as base64, and pytest will save the reconstructed JPEG artifact automatically. If the new image is intended to become the expected output, update `golden_output.jpeg` as well.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
(For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you as soon as possible.)
|
||||
|
||||
Reference in New Issue
Block a user