1

I want to use tensorflow MobileNet (https://www.tensorflow.org/hub/tutorials/image_retraining#training) to detect that object in a video. I have tried many ways to put video frame from videocapture opencv into tensorflow image but I still fail to run it. How do we detect that object in a video in tensorflow?

This is the way to detect the category of that object in an image

python label_image.py \
--graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt \
--input_layer=Placeholder \
--output_layer=final_result \
--input_height=224 --input_width=224 \
--image=$HOME/flower_photos/daisy/21652746_cc379e0eea_m.jpg

And I want to apply it on a video like

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret:
        label_image --graph=../tf_files/retrained_graph.pb --image=frame

But I could not put opencv video frame into tensorflow function

Sghwhw
  • 117
  • 1
  • 16
  • Is pre extracting the frames from the target video before feeding them to the classifier an option? – Lafa Sep 27 '18 at 03:50
  • I wish i could run label-image directly on the video too but I dont know how to do that! Do you know how to do it? – Sghwhw Sep 27 '18 at 03:59
  • But I would prefer the pre-extracting way because I can perform other calculation on each frame right after i did object detection – Sghwhw Sep 27 '18 at 04:01

1 Answers1

0

You can extract frames from a video with an approach similar to this one using moviepy:

from moviepy.editor import VideoFileClip

clip = VideoFileClip(video_file)

video_duration = clip.duration

intervals = range(3, int(video_duration), 3) #suposing you want a frame every 3 seconds of the video


for t in intervals:
        clip_file = "frame_num_%d.png"%t
        clip.save_frame(clip_file, t)

Let me know if this helps clarifying things for you.

Lafa
  • 461
  • 1
  • 5
  • 14
  • is it possible to use opencv instead of moviepy? – Sghwhw Sep 27 '18 at 07:03
  • Yes it is, however I never did it myself. Take a look here: https://stackoverflow.com/questions/33311153/python-extracting-and-saving-video-frames – Lafa Sep 27 '18 at 07:10
  • 1
    I can get the video frame and show it. My question is how do I feed the frame into tensorflow function with opencv – Sghwhw Sep 27 '18 at 07:16