Tuesday 14 July 2015

Playing a video with OpenCV

Playing a video with OpenCV is almost as easy as displaying a single picture. Th e only newissue we face is that we need some kind of loop to read each frame in sequence; we mayalso need some way to get out of that loop if the movie is too boring.


#include "highgui.h"
int main( int argc, char** argv ) {
    
char *videoName="how-to-read-books.avi";    
cvNamedWindow( "Play Video", CV_WINDOW_AUTOSIZE );


CvCapture* capture = cvCreateFileCapture( videoName );
IplImage* frame;
while(1) {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage(  "Play Video", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow(  "Play Video" );
}




Th e function cvCreateFileCapture() takes as its argument the name of the AVI fi le to be
loaded and then returns a pointer to a CvCapture structure. Th is structure contains all of
the information about the AVI fi le being read, including state information. When created
in this way, the CvCapture structure is initialized to the beginning of the AVI.


CvCapture* capture = cvCreateFileCapture( videoName );

Th e function cvCreateFileCapture() takes as its argument the name of the AVI fi le to be
loaded and then returns a pointer to a CvCapture structure. Th is structure contains all of
the information about the AVI fi le being read, including state information. When created
in this way, the CvCapture structure is initialized to the beginning of the AVI.

frame = cvQueryFrame( capture );

Once inside of the while(1) loop, we begin reading from the AVI fi le. cvQueryFrame()
takes as its argument a pointer to a CvCapture structure. It then grabs the next video
frame into memory (memory that is actually part of the CvCapture structure). A pointer
is returned to that frame. Unlike cvLoadImage, which actually allocates memory for the
image, cvQueryFrame uses memory already allocated in the CvCapture structure. Th us it
will not be necessary (or wise) to call cvReleaseImage() for this “frame” pointer. Instead,
the frame image memory will be freed when the CvCapture structure is released.

c = cvWaitKey(33);
if( c == 27 ) break;

Once we have displayed the frame, we then wait for 33 ms.* If the user hits a key, then c
will be set to the ASCII value of that key; if not, then it will be set to –1. If the user hits
the Esc key (ASCII 27), then we will exit the read loop. Otherwise, 33 ms will pass and
we will just execute the loop again.

It is worth noting that, in this simple example, we are not explicitly controlling
the speed of the video in any intelligent way. We are relying solely on the timer in
cvWaitKey() to pace the loading of frames. In a more sophisticated application it would
be wise to read the actual frame rate from the CvCapture structure (from the AVI) and
behave accordingly!

cvReleaseCapture( &capture );

When we have exited the read loop—because there was no more video data or because
the user hit the Esc key—we can free the memory associated with the CvCapture structure.
Th is will also close any open file handles to the AVI fi le.

Saturday 20 December 2014

Surveillance Robot using Arduino MEGA 2560


Features:


1- Ultrasonic Range Module

2- Motion Detector

3- GPS Tracker

4- Signal Jammer

 5- Camera < To view location through internet > 

6- Grip Tires 

7- Speed Controller Chips <integrated inside >

8- Head LED's < contains BLUE for night view >

9- USB Charging 

10- MEGA Chargeable Batteries


Tuesday 14 October 2014

ANDROID DEVELOPMENT Using Android Studio

Android Studio

The following steps are to be focused while creating a new project.
1. Launch Android Studio and select New Project.
2. Select the form factors your app will run on.
3. Configure you new project.
4. Add an activity to Mobile.
5. Choose options for your new file.

How to create AVD ?
Open AVD Manager. Tools > Andriod 
After opening the AVD Manager , create a new AVD and Set the following
1- AVD Name *whatever doesn't matter
2- Device * Depends on your pc resolution 
3- Target *The API level of your target device
4- CPU * The processing i.e Intel Atom (x86)
5- Keyboard *Check it if you want to use your pc's Keyboard.
6- Skin * Whatever as your taste
7- Memory Options 
  • RAM  *   Depends on your system (PC's RAM) divide it with your pc and avd
  • VM Heap : * Configure it as required .
8- Check Use Host CPU  
Click OK,



Wednesday 9 July 2014

Ultrasonic range module HC-SR04

Hardware Required:

Arduino 
HC-SR04 Ultrasonic Sensor
Blue LED
White LED
560 ohm (Green, Blue, Brown, Gold) Resistors
Breadboard


Connect as Shown Above.
Code:
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance < 4) {  // This is where the LED On/Off happens
    digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
  digitalWrite(led2,LOW);
}
  else {
    digitalWrite(led,LOW);
    digitalWrite(led2,HIGH);
  }
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

Monday 7 July 2014

Stop Watch in JAVA


The following code displays the seconds. Starts from 60 to 1 second.
You can modify the following code as your requirement. The variable delay 1000 means of 1 second..

Code:

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Timer;
import java.util.TimerTask;

public class StopWatch {
static int interval;
static Timer timer;

 public static void main(String[] args) throws UnknownHostException, IOException   {

    System.out.print("Counter : ");
    String secs = "60";

    int delay = 1000;
    int period = 1000;
    timer = new Timer();
    interval = Integer.parseInt(secs);
    System.out.print(" "+secs);
    timer.scheduleAtFixedRate(new TimerTask() {public void run() {System.out.print(" "+setInterval());} }, delay, period);
}

private static final int setInterval() {
   
    if (interval == 2)
        timer.cancel();
    return --interval;
}
}

Ping In JAVA

What is ping ?
If you are having connectivity problems, you can use the ping command to check the destination IP address you want to reach and record the results. The ping command displays whether the destination responded and how long it took to receive a reply. If there is an error in the delivery to the destination, the ping command displays an error message.
You can use the ping command to:
  • Ping your computer (by address, not host name) to determine that TCP/IP is functioning. (Pinging your computer does not verify that your network adapter is functioning.)
  • Ping the local router to determine whether the router is running.
  • Ping beyond your local router.
JAVA Code


import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Ping {
    public static void main(String[] args) throws UnknownHostException, IOException   {
    Scanner in = new Scanner(System.in);
    String ipAddress ;
    System.out.print("Enter Ip to Ping: ");
    ipAddress=in.nextLine();
    InetAddress inet = InetAddress.getByName(ipAddress);
    for(int i=1;i<201;i++){
    System.out.print(i +"- "+ "Sending Ping Request to " + ipAddress);
    System.out.println(inet.isReachable(5000) ? "  Host is reachable" : "  Host is NOT reachable");}}

}

Note: This code will ping the ip address entered by user upto 200 times.

Sunday 6 July 2014

Arduino Run DC Motor

Hardware Required:
1-Small 6V DC Motor 
2-PN2222 Transistor 
3-1N4001 diode 
4-270 Ω Resistor (red, purple, brown stripes) 
5-Breadboard 
6-Arduino Uno R3 
7-Jumper wire pack

Breadboard Layout
When you put together the breadboard, there are two things to look out for.
Firstly, make sure that the transistor is the right way around. The flat side of the transistor
should be on the right-hand side of the breadboard.
Secondly the striped end of the diode should be towards the +5V power line 
The motor can be connected either way around.

Run The Following Code on Arduino IDE:

int motorPin = 3;
void setup()
{
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
while (! Serial);
Serial.println("Speed 0 to 255");
}
void loop()
{
if (Serial.available())
{
int speed = Serial.parseInt();
if (speed >= 0 && speed <= 255)
{
analogWrite(motorPin, speed);
}
}
}