my camera doesnt turn on while Coding in opencv - python

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

Related

Python VideoCapture(0) my camera bad work

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.

Why does OpenCv returns a false ret, frame=cap.read()?

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

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

live frames from webcam

import numpy as np
import cv2
#holder for the camera object
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here (color)
color = cv2.cvtColor(frame, 1)
# Display the frame
cv2.imshow('LiveCam',color)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
'''When everything done, release the capture'''
cap.release()
cv2.destroyAllWindows()
This code will display my own live frames on my own screen.
I want to send my live frames to another computer,and to receive the other's computer own live in order to display it.
Can I get some help please?

error with cv2.destroyAllWindws() on ubuntu 14.04

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

Categories

Resources