darkflow icon indicating copy to clipboard operation
darkflow copied to clipboard

Real-Time Detection from Video File

Open loretoparisi opened this issue 9 years ago • 18 comments

As described in Real-Time Detection on a Webcam when combining YOLO + OpenCV it is possibile to read a video stream from file (if OpenCV can read that file) like:

./darknet detector demo cfg/coco.data cfg/yolo.cfg yolo.weights <video file>

Starting from the darkflowcamera demo, is it possible to read from a local video file?

loretoparisi avatar Mar 28 '17 23:03 loretoparisi

Yes it should work..but make sure u have opencv compiled with ffmpeg(version 3.1) , so that it supports wide range of video formats

Prakash19921206 avatar Mar 29 '17 05:03 Prakash19921206

@Prakash19921206 ok thanks. I can see here that darkflow it is using OpenCV VideoCapture on device 0 i.e. the default device for the camera:

camera = cv2.VideoCapture(0)

While darknet, it takes a video file as optional input parameter here so that it's passed to the demo function that will call here the OpenCV capture method from file cvCaptureFromFile(filename);:

if(filename){
        printf("video file: %s\n", filename);
        cap = cvCaptureFromFile(filename);
    }else{
        cap = cvCaptureFromCAM(cam_index);
    }

So how to pass the video file to darkflow?

loretoparisi avatar Mar 29 '17 07:03 loretoparisi

You could try loading the network in your program and pass it video frames. This should help you get started

#from the opencv3.0 docs

from net.build import TFNet
import cv2

cap = cv2.VideoCapture('your_video_file')
model_path = "your_network_cfg.cfg"
weights_path = "your_network_weights.weights"

options = {"model": model, "load": weights_path, "threshold": 0.1, "gpu": 0.3}
tfnet = TFNet(options)
while(cap.isOpened()):
    ret, frame = cap.read()
    result = tfnet.return_predict(frame)
    #DO SOMEHTING WITH RESULTS

cap.release()

Dhruv-Mohan avatar Mar 29 '17 12:03 Dhruv-Mohan

Simpler solution, in net/help.py change

def camera(self, file):     
    camera = cv2.VideoCapture(0)

to

def camera(self, file):     
    camera = cv2.VideoCapture(file)

Then you should be able to use a video file from command line

jcarletgo avatar Mar 29 '17 14:03 jcarletgo

@jcarletgo thanks this could be a good PR!

loretoparisi avatar Mar 29 '17 15:03 loretoparisi

@jcarletgo hey this is exactly my intention when building the system. The thing is I still cannot compile opencv with ffmpeg to test if the code is working or not. Please do a pull request for this :)

thtrieu avatar Mar 29 '17 15:03 thtrieu

I did this.

thtrieu avatar Mar 29 '17 17:03 thtrieu

@thtrieu great I will try it out!!!

loretoparisi avatar Mar 29 '17 17:03 loretoparisi

hey sorry to come back to this closed thread but, there's a problem i encounter after trying out your newly updated --demo videofile. First, It seems at the end of the video, an empty frame is captured and is fed into self.framework.preprocess(frame) which gives an error. I think it should be fixed. Second, what if i wanna save the annotated video, how can i do that? thanks

borasy avatar Mar 30 '17 05:03 borasy

basically if end of Video is reached then nothing will be grabbed from cap.read() to frame. so frame object will be empty. i haven't tried the code, but it should work

from net.build import TFNet
import cv2

cap = cv2.VideoCapture('your_video_file')
model_path = "your_network_cfg.cfg"
weights_path = "your_network_weights.weights"

options = {"model": model, "load": weights_path, "threshold": 0.1, "gpu": 0.3}
tfnet = TFNet(options)
stop =False
while(not(stop)):
    ret, frame = cap.read()
  if(frame.size == 0):  #checking here for empty frame
    cap.release()    
    stop=True    
 result = tfnet.return_predict(frame)
 #DO SOMEHTING WITH RESULTS

alternatively, you can also check for if(frame.rows==0): #or

if(frame.cols==0): #or

if(frame.isEmpty()): #e.t.c

Prakash19921206 avatar Mar 30 '17 06:03 Prakash19921206

I already fixed that empty frame error and created a new pull request for that.

borasy avatar Mar 30 '17 08:03 borasy

Guys, is it possible to store the output video along with bounding box and also the JSON output when an input is from a video file. @Prakash19921206 @borasy @thtrieu

ramarajan09 avatar Apr 01 '17 11:04 ramarajan09

I'm quite busy now, but its very easy to make it. both video & webcam/ip cam inputs can be saved to video (including bounding box, labels ) opencv's VideoWriter class makes it possible there's a small example at opencv's website .. here's a complete example...hopefully someone will create pull request

Prakash19921206 avatar Apr 01 '17 12:04 Prakash19921206

could you tell me which file I should edit to include videowriter details. @Prakash19921206

ramarajan09 avatar Apr 01 '17 12:04 ramarajan09

@Prakash19921206 I did some changes in the python scripts, how do I compile them to test the changes. It would be great if you can provide this input. BTW, im running this on windows machine

ramarajan09 avatar Apr 01 '17 14:04 ramarajan09

@ramarajan09, i have created PR, its tested. u can save video using that code.waiting for @thtrieu to merge PR.

Prakash19921206 avatar Apr 01 '17 17:04 Prakash19921206

hello guys, I am getting following error on windows; can anyone help please:

Process finished with exit code -1073741819 (0xC0000005)

My code is

from darkflow.net.build import TFNet import cv2

model_path = './config/yolo-voc.cfg' weights_path = './weights/yolo-voc.weights'

options = {"model": model_path, "load": weights_path, "threshold": 0.1} tfnet = TFNet(options)

imgcv = cv2.imread("DSC_0016.jpg") result = tfnet.return_predict(imgcv) print(result)

TarunPathak avatar Apr 29 '17 06:04 TarunPathak

basically if end of Video is reached then nothing will be grabbed from cap.read() to frame. so frame object will be empty. i haven't tried the code, but it should work

from net.build import TFNet
import cv2

cap = cv2.VideoCapture('your_video_file')
model_path = "your_network_cfg.cfg"
weights_path = "your_network_weights.weights"

options = {"model": model, "load": weights_path, "threshold": 0.1, "gpu": 0.3}
tfnet = TFNet(options)
stop =False
while(not(stop)):
    ret, frame = cap.read()
  if(frame.size == 0):  #checking here for empty frame
    cap.release()    
    stop=True    
 result = tfnet.return_predict(frame)
 #DO SOMEHTING WITH RESULTS

alternatively, you can also check for if(frame.rows==0): #or

if(frame.cols==0): #or

if(frame.isEmpty()): #e.t.c

if(frame.size == 0) resulted in an error because at the end of the video there is no frame and it is read as nonetype. When i checked, opencv doc suggests to use ret variable to check the end of the video. The gist is:

if ( ret == True ) :
   result = tfnet.return_predict(frame)
   #DO SOMEHTING WITH RESULTS
else :    
    break

Sreerag-ibtl avatar Dec 07 '18 05:12 Sreerag-ibtl