pytorch-retinanet icon indicating copy to clipboard operation
pytorch-retinanet copied to clipboard

Inference on a video?

Open aditjha opened this issue 4 years ago • 1 comments

Hello, is it possible to run an inference of the trained model on a .mp4 video??? Has anyone implemented this yet? Also, can the pre-trained model be used to just run a simple inference on an image that can be seen as an output image?

aditjha avatar May 18 '20 05:05 aditjha

@aditjha convert you video to frames and place it in folder use below code for inference.

https://github.com/yhenon/pytorch-retinanet/issues/200#issuecomment-748992020

code to split video to frames

import cv2
vidcap = cv2.VideoCapture('sample2.mp4')
success,image = vidcap.read()
count = 0
while success:
  cv2.imwrite("newvideo/frame%d.jpg" % count, image)     # save frame as JPEG file      
  success,image = vidcap.read()
  #print('Read a new frame: ', success)
  count += 1

code to combine frames to video

import cv2
import numpy as np
import glob
 
img_array = []
count = 0
for filename in glob.glob('results/*.jpg'):
    filename = "results/frame"+str(count)+".jpg"
    img = cv2.imread(filename)
    height, width, layers = img.shape
    size = (width,height)
    img_array.append(img)
    count+=1
 
 
out = cv2.VideoWriter('project.avi',cv2.VideoWriter_fourcc(*'DIVX'), 15, size)
 
for i in range(len(img_array)):
    out.write(img_array[i])
out.release()

ajithvcoder avatar Dec 21 '20 14:12 ajithvcoder