Home Detect timestamp of position of object detected from an mp4 video using OpenCV in Python
Reply: 1

Detect timestamp of position of object detected from an mp4 video using OpenCV in Python

Ram
1#
Ram Published in 2017-12-06 16:09:24Z

I am trying to detect objects from an mp4 video file using OpenCV in Python. I am able to detect the objects in the video.

I would like to get the timestamp of the position at which the object was detected in the video file and write it to a text file.

Here is my code so far:

import cv2

my_cascade = cv2.CascadeClassifier('toy.xml')

def detect(gray, frame):
    toys= my_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in toys:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
        #Logic to write time stamp to file goes here
    return frame 

video_capture = cv2.VideoCapture('home.mp4')
cv2.startWindowThread()
while True: 
    _, frame = video_capture.read() 
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
    canvas = detect(gray, frame) 
    cv2.imshow('Video', canvas) 
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video_capture.release()
cv2.destroyAllWindows()

I tried to use VideoCapture's method get() using property identifier like this:

 video_capture.get(CV_CAP_PROP_POS_MSEC)

but get an error name 'CV_CAP_PROP_POS_MSEC' is not defined

Seems like cv2 does not have this method or property identifier implemented.

Is there any other way in cv2 to implement what I want?

Please help.


SOLVED

This will print timestamp of the position of object everytime it is detected in the video. Here is the working code:

import cv2

my_cascade = cv2.CascadeClassifier('toy.xml')

def detect(gray, frame):
    toys= my_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in toys:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
        print("Toy Detected at: "+str(video_capture.get(cv2.CAP_PROP_POS_MSEC)))
    return frame 

video_capture = cv2.VideoCapture('home.mp4')
cv2.startWindowThread()
while True: 
    _, frame = video_capture.read() 
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
    canvas = detect(gray, frame) 
    cv2.imshow('Video', canvas) 
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video_capture.release()
cv2.destroyAllWindows()
Chris Hill
2#
Chris Hill Reply to 2017-12-08 11:56:33Z

cv2 does have the property implemented, but the full name needs to be used.

This should work

import cv2

my_cascade = cv2.CascadeClassifier('toy.xml')

def detect(gray, frame):
    toys= my_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in toys:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
        #Logic to write time stamp to file goes here
    return frame 

video_capture = cv2.VideoCapture('home.mp4')
cv2.startWindowThread()
while True: 
    _, frame = video_capture.read() 
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
    canvas = detect(gray, frame) 
    print (video_capture.get(cv2.CAP_PROP_POS_MSEC))
    cv2.imshow('Video', canvas) 
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video_capture.release()
cv2.destroyAllWindows()

i.e. you want the property of the instance video_capture not the generic class VideoCapture and the property name is prefixed by the class cv2.. The property name is either CAP_PROP_POS_MSECor CV_CAP_PROP_MSEC - depending on the OpenCV version (see Can't get VideoCapture property as the property identifier are not defined ).

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303401 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO