2

I got the video stream from cctv camera using opencv. Now I want to detect a simple motion / object from this video stream. For example, if any person come in any selected zone then rectangular border should be generated around him. I search some opencv tutorial, but I didn't got any proper solution or idea.I used the following code to display video stream.

    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferByte;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;

    import org.opencv.core.Core;
    import org.opencv.core.Mat;
    import org.opencv.highgui.Highgui;
    import org.opencv.highgui.VideoCapture;
    import org.opencv.imgproc.Imgproc;
    import org.opencv.objdetect.CascadeClassifier;

    public class VideoStream
    {
        static BufferedImage tmpImg=null;
        public static void main(String args[]) throws InterruptedException
        {
            System.out.println("opencv start..");

            // Load native library
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

            VideoCapture camView=new VideoCapture();
            camView.open("http://192.168.1.7:80/cgi-bin/view.cgi?chn=6&u=admin&p=");

            if(!camView.isOpened())
            {
                System.out.println("Camera Error..");
            }
            else
            {
                System.out.println("Camera successfully opened");
            }

            videoCamera cam=new videoCamera(camView);

            //Initialize swing components
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            frame.add(cam);
            frame.setSize(1024,768);  
            frame.setVisible(true);

            while(camView.isOpened())
            {
                cam.repaint();
            }
        }
    }

@SuppressWarnings("serial")
        class videoCamera extends JPanel
{
    VideoCapture camera;
    Mat mat=new Mat();


    public videoCamera(VideoCapture cam) 
    {
        camera  = cam;
    }

    public BufferedImage Mat2BufferedImage(Mat m)
    {
        int type = BufferedImage.TYPE_BYTE_GRAY;
        if (m.channels() > 1)
        {
            type = BufferedImage.TYPE_3BYTE_BGR;
        }
        int bufferSize = m.channels() * m.cols() * m.rows();
        byte[] b = new byte[bufferSize];
        m.get(0, 0, b); // get all the pixels
        BufferedImage img = new BufferedImage(m.cols(), m.rows(), type);
        final byte[] targetPixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
        System.arraycopy(b, 0, targetPixels, 0, b.length);
        return img;
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Mat mat = new Mat();

        camera.read(mat);

        BufferedImage image = Mat2BufferedImage(mat);

        g.drawImage(image,0,0,image.getWidth(),image.getHeight(), null);
    } 
}

Can any one knows how could we do that.

Tushar Kulkarni
  • 323
  • 4
  • 19
  • I don't quite get, what you are asking for. Do you have problems with the video stream? Or do you want to know, how to detect the object in the image? In the latter case, we would need some more information on _what_ object you want to detect. – luator May 23 '15 at 10:18
  • I got video stream successfully, but I didn't understand how to detect any object from image. For example people. – Tushar Kulkarni May 23 '15 at 10:21
  • Well, object detection in general is a very hard problem. It is a whole field of active research. For some specific types of objects (e.g. faces), there are some robust methods by now, but this is not the case for all types of objects. So the way _how_ you detect an object strongly depends on _what_ kind of object you want to detect. Aside from very simple objects (like balls or similar), theses methods can be quite complicated, though, so you probably won't find a simple tutorial for this. – luator May 23 '15 at 10:34
  • If you are really interested in this, you can look for scientific publications to get an idea of possible methods, for example on [Google Scholar](http://scholar.google.de/scholar?hl=de&q=human+detection+in+image&btnG=Senden&lr=). But this will be a hard and time-consuming work, especially if you have no previous knowledge on object detection. – luator May 23 '15 at 10:45
  • 1
    Maybe we could help you further if you show us an Image and tell us what are you going to try to detect. Object detection is an extremely large field see: https://xkcd.com/1425/ – David_D May 23 '15 at 11:26
  • can any one give me the idea for simple motion detection – Tushar Kulkarni May 24 '15 at 13:00

1 Answers1

0

please check this code, but i implement it by python

import cv2, time, pandas
from datetime import datetime

first_frame = None
status_list = [None,None]
times = []
df=pandas.DataFrame(columns=["Start","End"])

video = cv2.VideoCapture('rtsp://admin:Paxton10@10.199.27.128:554')

while True:
    check, frame = video.read()
    status = 0
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray,(21,21),0)

    if first_frame is None:
        first_frame=gray
        continue

    delta_frame=cv2.absdiff(first_frame,gray)
    thresh_frame=cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]
    thresh_frame=cv2.dilate(thresh_frame, None, iterations=2)

    (cnts,_)=cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for contour in cnts:
        if cv2.contourArea(contour) < 200000:
            continue
        status=1

        (x, y, w, h)=cv2.boundingRect(contour)
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0,255,0), 3)
    status_list.append(status)

    status_list=status_list[-2:]


    if status_list[-1]==1 and status_list[-2]==0:
        times.append(datetime.now())
    if status_list[-1]==0 and status_list[-2]==1:
        times.append(datetime.now())


    #cv2.imshow("Gray Frame",gray)
    #cv2.imshow("Delta Frame",delta_frame)
    imS = cv2.resize(thresh_frame, (640, 480))
    cv2.imshow("Threshold Frame",imS)

    imS = cv2.resize(frame, (640, 480))
    cv2.imshow("Color Frame",imS)
    #cv2.imshow("Color Frame",frame)

    key=cv2.waitKey(1)

    if key == ord('q'):
        if status == 1:
            times.append(datetime.now())
        break

print(status_list)
print(times)

for i in range(0, len(times), 2):
    df = df.append({"Start": times[i],"End": times[i+1]}, ignore_index=True)

df.to_csv("Times.csv")

video.release()
cv2.destroyAllWindows
Paul Peter
  • 31
  • 3