3

As described in post title, I'm looking for a way to detect motion/movement on the input stream from CCTV camera (IP/WiFi). Anyone know best way how I can connect to IP video stream and monitor for motion?

PsychoX
  • 1,088
  • 4
  • 21
  • 43
  • Does that mean _any_ camera or the device camera of an android device? And secondly: Are you going to use some library or do want to write motiondetection yourself? – Fildor Oct 02 '12 at 12:06
  • LAN CCTV camera with IP streaming. – PsychoX Oct 02 '12 at 12:07
  • RTSP? And what format? H.264 ? ... Or is this the first part of your question? :) – Fildor Oct 02 '12 at 12:09
  • RTSP (I think), jpeg-n compression, 25fps Pal, 30fps Ntsc, resolution:VGA (640*480), QVGA (320*240), – PsychoX Oct 02 '12 at 12:11
  • Have you already managed to get a stream from the camera? Like through a REST-API or the like? Can you connect via browser? – Fildor Oct 02 '12 at 12:12
  • There is no problem to connect via browser. What I'm looking for are classes or demo source code which is using technology like this... so I can learn how to do app like that. Or maybe android have functions to manage streams like that and/or detect movement comparing (for example) two jpeg files? – PsychoX Oct 02 '12 at 12:18
  • You know there are whole companies doing nothing else than that, don't you? Different Cameras will use different technologies, so there is not really "the one and only" way to do it. Even if you take RTSP ones: different manufacturers will add their pretty bit of extra hacks, you need to make. Motion detection is another story... – Fildor Oct 02 '12 at 12:57
  • I'm not looking for "the one and only" way to do it. Any tip will be appreciated. – PsychoX Oct 02 '12 at 14:11
  • Ok, then. I guess you have at least one Camera for testing? What is it? Maybe I happen to know it. – Fildor Oct 02 '12 at 14:14
  • Hello, I am looking for the same. I want to detect motion while streaming RTSP video. Did you find any way? – Megha Maniar Apr 22 '17 at 07:31

1 Answers1

0

this is the opencv code in python, java is simiar, you need use opencv for the image operation

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