VideoCapture so slow in reading video file.
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()
Comments
- 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