#!/usr/bin/python
import detect
import timeit
import cv2
import socket
import time

from imutils.video import FPS
calibration_width = 0.75
image = "img7.jpg"
images = ("img2.jpg", "img3.jpg", "img4.jpg", "img5.jpg", "img6.jpg", "img7.jpg", "img8.jpg")
#images = ("img.jpg", "img2.jpg")
video = True
def go():
    for file in images:
        items,output = detect.detect(calibration_width, file, True, False)
        print(str(items))
        if "Penny" in items:
            items.remove("Penny")
            itema = items[0]
            valid = True
            for item in items:
                if item != itema:
                    print("Too many items!")
                    valid = False
                    break
            if valid:
                print("Found " + itema)
                #selectedBin = sort.sort(itema)
                #control_motor.goToBin(selectedBin)

def sendString(string):
    print("Found " + string)
    try:
        conn.sendall(string.encode('utf-8'))
    except socket.error:
        print("Error Occured.")


if not video:
	elapsed_time = timeit.timeit(go, number=1)/1
	print(elapsed_time)

else :
    #capture = cv2.VideoCapture('tcpclientsrc port=5001 host=192.168.1.129 ! gdpdepay !  rtph264depay ! avdec_h264 ! videoconvert ! videorate ! video/x-raw,framerate=5/1 ! appsink', cv2.CAP_GSTREAMER)
    capture = cv2.VideoCapture('udpsrc port=9000 caps="application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264" ! rtph264depay ! avdec_h264 ! videoconvert ! appsink sync=false', cv2.CAP_GSTREAMER)
    host = ''        # Symbolic name meaning all available interfaces
    port = 9001     # Arbitrary non-privileged port
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #s.setsockopt(s, socket.SOL_SOCKET, socket.SO_REUSEADDR)
    s.bind((host, port))
    print("waiting for connection")
    s.listen(1)
    conn, addr = s.accept()
    print('Connected to', addr)
    time.sleep(2)
    waitForPi = True
    while waitForPi is True:
        try:
            print("waiting for data")
            data = conn.recv(1024)
            if not data: break
            encoding = 'utf-8'
            print ("Client: "+str(data))
            waitForPi = False
        except socket.error:
            print("Error Occured.")
            break
    # server command for imx135 camera ./video2stdout | gst-launch-1.0 -v fdsrc ! h264parse !  rtph264pay config-interval=1 pt=96 ! gdppay ! tcpserversink host=192.168.43.152 port=5001
    # server command for udp: ./video2stdout | gst-launch-1.0 -v fdsrc !  h264parse ! rtph264pay config-interval=10 pt=96 ! udpsink host=192.168.43.40 port=9000
    x = 0
    currentItem = ""
    samples = 0
    while True:
        ret = capture.grab()
        x+=1
        if not ret:
            print('empty frame')
            break
        #print('frame')
        if x > 1:
            ret,frame = capture.retrieve()
            frame = frame[130:1000, 450:1475]
            items,output = detect.detect(calibration_width, frame, True, True)
            cv2.imshow('Item Sorter', output)
            x = 0
            if "Penny" in items:
                items.remove("Penny")
                itema = items[0]
                valid = True
                for item in items:
                    if item != itema:
                        print("Too many items!")
                        valid = False
                        break
                if valid:
                    if currentItem == itema:
                        samples += 1
                    else:
                        currentItem = itema
                        samples = 0
                    if samples == 5:
                        samples = 0
                        sendString(currentItem)
                        waitForPi = True
                        while waitForPi is True:
                            try:
                                data = conn.recv(1024)
                                if not data: break
                                if data == "Continue":
                                    waitForPi = False
                            except socket.error:
                                print("Error Occured.")
                                break
                        
        if cv2.waitKey(1)&0xFF == ord('q'):
            s.close()
            break

