Last active
February 17, 2024 05:54
-
-
Save cbednarski/8450931 to your computer and use it in GitHub Desktop.
Webcam capture for python - Pops up a preview from your webcam and snaps a picture when you press 'q'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
# Windows dependencies | |
# - Python 2.7.6: http://www.python.org/download/ | |
# - OpenCV: http://opencv.org/ | |
# - Numpy -- get numpy from here because the official builds don't support x64: | |
# http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy | |
# Mac Dependencies | |
# - brew install python | |
# - pip install numpy | |
# - brew tap homebrew/science | |
# - brew install opencv | |
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() |
Yes! I did the same thing. But it is much easier to run it with the visual studio code. Because it has access to the Mac native terminal
I just had the same problem and instead of running the script on an iDE I opened up Terminal on Mac and cd 'd into the folder that contained .py file and ran it and it worked
python 3 face-det-cam.py
If you run this for the first time it may ask for a permission. I have not tried it for a 3rd party terminal i.e. iTerm2 but process must be the same
Thanks man it helped a lot
How could you change this to not need the 'q' key pressed to take the photo? I tried doing this but was unsuccessful:
import time
while(True):
ret, frame = cap.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', rgb)
time.sleep(3)
out = cv2.imwrite('capture.jpg', frame)
break
hi, how can I turn off the camera indicator
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just had the same problem and instead of running the script on an iDE I opened up Terminal on Mac and cd 'd into the folder that contained .py file and ran it and it worked
python 3 face-det-cam.py
If you run this for the first time it may ask for a permission. I have not tried it for a 3rd party terminal i.e. iTerm2 but process must be the same