Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Published © CC BY-NC-SA

Barbara: The Talking AI Camera

A retro camera captioning every picture.

IntermediateFull instructions provided20 hours2,348

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Camera Module
Raspberry Pi Camera Module
×1
Powerbank
×1
Headphones
×1
Old Camera
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian
OpenCV
OpenCV
DeepAI

Hand tools and fabrication machines

Tape, Electrical
Tape, Electrical
scissors

Story

Read more

Code

Code

Python
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)

Credits

Comments