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?
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 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?
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()
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 thanks this could be a good PR!
@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 :)
I did this.
@thtrieu great I will try it out!!!
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
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
I already fixed that empty frame error and created a new pull request for that.
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
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
could you tell me which file I should edit to include videowriter details. @Prakash19921206
@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, i have created PR, its tested.
u can save video using that code.waiting for @thtrieu to merge PR.
hello guys, I am getting following error on windows; can anyone help please:
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)
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 RESULTSalternatively, 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
Most helpful comment
Simpler solution, in net/help.py change
to
Then you should be able to use a video file from command line