liam hogan
liam hogan

Reputation: 13

How to decode an image file received from another radio module (Raspberry Pi 4)?

I am currently using a Raspberry Pi 4 to send and receive data in Python from sensors, DHT22, AS312 etc. My main issue is when I receive an image here is the abstraction (note paths seen here aren't real ones).

  1. Firstly I import these libraries (note memory_mangement is just a bash script I created to track how much space I am using and is not important to my issue).
import time
import serial
import pandas as pd
import numpy as np
import threading
from memory_mangment import sensor_data
  1. I made a class init function
class Transceiver:
    def __init__(self,data):
        self.transceive=serial.Serial(port='/dev/tty50',
                                baudrate=9600,
                                parity=serial.Parity_NONE,
                                stopbits=serial.STOPBITS_ONE,
                                bytesize=serial.EIGHTBITS,
                                timeout=1)
        self.message="Hello world! "
        self.data=data
        self.txt_fname="/path/to/txtfile.txt"
        self.png_fnam="/path/to/pngfile.png"
        self.csv_fname=sensor_data().fname
        self.recived=self.transceive.in_waiting
        self.event=threading.Event()
  1. I then transmit a PNG file as follows:
def Transmit_test_png_file(self):
        """Transmit a PNG file"""
        with open(self.png_fname, 'rb') as f:
            data = f.read()
        self.transceive.write(data)
  1. I then have my receive function:
def Receive_test_png_file(self):
        "Receive a PNG file"""
        self.transceive.attachInterrupt(self.serial_interrupt)
        if self.event.is_set():
            data_read = self.transceive.readlines()

This is my main issue: it will print" [b'\x89PNG\r\n', b'\x1a\n', b'\x00\x0 ... " etc. If you don't have a radio module here is a way to replicate my issue:

import cv2
img_fname=r"/path/to/testfile.png"
with open(img_fname,'rb') as f:
    data=f.readlines()

I'm wondering if I can use cv2.imdecode to decode this. I have looked at some of the flags, just wondering if anyone had any recommendations on flags I can use to decode this or if there's an easier way of doing this. Let me know (note: I know some).

Upvotes: 0

Views: 51

Answers (1)

dda
dda

Reputation: 6213

Unless your PNG image is under ~240 bytes total, it will not be transmitted in full. The maximum number of bytes that can be transmitted in a LoRa packet is 256, and that includes the overhead.

If you want to have a fighting change to get that image, you need to split it in chunks, add a header (xx/yy chunk xx out of yy chunks), and send them one by one, with a pause in between chunks (keep in mind also the fair use rule, usually 1% airtime).

But seriously, LoRa is not suitable for transmission of heavy payloads like images.

Upvotes: 0

Related Questions