Ask Your Question
0

VideoCapture so slow in reading video file.

asked 2018-01-31 09:32:54 -0600

Ezkargon gravatar image

updated 2018-02-04 06:05:49 -0600

Why is it if i read a video file using cv2.VideoCapture('video.avi') the fps is very very very low? I want the fps to be the same as when i play the video using a video player(30fps). What changes should i make to achieve this? Btw, i am using a raspberry pi 3 with python.

import cv2 import numpy as np

cap = cv2.VideoCapture('Fchecking.avi')
kernel = np.ones((5,5), np.uint8)

while(1):

    # Take each frame
    ret, frame = cap.read()
    img2gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    ret,mask = cv2.threshold(img2gray,140,255,cv2.THRESH_BINARY) 
    w3w = cv2.adaptiveThreshold(mask,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,47,2)   
    cv2.imshow("mask", mask)

    mask_inv = cv2.bitwise_not(w3w) 

    img2_fg = cv2.bitwise_and(frame, frame, mask=mask_inv)

    hsv = cv2.cvtColor(img2_fg, cv2.COLOR_BGR2HSV)

    lower_red= np.array([0,58,130])
    upper_red = np.array([255,255,255])

    erosion = cv2.erode(mask,kernel,iterations = 3)
    rmask = cv2.inRange(hsv, lower_red, upper_red)


    mask2 = cv2.morphologyEx(rmask, cv2.MORPH_CLOSE, kernel)
    final = cv2.bitwise_and(frame, frame, mask=mask2)
    cv2.imshow('final',final)
    cv2.imshow('original',frame)


    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

2
  • your raspi is slow.
  • that's a lot of operations (and your video player does not do any ofthem, so you compare apples to pears)
  • i don't see any profiling code here
  • what is the resolution of that video ? cutting by 2 will make it process only 1/4 of the pixels
berak gravatar imageberak ( 2018-01-31 09:37:27 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-02-04 05:07:33 -0600

supra56 gravatar image

updated 2018-02-04 09:58:32 -0600

I tested your code with my raspi 3 and video. But there is nothing wrong with raspi 3. Edit: Your code is not doing probably.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-01-31 09:32:54 -0600

Seen: 7,034 times

Last updated: Feb 04 '18