# Collect ML sample images through serial

**URL:** <https://forums.openmv.io/t/collect-ml-sample-images-through-serial/853>\
**Category:** OpenMV Boards\
**Created:** [August 6, 2018, 10:44pm UTC](https://forums.openmv.io/t/collect-ml-sample-images-through-serial/853 "2018-08-06T22:44:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pvadam](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@pvadam](https://forums.openmv.io/u/pvadam)\
**Post date:** [August 6, 2018, 10:44pm UTC](https://forums.openmv.io/t/collect-ml-sample-images-through-serial/853/1 "2018-08-06T22:44:35Z")

</div>

Hi,

I am playing with the machine learning capabilities of the camera, therefore, I’d like to collect positive and negative sample images. Saving them on the SD card seemed to be unstable since if the camera was reset or disconnected, the captured data was lost sometimes. I was looking for a way to save the raw image through serial port. **_img.save(‘img.bmp’)_** works fine, but sending it over the serial port and saving on a host machine does not. At the end, the file is different than the one saved on the SD card.

The code on the cam:

```python
...
usb = USB_VCP()
...
img = sensor.snapshot() # Take a picture and return the image.
usb.send(img)

```

The code on the host:

```python
ser = serial.Serial('/dev/ttyACM0') # open serial port
ser.baudrate = 115200

# This is just a simplified version, I am sure the correct section of the data is captured
# since data separators are used.
data = ser.read(38400) # Read data

f = open("img.bmp", 'wb') # open in binary
f.write(data)
f.close()

```

Do you have any idea what’s wrong here?

Thanks,  
Peter

---

<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:** [August 7, 2018, 2:29am UTC](https://forums.openmv.io/t/collect-ml-sample-images-through-serial/853/2 "2018-08-07T02:29:03Z")

</div>

Hi, just use the ImageWriter method. The latest OpenMV IDE release can turn the ImageWriter files to a set of PNG/BMP/JPG image file outputs. You just have to say “\*.jpg” as the target filename and FFMPEG will convert the video to a set of images.

---

<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:** [August 7, 2018, 2:31am UTC](https://forums.openmv.io/t/collect-ml-sample-images-through-serial/853/3 "2018-08-07T02:31:29Z")

</div>

As for your code. The data sent by the OpenMV Cam is not a BMP file but instead the raw image data. Do this instead:

```python
...
usb = USB_VCP()
...
img = sensor.snapshot().compress() # Take a picture and return the image.
usb.send(struct.pack("<L", img.size()))
usb.send(img)

```

```python
ser = serial.Serial('/dev/ttyACM0') # open serial port
ser.baudrate = 115200

# This is just a simplified version, I am sure the correct section of the data is captured
# since data separators are used.
size = struct.unpack("<L", ser.read(4))[0]
data = ser.read(size) # Read data

f = open("img.jpg", 'wb') # open in binary
f.write(data)
f.close()

```

---

<div class="post-metadata">

**Author:** ![pvadam](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@pvadam](https://forums.openmv.io/u/pvadam)\
**Post date:** [August 7, 2018, 2:39pm UTC](https://forums.openmv.io/t/collect-ml-sample-images-through-serial/853/4 "2018-08-07T14:39:01Z")

</div>

Ok, a bit more detail: my goal is to record many images through serial from many cameras. Positive and negative ones. It’s already done btw. I was about to record raw images, but you are right, it’s far better to save compressed images and decompress them before using them in ML.

I do not know yet if I will be able to train a small enough network for the camera to work with. If the CNN network turns out to be too complex for the cam, I will be using the versatile built-in tools of the OpenMV cam manually (no ML involved), but I would like to backtest my code with the same images so they need to be reloaded into the camera.

I am able to save the compressed images via serial, but reusing them with OpenMV IDE is a partial success. I am converting the compressed image back into bitmap, and loading the image. The image shows up in the IDE, but I have some issues with the colors. 🙂 Blue turns out to be orange for some reason.

The compressed image recorded:  
 ![img_cake.jpg](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/1X/458ac94de4a8437fdd828d28f61abf87160250a2.jpeg)  
_The decompressed image: not able to attach in the forum, but generated with the code below._

The image showing up in the IDE:  
 ![img_cake_ide.jpg](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/1X/ea2635273ab6a60fe2c851bd3aba6ac9043c570d.jpeg)  
The way I am converting it back to bitmap:

```python
from PIL import Image

img = Image.open('images/img_cake.jpg')
im_conv = img.convert('RGB')
im_conv.save('images/img_cake.bmp')

```

---

<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:** [August 7, 2018, 4:37pm UTC](https://forums.openmv.io/t/collect-ml-sample-images-through-serial/853/5 "2018-08-07T16:37:22Z")

</div>

Mmm, um, it’s likely the BMP byte order being wrong. My BMP code is old and needs to be updated. Just haven’t got to it yet. Um, save as a PPM file and you won’t have any issues. PPM/PGM are much simpler file formats and we can parse all variants.

---

<div class="post-metadata">

**Author:** ![pvadam](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@pvadam](https://forums.openmv.io/u/pvadam)\
**Post date:** [August 8, 2018, 6:23am UTC](https://forums.openmv.io/t/collect-ml-sample-images-through-serial/853/6 "2018-08-08T06:23:31Z")

</div>

Perfect, it works! Thank you for the great support!
