MemoryError when read wave frames

Discussion related to "under the hood" OpenMV topics.
Falong
Posts: 8
Joined: Fri Jun 01, 2018 2:28 am

MemoryError when read wave frames

Postby Falong » Tue Jun 05, 2018 11:04 pm

Hi,
when using openMV DAC to play wav file, MemoryError exception is raised in chunk.py when readframes function in wave.py module is called.
I got the chunk.py and wave.py and test wav file from below article:

https://docs.micropython.org/en/latest/ ... _skin.html

the tested wave file is near 9KB, 8-bit, 16KHZ.

def play(filename):
f = wave.open(filename, 'r')
total_frames = f.getnframes()
framerate = f.getframerate()

for position in range(0, total_frames, framerate):
f.setpos(position)
dac.write_timed(f.readframes(framerate), framerate)
delay(1000)

Is this MemoryError due to limited heap size? how to check how much heap available? the tested wav file is less than 9kB.
is there anyone succeed in play wav file with openMV?
Thanks,
-Falong
User avatar
kwagyeman
Posts: 2192
Joined: Sun May 24, 2015 2:10 pm

Re: MemoryError when read wave frames

Postby kwagyeman » Tue Jun 05, 2018 11:53 pm

Hi, I see no reason why this doesn't work. However, we've never tested this.

Can you give use the exact line of code that error happened on?
Nyamekye,
Falong
Posts: 8
Joined: Fri Jun 01, 2018 2:28 am

Re: MemoryError when read wave frames

Postby Falong » Wed Jun 06, 2018 7:22 am

Please see the attached screenshot.
Thanks,
Attachments
exception.png
exception.png (35.23 KiB) Viewed 284 times
-Falong
User avatar
iabdalkader
Posts: 524
Joined: Sun May 24, 2015 3:53 pm

Re: MemoryError when read wave frames

Postby iabdalkader » Wed Jun 06, 2018 11:49 am

Falong wrote:
Tue Jun 05, 2018 11:04 pm
Is this MemoryError due to limited heap size? how to check how much heap available? the tested wav file is less than 9kB.
import gc

print("Free: %dKBs"%(gc.mem_free()//1024))

Note memory could be fragmented.

Please share the whole script(s)+wave file(s) in a .zip and I'll test them.
Falong
Posts: 8
Joined: Fri Jun 01, 2018 2:28 am

Re: MemoryError when read wave frames

Postby Falong » Thu Jun 07, 2018 8:21 pm

Can you please leave me your mail address, I'll mail you the script.
Also find when wav file is very small(tested with 1KB), it is ok.
Thanks,
-Falong
User avatar
kwagyeman
Posts: 2192
Joined: Sun May 24, 2015 2:10 pm

Re: MemoryError when read wave frames

Postby kwagyeman » Thu Jun 07, 2018 8:46 pm

OpenMV at OpenMV . io
Nyamekye,
User avatar
iabdalkader
Posts: 524
Joined: Sun May 24, 2015 3:53 pm

Re: MemoryError when read wave frames

Postby iabdalkader » Sat Jun 09, 2018 4:14 pm

This is just a fragmented memory issue, the scripts run fine on H7. I think most of memory is consumed by just loading/parsing/compiling the modules wave and chunk.

This seems to fix it:

Code: Select all

            frames = f.readframes(frame_rate)
            self.dac.write_timed(frames, frame_rate)
            del frames
            delay(1000)
            gc.collect()
User avatar
kwagyeman
Posts: 2192
Joined: Sun May 24, 2015 2:10 pm

Re: MemoryError when read wave frames

Postby kwagyeman » Sat Jun 09, 2018 5:13 pm

Are you on an M4?
Nyamekye,
Falong
Posts: 8
Joined: Fri Jun 01, 2018 2:28 am

Re: MemoryError when read wave frames

Postby Falong » Mon Jun 11, 2018 10:16 pm

Thanks!
It's on M7.
-Falong
Falong
Posts: 8
Joined: Fri Jun 01, 2018 2:28 am

Re: MemoryError when read wave frames

Postby Falong » Tue Jun 12, 2018 7:01 pm

iabdalkader wrote:
Sat Jun 09, 2018 4:14 pm
This is just a fragmented memory issue, the scripts run fine on H7. I think most of memory is consumed by just loading/parsing/compiling the modules wave and chunk.

This seems to fix it:

Code: Select all

            frames = f.readframes(frame_rate)
            self.dac.write_timed(frames, frame_rate)
            del frames
            delay(1000)
            gc.collect()
Just tested, it seems not fix my issue on M7. do you have an estimate of memory cost of openmv itself excluding framebuffer?

Thanks
-Falong
User avatar
iabdalkader
Posts: 524
Joined: Sun May 24, 2015 3:53 pm

Re: MemoryError when read wave frames

Postby iabdalkader » Tue Jun 12, 2018 8:19 pm

Falong wrote:
Tue Jun 12, 2018 7:01 pm
Just tested, it seems not fix my issue on M7. do you have an estimate of memory cost of openmv itself excluding framebuffer?
Thanks
Hi,

Add a gc.collect at the end of detect_face_image:

Code: Select all

        self.check_head_sideway(rotate_angle)
        gc.collect()
And gc.collect() before reading frames:

Code: Select all

            gc.collect()
            frames = f.readframes(frame_rate)
            self.dac.write_timed(frames, frame_rate)
            del frames
            gc.collect()
            print("Free: %dKBs"%(gc.mem_free()/1024))
Unfortunately MicroPython's GC doesn't do very good defragmentation, reading bigger chunks if possible, will help.

Return to “Technical Discussion”

Who is online

Users browsing this forum: phantom8 and 3 guests