import requests
import calendar
import time
from picamera import PiCamera
from PIL import Image
import cv2
import os
from gtts import gTTS

camera = PiCamera()

def speak(text):

	file = "file.mp3"
	tts = gTTS(text, 'en')
	tts.save(file)
	os.system("mpg123 " + file)

def captionImage(imagePath):

    response = requests.post(
        "https://api.deepai.org/api/neuraltalk",
        files={
            'image':  open(imagePath, 'rb')
        },
        headers={'api-key': 'XXXXXXXXXXXXX'}
    )

    return response.json().get("output")

def takePicture(camera):
	
	imageName ='./images/raw/image_' + str(calendar.timegm(time.gmtime()))+'.jpg'
	rawImg = camera.capture(imageName)
	return imageName

while True:
	
	imgPath = takePicture(camera)
	img = Image.open(imgPath)
  
  #Check the min and max colour valeu
	valeus = img.convert("L").getextrema()
  
  #If the brighest colour is below this, shutter must be closed
	if valeus[1] < 160:
		os.remove(imgPath)
	else:
		speak("click!")
		resultCaption = captionImage(imgPath)
		speak(resultCaption)

	
	time.sleep(0.5)


