# Load jpegs taken by H7+ for post-processing

**URL:** <https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672>\
**Category:** OpenMV Boards\
**Created:** [December 27, 2021, 1:19am UTC](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672 "2021-12-27T01:19:02Z")\
**Posts on this page:** 16\
**Page:** 2

<div class="post-metadata">

**Author:** ![darrask](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@darrask](https://forums.openmv.io/u/darrask)\
**Post date:** [December 31, 2021, 5:04am UTC](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672/21 "2021-12-31T05:04:28Z")

</div>

The squashing of the image loading bug and the new TF code, both of them fixed in the newest firmware by @kwagyeman , make this minimal code run fine with images generated by the board (no need for img.to\_rgb565() anymore):

```auto
#import libraries
import image, os, tf, pyb
#import mobilenet model and labels
mobilenet = "trained.tflite"
labels = [line.rstrip('\n') for line in open("labels.txt")]

#scan jpegs on card
files=os.listdir()
jpegs=[files for files in files if "jpg" in files]

#open and classify each jpeg
for jpeg in jpegs:
    print("Loading:",jpeg)
    img=image.Image(jpeg,copy_to_fb=True)
    #starting classification
    print("classifying image", jpeg, "with tensorflow lite...")
    for obj in tf.classify(mobilenet, img, min_scale=1, scale_mul=0.5, x_overlap=0.5, y_overlap=0.5):
        predictions_list = list(zip(labels, obj.output()))
        print(predictions_list)

```

I will create another thread for loading other images (not generated by board)

---

<div class="post-metadata">

**Author:** ![darrask](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@darrask](https://forums.openmv.io/u/darrask)\
**Post date:** [March 21, 2022, 4:24am UTC](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672/22 "2022-03-21T04:24:07Z")

</div>

Hello,  
it seems that I regularly run into problems when wanting to post-process images with the board.  
I recently wanted to crop a JPEG taken with the board, using this code:

```auto
import sensor, image, time, os, pyb, machine, sys

#x,y,w,h tuple
roi1=(1296,0,648,486)

#combine rois into list
rois=[roi1]

if not "crop" in os.listdir(): os.mkdir("crop")

#scan jpegs on card
files=os.listdir()
jpegs=[files for files in files if "jpg" in files]

#open each jpeg
for jpeg in jpegs:
    print("Loading:",jpeg)
    img=image.Image(jpeg,copy_to_fb=True)

    for roi_temp in rois:
        #check presence of img_s
        try:
            img_s
        except NameError:
            img_s_exists = False
        else:
            img_s_exists = True
        if(not img_s_exists):
            img_s=image.Image(roi_temp[2],roi_temp[3],sensor.RGB565,copy_to_fb=True)
        print("roi temp:",roi_temp)
        img.to_rgb565(roi=roi_temp,copy=img_s)
        img_s.save("crop/"+jpeg+"_"+str(roi_temp[0])+"_"+str(roi_temp[1])+"_"+str(roi_temp[2])+"_"+str(roi_temp[3])+".jpg")

```

However, all I get are blue and yellow banded images:  
 ![269.jpg_0_0_648_486](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/2X/9/9bedf206e83ed8679cf91a08afb38bc26043d95f.jpeg)  
Am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![darrask](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@darrask](https://forums.openmv.io/u/darrask)\
**Post date:** [March 21, 2022, 7:30am UTC](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672/23 "2022-03-21T07:30:03Z")

</div>

Note that this often happens only with the first loaded picture. Sample pictures to reproduce the bug are here:

 ![20 - 321 ms - 10 dB - FNA](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/2X/9/91dfe3d17f52b82eb18c69393f83463159b268cb.jpeg)  
 ![22 - 321 ms - 10 dB - FNA](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/2X/c/c0e339d5bf072f4a562e59a89ed50a0cc9645dd1.jpeg)

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [March 21, 2022, 4:13pm UTC](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672/24 "2022-03-21T16:13:21Z")

</div>

Which firmware are you on? There was an issue with JPEG decoding we only recently fixed.

---

<div class="post-metadata">

**Author:** ![darrask](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@darrask](https://forums.openmv.io/u/darrask)\
**Post date:** [March 21, 2022, 11:42pm UTC](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672/25 "2022-03-21T23:42:05Z")

</div>

I took the pictures with a 4.2.3 firmware board and ran the crop script on the same board directly thereafter.  
I am wondering whether there is a better way to achieve the same intended result: a crop of the original image which replicates the entire image processing pipeline of the H7+. Would that result theoretically be the same as if the original image had been saved as JPEG from the same region with sensor windowing?

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [March 22, 2022, 1:49am UTC](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672/26 "2022-03-22T01:49:20Z")

</div>

Yeah, I implemented support for jpeg cropping but this depends on decompression and compression working. In general… using jpegs is a bad idea for performance. BMP would be better.

---

<div class="post-metadata">

**Author:** ![darrask](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@darrask](https://forums.openmv.io/u/darrask)\
**Post date:** [March 22, 2022, 2:28am UTC](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672/27 "2022-03-22T02:28:44Z")

</div>

So how would you recommend that I crop pictures with the board?

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [March 22, 2022, 2:40pm UTC](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672/28 "2022-03-22T14:40:15Z")

</div>

I’ll try to check this in a bit.

---

<div class="post-metadata">

**Author:** ![darrask](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@darrask](https://forums.openmv.io/u/darrask)\
**Post date:** [March 23, 2022, 1:03am UTC](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672/29 "2022-03-23T01:03:02Z")

</div>

After several more trials, my tests bear out that it is only the first picture that is loaded in the sequence of pictures present on the card (I use a for loop to load them sequentially) that has problems, and this is the range of problems that it can get (a rare few were correctly saved):

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/2X/b/bef704d07472d7d91946cc6e85a4fdf81bf06a79.jpeg)  
It does not matter what picture it is - only that it is the first, because when I duplicated the first picture in the filesystem, it was saved fine the second time it was loaded with a different name.  
The first picture always needs “Warning: JPEG/PNG too big! Trying framebuffer transfer using fallback method!”

---

<div class="post-metadata">

**Author:** ![darrask](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@darrask](https://forums.openmv.io/u/darrask)\
**Post date:** [March 25, 2022, 1:35am UTC](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672/31 "2022-03-25T01:35:41Z")

</div>

The error is slightly different now that I installed firmware 4.3.0:

> <https://github.com/openmv/openmv/issues/1598>
>
> When I need to crop jpegs taken by the H7+, the jpeg decoder fails when running …to\_rgb565 on a roi of the image.
> 
> To reproduce:
> 
> 1. take jpegs with the H7+ board at full resolution, save on SD card root
> 2. try to crop the jpegs with this script:
> 
> \`\`\`python
> \#import libraries
> import sensor, image, time, os, pyb, machine, sys
> 
> roi=(0,719,519,1062)
> 
> if not "crop" in os.listdir(): os.mkdir("crop")
> 
> \#scan jpegs on card
> files=os.listdir()
> jpegs=\[files for files in files if "jpg" in files\]
> 
> \#open each jpeg
> for jpeg in jpegs:
> print("Loading:",jpeg)
> img=image.Image(jpeg,copy\_to\_fb=True)
> 
> #check presence of img\_s
> try:
> img\_s
> except NameError:
> img\_s\_exists = False
> else:
> img\_s\_exists = True
> if(not img\_s\_exists):
> img\_s=image.Image(roi\[2\],roi\[3\],sensor.RGB565,copy\_to\_fb=True)
> print("Created empty image with width:",roi\[2\])
> print("roi:",roi)
> img.to\_rgb565(roi=roi,copy=img\_s)
> img\_s.save("crop/"+jpeg+"\_"+str(roi\[0\])+"\_"+str(roi\[1\])+"\_"+str(roi\[2\])+"\_"+str(roi\[3\])+".jpg")
> \`\`\`
> 
> I expect crops of the images to be saved, however, I get: OSerror: JPEG decoder failed
> Original images: 
> \[originals.zip\](https://github.com/openmv/openmv-ide/files/8346687/originals.zip)
> 
> Note that this script failed differently, and only on the first loaded image with the fallback frame buffer transfer method with the previous firmware 4.2.3, as described here: https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672/31
> 
> IDE: 2.9.0
> Board: h7+ with 4.3.0 firmware
> Running on Linux

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [March 28, 2022, 12:47am UTC](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672/32 "2022-03-28T00:47:40Z")

</div>

Great!

---

<div class="post-metadata">

**Author:** ![darrask](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@darrask](https://forums.openmv.io/u/darrask)\
**Post date:** [March 28, 2022, 1:08am UTC](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672/33 "2022-03-28T01:08:42Z")

</div>

standing by to test any new firmware 🙂

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [March 28, 2022, 1:25am UTC](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672/34 "2022-03-28T01:25:44Z")

</div>

Finally have some more time to work on OpenMV now.

---

<div class="post-metadata">

**Author:** ![darrask](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@darrask](https://forums.openmv.io/u/darrask)\
**Post date:** [March 30, 2022, 10:47am UTC](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672/35 "2022-03-30T10:47:30Z")

</div>

Hi there, sorry to be nosy… any workaround so far maybe?

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [March 30, 2022, 4:03pm UTC](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672/36 "2022-03-30T16:03:23Z")

</div>

I’ve been working on something since you’ve been asking consistently. Should be done by the end of the weekend.

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [April 2, 2022, 7:17pm UTC](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672/37 "2022-04-02T19:17:17Z")

</div>

Hardware jpeg is implemented. [imlib: Enable hardware JPEG decoder. by kwagyeman · Pull Request #1604 · openmv/openmv · GitHub](https://github.com/openmv/openmv/pull/1604)

Code is clear and tested. Any issues you have should be taken care of.

[Previous page](https://forums.openmv.io/t/load-jpegs-taken-by-h7-for-post-processing/6672.md?page=1)
