this code here trying apply a filter which i create to an video frames the video from my webcam the problem is when i used functionVideoCapture()
then read()
i get nothing and when i apply isOpened()
it return True so i can not understand read()
does not return true or the frame continuously so i could apply the filter on it
import numpy as np
import cv2
load_from_disk = False
#if load_from_disk:
#penval = np.load('penval.npy')
capp = cv2.VideoCapture(0)
capp.set(3,1280)
capp.set(4,720)
# Creating A 5x5 kernel for morphological operations
kernel = np.ones((5,5),np.uint8)
while(capp.isOpened()):
ret ,frame = capp.read()
if not ret:
print("could not read the frame")
break
frame = cv2.flip(frame,1)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# If you're reading from memory then load the upper and lower ranges from there
if load_from_disk:
lower_range = penval[0]
upper_range = penval[1]
else:
lower_range = np.array([26,80,147])
upper_range = np.array([81,255,255])
mask = cv2.inRange(hsv, lower_range, upper_range)
mask = cv2.erode(mask, kernel, iterations = 1)
mask = cv2.dilate(mask, kernel, iterations = 2)
res = cv2.bitwise_and(frame,frame, mask= mask)
mask_3 = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
stacked = np.hstack((mask_3,frame,res))
cv2.imshow('Trackbars',cv2.resize(stacked,None,fx=0.4,fy=0.4))
k = cv2.waitKey(10)& 0xff
if k == 27:
break
cv2.destroyAllWindows()
capp.release()
so penval.npy is file i made to store values from another script but her i deactivate it until the problem solved requirement: opencv = 4.2.0 ubuntu 20.04 python 3.7.7