#libraries
from cv2 import *
from PIL import Image
import serial

ser = serial.Serial('COM3', 9600)#you will have to change the port if you are running Linux

cam = VideoCapture(0)   # 0 -> index of camera
s, img = cam.read()

while(1):
    s, img = cam.read()
    if s:    # frame captured without any errors
        imwrite("sidewalk.png",img) #save image
    img = Image.open("sidewalk.png")
    pix = img.load()
    width, height = img.size
    for x in range(width):#iterating through the pictutr
        for y in range(height):
            if pix[x,y][0]  < 5 and pix[x,y][1]  < 5 and pix[x,y][2] < 5:
                if x > width/3*2 and y > height/3 and y < height/3*2:#if hand is right middle
                    ser.write('1')
                    print('full-right')
                    break
                elif x < width/3 and y > height/3 and y < height/3*2:#if hand is left middle
                    ser.write('2')
                    print('full-left')
                    break
                elif y < height/3 and x > width/3 and x < width/3*2:#if hand is middle up
                    ser.write('3')
                    print('forwards')
                    break
                if y > height/3 and x > width/3 and x < width/3*2:#if hand is middle down
                    ser.write('4')
                    print('backwards')
                    break
                elif x > width/3 and x < width/3*2 and y > height/3 and y < height/3*2: # if hand is middle middle
                    ser.write('5')
                    print('stop')
                    break
            else:
                continue
            break
    ser.write('5')
    print('stop')
