Java -运动/物体检测(人类检测)使用OpenCV



我使用opencv从闭路电视摄像机获得视频流。现在我想从视频流中检测一个简单的运动/对象。例如,如果有人进入任何选定的区域,那么应该在他周围生成矩形边界。我搜索了一些openv教程,但我没有得到任何合适的解决方案或想法。我使用下面的代码来显示视频流。

    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);
    } 
}

有谁知道我们是怎么做到的吗

请检查此代码,但我是通过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

相关内容

  • 没有找到相关文章

最新更新