When testing my code on my RPi 2 B, I come along a name 'RPI' is not defined error. Can someone please help me to figure out why this is happening?
from PIL import Image
import os, sys
import colorsys
import time
import picamera
import RPi.GPIO as GPIO
class Process_Picture(object):
''' loop through each pixel and average rgb '''
def __init__(self, imageName):
self.pic = imageName
self.imgData = self.pic.load()
def averagePixels(self):
r, g, b = 0, 0, 0
count = 0
for x in range(int(self.pic.size[0])):
for y in range(int(self.pic.size[1])):
tempr,tempg,tempb = self.imgData[x,y]
r += tempr
g += tempg
b += tempb
count += 1
# calculate averages
ravg = int(r/count)
gavg = int(g/count)
bavg = int(b/count)
# print(ravg, gavg, bavg, count)
return (ravg,gavg,bavg)
def computeDGCI(self, rg, gg, bg):
h,s,v =colorsys.rgb_to_hsv( rg/255, gg/255, bg/255 )
print (h, s, v)
HUE = 360*h
print (HUE)
DGCI = (((HUE-60)/60 + (1-s) + (1-v))/3)
print (DGCI)
return DGCI
if __name__ == '__main__':
while True:
curr_time = time.localtime()
if curr_time.tm_hour == 15 and curr_time.tm_min == 36:
print(curr_time)
with picamera.PiCamera() as camera:
camera.resolution = (400, 400)
camera.start_preview
time.sleep ( 2 )
camera.capture('test.jpg')
picture1 = Image.open("test.jpg")
# picture1.imgData = picture1.load()
pc1 = Process_Picture(picture1)
a,b,c = pc1.averagePixels()
print(a,b,c)
pc1.computeDGCI(a, b, c)
# Divide picture in four parts
x = int(picture1.size[0])
y = int(picture1.size[1])
x1 = int(picture1.size[0]/2)
y1 = int(picture1.size[1]/2)
picture1_1 = picture1.transform(picture1.size, Image.EXTENT, (0,0,x1,y1))
picture1_2 = picture1.transform(picture1.size, Image.EXTENT, (x1,0,x,y1))
picture1_3 = picture1.transform(picture1.size, Image.EXTENT, (0,y1,x1,y))
picture1_4 = picture1.transform(picture1.size, Image.EXTENT, (x1,y1,x,y))
# Compute DGCI for image segment1
# picture1_1.imgData = picture1_1.load()
pc1_1 = Process_Picture(picture1_1)
R1_1,G1_1,B1_1 = pc1_1.averagePixels()
print(R1_1,G1_1,B1_1)
DGCI_1_1 = pc1_1.computeDGCI(R1_1,G1_1,B1_1)
# Compute DGCI for image segment2
# picture1_2.imgData = picture1_2.load()
pc1_2 = Process_Picture(picture1_2)
R1_2,G1_2,B1_2 = pc1_2.averagePixels()
print(R1_2,G1_2,B1_2)
DGCI_1_2 = pc1_2.computeDGCI(R1_2,G1_2,B1_2)
# Compute DGCI for image segment3
# picture1_3.imgData = picture1_3.load()
pc1_3 = Process_Picture(picture1_3)
R1_3,G1_3,B1_3= pc1_3.averagePixels()
print(R1_3,G1_3,B1_3)
DGCI_1_3 = pc1_3.computeDGCI(R1_3,G1_3,B1_3)
# Compute DGCI for image segment4
# picture1_4.imgData = picture1_4.load()
pc1_4 = Process_Picture(picture1_4)
R1_4,G1_4,B1_4 = pc1_4.averagePixels()
print(R1_4,G1_4,B1_4)
DGCI_1_4 = pc1_4.computeDGCI(R1_4,G1_4,B1_4)
time.sleep(60)
GPIO.setmode(GPIO.BOARD)
RPI.GPIO.setup(2, RPi.GPIO.OUT)
GPIO.output(24, 1)
sleep(3)
GPIO.output(24, 0)
sleep(3)
if curr_time.tm_hour == 16 and curr_time.tm_min == 00:
print(curr_time)
with picamera.PiCamera() as camera:
camera.resolution = (400, 400)
camera.start_preview
time.sleep ( 2 )
camera.capture('test.jpg')
picture1 = Image.open("test.jpg")
# picture1.imgData = picture1.load()
pc1 = Process_Picture(picture1)
a,b,c = pc1.averagePixels()
print(a,b,c)
pc1.computeDGCI(a, b, c)
# Divide picture in four parts
x = int(picture1.size[0])
y = int(picture1.size[1])
x1 = int(picture1.size[0]/2)
y1 = int(picture1.size[1]/2)
picture1_1 = picture1.transform(picture1.size, Image.EXTENT, (0,0,x1,y1))
picture1_2 = picture1.transform(picture1.size, Image.EXTENT, (x1,0,x,y1))
picture1_3 = picture1.transform(picture1.size, Image.EXTENT, (0,y1,x1,y))
picture1_4 = picture1.transform(picture1.size, Image.EXTENT, (x1,y1,x,y))
# Compute DGCI for image segment1
# picture1_1.imgData = picture1_1.load()
pc1_1 = Process_Picture(picture1_1)
R1_1,G1_1,B1_1 = pc1_1.averagePixels()
print(R1_1,G1_1,B1_1)
DGCI_1_1 = pc1_1.computeDGCI(R1_1,G1_1,B1_1)
# Compute DGCI for image segment2
# picture1_2.imgData = picture1_2.load()
pc1_2 = Process_Picture(picture1_2)
R1_2,G1_2,B1_2 = pc1_2.averagePixels()
print(R1_2,G1_2,B1_2)
DGCI_1_2 = pc1_2.computeDGCI(R1_2,G1_2,B1_2)
# Compute DGCI for image segment3
# picture1_3.imgData = picture1_3.load()
pc1_3 = Process_Picture(picture1_3)
R1_3,G1_3,B1_3= pc1_3.averagePixels()
print(R1_3,G1_3,B1_3)
DGCI_1_3 = pc1_3.computeDGCI(R1_3,G1_3,B1_3)
# Compute DGCI for image segment4
# picture1_4.imgData = picture1_4.load()
pc1_4 = Process_Picture(picture1_4)
R1_4,G1_4,B1_4 = pc1_4.averagePixels()
print(R1_4,G1_4,B1_4)
DGCI_1_4 = pc1_4.computeDGCI(R1_4,G1_4,B1_4)
time.sleep (60)
GPIO.setmode(GPIO.BOARD)
GPIO.output(24, 1)
sleep(3)
GPIO.output(24, 0)
sleep(3)
else:
print ("Not time yet!")
time.sleep(60)