OpenCV - first frame covers second frame - python

I am trying to take 2 photos, but as result I see first one two times. I have tried using two variables for frames and saving them as file. How can I fix the problem?
#!/usr/bin/python
import cv2
import time
cap = cv2.VideoCapture(0)
time.sleep(0.25)
ret1, t1 = cap.read()
print 'ret1:', ret1
cv2.imshow('test',t1)
cv2.waitKey(0)
ret1, t1 = cap.read()
print 'ret1:', ret1
cv2.imshow('test1',t1)
cap.release()
while True:
time.sleep(1)
if cv2.waitKey(1) == 27:
break
cv2.destroyAllWindows()

Related

my camera doesnt turn on while Coding in opencv

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

vedio frame is closing automatically in open cv python

i am trying to stream a live vedio from cctv camera using open cv but vedio frame is showing and closing immediately. kindly help me out
import cv2
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
kernel=None
# This is a test video
cap = cv2.VideoCapture('rtsp://192.168.18.2277:554/user=admin_password=_channel=4_stream=0.sdp?real_stream.sdp')
while (cap.isOpened()):
ret, frame = cap.read()
if not ret:
break
# This function will return a boolean variable telling if someone was present or not, it will also draw boxes if it
# finds someone
ret = cap.set(3, 200)
ret = cap.set(4, 300)
cv2.imshow('frame', frame)
# Calculate the Average FPS
frame_counter += 1
fps = (frame_counter / (time.time() - start_time))
# Exit if q is pressed.
if cv2.waitKey(30) == ord('q'):
break
# Release Capture and destroy windows
cap.release()
cv2.destroyAllWindows()
Instead of using a if and else you can just write cv2.waitKey(1) at the end of your while loop. Hope that fixes the problem
Remove the if not ret: break statement and your code should work properly
I see 3 points to be changed above:
you have not imported time library but you have called the current time function in your algorithm.
start_time is specified but what will be the start time isn't. If it is the time that the program is executed then please define it.
Also you have to first define what is Frame_counter, and cannot directly increment it by 1.
import cv2
#import time function
import time
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
kernel=None
cap = cv2.VideoCapture(0)
#define start_time
start_time = time.time()
#frame counter value
frame_counter = 1
while (cap.isOpened()):
ret, frame = cap.read()
if not ret:
break
ret = cap.set(3, 200)
ret = cap.set(4, 300)
cv2.imshow('frame', frame)
frame_counter += 1
fps = (frame_counter / (time.time() - start_time))
if cv2.waitKey(30) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Please excuse if there are any mistakes, I am also a beginner.

OpenCV input delay in capturing frames

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

Processing frame every second in opencv python

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

How to capture a picture in opencv

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

Categories

Resources