sagarrabanana
Published © CC BY-NC

bAIwatch, deep learning and surf

bAIwatch gets images from a public webcam from a beach, evaluates the surfing conditions and warns me it the waves are good enough.

AdvancedFull instructions provided669
bAIwatch, deep learning and surf

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Linear Solenoid, 12 VDC
Linear Solenoid, 12 VDC
×1
Voltage Regulator Module
Digilent Voltage Regulator Module
×1
Grove - 2-Channel SPDT Relay
Seeed Studio Grove - 2-Channel SPDT Relay
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian
Google Colaboratory
TensorFlow
TensorFlow
Fusion 360
Autodesk Fusion 360

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Schematics

Schema

Code

pictureDownloader

Python
Use this Ipynb on Google Colab to get the images from the target webcam.
from google.colab import drive
drive.mount('/content/gdrive')

import time
import datetime
import urllib.request
import socket
import matplotlib.pyplot as plt
socket.setdefaulttimeout(30)



#while True:
while (int(datetime.datetime.now().strftime("%H"))>5 and int(datetime.datetime.now().strftime("%H"))<17): 
  # setting filename and image URL
  try:
    print('dentro del try')
    name=str(datetime.datetime.now())
    filename = '/content/gdrive/MyDrive/Dataset/'+name+'.jpg' #here goes the path to your dataset folder on your google drive
    image_url = "http://212.8.113.121:8081/jpg/1/image.jpg"
    urllib.request.urlretrieve(image_url, filename)
    print('imagen '+name+' descargada.')

    #####
    imgBig=image.load_img(filename)
    plt.imshow(imgBig)
    plt.show()


    ####


    print('waiting...')
    time.sleep(30)
  except:
    print('try again in 30 sec')
    time.sleep(30)

imageClassificator

Python
Use this script to classify the images on the raw dataset.
# this script is made to help in the manual classificatio. You will have to modify the directories before using.
# the script will show you all the images from the main directory and move them to their correct place by pressing B(for good waves), M(for bad ones) and D(for discards)

import os
import shutil
import cv2
import time
i=0
directory = r'dataset directory'#'/home/sagarrabanana/Escritorio/dataSet_Plentzia/SanValentin'
directorySaveBuenas = r'folder for good waves'
directorySaveMalas = r'folder for bad waves'
directorySaveDescarte = r'folder for discarted images'
for filename in sorted(os.listdir(directory)):
    cv2.destroyAllWindows()
    if filename.endswith(".jpg") or filename.endswith(".png"):
        img=cv2.imread(os.path.join(directory, filename))
        cv2.imshow(filename,img)
        key=cv2.waitKey(0)
        if key== ord('b'):
            print('tecla B pulsada')
            shutil.move(directory+'/'+ filename,directorySaveBuenas+'/'+filename)
            print('movida a Buenas')
        elif key== ord('m'):
            print('tecla M pulsada')
            shutil.move(directory+'/'+ filename,directorySaveMalas+'/'+filename)
            print('movida a Malas')
        elif key== ord('d'):
            print('tecla D pulsada')
            shutil.move(directory+'/'+ filename,directorySaveDescarte+'/'+filename)
            print('movida a Descartes')
    else:
        continue

mainScript

Python
This is the script runing on the raspberry when it turns on. It checks the image from the webcam and vibrates de solenoid if it sees a good wave.
import os
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing import image
from tensorflow.keras import layers, callbacks, optimizers
from tensorflow.keras.models import Sequential, save_model, load_model
import numpy as np
print(tf.__version__)


import time
import datetime
import urllib.request
import socket
socket.setdefaulttimeout(30)

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)



model = load_model(
    'path to he ".h5" file',
    custom_objects=None,
    compile=True
)

GPIO.setup(21, GPIO.OUT)
for i in range(3): #lets me know that the script is runing
    GPIO.output(21,True)
    time.sleep(0.02)
    GPIO.output(21,False)
    time.sleep(0.02)
GPIO.cleanup()


while True:
  if (int(datetime.datetime.now().strftime("%H"))>5 and int(datetime.datetime.now().strftime("%H"))<17): #todo este jaleo para quedarme con la hora en formato integer del datatime...
  # setting filename and image URL
  try:
    print('dentro del try')
    name=str(datetime.datetime.now())
    filename = '/content/gdrive/MyDrive/Dataset/'+name+'.jpg' #here goes the path to your dataset folder on your google drive
    image_url = "http://212.8.113.121:8081/jpg/1/image.jpg"
    urllib.request.urlretrieve(image_url, filename)
    print('imagen '+name+' descargada.')

    #####
    img = image.load_img(filename, target_size=(150, 150))
    img = np.expand_dims(img, axis=0)
    result_c=model.predict_classes(img)
    print(result_c)
    if result_c==2:
      print('Buenas')
      GPIO.setmode(GPIO.BCM)
      GPIO.setup(21, GPIO.OUT)
      for i in range(3):
        GPIO.output(21,True)
        time.sleep(0.02)
        GPIO.output(21,False)
        time.sleep(0.02)
      GPIO.cleanup()
    elif result_c==1:
      print('Descarte')
    elif result_c==0:
      print('Malas')
    result=model.predict(img)
    print(result)
    print('esperamos...')
    time.sleep(30)
  except:
    print('hay algun problemilla, esperamos medio minuto y reintentamos...')
    time.sleep(30)
  else:
    time sleep(60)

Credits

sagarrabanana

sagarrabanana

2 projects • 0 followers

Comments