"""
Salman Oraibi Fab Academy 2017

In the following code a webcam multiple video frames are captured and 
the BackgroundSubtractorMOG() algorithm is applied to separte the 
changing pixel values from the average intensity of the fixed values.
This would result in the seperating the image into a foreground and a
background with the assumption that the moving parts are background.
The code implements this algorithm to create a video interface that 
would allow the user to communicate his interactions with a microcontroller 
over serial port.

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>

"""
import cv2
import numpy as np
import time
from pyfirmata import Arduino, util

board = Arduino('/dev/ttyACM0')
it = util.Iterator(board)
it.start()
digital_6 = board.get_pin('d:6:o')
digital_5 = board.get_pin('d:5:o')
digital_4 = board.get_pin('d:4:o')
digital_3 = board.get_pin('d:3:o')

roiSize = 6400
c = cv2.VideoCapture(0)
_,f = c.read()

avg1 = np.float32(f)
avg2 = np.float32(f)
font = cv2.FONT_HERSHEY_SIMPLEX

while(1):
    _,f = c.read()
    f = cv2.flip(f,1)
    cv2.accumulateWeighted(f,avg1,0.1)
    cv2.accumulateWeighted(f,avg2,0.01)

    res1 = cv2.convertScaleAbs(avg1)
    res2 = cv2.convertScaleAbs(avg2)
    fgray = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
    res1 = cv2.cvtColor(res1, cv2.COLOR_BGR2GRAY)
    res2 = cv2.cvtColor(res2, cv2.COLOR_BGR2GRAY)
    d1 = cv2.absdiff(fgray,res1)
    d2 = cv2.absdiff(fgray,res2)


    cv2.rectangle(f,(50,50),(130,130),(0,0,255),4)
    cv2.rectangle(f,(210,50),(290,130),(0,255,0),4)
    cv2.rectangle(f,(370,50),(450,130),(255,0,0),4)
    cv2.rectangle(f,(530,50),(610,130),(255,0,127),4)
    ret,th1 = cv2.threshold(d2,50,255,cv2.THRESH_BINARY)
    roi1 = th1[50:130,50:130]
    roi2 = th1[50:130,210:290]
    roi3 = th1[50:130,370:450]
    roi4 = th1[50:130,530:610]
    if np.count_nonzero(roi1) > 1000 :
        cv2.putText(f,'Red',(10,400), font, 4,(255,255,255))
        digital_6.write(1)
        digital_6.write(0)
    elif np.count_nonzero(roi4) > 1000 :
        cv2.putText(f,'Violet',(10,400), font, 4,(255,255,255))
        digital_5.write(1)
        digital_5.write(0)
    elif np.count_nonzero(roi2) > 1000:
        cv2.putText(f,'Green',(10,400), font, 4,(255,255,255))
        digital_4.write(1)
        digital_4.write(0)
    elif np.count_nonzero(roi3) > 1000:
        cv2.putText(f,'Blue',(10,400), font, 4,(255,255,255))
        digital_3.write(1)
        digital_3.write(0)

    cv2.imshow('img',f)
    # cv2.imshow('avg1',th1)
    # cv2.imshow('avg2',res2)

    k = cv2.waitKey(20)

    if k == 27:
        break

cv2.destroyAllWindows()
c.release()
