Categories
Arduino Artifical Intelligence Classification Image Classification Machine Learning Object Tracking OpenCV

Getting Started with OpenCV library, Loading Images, and videos.

OpenCV is a cross-platform library for real-time computer vision. It was initially developed by Intel and later released as Open Source product.

I would try to explain the library and create a simple application. Further, I will try to extend the application as an object tracking device.

First thing first – I am using python, specifically python 2.7 and already installed it on my windows laptop. OpenCV requires numpy and matplotlib ( optional) libraries to be present in the python environment. Please make them available.

I have already downloaded the opencv-3.4.3-vc14_vc15.exe from the official website and as soon as I executed the exe file – it is extracting the files to a directory called opencv at the same location. I moved the opencv to root directory c.

The folder C:\opencv\build\python\2.7\x86 has cv2.pyd file, Please copy this to python root python27\Lib\site-packages folder.  With this step installation of opencv2 to python is complete. This can be verified by import cv2 on python prompt.

Opencv documentation here has a quite extensive explanation to handle images. I would try to use one or two of them before diving deeper into the library.

Loading and Saving of Image

One pretty neat example is loading image – There is two version of this example, one without matplotlib and other with matplotlib.

Example 1 – This example is reading and Image and showing. The waitKey and destroyAllWindows are to close the Image windows on pressing esc (27) key. The Image path must be in forward (/) slashes or double backslashes (\\) – otherwise, it may throw an error.

Opencv_image1

Example 2 – This uses matpltlib to show the image using this library. matplotlob gives greater control over the image.

Opencv_image2

The Output of the two examples is the following.

Opencv_image3

Capture, Load and Save Video from Webcam

Capturing live stream from a video camera is most basic operation in the real-time computer vision applications.

cap = Cv2.VideoCapture(0) method encapsulates the camera attached to the computer. 0 is the default camera, it can be -1 or 1 depending on the secondary camera.

cap.read() method returns a captured frame, which can be saved in a file or some other operation like filtering, the transformation of the image can be done.

##########################################################
#VideoCapture1.py - Show Video from a webcam.
##########################################################
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    # Our operations on the frame come here
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    # Display the resulting frame
    cv.imshow('frame',gray)
    if cv.waitKey(1) & 0xFF == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
##########################################################
#VideoCapture2.py - Play A video from a file.
##########################################################
import numpy as np
import cv2 as cv
cap = cv.VideoCapture('C:/opencv/videos/output.avi')
while(cap.isOpened()):
    ret, frame = cap.read()
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    cv.imshow('frame',gray)
    if cv.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv.destroyAllWindows()
##########################################################
#VideoCapture3.py - Save the Video from Webcam.
##########################################################
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv.VideoWriter_fourcc('M','J','P','G')
#fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('C:/opencv/videos/output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
		# Filp The Frame
        # frame = cv.flip(frame,0)
        # write the frame
        out.write(frame)
        cv.imshow('frame',frame)
        if cv.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
# Release everything if job is finished
cap.release()
out.release()
cv.destroyAllWindows()

There are multiple application of capturing and loading images using OpenCV. We can use the image or video captured for face detection, face tracking, identification of abnormality in medical scans, marketing campaigning etc.

Categories
Arduino

Arduino – Open Source Prototyping Board

Arduino is open source hardware platform which allows to control devices using simple easy to understand programs.  The interface is so simple that anyone with little knowledge of electronics and programming can easily connect sensors, motors, servos to create programmable devices.

The hardware is actually a micro controller sitting on a prototyping board with inbuilt power supply, USB interface to connect to modern computers and bunch of input output pins to connect the other devices. The micro controller is low cost, low power 8 bit Atmega328p chip from Atmel corporation. The datasheet for the micro controller can be found here.

There are many version of the device available, the entry level module Arduino UNO is very popular (Below). It has all the components (USB, Power Supply Unit, Micro Chip) integrated on a single board.

Arduino_Uno

The original device can be brought from various online stores. There are many clones available in the market. All of them work pretty similar. Most of the time the device shipped from manufacturers have boot loader burnt into them.  Boot loader is a program which makes this device programmable, i.e. You can burn your own code on top of it and it will execute your code as soon as you power it.

The heart of the Arudino is its IDE (Integrated Development Environment). Which is a java based console with 100s of easy to use library, well documented online guides, millions of helpful communities and users. It makes Arduino a breeze.

Setting up the IDE

You can follow the guide available on Arduino website to install the IDE and USB driver. The IDE looks like below screenshot. It is a typical GUI environment with multiple menus and console to write code.

Arduino_IDE

Once the device is plugged using USB it shown as  COM device in the windows device manager (image below). This is basically a virtual com port, i.e. Physically the device is connected using USB, but the computer sees this as COM device. There is no magic, the UNO board has USB to serial converter chip (shown in the image above), which takes care of USB to serial conversion. The Driver software loaded while running setup helps the chip to communicate with the PC using USB interface.  Sometimes, Driver software is not installed automatically and you have to find the driver for the chip  used in the UNO board and install it manually.

COM_Port_IDE

Running the first Example (Blink)

Step 1: Select the device

After plugging the board and opening the IDE, we have to select the port. Port is channel which the IDE and the device would communicate. In my case it is COM7 as seen in device manager. Most of the times it appears automatically in the port menu. This can be anything from COM0 to COMXX where XX is a number. Select this in your setup.

Step 2: Select the board

Arduino comes in many versions. We have to select the board we are using. In my case “Arduino UNO” is used. So, I selected Tools -> Boards -> Ardunio/Genuino UNO.

Arduino Steps

Step 3 : Select an example ( Blink  )

The IDE has 100s of inbuilt examples, the easiest of these are Blink. The code blinks inbuilt LED. A typical Arduino example has two parts in code, function setup() and the function loop().

The setup function runs once when reset button is pressed or the board is powered on. This is used to initialize all the variables, pin, objects to be used in the subsequent code.

The loop function runs continuously till the board is powered on. It does an operation in loop and repeat  itself. Below is the blink example, its self explanatory. Further you can look at Arduino website here . to understand more about the blink example.

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
delay(1000);                       // wait for a second
digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
delay(1000);                       // wait for a second
}

Step 4: Upload the Code

Once the code is written/copied in the IDE console. You can hit Switch -> Upload ( ctrl + u) to load the code from console to the Arduino board.  The bottom of the IDE would show “Done uploading”. Now the Blink example in loaded into the Board.

Code_Upload

Time to Blink 🙂

Once the code is uploaded to the board, it starts working. The image below shows the blinking inbuilt LED.

Blink

There are many examples and projects which can be done using Arduino as a micro controller. Keep exploring and ask questions if you have any.