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?
Related
I am a complete beginner with coding and needed to code a few lines of code for getting pictures from a USB camera for a research project that i am responsible for. admittingly i took help of a few source codes from a few blogs online, they did show a couple of errors but most of them were resolved except this one that keeps popping up no matter what i do. does anyone have any idea where exactly the problem could be arising from? all the errors in the previous lines of code seem to be dealt with. I really need to get this right as this project decides my bacheklor thesis
the following was the code that i used, even though i am really really dumb with this stuff:
`
#new camera code
import cv2
import imutils
import time
#loop for taking picture and saving
cap = cv2.VideoCapture(0)
frame = cap.read()
def takePicture():
global showimg
(frame) = cap.read()
showimg=frame
return image
cv2.imshow('image', img)
cv2.imshow('img1',showimg) # display the captured image
cv2.waitKey(1)
time.sleep(0.3) # Wait 300 miliseconds
image ='C:/Users/whale/Desktop/REMOVE/capture.png'
cv2.imwrite(image, frame)
img = cv2.imread('no-such-file.jpg', 0)
cap.release()
print(takePicture())
`
There are a few issues with your code:
The takePicture() function is not returning anything. It needs to return showimg instead of image.
The line global showimg (frame) = cap.read() should be changed to global showimg, frame; frame = cap.read().
The line img = cv2.imread('no-such-file.jpg', 0) should be removed as it is not being used and it may cause confusion.
The line print(takePicture()) should be changed to takePicture() as the function already displays the image.
Here's the correct code:
#new camera code
import cv2
import imutils
import time
#loop for taking picture and saving
cap = cv2.VideoCapture(0)
def takePicture():
global showimg, frame
frame = cap.read()
showimg = frame
cv2.imshow('image', showimg) # display the captured image
cv2.waitKey(1)
time.sleep(0.3) # Wait 300 miliseconds
image ='C:/Users/whale/Desktop/REMOVE/capture.png'
cv2.imwrite(image, frame)
return showimg
takePicture()
cap.release()
I'm taking pictures with OpenCV library:
def display_frame(self, frame, dt):
texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
texture.blit_buffer(frame.tobytes(order=None), colorfmt='bgr', bufferfmt='ubyte')
texture.flip_vertical()
self.image.texture = texture
cv2.imwrite('/home/mark/frontend/picture_taken.jpg', self.image)
cam.release()
cv2.destroyAllWindows()
The above throws:
cv2.imwrite('/home/mark/frontend/picture_taken.jpg', self.image)
TypeError: Expected Ptr<cv::UMat> for argument 'img'
I'm not including all parts of the code but the above is giving you idea what kind
of image I'm trying to write on disk. On GitHub I found that this error is usually thrown when we try to write an image that we passed in in a wrong form. In some cases this can
be solved by including img = numpy.array(self.image) right above the line where I write the image. However that didn't work for me.
How this can be fixed ?
Hi this is just a guess but from my own experience i think your image variable is a None type. Can you try and print out if your self.image has any value
This is my first attempt at making a video file and I seem to be very clumsy.
Inspired by these instructions to put several images in a single video, I modified the code by creating a function that can loop through the folder with the images. But it is taking too long. I thought it was because there are many images, but even if I only use two images to do it, it still runs forever.
I get no error message, the script just never stops.
Could anybody please explain what is wrong with my code? There must be something silly which I didn't spot and is making it an infinite loop or something...
import cv2
import os
forexample = "C:/Users/me/Pictures/eg/"
eg = cv2.imread(forexample+'figure.jpg')
height , width , layers = eg.shape
print "ok, got that"
def makeVideo(imgPath, videodir, videoname, width,height):
for img in os.listdir(imgPath):
video = cv2.VideoWriter(videodir+videoname,-1,1,(width,height))
shot = cv2.imread(img)
video.write(shot)
print "one video done"
myexample = makeVideo(forexample,forexample, "example.avi", width, height)
cv2.destroyAllWindows()
myexample.release()
Running on a windows machine, Python 2.7.12, cv2 3.3.0
UPDATE
Eventually created the video using FFmpeg.
When you are running the for-loop, you are creating VideoWriters for every frame with same filename. Therefore it is over-writing the file with the new frame.
So, you have to create the VideoWriter object before entering the for-loop.
But doing that will not make your code working. There are some other errors due to misuse of commands.
First, os.listdir(path) will return list of filenames but not filepaths. Therefore you will need to add the folder path to that file name when you calling file read function (cv2.imread(imgPath+img)).
cv2.VideoWriter() will create the video file in the folder. Therefore it will also be listed in os.listdir(path). So you will need to remove files other than image files that you need. It can be done by checking the file extension.
After writing all the frames to the video, you will need to call the release() function to release the file handle.
And finally, makeVideo() function will not return anything. So there is no need to get it into a variable. (What you have to release() is file handler, but not the function as I said above).
Try the following code..
import cv2
import os
forexample = "C:/Users/me/Pictures/eg/"
eg = cv2.imread(forexample+'figure.jpg')
height , width , layers = eg.shape
print("ok, got that ", height, " ", width, " ", layers)
def makeVideo(imgPath, videodir, videoname, width, height):
video = cv2.VideoWriter(videodir+videoname,-1,1,(width, height))
for img in os.listdir(imgPath):
if not img.endswith('.jpg'):
continue
shot = cv2.imread(imgPath+img)
video.write(shot)
video.release()
print("one video done")
makeVideo(forexample,forexample, "example.avi", width, height)
cv2.destroyAllWindows()
we are using a Raspberry Pi + Python 3.4 + PyGame to capture an image from a specific USB webcam. We use this simple code to capture (it works ok):
pygame.camera.init()
cam = pygame.camera.Camera(pygame.camera.list_cameras()[0],(1280,720))
cam.start()
time.sleep(1)
webcamImage = cam.get_image()
The problem comes here: we have to convert this webcamImage into a PIL image. We follow this link but unfortunately the function Image.fromstring() not exists anymore. So, we can't do that:
pil_string_image = pygame.image.tostring(webcamImage, "RGBA",False)
pil_image = Image.fromstring("RGBA",(1280,720),pil_string_image)
PIL says that Image.fromstring() is deprecated, and suggests to use the function Image.frombytes(). Clearly we not found the equivalent pygame.image function that convert the webcamImage into an array of bytes. So we are stucked here: can you help us, please?
Thank you :-)
As per Damian Yerrick's comment, under Python 3 the result of pygame.image.tostring() is a bytes, despite the method's name. Thus we can go out of this situation with this simple code:
pygame.camera.init()
cam = pygame.camera.Camera(pygame.camera.list_cameras()[0],(1280,720))
cam.start()
time.sleep(1)
webcamImage = cam.get_image()
pil_string_image = pygame.image.tostring(webcamImage,"RGBA",False)
im = Image.frombytes("RGBA",(1280,720),pil_string_image)
I've showed in various ways how to take images with a webcam in Python (see How can I take camera images with Python?). You can see that the images taken with Python are considerably darker than images taken with JavaScript. What is wrong?
Image example
The image on the left was taken with http://martin-thoma.com/html5/webcam/, the one on the right with the following Python code. Both were taken with the same (controlled) lightning situation (it was dark outside and I only had some electrical lights on) and the same webcam.
Code example
import cv2
camera_port = 0
camera = cv2.VideoCapture(camera_port)
return_value, image = camera.read()
cv2.imwrite("opencv.png", image)
del(camera) # so that others can use the camera as soon as possible
Question
Why is the image taken with Python image considerably darker than the one taken with JavaScript and how do I fix it?
(Getting a similar image quality; simply making it brighter will probably not fix it.)
Note to the "how do I fix it": It does not need to be opencv. If you know a possibility to take webcam images with Python with another package (or without a package) that is also ok.
Faced the same problem. I tried this and it works.
import cv2
camera_port = 0
ramp_frames = 30
camera = cv2.VideoCapture(camera_port)
def get_image():
retval, im = camera.read()
return im
for i in xrange(ramp_frames):
temp = camera.read()
camera_capture = get_image()
filename = "image.jpg"
cv2.imwrite(filename,camera_capture)
del(camera)
I think it's about adjusting the camera to light. The former
former and later images
I think that you have to wait for the camera to be ready.
This code works for me:
from SimpleCV import Camera
import time
cam = Camera()
time.sleep(3)
img = cam.getImage()
img.save("simplecv.png")
I took the idea from this answer and this is the most convincing explanation I found:
The first few frames are dark on some devices because it's the first
frame after initializing the camera and it may be required to pull a
few frames so that the camera has time to adjust brightness
automatically.
reference
So IMHO in order to be sure about the quality of the image, regardless of the programming language, at the startup of a camera device is necessary to wait a few seconds and/or discard a few frames before taking an image.
Tidying up Keerthana's answer results in my code looking like this
import cv2
import time
def main():
capture = capture_write()
def capture_write(filename="image.jpeg", port=0, ramp_frames=30, x=1280, y=720):
camera = cv2.VideoCapture(port)
# Set Resolution
camera.set(3, x)
camera.set(4, y)
# Adjust camera lighting
for i in range(ramp_frames):
temp = camera.read()
retval, im = camera.read()
cv2.imwrite(filename,im)
del(camera)
return True
if __name__ == '__main__':
main()