Sunday, 12 August 2018

OSI Layer : Network and Communication - A Simple Explanation

Open System Interconnection (OSI) Layer is an ISO standard conceptual model used for visualising or troubleshooting computer networks. It has 7 layers namely

1) Physical layer - is where the real bits are transmitted with ethernet cables. Data here is in binary format 0's and 1's. Data will be in the form of electrical pulses if it is coaxial or twisted cables and in the form of light if it is optical fibre cable.

2) Data Link - is responsible to establish reliable links between two directly connecting nodes via switches or bridges. It organises bits into frame and ensures hop to hop delivery. It consists of Media Access Control (MAC) and Logical Link Control (LLC). MAC maintains how devices connected to the network gain access, whereas LLC ensures error checking and packet synchronisation.  Data here is converted from packets to frames.


3) Network - is responsible for routing the information packets from source to destination via the best path available. This is done via router using routing protocols. Source and destination is determined by the ip address of the router. Data segment is converted into packets here.

4) Transport -  is responsible for information transferred between the operating system and the web server. It also decides how much data is sent and received and used for end to end connectivity. It identifies service TCP/UDP, also segmentation segments the data into small data segments, sequencing/reassembling takes care that data is arranged in correct sequence. In case if a data segment is missed in sending to the target node, error correction mechanism ensures that the missing data segment is re-transmitted to target node.

5) Session - maintains sessions/connections between operating system and third party system. It is responsible for establishing, maintaining and terminating sessions. When we visit a url on the web, we interact with application layer, application layer interacts with presentation layer, the session later then interacts with the web server.

6) Presentation - is where operating system interacts with the data. It's responsible for translation, encoding/decoding, compression/decompression and encryption/decryption of data. User interacts with application layer, the application layer interacts with the presentation layer.


7) Application - is where a user interacts directly. It provides networking services to the user using port numbers. For example Web browser (http request on port 80).


An easy way to remember the layer name is

Application    >>         All

Presentation   >>         People

Session           >>         Seem

Transport        >>         To

Network          >>         Need

Data Link       >>          Data

Physical          >>         Processing




Sunday, 5 August 2018

Face Detection with Open CV - Python

OpenCV is one of the most popular library for computer vision. It uses machine learning algorithms for computer vision.

I Installed OpenCV using conda on my Mac with below command

"conda install -c conda-forge opencv"

Face detection algorithms work by reading a block of image and matching against thousands of small patterns and features to detect if it is the desired object. OpenCv uses cascades for this. Cascades are trained with a sample views of a particular objects. Read more about cascades here.

OpenCV cascade makes object detection faster because it checks based on stages. If initial tests are positive, it proceeds with further tests. If the input picture has failed for the test, the algorithm will not test it for all the patterns and features. This makes object detection faster and in real time.

Below is a simple example of face detection in action.

1. First import OpenCV and Sys 

2. Create the OpenCv cascade and initialise with face cascade.
This loads the face cascade into memory. We will use haarcascade_frontalface_default.xml which comes with OpenCV. It is an xml file which contains data to detect faces.

3. Read the input image and convert it into gray scale

4. Call the function detectMultiScale on face cascade to detect face. Pass the desired parameters like scaleFactor, minNeighbors, minSize etc. This function returns a list of rectangles which it believes is a face. It returns the x,y coordinates of the face along with the width and height.

5. Draw a rectangle around the face using the values returned from detectMultiScale function.

The source code looks like below




In order to test this program. Go to terminal and type

Python DetectFace.py <InputImage.png without the angular brackets> 

Make sure that face cascade xml is placed in the same folder as your source .py file. Face cascade xml will be supplied by the OpenCV library. The other option is to fully qualify the path during initialization of the cascade.

The output image I got looks like below




OpenCV is very interesting and promising. I hope to catch up on these in future.