My code:
import cv2
cap = cv2.VideoCapture(0)
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
while True:
key = cv2.waitKey(0) & 0xFF
ret, frame = cap.read()
cv2.imshow('frame', frame)
if key == ord('q'):
cap.release()
cv2.destroyAllWindows()
break
if key == ord('c'):
cv2.imwrite('capture.jpg', frame)
cap.release()
cv2.destroyAllWindows()
I run this code.
It shows the gray display.
I point the camera at an object and press the 'c' key.
It shows not the object image but the image of what the camera pointed at when I run the code, and saves it.
I point the camera at somewhere else and press 'c' key agein.
It shows the image of the object which it saw at 3. and save it.
The camera lags 1 step. Why?
This could be to do with a lack of cv::waitKey(0) and the window is not getting updated, although this is odd.
Try adding a cv::waitKey command after imshow like this
import cv2
cap = cv2.VideoCapture(0)
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
while True:
key = cv2.waitKey(0) & 0xFF
ret, frame = cap.read()
cv2.imshow('frame', frame)
cv2.waitKey(0)
if key == ord('q'):
cap.release()
cv2.destroyAllWindows()
break
if key == ord('c'):
cv2.imwrite('capture.jpg', frame)
cap.release()
cv2.destroyAllWindows()
I think it might be this as when you do the imwrite you are effectively breaking out of the while loop (albeit slightly) to do something else with opencv.
Related
I am writing a code in python using the OpenCV Python library.
I wanted to access the webcam using the code written, but after executing the code in the terminal, the following error arises again and again.
Code written:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Error:
If this is a camera installed on your computer:
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
If this is a camera connected via USB:
import cv2
cap = cv2.VideoCapture(1)
while True:
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.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 am trying to learn some image processing using openCV and i wrote a basic code but the "frame" windows won't show.I've tried installing on a separate computer but i get the same problem.
import cv2
cap = cv2.VideoCapture(0);
while (True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Based on the code you added your if statement is outside the while loop and therefore can never be checked. It should have thrown a different error however if that was the case. Your camera might be detected but not working. Try using it like a traditional webcam on another program like camera or photobooth to help troubleshoot the issue.
import cv2
cap = cv2.VideoCapture(0);
while (True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
im new to python openCV,found this code from openCV page;
import cv2
cap = cv2.VideoCapture('Megamind.avi')
while (cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('MMR3', gray)
if cv2.waitkey(25) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()*
Tried to run it but it gives error which after done some digging up, i replaced this line:
"cap = cv2.VideoCapture('Megamind.avi')" with this line:
"cap = cv2.VideoCapture('Megamind.avi', cv2.CAP_FFMPEG)"
The program run without error but the video windows("MMR3") was not displayed.
**Im using python 2.7.13 with opencv3 running on MacOs Sierra.
**Megamind.avi is available in the same folder where the code is
your cv2.imshow has to be in the whille loop. If your cv2.imshow() is outside while loop than it will only display the last frame of you video. Change you code to below code
import cv2
cap = cv2.VideoCapture('Megamind.avi')
while (cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('MMR3', gray)
if cv2.waitkey(25) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Since a couple of days I can't open my iSight camera from inside an opencv application any more. cap = cv2.VideoCapture(0) returns, and cap.isOpened() returns true. However, cap.grab() just returns false. Any ideas?
Example Code:
import cv2
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
rval = True
while rval:
rval, frame = vc.read()
cv2.imshow("preview", frame)
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
Mac OS 10.8.5
Python 2.7.5 (but also not working from inside a C++ app)
OpenCV 2.4.6.1
This is how I got the camera working for your code (on OSX 10.6):
import cv2
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
rval, frame = vc.read()
while True:
if frame is not None:
cv2.imshow("preview", frame)
rval, frame = vc.read()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
I had a segmentation fault after I grab an image. It turned out that I used cv2.destroyAllWindows() before cap.release(). Below I show working code.
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#do some ops
cap.release()
cv2.imshow("output", output)
cv2.waitKey(0)
cv2.destroyAllWindows()
This code works on El Captain.