i would like to crop the image using this command.
def imgCrop(im):
box = (0, 150, 640, 200)
region = im.crop(box)
region.save('crop.jpg')
return region
then the error come out like this
AttributeError: 'tuple' object has no attribute 'crop'
and actually im refering to http://effbot.org/imagingbook/image.htm, this method is correct. Please advise.
Here my full program, please advise
import cv2
from cv2 import *
from PIL import Image
import RPi.GPIO as 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 is LOW
GPIO.output(22, GPIO.LOW) #input 2 is High
GPIO.output(9, GPIO.HIGH) #input 1 is LOW
GPIO.output(10, GPIO.LOW) #input 2 is High
def backward():
p.ChangeDutyCycle(100)
q.ChangeDutyCycle(100)
GPIO.output(27, GPIO.LOW) #input 1 is LOW
GPIO.output(22, GPIO.HIGH) #input 2 is High
GPIO.output(9, GPIO.LOW) #input 1 is LOW
GPIO.output(10, GPIO.HIGH) #input 2 is High
def left():
p.ChangeDutyCycle(100)
q.ChangeDutyCycle(100)
GPIO.output(27, GPIO.LOW) #input 1 is LOW
GPIO.output(22, GPIO.HIGH) #input 2 is High
GPIO.output(9, GPIO.HIGH) #input 1 is LOW
GPIO.output(10, GPIO.LOW)
def right():
p.ChangeDutyCycle(100)
q.ChangeDutyCycle(100)
GPIO.output(27, GPIO.HIGH) #input 1 is LOW
GPIO.output(22, GPIO.LOW) #input 2 is High
GPIO.output(9, GPIO.LOW) #input 1 is LOW
GPIO.output(10, GPIO.HIGH)
def stop():
p.ChangeDutyCycle(100)
q.ChangeDutyCycle(100)
GPIO.output(27, GPIO.LOW) #input 1 is LOW
GPIO.output(22, GPIO.LOW) #input 2 is High
GPIO.output(9, GPIO.LOW) #input 1 is 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
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
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 an image. As listed in the documentation, it returns a retval, image
tuple. Dunno what the retval
is, but that's your problem.
im
is a tuple. Tuples don't have a crop
method. If im
isn't supposed to be a tuple, look at where this function is called from and figure out why it's being passed a tuple.
Also, you have mixed tabs and spaces for indentation. That may or may not be causing problems right now, but it's guaranteed to be a headache at some point. Switch to all tabs or all spaces; PEP 8 recommends all spaces.