from VideoCapture import Device
cam = Device()
cam.setResolution(320, 240)
cam.saveSnapshot('demo.jpg')
I use VideoCapture in Windows Python2.7 .I do not know what`s wrong.How do I solve this problem.
Traceback (most recent call last):
File "D:/ideaProject/python_workspace/webcap/webcap/test/vc.py", line 8, in <module>
cam.setResolution(320, 240)
File "D:\SoftWare\Python27\lib\VideoCapture.py", line 90, in setResolution
self.dev.setresolution(width, height)
vidcap.Error: Cannot set capture resolution.
After I read the VideoCapture.py with a deep look,I find a solution :
from VideoCapture import Device
cam = Device()
cam.getImage(timestamp=0).resize((320, 240)).save('demo.jpg', quality=80)
I can get the right size of the photo.Perhaps I use wrong method.
I have a question, your solution looks more like a workaround. I guess you didn't change your camera's resolution, what your code did is to use your camera to capture an original picture and by using resize() function to change the captured picture's size then. That means, what you are doing looks like you capture a picture first(which doesn't have your desired resolution), then you go to edit that captured picture. If I'm wrong, I appreciate you can point that out cause I'm faced with this problem, too.
Related
I am trying to access my webcam on Pygame to take pictures and save them BUT everytime I run the code:
import pygame.camera
import pygame.image
import sys
pygame.camera.init()
cameras = pygame.camera.list_cameras()
print ("Using camera %s ..." % cameras[0])
webcam = pygame.camera.Camera(cameras[0])
webcam.start()
# grab first frame
img = webcam.get_image()
WIDTH = img.get_width()
HEIGHT = img.get_height()
screen = pygame.display.set_mode( ( WIDTH, HEIGHT ) )
pygame.display.set_caption("pyGame Camera View")
while True :
for e in pygame.event.get() :
if e.type == pygame.QUIT :
sys.exit()
# draw frame
screen.blit(img, (0,0))
pygame.display.flip()
# grab next frame
img = webcam.get_image()
I get this in IDLE:
Traceback (most recent call last):
File "/Users/Victor/Documents/Python Related/Python Code for Class/blah.py", line 5, in <module>
pygame.camera.init()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pygame/camera.py", line 42, in init
from pygame import _camera
ImportError: cannot import name '_camera'
Is there something I am doing wrong or another module I can install on Python that can take pictures through the internal or external webcam of the device and store it or send it where it needs to be?
Thank you
According to the documentation for pygame.camera:
Pygame currently supports only Linux and v4l2 cameras.
Seeing as you're using a Mac and not Linux, this won't work. For the future, it is very helpful to read the documentation for libraries and modules you're using, since common issues such as this are usually addressed.
You can use OpenCV which is a robust tool for image acquisition and manipulation.
OpenCV accepts different languages and python is one of them, so you can just import the module and create a function that takes the picture and returns the image in a variable (or you can store the picture in a file and later read it).
Take a look at this link to get an idea of how to acquire images or videos with openCV 2 (and python 2.7)
https://codeplasma.com/2012/11/02/getting-webcam-images-with-python-and-opencv/
Or if you want to use the latest 3.0-beta version of openCV:
http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html
EDIT:
To install OpenCV on Mac you can follow the following link:
http://docs.opencv.org/2.4/doc/tutorials/introduction/ios_install/ios_install.html#ios-installation
PD: You can also install Linux in your Mac and join the opensource comunity :)
Im learning OpenCV at the moment. The goal of this exercise is to take the 4 corner points of a Pokercard from a laying perspective and warping it flat in front of you.
As you can see I have the coordinates mapped out in pts1/ corners assigned correctly (It seems like that at least, after checking).
After outputting imgWarped it throws the error:
traceback (most recent call last):
File "C:\Users\draco\PycharmProjects\Reader\main.py", line 101, in <module>
imgWarped = cv2.warpPerspective(img,matrix,(width,height))
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-95hbg2jt\opencv\modules\imgproc\src\imgwarp.cpp:3143: error: (-215:Assertion failed) _src.total() > 0 in function 'cv::warpPerspective'
The documentations online did not help me much resolving this problem. What does my error message actually mean and how does it happen? Is there a better practice?
Here is the code:
import cv2
import numpy as np
img = cv2.imread("Resources/cards.jpg")
width,height = 250,350
pts1 = np.float32([[124,161],[189,155],[200,231],[135,245]])
pts2 = np.float32([[0,0],[width,0],[width,height],[0,height]])
matrix = cv2.getPerspectiveTransform(pts1,pts2)
---Here is the error --> imgWarped = cv2.warpPerspective(img,matrix,(width,height))
cv2.imshow("Warped Img", imgWarped)
cv2.waitKey(0)
I have used your code with my own image to regenerate the error, but it worked fine.
You can either post to us your input image, or make sure the point you select is not larger than your image it self, coz I can see that you are hard coding the points
Getting the error bellow in the code
TypeError: 'NoneType' object is not subscriptable
line : crop_img = img[70:300, 70:300]
Can anyone please help me with this?
thanks a lot
img_dofh = cv2.imread("D.png",0)
ret, img = cap.read()
cv2.rectangle(img,(60,60),(300,300),(255,255,2),4) #outer most rectangle
crop_img = img[70:300, 70:300]
crop_img_2 = img[70:300, 70:300]
grey = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
You don't show where your img variable comes from. But somehow it is None instead of containing image data.
Often this happens when you write a function that is supposed to return a valid object for img, but you forget to include a return statement in the function, so it automatically returns None instead.
Check the code that creates img.
UPDATE
Responding to your code posting:
It would be helpful if you could provide a minimal, reproducible example. That might look something like this:
import cv2
cap = cv2.VideoCapture(0)
if cap.isOpened():
ret, img = cap.read()
if img is None:
print("img was not returned.")
else:
crop_img = img[70:300, 70:300]
print(crop_img) # should show an array of image data
Looking at the documentation, it appears that your camera may not have grabbed any frames by the time you reach this point in your code. The documentation says "If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the methods return false and the functions return NULL pointer." I would bet that the .read() function is returning a NULL pointer, which gets converted to None when it is sent back to Python.
Unfortunately, since no one else has your particular camera setup, other people may have trouble reproducing your problem.
The code above works fine on my MacBook Pro, but I have to give Terminal permission to use the camera the first time I try it. Have you tried restarting your terminal app? Does your program have access to the camera?
i'm working with camera, with pygame, i wrote this code:
import pygame.camera
import pygame.image
pygame.camera.init()
list_webCam = pygame.camera.list_cameras()
webcam = pygame.camera.Camera(list_webCam[0],(640,480))
webcam.start()
img = webcam.get_image()
pygame.image.save(img, "photo.jpg")
pygame.camera.quit()
but it returns me this error:
Traceback (most recent call last):
File "C:\Users\Fulvio\Desktop\pygame.Camera.py", line 11, in <module>
pygame.image.save(img, "photo.jpg")
TypeError: must be pygame.Surface, not None
Fors ome reason, you didn't get the expected image. This error means the "img" object is "None" - that is, your call to get_image didn't get the expected result.
As far as I can tell, you are making all the needed calls to get the camera roling, and grabbing an image - so, you'd better do some debugging in there, and check introspect what your webcam object is, and try to call the .get_image() method interactively, observing what it returns.
To do that, you may either paste the relevant lines directly in a Python console, or
put the `import pdb; pdb.set_trace()" statements at the beginning, and proceed through using the Python Debugger. (For such a short snippet, you'd probably be better pasting/typing directly in the interpreter).
If everything is fine in your setup, it may be that the camera may need some "warm-up" time between the start and get_image calls. If using the interactive mode you get your image, try adding an arbitrary delay (say 200ms), after the start call (use pygame.time.delay for that)
Also, as far as I know, pygame can't encode images to "jpg" - you have to save then as "bmp" - or use some other library to handle the pygame.Surface object and save to other image formats.
I just passed from python 3.5 to 2.7 and I was having the same problem you have.
After a quick search I solved it:
get_image want 1 parameter: the pygame suface!
So, you can do this:
import pygame
import pygame.camera
from pygame.locals import *
pygame.camera.init()
cam = pygame.camera.Camera(0,(640,480),"RGB")
cam.start()
img = pygame.Surface((640,480))
cam.get_image(img)
pygame.image.save(img, "img.jpg")
cam.stop()
img = pygame.Surface((640,480))
cam.get_image(img)
I guess you don't need this anymore since it's passed more than 1 year but I hope it can help someone else.
I am running into strange problems with the Python wrapper for OpenCV. I am using the cv2 binding and have been able to do a lot with it but the latest problem is my inability to create a VideoWriter.
When I try to create a video writer using this command:
cv2.VideoWriter('foo.out.mov', cv2.cv.CV_FOURCC('m','p','4','v'), 25, (704, 480), 1)
I get the following error:
error: /builddir/build/BUILD/OpenCV-2.3.1/modules/highgui/src/cap_gstreamer.cpp:483: error: (-210) Gstreamer Opencv backend doesn't support this codec acutally. in function CvVideoWriter_GStreamer::open
When create a VideoCapture I can successfully retrieve frames by using the read method, but any calls to the get method to retrieve parameters such as frame width, frame height, or FOURCC code all return 0.0.
I wanted to get the exact codec from the file I am opening to pass this into VideoWriter, but since this only returns 0.0 I don't know what to do.
Any help would be greatly appreciated.
Try passing -1 as the fourcc parameter. This should pop up a dialog and let you choose a video codec. I use it this way, and works great.
cv2.VideoWriter('foo.out.mov', -1, 25, (704, 480), 1)