My problem is when I try to run this code on my Mac, the camera turns on the green light but it doesn't open at all. I have no idea why this is happing. I tried a lot of things but nothing worked for me, I am just thinking the new update from Apple messed up some stuff, because it used to work before.
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_BGR2BGRA)
# 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()
In mac you have to use the Mac Terminal for the cv2 library since currently no other terminal to my knowledge asks for the camera permission.
When I take a webcam picture with python using opencv the picture gets very dark. How do I fix this?
I have tried to make it wait a few seconds after starting the camera to see if that was the problem but that didnĀ“t work either.
import cv2
cam = cv2.VideoCapture(0)
check, frame = cam.read()
cv2.imwrite('image.png', frame)
cam.release()
*Answer en English -.-
I think it's because your webcam have too litle time to take the picture, it needs light. I do this and it works:
import cv2
webcam = cv2.VideoCapture(0)
contador = 0
while True:
check, frame = webcam.read()
cv2.imshow("Captura", frame)
key = cv2.waitKey(1)
if contador==50: #Low contador means low light
cv2.imwrite(filename='image.png', img=frame)
break
contador=contador+1
print(contador)
webcam.release()
cv2.destroyAllWindows()
I am trying to open a video using opencv 3.1 and python 3.6 using an Industrial camera "catchBest" . Its driver is installed and device is listed in device manager. But it is not opening using opencv.
Here's the code:
import cv2
video = cv2.VideoCapture(0)
while True:
cam = video.read()
cv2.imshow("video", cam)
cv2.waitKey(0)
video.release()
cv2.destroyAllWindows()
I tried index from 0 to 9 but its not working.
This should work:
import cv2
#you can run a loop from 0 to 100 to check which gives true, if you're unsure which number to use.
video = cv2.VideoCapture(0)
print(video.isOpened()) #this should return True if camera opens successfully.
while True:
#first returned value of video.read() is boolean and second is frame so we have to use second.
ret,cam = video.read()
cv2.imshow("video",cam)
#you have to break from infinite loop to release the camera usage, here I'm using escape key to break you can choose any.
if cv2.waitKey(1) & 0xFF == 27:
break
video.release()
cv2.destroyAllWindows()
I am following this tutorial from openCV to play back a video on centos .
openCV tutorial
see the code below , it doesn't throw any errors but does not show any videos being played. If I use the image display program it works fine which tells me the X11 forwarding part is working fine .
[root#hadoop1 basic-motion-detection]# more demo1.py
import numpy as np
import cv2
cap = cv2.VideoCapture('/home/admin/example_02.mp4')
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.startWindowThread()
cv2.namedWindow("preview")
cv2.imshow('preview',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
I am using the code below, but I get a black image. Could you please help me rectify the error?
import cv2
import numpy as np
c = cv2.VideoCapture(0)
while(1):
_,f = c.read()
cv2.imshow('e2',f)
if cv2.waitKey(5)==27:
break
cv2.destroyAllWindows()
Update: See github.com/opencv/opencv/pull/11880 and linked conversations, only few backends support -1 as index.
Although this is an old post, this answer can help people who are still facing the same problem. If you have a single webcam but it renders all black, use cv2.VideoCapture(-1). This will get you the working camera.
Just change cv2.waitKey(0) to cv2.waitKey(30) and this issue will be resolved.
I've faced with same problem. Updating neither opencv nor webcam driver works. I am using kaspersky as antivirus. When I disable the kaspersky, then black output problem solved.
BTW, I can see the running .py file in kaspersky console > reports > host intrusion prevention. It reports application privilege control rule triggered - application: myfile.py, result: blocked: access to video capturing devices
Try this:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
This worked for me:
I did a pip install imutils. Imutils is a library with series of convenience functions to make basic image processing functions such as translation, rotation, resizing, skeletonization, displaying Matplotlib images, sorting contours, detecting edges, and much more easier with OpenCV and both Python 2.7 and Python 3.
import cv2
import imutils
cap = cv2.VideoCapture(0) # video capture source camera (Here webcam of laptop)
ret, frame = cap.read() # return a single frame in variable `frame`
while (True):
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
(grabbed, frame) = cap.read()
frame = imutils.resize(frame, width=400)
cv2.imshow('img1', frame) # display the captured image
if cv2.waitKey(1) & 0xFF == ord('q'): # save on pressing 'y'
cv2.imwrite('capture.png', frame)
cv2.destroyAllWindows()
break
cap.release()
Try put -0 on the index and pause any antivirus running
import cv2
import numpy as np
cap = cv2.VideoCapture(-0)
cap.set(3,640)
cap.set(3,480)
while(True):
success, img = cap.read()
cv2.imshow('frame',img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
I faced the same issue after many calls with:
cap = cv2.VideoCapture(0)
and it solved when I changed the index to 1 :
cap = cv2.VideoCapture(1)
In my case just disabling Kaspersy has solved the problem.