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()
Related
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 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()
I'm trying to resize frame image-array in original (480,640,3) to (224,224,3), but I use
CV_CAP_PROP_FRAME_WIDTH, CV_CAP_PROP_FRAME_HEIGHT, only change size of frame that displayed on my screen. Thank you very much!
My code here!
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
frame = cv2.flip(frame, 1)
# Display the resulting frame
cv2.imshow('frame',frame)
print(frame.shape)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
you can add following code in your while loop,
frame = cv2.resize(frame, (224, 224))
print(frame.shape)
When I try to run the following 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',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
I receive the message: Floating point exception
Where is my mistake?
I'm programing in python and opencv on Ubuntu but the cv2.destoyAllWindows() doesn't seem to work,as in this example:
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()
when i press 'q' the program end but the window remain on the screen how ever on windows i didn't face such thing
any help I will be thankful