-1

i would like to do some program by capture image from webcam, then cropped it. after crop, i do some image processing and from the process it will run my robots. Here the full program:

import cv2
from cv2 import *
import numpy as np
import pylab
import pymorph
import mahotas
from matplotlib import pyplot
from PIL import Image 

# initialize the camera
cam = VideoCapture(0)   # 0 -> index of camera
s, img = cam.read()

# frame captured without any errors
if s:    
    imwrite("img.jpg",img) #save image

#Crop Image
imageFile = "img.jpg"
im1 = Image.open(imageFile)

def imgCrop(im):

        box = (0, 199, 640, 200)
        region = im.crop(box)
        region.save('crop.jpg')

cImg = imgCrop(im1)

#thresholding
def greyImg(im):
    gray = im.convert('L')
    bw = gray.point(lambda x: 0 if x<128 else 255, '1')
    bw.save("bw.jpg")

tImg = greyImg(cImg )

#direction

def find_centroid(im, rez):
        width, height = im.size
        XX, YY, count = 0, 0, 0
        for x in xrange(0, width, rez):
            for y in xrange(0, height, rez):
                    if im.getpixel((x, y)) == 255:
                        XX += x
                        YY += y
                        count += 1
        return XX/count, YY/count

print find_centroid(tImg, 1)

def robo_direct():
    cen = find_centroid(im, 1)
    diff = cen[0] - 320
    if diff > 10:
        print 'right'
    if diff < -10:
        print 'left'
    else:
        print 'straight'

print robo_direct()

The error was come out like this:

File "compile.py", line 32, in greyImg
gray = im.convert('L')
AttributeError: 'NoneType' object has no attribute 'convert'
timekeeper
  • 43
  • 6

2 Answers2

1

That is because im is a None object.

Try again the code with:

print im is None

And you'll see. I don't know about threshold, but obviously you are creating the im object the wrong way.

bgusach
  • 13,019
  • 10
  • 44
  • 61
0

Your function imgCrop(im1) has no return statement and as such returns None. And then your greyImg(im) function also has no return statement and also will return None.

To fix that add return statements to both functions that for the first return region and the second return bw.

Also your robo_direct() function should return and not print the direction so that the call to it in the statement print robo_direct() would print the direction.

Dan D.
  • 67,516
  • 13
  • 93
  • 109
  • can u explain more? because i not really understand the problem. Im new in programming so im still learning. – timekeeper Feb 05 '14 at 02:26
  • @timekeeper In Python functions that don't have a return statement implicitly return `None`. Those two functions don't have return statements therefore they also return `None`. As you evidently want them to return the value calculated in the function thus my suggestion of that the return statement should be for those functions. – Dan D. Feb 05 '14 at 03:09
  • ok thanks...i was fix it...but new problem come out.. Traceback (most recent call last): File "compile3.py", line 51, in find_centroid return XX/count, YY/count ZeroDivisionError: integer division or modulo by zero – timekeeper Feb 05 '14 at 06:34