Welcome to moblabpy’s documentation!¶
What is Moblabpy¶
Moblabpy is a software toolbox designed to emulate the working of a communication channel and enable the conducting of communication related experiments in associated courses, System View of Communications: from Signals to Packets or Error Control Coding and Information Theory. Moblabpy gives users the ability to do experiments without dedicated hardware and software license, but with their laptop (or desktop) computers and mobile phones anywhere at any time. This toolbox is built upon Python as a module package. Python 3.9 and the Moblabpy module are required on the computer. “IP Camera Lite” for iPhones and iOS devices, or “IP Camera” for Android devices are also required (Both developed by ShenYao). Moblabpy acts as a control panel of a visible light communication system. The computer display acts as a transmitter and the phone camera as a receiver. Users can experiment the effect of noise and error correcting codes have in communication.
Get Started¶
PYTHON AND MOBLABPY INSTALLATION GUIDE¶
Installing and managing packages in Python is complicated, there are a number of alternative solutions for most tasks. This guide tries to give the reader a sense of the best (or most popular) solutions, and give clear recommendations. It focuses on users of Python and the Moblabpy on common operating systems and hardware.
PYTHON AND MOBLABPY INSTALLATION (WINDOWS USERS)¶
Check if Python is already installed (You can type python –version on cmd). If you have already installed Python, you can skip the next two steps, otherwise, please follow the next step to install Python.
Install Python here and select the most recent version.
Follow the instruction of the installer and finish installation. Make sure to tick the option Add Python 3.x (the version that you download) to PATH at the start of the installer window.
After the installation finished, press
Win
key, type cmd and hitEnter
.Type
pip install moblabpy
in the cmd to install Moblabpy.
PIP IS NOT RECONGNIZED AS AN INTERNAL OR EXTERNAL COMMAND¶
If you see the pip is not recognized as an internal or external command
when you type pip install moblabpy
in the cmd. It means that you forgot to add Python in PATH during the installation of Python. You can add it
by the following step:
Press
Win + R
, type%AppData%
and hitEnter
Back out one directory from Roaming
Navigate to
Local/Programs/Python/Python39/Scripts
Copy the file path by selecting the whole directory in the address bar
Press
Win
, type Control Panel and hitEnter
Navigate to System and Security → System → Advanced System Setting → Environment Variables
Select Path in the top box and Edit
Select New and paste the file path copied from step 4
PYTHON AND MOBLABPY INSTALLATION (MAC USERS)¶
Same as step 1 to 3 of window users (terminal for mac users)
After the installation finished, press
Cmd
and the space bar simultaneously, typeterminal
and hitEnter
.Type
pip install moblabpy
in the cmd to install Moblabpy.
PIP: COMMAND NOT FOUND¶
If you see the pip: command not found
when you type pip install moblabpy
in the terminal.
It means that you forgot to add Python in PATH during the installation of Python. You can add it
by the following step:
Type
which python3
in the terminal and the path of where python is located will be shownType
echo $PATH
in the terminal and it will show the path of your machine to look for commandNavigate back to home file by typing
cd
Type
nano .bash_profile
in the terminal and hitEnter
Type
PATH="path/of/bin/folder/that/you/install/your/python:${PATH}"
andexport PATH
in the next line and save the changesrestart the terminal and it should be fine now
Moblabpy Documentation¶
-
class
PROPS
¶ A class that contains all the configurations of the Moblabpy module
-
ROW
: int = 4¶ The number of row of color matrix in each frame.
ROW
must be equal toCOL
and they are defined by the square root ofBPS
.
-
INFO_BIT_SIZE
: int = 50¶ The height and width of each color matrix. It is defined by
ROW
divides 200.
-
VID_SOURCE
: str = "./16bps.mp4"¶ The video name of the generated video. It will be set by
set_BPS()
automatically if py:attr:BPS is modified.
-
set_BPS
(bps)¶ Set the
BPS
of the video. It will also change the value ofROW
,COL
,INFO_BIT_SIZE
andVID_SOURCE
automatically.- Parameters
bps (int) – The BPS of the video
-
-
class
MobLabPy
¶ A class that allows users to connect their ip camera with the program to simulate a telecommunication system. It use the python
multiprocessing
modules to reduce the time delay of decoding images and capturing images. The program usesmultiprocessing.Pipe
object to send images between thesend_frame()
andrecv_frame()
.-
__init__
(self, ip_address, bit_mess, props=PROPS)¶ Initialize the member variable of the
MobLabPy
object.
-
start
(self)¶ Start getting frames from the ip camera and send it to the decoding function.
-
-
img_to_bit_seq
(frame, pt1=(), pt2=(), props=PROPS)¶ Convert black and white color to “0” and “1” respectively.
- Parameters
- Returns bit_seq, pt1, pt2
Decoded bit sequence and vertices of the area bounded by qr codes in opposite direction.
- Return type
- Raises
IndexError – If it fails to calculate the mean of the RGB of the color matrix
-
check_orientation
(frame, pt1, pt2)¶ Check the orientation of the image according to the qr codes in the corner.
- Parameters
- Return result
Number of rotation needed to be performed
- Return type
- Raises
IndexError – If it fails to locate the qr codes
-
point_rotation
(pt1, pt2, width, rotation)¶ Perform the rotation of vertices of the color matrix area.
- Parameters
- Type
- Returns pt1, pt2
Vertices of the color matrix area after rotation
- Type
-
find_corner
(frame, pt1=(), pt2=())¶ Find the vertex of the color matrix surrounded by but exclude qr codes.
- Parameters
frame (
numpy.ndarray
) – Imagept1 (tuple(int, int)) – Vertex of the area include qr codes, defaults to ()
- Prarm pt2
Vertex of the area include qr codes opposite to pt1, defaults to ()
- Returns size, pt1, pt2
size of the color matrix area and its vertices in opposite direction
- Return type
-
find_min_and_max
(points, min_pt, max_pt)¶ Find the minimum and maximum xy-points in a list and the provides points.
-
all_zeros_or_ones
(bit_seq)¶ Check if the bit sequence contains only 0 or 1.
- Parameters
bit_seq (str) – The bit sequence needed to be checked
- Returns
“True” if the bit sequence only contains 0 or 1, “False” otherwise
- Return type
- Raises
ValueError – it input contains non numerical values
-
str_to_ascii_seq
(my_str='Apple')¶ Convert each character in a string to its corresponding ascii code.
-
ascii_seq_to_bit_seq
(ascii_arr)¶ Convert a list of ascii code to its binary representation.
-
bit_seq_to_ascii_seq
(bit_arr)¶ Perform byte conversion of an array.
-
ascii_seq_to_str
(ascii_arr)¶ Convert a list of ascii code to its corresponding character.
-
append_zeros
(bit_seq, props=PROPS, is_str=False)¶ Append zeros until the length of the input equals to the products of desired length.
-
generate_img
(dir_name, my_str='Hello World', if_bin=False, props=PROPS)¶ Generate images that contain color matrix of my_str parameter, with black represents 0 and white represents 1, and paste them with the qr codes (finder patterns) together. The generated images will be saved at the dir_name directory.
- Parameters
dir_name (str) – The directory name used to save the image generated
my_str (str, list(int)) – The integer binary array or string which will be convert to color code formats, defaults to “Hello World”
if_bin (bool) – True if my_str is an integer binary array, False otherwise, defaults to False
props (
PROPS
) – thePROPS
object that contains the properties of the program
-
generate_res
()¶ Generate the res folder which contains all the head and end messages, start and end signals and finder patterns.
-
generate_video
(video_name='16bps.mp4', mystr='Hello World', if_bin=False, props=PROPS)¶ Generate a video which contains start and end messages, start and end signals, finder patterns and color matrix of the mystr parameter, with desired fps.
- Parameters
video_name (str) – The name of the generated video in mp4 format
mystr (str, list(int)) – The message that will be converted to color code, defaults to “Hello World”
if_bin (bool) – True if mystr is an integer binary array, False otherwise, defaults to False
props (
PROPS
) – thePROPS
object that contains the properties of the program
-
recv_frame
(fps, is_pilot_bit_found, recver, bit_seq, flag, bit_mess, props=PROPS)¶ Funtion that received frames from the sender function. It does all the manipulation of the captured iamge here, such as detecting start and end signals, checking rotations and decoding color codes back to 0 and 1.
- Parameters
fps (
multiprocessing.Value
) – Amultiprocessing.Value
object which represents the fps of the camerais_pilot_bit_found (
multiprocessing.Value
) – Amultiprocessing.Value
object which is True if the start signal is detected, False otherwiserecver (
multiprocessing.Pipe
) – Amultiprocessing.Pipe
object that received images sent from the sender functionbit_seq (
multiprocessing.Value
) – Amultiprocessing.Value
object whcih is the decoded bit sequence of received imagesflag (
multiprocessing.Value
) – Amultiprocessing.Value
object which is the state of the play video buttonbit_mess (str) – The transmitted bit sequence
props (
PROPS
) – ThePROPS
object that contains the properties of the program
-
vid_player
(vid_source, flag, props=PROPS)¶ A video player will be appeared if the play video button is clicked
- Parameters
vid_source (str) – The name of the video to pe played
flag (
multiprocessing.Value
) – Amultiprocessing.Value
object which is the state of the play video buttonprops (
PROPS
) – ThePROPS
object that contains the properties of the program
-
send_frame
(fps, is_pilot_bit_found, sender, ip_address, props=PROPS)¶ A sender function which gets images from the connected camera and send it to the receiver function.
- Parameters
fps (
multiprocessing.Value
) – Amultiprocessing.Value
object which represents the fps of the camerais_pilot_bit_found (
multiprocessing.Value
) – Amultiprocessing.Value
object which is True if the start signal is detected, False otherwisesender (
multiprocessing.Pipe
) – Amultiprocessing.Pipe
object that send images captured by the connected cameraip_address (str) – IP address of the camera that is going to be connected
props (
PROPS
) – The :py:class:PROPS object that contains the properties of the program
-
encode
(D, G)¶ Perform matrix multiplication on parameter D and G, which is D x G.
- Parameters
G (
numpy.ndarray
) – A generator matrix
- Return C
A 1 x n matrix
- Return type
- Raises
ValueError – If the number of columns in the first matrix not equals to the number of rows in the second matrix
UFuncTypeError – If both parameters are not integer
numpy.ndarray
-
syndrome
(R, H)¶ Calculate the syndrome of the received bit sequence.
- Parameters
H (
numpy.ndarray
) – A parity check matrix
-
class
monitor
¶ -
__init__
(self, master, bit_mess)¶ Defines the monitor window.
- Parameters
master (Tk) – Root window
bit_mess (str) – The bit sequence to be sent
-
update_recv
(self, bit_seq)¶ Update the received and error bits textbox upon call.
- Parameters
bit_seq – The bits that are collected from the receiver
Switch the status of the button to True, which indicates it has been pressed.
Return the status of the button.
- Returns
The status of the button, whether it has been pressed
- Return type
-
close_windows
(self)¶ Destroy the root window.
-
-
class
VidCap
¶ -
__init__
(self, scale, vidsource=0)¶ Defines the video to be played and its porperty.
-
get_frame
(self)¶ Get one frame from the video.
- Returns ret, frame
whether a frame is successfully grabbed; the image from the video, None otherwise.
- Return type
tuple(
bool
,numpy.ndarray
)
-
reset
(self, vidsource=0)¶ Reset the video source and start from the beginning.
-
__del__
(self)¶ Release the video when the window is closed.
-