Capture a Single Frame OpenCV-Python - python

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.)

Related

Code editor not displaying suggestions for functions when working with opencv

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.

OpenCV is not loading my webcam in PyCharm

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.

OpenCV streamming ip camera always return False

So far I have used OpenCV for streamming ip camera from Raspberry pi + pi camera. I want to save the video from ip camera with codec H.264 and it didn't work. I find out from this post https://github.com/skvark/opencv-python/issues/100 which told me that only manually built opencv library would support H264 codec.
So i followed this link to manually build opencv https://www.learnopencv.com/install-opencv3-on-ubuntu/ and succeeded. But when I use manually built opencv, I can no longer access my ip camera, the cap.open() always return None. Here is my code:
import cv2
cap = cv2.VideoCapture("http://10.10.1.240:8081/")
while True:
ret, frame = cap.read()
frame2 = cv2.flip(frame, 1)
cv2.imshow("frame2", frame)
key = cv2.waitKey(25)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
In the above code, ret is always False.
I have been stucked in this for 2 days without a real solution and explaination. Any help would be appriciated, thanks!
But when I use manually built opencv, I can no longer access my ip camera, the cap.open() always returns none.
Have you tried, checking if your ip camera is working properly and present on your network after building opencv from source? Did you try to stream from the camera using any media player after installation off opencv from source?.
As far as your code is concerned, try the following format for cv2.VideoCapture
cap = cv2.VideoCapture()
cap.open("rtsp://yourusername:yourpassword#172.16.30.248:555/Streaming/channels/2/")
yourusername-username given to your ip camera.
yourpassword-password for the given user name.
You can try the following as well.
cv2.VideoCapture("rstp://admin:PASSWORD#192.168.1.64/doc/page/previw.asp")
Thanks.
Make sure the ip address of your computer and your camera are in the same subnet mask.

How to load video with opencv-python on Windows machine? (Difference btw Windows / Linux ?)

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

cant find ReleaseCapture in opencv while using python?

I am trying to release a cam capture which has been acquired by the API call
camera = cv.CaptureFromCAM(-1)
How can I release it. There is a function named "ReleaseCapture" but it has no python binding.
Can anybody suggest any alternative?
Thanks a lot
I found a similar answered question here
capture = cv.CaptureFromCAM(0)
frame = cv.QueryFrame(capture)
#some code ...
del(capture)
be carefull of using frame or another image captured from cam after deleting capture
regards

Categories

Resources