python - tuple error while cropping -
i crop image using command.
def imgcrop(im): box = (0, 150, 640, 200) region = im.crop(box) region.save('crop.jpg') return region
then error come out this
attributeerror: 'tuple' object has no attribute 'crop'
and im refering http://effbot.org/imagingbook/image.htm, method correct. please advise.
here full program, please advise
import cv2 cv2 import * pil import image import rpi.gpio gpio import time im = 0 gpio.setwarnings(false) gpio.setmode(gpio.bcm) gpio.setup(27, gpio.out) #input 1 gpio.setup(22, gpio.out) #input 2 gpio.setup(9, gpio.out) #input 3 gpio.setup(10, gpio.out) #input 4 gpio.setup(7, gpio.out) #enable 1 gpio.setup(8, gpio.out) #enable 2 gpio.setup(15, gpio.in) #turn right gpio.setup(18, gpio.in) #turn left gpio.setup(23, gpio.in) #backward gpio.setup(24, gpio.in) #forward p = gpio.pwm(7,50) q = gpio.pwm(8,50) p.start(0) q.start(0) def forward(): p.changedutycycle(100) q.changedutycycle(100) gpio.output(27, gpio.high) #input 1 low gpio.output(22, gpio.low) #input 2 high gpio.output(9, gpio.high) #input 1 low gpio.output(10, gpio.low) #input 2 high def backward(): p.changedutycycle(100) q.changedutycycle(100) gpio.output(27, gpio.low) #input 1 low gpio.output(22, gpio.high) #input 2 high gpio.output(9, gpio.low) #input 1 low gpio.output(10, gpio.high) #input 2 high def left(): p.changedutycycle(100) q.changedutycycle(100) gpio.output(27, gpio.low) #input 1 low gpio.output(22, gpio.high) #input 2 high gpio.output(9, gpio.high) #input 1 low gpio.output(10, gpio.low) def right(): p.changedutycycle(100) q.changedutycycle(100) gpio.output(27, gpio.high) #input 1 low gpio.output(22, gpio.low) #input 2 high gpio.output(9, gpio.low) #input 1 low gpio.output(10, gpio.high) def stop(): p.changedutycycle(100) q.changedutycycle(100) gpio.output(27, gpio.low) #input 1 low gpio.output(22, gpio.low) #input 2 high gpio.output(9, gpio.low) #input 1 low gpio.output(10, gpio.low) def imgcrop(im): box = (0, 150, 640, 200) region = im.crop(box) region.save('crop.jpg') return region def imgthres(im): gray = im.convert('l') bw = gray.point(lambda x: 0 if x<50 else 255, '1') bw.save("bw.jpg") return bw def find_centroid(im, rez): width, height = im.size xx, yy, count = 0, 0, 0 x in xrange(0, width, rez): y in xrange(0, height, rez): if im.getpixel((x, y)) == 255: xx += x yy += y count += 1 return xx/count, yy/count def camera (): cam = videocapture(0) # 0 -> index of camera img = cam.read() cimg = imgcrop(img) timg = imgthres(cimg) print find_centroid(timg, 1) def robo_direct(): cen = find_centroid(timg, 1) diff = cen[0] - 320 if diff > 10: right() print 'right' if diff < -10: left() print 'left' else: forward() print 'straight' ####---------------------------------------------------------------------------------#### forward() while true: camera () robo_direct()
videocapture.read
doesn't return image. listed in documentation, returns retval, image
tuple. dunno retval
is, that's problem.
im
tuple. tuples don't have crop
method. if im
isn't supposed tuple, @ function called , figure out why it's being passed tuple.
also, have mixed tabs , spaces indentation. may or may not causing problems right now, it's guaranteed headache @ point. switch tabs or spaces; pep 8 recommends spaces.
Comments
Post a Comment