I have written a code to enable capturing images from webcam feed using OpenCV. However there is an input delay whenever I press the key to capture my frame. There is no delay when I use it to quit, but there a significant delay when I use capture. I measured this by printing a statement inside both the cases, on pressing c the statement takes a delay before printing. The problem seems to me something like...the camera resources are being used and not freed up in time for the next key press or something like that....but not sure.
import cv2 as cv
import numpy as np
import glob
import matplotlib.pyplot as plt
cap = cv.VideoCapture(1)
img_counter = 0
while True:
ret, frame = cap.read()
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# cv.imshow('frame',frame)
cv.imshow('gray', gray)
if not ret:
break
if cv.waitKey(1) & 0xFF == ord('q'):
print('helloq')
break
elif cv.waitKey(1) & 0xFF == ord('c'):
print('hello{}'.format(img_counter))
img_name = "opencv_frame_{}.png".format(img_counter)
cv.imwrite(img_name, gray)
img_counter += 1
I am using an external web camera and
cv2.__version__ = 3.4.2`
Solved your issue, it seems like its caused by your key check.
You should not call waitKey(1) more than once. It causes lag.
Try this solution:
cap = cv.VideoCapture(0)
img_counter = 0
while True:
ret, frame = cap.read()
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# cv.imshow('frame',frame)
cv.imshow('gray', gray)
if not ret:
break
key = cv.waitKey(1)
if key==ord('c'):
print('img{}'.format(img_counter))
img_name = "opencv_frame_{}.png".format(img_counter)
cv.imwrite(img_name, gray)
img_counter += 1
print("Succesfully saved!")
if key==ord('q'):
print('Closing cam...')
break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
Related
I am beginner in CV. My camera crash every time.
I mean camera light is ON but the there is no camera display
Could someone help me?
Here is the code
import cv2 as cv
import numpy as np
cap=cv.VideoCapture(0)
# read webcam untill the end
while (cap.isOpened()):
# capture frame by frame
ret,frame=cap.read()
if ret == True:
# to display frame
cv.imshow("Frame",frame)
else:
break
cv.waitKey(1)
cap.release()
cv.destroyAllWindows()
i think you put waitKey in wrong place, I hope below code will work !
import cv2 as cv
cap = cv.VideoCapture(0)
# read webcam untill the end
while (cap.isOpened()):
# capture frame by frame
ret, frame = cap.read()
if ret == True:
# to display frame
cv.imshow("Frame", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv.destroyAllWindows()
#tahir mehmood. I modified your code, and I tested it. Worked well.
import cv2 as cv
import numpy as np
cap=cv.VideoCapture(0)
# read webcam untill the end
while (cap.isOpened()):
# capture frame by frame
ret,frame=cap.read()
if not ret:
break
cv.imshow("Frame",frame)
cv.waitKey(1)
cap.release()
cv.destroyAllWindows()
This my codes :
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
I write the code like this and try to open my camera, but the camera image looks like the screenshot.
Stop working screen shot
Any idea ? What is the reason?
It's indentation problem.
Try this code:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Provide proper indentation so the program works fine.
Press "q" to stop the program.
I'm simply trying to get the image of my webcam in python with OpenCV 4.2.0 (on Spyder - python 3.7 running on windows 10). I just copy-pasted the code of the OpenCV documentation (see below) and most of the time it prints: "Can't receive frame (stream end?). Exiting ..."
So I know the error comes from: ret, frame = cap.read(). But I don't know how to fix this.
Sometimes it prints: "Cannot open camera" and then it exits the Spyder Console and starts a new one
And sometimes it just works great...
Can anyone help?
Here is my code:
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
if not cap.isOpened():
print("Cannot open camera")
exit()
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# if frame is read correctly ret is True
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
# Our operations on the frame come here
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# Display the resulting frame
cv.imshow('frame', gray)
if cv.waitKey(1) == ord('q'):
break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
Added the last line to close the window
I had a similar issue (now for a while). I run Linux, not Windows. I read video feeds from multiple IP cameras, not just one webcam. But I believe the solution would be the same. In my opinion, it looks as if the feed, or OpenCv stops supplying frames. So I simply recreate cap, test ret, and move on. This will create a endless loop, it will never stop. In my scenario this correct. I want to real all frames, all the time.
It will look something like:
import numpy as np
import cv2 as cv
cap = cv.VideoCapture('rtsp://1.2.3.4:554//Streaming/Channels/1')
if not cap.isOpened():
print("Cannot open camera")
exit()
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# if frame is read correctly ret is True
while ret == False:
print("Can't receive frame. Retrying ...")
cap.release()
cap = cv.VideoCapture('rtsp://1.2.3.4:554//Streaming/Channels/1')
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
print('frame')
# Display the resulting frame
cv.imshow('frame', gray)
if cv.waitKey(1) == ord('q'):
break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)# In place of zero we gonna use path of the video file.
while cap.isOpened():
# Capture frame-by-frame
ret, frame = cap.read()
# if frame is read correctly ret is True
if ret:
# Our operations on the frame come here
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# Display the resulting frame
cv.imshow('frame', gray)
if cv.waitKey(1) == ord('q'):
break
else:
print("camer not streaming")
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
Here is the code for reading video file from webcam using opencv website. I just want to process the frame every second.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
How should I modify the code:
You should just make your process wait for one second before each read:
import numpy as np
import cv2
import time
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
start_time = time.time()
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
time.sleep(1.0 - time.time() + start_time) # Sleep for 1 second minus elapsed time
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
python newbie here I have following code which I'm using to capture a picture using opencv. It captures the picture when I press q key on keyboard.
Working fine so far.
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', rgb)
if cv2.waitKey(1) & 0xFF == ord('q'):
out = cv2.imwrite('capture.jpg', frame)
break
cap.release()
cv2.destroyAllWindows()
I need it to capture the picture when I give a command (like 'Capture now'). Can anyone help me how to capture a frame when user gives the written command rather than by pressing the key. Thanks
You can write like
reqCommand = 'Capture_pic'
command = input('Enter command')
if command == reqCommand:
out = cv2.imwrite('capture.jpg', frame)
Update:
This update is to make it enable to not block the execution of the program
import cv2
import threading
command = None
def process():
while True:
command = input('Enter command')
thread = threading.Thread(target=process)
thread.start()
cap = cv2.VideoCapture(0)
reqCommand = 'Capture_pic'
while(True):
ret, frame = cap.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', rgb)
if command == reqCommand:
out = cv2.imwrite('capture.jpg', frame)
thread.terminate()
break
cap.release()
cv2.destroyAllWindows()