1

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

  • Hi Karim, let me see if I understand the problem, you are trying to execute your code but the video does not appear, is that it? If that's the case, it's strange, because apparently everything is ok and your video should appear. Does any message appear on your terminal, when you run it? – ThiagoRTK Jun 08 '20 at 20:44
  • no it does appear any message it does not even go into the while() because it finds isOpend() false and i return the argument of isOpend() before while() it return True so i do not understand what is the problem?! or how i should ask the question – Karim El_gohary Jun 10 '20 at 17:26
  • Well, I tried your code here and the videoCapture is working, isOpened() is True, going inside the while(). Perhaps, a possibility is your webcam is already running by other application? If it’s not that simple. Perhaps the answers to this question will help you (related to ffmpeg codec): https://stackoverflow.com/questions/42210880/python-cv2-videocapture-does-not-work-cap-isopened-returns-false Hope it helps. – ThiagoRTK Jun 10 '20 at 18:34
  • 1
    I solved the problem through updating the anaconda environment and reset the back end of the VideoCapture() and it worked ,thank you for your help and your time without you i was going to leave the whole thing. – Karim El_gohary Jun 10 '20 at 21:29
  • Cool! I'm glad to hear that! – ThiagoRTK Jun 10 '20 at 22:50

0 Answers0