tuple error while cropping - python

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.

Related

Is there a way to have the red LED turn on first upon pressing button, then have blue LED turn on and lastly green LED turn on?

I am working through some LED tasks on my raspberry pi 4. I have the following code and I am getting the output on the breadboard wrong, which mean the code is faulty. Since I set the 'previous_button_state = 0', this would mean only the first 'if' would be executed (red LED). Since the previous_button_state is assigned a 1 in that 'if' statement, it should not be turning the red LED on again until the code gets to the 'elif', where the previous_button_state is reassigned the number 0 again. Instead, all three LED keep turning on every time I press the button.
import RPi.GPIO as GPIO
import time
previous_button_state = 0
LED_PIN3 = 22
LED_PIN2 = 27
LED_PIN = 17
BUTTON_PIN = 26
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_PIN, GPIO.IN)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(LED_PIN2, GPIO.OUT)
GPIO.setup(LED_PIN3, GPIO.OUT)
GPIO.output(LED_PIN, GPIO.LOW)
GPIO.output(LED_PIN2, GPIO.LOW)
GPIO.output(LED_PIN3, GPIO.LOW)
while True:
if (GPIO.input(BUTTON_PIN) == GPIO.HIGH) and previous_button_state == 0:
GPIO.output(LED_PIN, GPIO.HIGH)
GPIO.output(LED_PIN2, GPIO.LOW)
GPIO.output(LED_PIN3, GPIO.LOW)
previous_button_state = 1
elif (GPIO.input(BUTTON_PIN) == GPIO.HIGH) and previous_button_state == 1:
GPIO.output(LED_PIN2, GPIO.HIGH)
GPIO.output(LED_PIN, GPIO.LOW)
GPIO.output(LED_PIN3, GPIO.LOW)
previous_button_state = 2
elif (GPIO.input(BUTTON_PIN) == GPIO.HIGH) and previous_button_state == 2:
GPIO.output(LED_PIN3, GPIO.HIGH)
GPIO.output(LED_PIN2, GPIO.LOW)
GPIO.output(LED_PIN, GPIO.LOW)
previous_button_state = 0
else:
GPIO.output(LED_PIN, GPIO.LOW)
time.sleep(0.01)
GPIO.cleanup()

Ultrasonic sensor, dc motor and sw420 in Python

I am developing a system where I am avoiding an accident by slowing down the motor speed using distance provided by ultrasonic sensor. Also, if accident happens, it must be sense through sw420 and accident occurred result should be displayed.
The problem is that, i have code for both motor mgmt. - distance sensing and impact detection. But as I want them to run parallel i.e.., both are running at same time. How to do it?
Eg. If car is running smoothly, suddenly another car hits you from, let's say, from back.....then automatically, sw420 sensor should sense it and alert the accident occurred message.
Or in other words, some type of multiprocessing concept is needed which i am unaware of.
Here's what i have done till now -
1. Motor is perfectly slowing down when ultrasonic sensor senses shorter distance.
import RPi.GPIO as GPIO
import time
# Define GPIO For Driver motors
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
gpio.setup(7,gpio.OUT)
pwm=GPIO.PWM(18,100)
# Define GPIO for ultrasonic central
GPIO_TRIGGER_CENTRAL = 23
GPIO_ECHO_CENTRAL = 24
GPIO.setup(GPIO_TRIGGER_CENTRAL, GPIO.OUT) # Trigger > Out
GPIO.setup(GPIO_ECHO_CENTRAL, GPIO.IN) # Echo < In
# Functions for driving
def goforward():
pwm.start(100)
GPIO.output(12, True)
GPIO.output(16, False)
GPIO.output(18, True)
def goforwardslow():
GPIO.output(12, True)
GPIO.output(16, False)
GPIO.output(18, True)
def stopmotors():
GPIO.output(12, False)
GPIO.output(16, False)
GPIO.output(18, False)
def buzzer():
try:
while True:
gpio.output(7,0)
time.sleep(0.2)
gpio.output(7,1)
time.sleep(0.2)
except KeyboardInterrupt:
gpio.cleanup()
exit
#Detect front obstacle
def frontobstacle():
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER_CENTRAL, False)
# Allow module to settle
time.sleep(0.2)
# Send 10us pulse to trigger
GPIO.output(GPIO_TRIGGER_CENTRAL, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER_CENTRAL, False)
start = time.time()
while GPIO.input(GPIO_ECHO_CENTRAL) == 0:
start = time.time()
while GPIO.input(GPIO_ECHO_CENTRAL) == 1:
stop = time.time()
# Calculate pulse length
elapsed = stop - start
# Distance pulse travelled in that time is time
# Multiplied by the speed of sound (cm/s)
distance = elapsed * 17150 # distance of both directions so divide by 2
print "Front Distance : %.1f" % distance
return distance
# Check front obstacle and stop if there is an obstacle
def checkanddrivefront():
while frontobstacle() < 15:
pwm.ChangeDutyCycle(25)
buzzer()
goforwardslow()
time.sleep(3)
goforward()
# Avoid obstacles and drive forward
def obstacleavoiddrive():
goforward()
start = time.time()
# Drive 5 minutes
while start > time.time() - 300: # 300 = 60 seconds * 5
if frontobstacle() < 15:
pwm.ChangeDutyCycle(25)
goforwardslow()
checkanddrivefront()
#elif rightobstacle() < 30:
# stopmotors()
# checkanddriveright()
#elif leftobstacle() < 30:
# stopmotors()
#checkanddriveleft()
# Clear GPIOs, it will stop motors
#cleargpios()
#def cleargpios():
#print "clearing GPIO"
#GPIO.output(12, False)
#GPIO.output(13, False)
#print "All GPIOs CLEARED"
def main():
# First clear GPIOs
#cleargpios()
print "start driving: "
# Start obstacle avoid driving
obstacleavoiddrive()
if __name__ == "__main__":
main()
Vibrations is also detecting by sw420
import RPi.GPIO as GPIO
from time import sleep
channel = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN)
sleep(0.1)
while True:
result = GPIO.input(channel)
if result == 1:
print ("Vibration")
Now the problem is that, where should i place or how can i use vibration/sw420 code in the MAIN CODE so that both run parallel and whenever accident happens, sw420'code occupies higher priority than motor code and gives accident occurred message.
Please share your advice.
NOW THIS IS WHAT LATEST I HAVE DONE TILL NOW -
import RPi.GPIO as GPIO
import time
from multiprocessing import Process
# Define GPIO For Driver motors
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
pwm=GPIO.PWM(18,100)
channel = 11
GPIO.setmode(GPIO.BOARD)
GPIO.setup(channel, GPIO.IN)
sleep(0.1)
# Define GPIO for ultrasonic central
GPIO_TRIGGER_CENTRAL = 23
GPIO_ECHO_CENTRAL = 24
GPIO.setup(GPIO_TRIGGER_CENTRAL, GPIO.OUT) # Trigger > Out
GPIO.setup(GPIO_ECHO_CENTRAL, GPIO.IN) # Echo < In
# Functions for driving
def goforward():
pwm.start(100)
GPIO.output(12, True)
GPIO.output(16, False)
GPIO.output(18, True)
def goforwardslow():
GPIO.output(12, True)
GPIO.output(16, False)
GPIO.output(18, True)
def stopmotors():
GPIO.output(12, False)
GPIO.output(16, False)
GPIO.output(18, False)
def buzzer():
while True:
GPIO.output(7,0)
time.sleep(0.2)
GPIO.output(7,1)
time.sleep(0.2)
def vib():
while True:
result = GPIO.input(channel)
if result == 1:
print ("Vibration")
#Detect front obstacle
def frontobstacle():
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER_CENTRAL, False)
# Allow module to settle
time.sleep(0.2)
# Send 10us pulse to trigger
GPIO.output(GPIO_TRIGGER_CENTRAL, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER_CENTRAL, False)
start = time.time()
while GPIO.input(GPIO_ECHO_CENTRAL) == 0:
start = time.time()
while GPIO.input(GPIO_ECHO_CENTRAL) == 1:
stop = time.time()
# Calculate pulse length
elapsed = stop - start
# Distance pulse travelled in that time is time
# Multiplied by the speed of sound (cm/s)
distance = elapsed * 17150 # distance of both directions so divide by 2
print "Front Distance : %.1f" % distance
return distance
# Check front obstacle and stop if there is an obstacle
def checkanddrivefront():
while frontobstacle() < 15:
pwm.ChangeDutyCycle(25)
buzzer()
goforwardslow()
time.sleep(3)
goforward()
# Avoid obstacles and drive forward
def obstacleavoiddrive():
goforward()
start = time.time()
# Drive 5 minutes
while start > time.time() - 300: # 300 = 60 seconds * 5
if frontobstacle() < 15:
pwm.ChangeDutyCycle(25)
buzzer()
goforwardslow()
checkanddrivefront()
#elif rightobstacle() < 30:
# stopmotors()
# checkanddriveright()
#elif leftobstacle() < 30:
# stopmotors()
#checkanddriveleft()
# Clear GPIOs, it will stop motors
#cleargpios()
#def cleargpios():
#print "clearing GPIO"
def main():
print "start driving: "
# Start obstacle avoid driving
obstacleavoiddrive()
if __name__ == "__main__":
p1 = multiprocess.Process(target = main)
p2 = multiprocess.Process(target = vib)
p1.start()
p2.start()
I HAVE USED MULTIPROCESSING ....BUT YET AGAIN ONLY MOTOR DRIVE FUNCTION IS EXECUTING, VIBRATION CODE IS NOT ACTIVE IN THE CODE....means no output from it.

Python+ Raspi: change LED color based on # of button presses

I wrote the following code to change the color of an LED on my breadboard from blue to violet to red, and then flash. When the program is run, the LED turns on and stays blue the entire time, regardless of the button presses.
import RPi.GPIO as GPIO
import time
limit = 10
GPIO.setmode(GPIO.BOARD)
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)
Rpin = 11
Bpin = 13
flow = 0
try:
while True:
if GPIO.input(15) == False:
flow += 1
print flow
time.sleep(0.1)
if flow >= limit:
False
if flow < 6*limit//10:
print("blue")
GPIO.setup(Bpin, GPIO.OUT)
GPIO.output(Bpin, GPIO.HIGH)
elif flow in range(6*limit//10, 8*limit//10):
print("violet")
GPIO.setup(Rpin, GPIO.OUT)
GPIO.output(Rpin, GPIO.HIGH)
elif flow in range(8*limit//10, limit):
print("red")
GPIO.output(Bpin, GPIO.LOW)
else:
print("flashing red...")
GPIO.output(Rpin, GPIO.LOW)
time.sleep(.2)
GPIO.output(Rpin, GPIO.HIGH)
time.sleep(.2)
GPIO.output(Rpin, GPIO.LOW)
time.sleep(.2)
GPIO.output(Rpin, GPIO.HIGH)
time.sleep(.2)
GPIO.output(Rpin, GPIO.LOW)
break
except KeyboardInterrupt:
GPIO.output(Rpin, GPIO.LOW)
GPIO.output(Bpin, GPIO.LOW)
GPIO.cleanup()
I can make the LED work correctly with key presses, and can count the button presses and print that value, but the two do not work in conjunction. I'm fairly certain that this is not a wiring issue, as other code can make these parts work on their own. I have read related posts on Stack and other websites and haven't found any code similar to mine, where an increment changes the color of the LED. As a relatively new python user, I suspect there is a problem in the code. Any help is greatly appreciated!
import RPi.GPIO as GPIO
import time #added for sleep mode
GPIO.setmode(GPIO.BOARD)
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)
Rpin = 11
Bpin = 13
flow = 0
GPIO.setup(Bpin, GPIO.OUT)
GPIO.setup(Rpin, GPIO.OUT)
GPIO.output(Rpin, GPIO.LOW)
GPIO.output(Bpin, GPIO.LOW)
limit = int(raw_input("How many gallons of water would you like to use for your shower? "))
print("You may begin showering")
print limit
def flowmeter(f, l):
if f == 0:
print("off")
GPIO.output(Rpin, GPIO.LOW)
GPIO.output(Bpin, GPIO.LOW)
GPIO.cleanup()
if f < 6*l//10:
print("blue")
GPIO.output(Bpin, GPIO.HIGH)
elif f in range(6*l//10, 8*l//10):
print("violet")
GPIO.output(Rpin, GPIO.HIGH)
elif f in range(8*l//10, l):
print("red")
GPIO.output(Bpin, GPIO.LOW)
else:
print("flashing red... time's up!")
GPIO.output(Rpin, GPIO.LOW)
time.sleep(.5)
GPIO.output(Rpin, GPIO.HIGH)
time.sleep(.5)
GPIO.output(Rpin, GPIO.LOW)
time.sleep(.5)
GPIO.output(Rpin, GPIO.HIGH)
time.sleep(.5)
GPIO.output(Rpin, GPIO.LOW)
print("cleaning GPIO")
GPIO.output(Rpin, GPIO.LOW)
GPIO.output(Bpin, GPIO.LOW)
GPIO.cleanup()
while True:
if GPIO.input(15) == False:
flow += 1
print("flow = %s" %flow)
time.sleep(0.1) #prevents infinite pushes if held down for a few seconds
flowmeter(flow,limit)
A debugging tip: if your code is never executing the code in an if statement, it's because the if test is always evaluating to false, and that's what you should investigate.
You need to read the python docs about the built-in range() function - it doesn't do what you are trying to do.
Instead of:
f in range(6*l//10, 8*l//10):
use comparisons e.g.
f >= 6*l//10 and f < 8*l//10

Raspberry Webiopi GPIO one time Email Trigger

I have been trying to make this code I have to only email once after the Door pin has been tripped. I am newish to python and have been reading the posts with similar question and trying what I have seen to my Script. But can't seem to get it to only send the email one time.
enter code here
import webiopi
import datetime
import smtplib
import subprocess
import os
GPIO = webiopi.GPIO
ARM = 7 # GPIO pin using BCM numbering
FLASH = 8
START = 25 # relay
DOOR = 11
HOUR_ON = 8 # Turn Light ON at 08:00
HOUR_OFF = 18 # Turn Light OFF at 18:00
def setup():
GPIO.setFunction(ARM, GPIO.OUT)
GPIO.setFunction(FLASH, GPIO.OUT)
GPIO.setFunction(START, GPIO.OUT)
GPIO.setup(DOOR, GPIO.IN, pull_up_down=GPIO.PUD_UP)
now = datetime.datetime.now()
# test if we are between ON time and tun the light ON
if ((now.hour >= HOUR_ON) and (now.hour < HOUR_OFF)):
GPIO.digitalWrite(ARM, GPIO.HIGH)
GPIO.digitalWrite(FLASH, GPIO.HIGH)
def loop():
# retrieve current datetime
now = datetime.datetime.now()
# toggle light ON all days at the correct time
if ((now.hour == HOUR_ON) and (now.minute == 0) and (now.second == 0)):
if (GPIO.digitalRead(ARM) == GPIO.LOW):
GPIO.digitalWrite(ARM, GPIO.HIGH)
# toggle light OFF
if ((now.hour == HOUR_OFF) and (now.minute == 0) and (now.second == 0)):
if (GPIO.digitalRead(ARM) == GPIO.HIGH):
GPIO.digitalWrite(ARM, GPIO.LOW)
if (GPIO.digitalRead(ARM) == GPIO.HIGH):
GPIO.digitalWrite(FLASH, GPIO.HIGH)
webiopi.sleep(.5)
GPIO.digitalWrite(FLASH, GPIO.LOW)
if (GPIO.digitalRead(DOOR) == GPIO.HIGH):
SendText()
# gives CPU some time before looping again
webiopi.sleep(1)
def SendText():
os.system("python /home/pi/html/myemail.py")
# destroy function is called at WebIOPi shutdown
def destroy():
GPIO.digitalWrite(ARM, GPIO.LOW)
GPIO.digitalWrite(START, GPIO.LOW)
#webiopi.macro
def getLightHours():
return "%d;%d" % (HOUR_ON, HOUR_OFF)
#webiopi.macro
def setLightHours(on, off):
global HOUR_ON, HOUR_OFF
HOUR_ON = int(on)
HOUR_OFF = int(off)
return getLightHours()
You can set a variable, send_email, to track if it is time to send an email and if that email has been sent.
Create a global variable, send_email = False, because the variable will go inside the loop method and needs to be initialized.
Add to the SendText() if statement and send_email
After the SendText() line set send_email = False
Add another if statement to check if GPIO.LOW and change the send_email to True
I have made the suggested changes to your code. I put * around all the text I added. Make sure to remove all the * from the code for it to work.
import webiopi
import datetime
import smtplib
import subprocess
import os
GPIO = webiopi.GPIO
ARM = 7 # GPIO pin using BCM numbering
FLASH = 8
START = 25 # relay
DOOR = 11
HOUR_ON = 8 # Turn Light ON at 08:00
HOUR_OFF = 18 # Turn Light OFF at 18:00
*send_email = False*
def setup():
GPIO.setFunction(ARM, GPIO.OUT)
GPIO.setFunction(FLASH, GPIO.OUT)
GPIO.setFunction(START, GPIO.OUT)
GPIO.setup(DOOR, GPIO.IN, pull_up_down=GPIO.PUD_UP)
now = datetime.datetime.now()
# test if we are between ON time and tun the light ON
if ((now.hour >= HOUR_ON) and (now.hour < HOUR_OFF)):
GPIO.digitalWrite(ARM, GPIO.HIGH)
GPIO.digitalWrite(FLASH, GPIO.HIGH)
def loop():
# retrieve current datetime
now = datetime.datetime.now()
# toggle light ON all days at the correct time
if ((now.hour == HOUR_ON) and (now.minute == 0) and (now.second == 0)):
if (GPIO.digitalRead(ARM) == GPIO.LOW):
GPIO.digitalWrite(ARM, GPIO.HIGH)
# toggle light OFF
if ((now.hour == HOUR_OFF) and (now.minute == 0) and (now.second == 0)):
if (GPIO.digitalRead(ARM) == GPIO.HIGH):
GPIO.digitalWrite(ARM, GPIO.LOW)
if (GPIO.digitalRead(ARM) == GPIO.HIGH):
GPIO.digitalWrite(FLASH, GPIO.HIGH)
webiopi.sleep(.5)
GPIO.digitalWrite(FLASH, GPIO.LOW)
if (GPIO.digitalRead(DOOR) == GPIO.HIGH) *and send_email*:
SendText()
*send_email = False*
*if (GPIO.digitalRead(DOOR) == GPIO.LOW) and not send_email*:
*send_email = True*
# gives CPU some time before looping again
webiopi.sleep(1)
def SendText():
os.system("python /home/pi/html/myemail.py")
# destroy function is called at WebIOPi shutdown
def destroy():
GPIO.digitalWrite(ARM, GPIO.LOW)
GPIO.digitalWrite(START, GPIO.LOW)
#webiopi.macro
def getLightHours():
return "%d;%d" % (HOUR_ON, HOUR_OFF)
#webiopi.macro
def setLightHours(on, off):
global HOUR_ON, HOUR_OFF
HOUR_ON = int(on)
HOUR_OFF = int(off)
return getLightHours()

Why this happening I don't have line 86

I tried the run this and i just used spaces not tabs but
import time
import sys
import numpy as np
import SimpleCV
import webiopi
from ayarlar import *
GPIO = webiopi.GPIO
GPIO.setFunction(4, GPIO.OUT)
GPIO.setFunction(17, GPIO.OUT)
GPIO.setFunction(18, GPIO.OUT)
GPIO.setFunction(27, GPIO.OUT)
GPIO.setFunction(10, GPIO.OUT)
GPIO.setFunction(25, GPIO.OUT)
def dist_from_color(img_color):
matrix = (img.getNumpy()[:,:,[2,1,0]] - color) ** 2
width, height = img.size()
return matirx.sum() ** 0.5 / (width * height)
def forward():
GPIO.digitalWrite(18, GPIO.HIGH)
GPIO.digitalWrite(10, GPIO.LOW)
def reverse():
GPIO.digitalWrite(18, GPIO.LOW)
GPIO.digitalWrite(10, GPIO.HIGH)
def right():
GPIO.digitalWrite(27, GPIO.HIGH)
GPIO.digitalWrite(25, GPIO.LOW)
def left():
GPIO.digitalWrite(27, GPIO.LOW)
GPIO.digitalWrite(25, GPIO.HIGH)
def strop():
GPIO.digitalWrite(18, GPIO.LOW)
GPIO.digitalWrite(10, GPIO.LOW)
GPIO.digitalWrite(27, GPIO.LOW)
GPIO.digitalWrite(25, GPIO.LOW)
#webiopi.macro
def ButtonForward():
forward()
#webiopi.macro
def ButtonReverse():
reverse()
#webiopi.macro
def ButtonTrunLeft():
left()
#webiopi.macro
def ButtonTrunRight():
right()
#webiopi.macro
def ButtonStop():
stop()
def main():
try:
print(__doc__)
GPIO.digitalWrite(4, GPIO.HIGH)
GPIO.digitalWrite(17, GPIO.HIGH)
server = webiopi.Server(port=80)
server.addMacro(ButtonForward)
server.addMacro(ButtonReverse)
server.addMacro(ButtonTrunLeft)
server.addMacro(ButtonTrunRight)
server.addMacro(ButtonStop)
cam = SimpleCV.Camera()
background = cam.getImage()
print("Tespite Baslaniyor")
while True:
try:
background = cam.getImage()
time.sleep(0.1)
img = cam.getImage()
to_show = img
to_show.save("../../..//usr/share/webiopi/htdocs/aaa.jpg")
dist = ((img - background) + (background - img)).dilate(6)
except(KeyboardInterrupt, SystemExit):
del cam
server.stop()
It gives me this eror even i don't have line 86
File "tespit.py", line 86 ^
IndentationError: unexpected unindent
Please help me i can't solve the problem...
You have two try statements, and only one except statement.
Add an except clause for the first try, at the end of the main function, indented 4 spaces, and it will work.
Line 86 is just past the end of your code, and is where Python is looking for a matching except.
Every try must have at least one matching except.
And you should clean up GPIOs before stop Webiopi server.

Categories