I am just trying a simple OpenCV code test in PyCharm but every time I run the code it just ends. I know it is not an issue with my webcam as I have run the code using Anaconda's command prompt and it works though it does take some time to start up.
The code I am using is:
import cv2
cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
cv2.imshow('Image', img)
cv2.waitKey(1)
I feel that OpenCV is taking time to open up my webcam and so the first few reads are coming back as false and causing the code to end before my webcam can show but I don't know how to fix it. I tried googling around but I was not able to find anything. If anyone knows how I can fix this I would really appreciate your help!
Edit: nevermind I was being dumb. PyCharm was not executing the file I was on! 😅
I don't know if the problem is hardware related but, you can try something like this:
import cv2
cap = cv2.VideoCapture(0)
while True:
try:
success, img = cap.read()
cv2.imshow('Image', img)
cv2.waitKey(1)
except Exception as e:
print("Exception happened: {}".format(e))
continue
This basically tries to execute the code inside of try and if an exception happens it executes the code inside of except. It also informs what the error is. You can also add some sleep using time.sleep(), before trying to read the webcam again.
For time.sleep(), you need to import time.
Hopefully this helps. :)
I think your error can be from the your interpreter.Or you can try kick in your code and run it. Dont use the triangle symbol in the above right corner for the first attemp
If you are running on Mac, the PyCharm usually does not have access to webcam. So, its best you run your application from the terminal when you need to access the webcam.
Related
I am trying to stream live video from an external camera using cv2. I was able to write the simple code to stitch the frames and stream it. But am struggling to find how to change the camera.
I tried to run it after disabling the main webcam from the task manager, but it still did not work.
So, if anyone can help me with some clue regarding the same, that would be a great help.
Cameras are numbered for Windows. You can try a few indeces and check which camera index belongs to the camera you want.
capture = cv2.VideoCapture(index)
I just installed opencv and was following a tutorial and realized that when I type down capture. vscode is not giving me the suggestions for the functions available for the 'capture' pointer. It is, however, showing me sugestions from the cv2 library.
This is what I want to see
but
This is what I am getting
So my question basically is, How can I fix this? I havnet been able to find any solution online. I want to be able to see what functions are available so I can learn and explore the library better.
Here's the complete code, just in case:
import cv2 as cv
capture = cv.VideoCapture('video.mp4')
while True:
isTrue, frame = capture.read() #grab video frame by frame
cv.imshow('Camera', frame)
if cv.waitKey(20) & 0xFF==ord('d'):
break
capture.release()
cv.destroyAllWindows()
Thanks in advance!
This problem has been answered here.
Python Language Server does not support Intesllisense from the .pyd file for now.
Good afternoon, I have a question about capturing a single image with OpenCV because most of the codes I have seen for videocapture get many frames until they stop but I am only interested in one as if we were taking a picture. I attach the code that I have been reviewing and I appreciate your advice.
import cv2
# Opens the Video file
cap= cv2.VideoCapture('C:/New/Videos/Play.mp4')
i=0
while(cap.isOpened()):
ret, frame = cap.read()
if ret == False:
break
cv2.imwrite('kang'+str(i)+'.jpg',frame)
i+=1
cap.release()
cv2.destroyAllWindows()
You should be able to use a function through pyautogui like screenshot. In my opinion this is easier. PYAutoGUI has some of OPEN-CV built in as well.
Example:
import pyautogui
screenshot = pyautogui.screenshot()
screenshot.save(r'currentscreenshot.jpg')
Hopefully this helps! (Edit you may or may not have to install pillow, if so run pip install pillow.)
I'm using wsl2 and VScode as the editor. The code in question is simply:
image = cv2.imread('sample.png')
cv2.imshow('image', image)
cv2.waitKey(0)
The first run goes smoothly and lets me inspect the image until I press a button. However after the first run the picture shows up for a quarter of a sec and then disappears. Any idea what could be causing this?
However after the first run the picture shows up for a quarter of a sec and then disappears.
This appears to be a problem triggered by the first run. Could it be that you'll need to add cv2.destroyAllWindows() to the end of your code?
import cv2
image = cv2.imread('sample.png')
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
I don't know the problem but a workaround worked for me.
I don't press any key to close the OpenCV window. I kill the terminal using the 🗑️ button in the VSCode terminal.
Please consider if even 1 time you press any key to close the OpenCV window, then you have to restart your WSL. So, from the beginning, just kill the terminal instead of pressing any key.
But this is just a temporary workaround. I hope other people can help to find the root cause.
I had the same issue and was able to fix it by updating my WSL version here:
aka.ms/wslstorepage
I am new to the opencv(3.0.0)-python(2.7).
There is an error with loading the video on a windows machine with cv2.Videocapture. I actually tried to use haar classifier using the video, but since the video file wasn't playing I tried the following simple code.
import cv2
cap = cv2.VideoCapture('C:\\Users\\jimmy\\Desktop\\bbd.mp4')
if cap.isOpened():
print 'Yes'
else:
print 'No'
And it keeps returning 'No'.
I don't think it's the problem of directory because it worked perfectly with images and imshow, imread functions. So, these kinds of errors always happen when I try to load a video.
I actually used raspberrypi to load a video before and it worked perfectly, so I am wondering is there any difference between rpi and windows machine that I don't know. Also please tell me how to fix it.
Thanks in advance !
Change your path from this:
cap = cv2.VideoCapture('C:\\Users\\jimmy\\Desktop\\bbd.mp4')
to this
cap = cv2.VideoCapture('C:/Users/jimmy/Desktop/bbd.mp4')
OpenCV doesn't like the forward slashes for some reason