# Image Processing + alphanumeric interpretation + step by step button
import pigpio
import RPi.GPIO as GPIO
import time
import numpy as np
import cv2
import argparse
from picamera import PiCamera
import subprocess
import os
from PIL import Image
import pytesseract
motor_array = [[0,1,0,1,1,0],[1,0,0,0,0,0],[1,1,0,0,0,0],[1,0,0,1,0,0],[1,0,0,1,1,0],
[1,0,0,0,1,0],[1,1,0,1,0,0],[1,1,0,1,1,0],[1,1,0,0,1,0],[0,1,0,1,0,0],
[1,0,0,0,0,0],[1,1,0,0,0,0],[1,0,0,1,0,0],[1,0,0,1,1,0],[1,0,0,0,1,0],
[1,1,0,1,0,0],[1,1,0,1,1,0],[1,1,0,0,1,0],[0,1,0,1,0,0],[0,1,0,1,1,0],
[1,0,1,0,0,0],[1,1,1,0,0,0],[1,0,1,1,0,0],[1,0,1,1,1,0],[1,0,1,0,1,0],
[1,1,1,1,0,0],[1,1,1,1,1,0],[1,1,1,0,1,0],[0,1,1,1,0,0],[0,1,1,1,1,0],
[1,0,1,0,0,1],[1,1,1,0,0,1],[0,1,0,1,1,1],[1,0,1,1,0,1],[1,0,1,1,1,1],
[1,0,1,0,1,1],[0,1,1,0,1,0],[0,0,1,0,0,0],[0,1,0,0,0,0],[0,0,1,0,0,1],
[0,1,0,0,1,1],[0,1,1,0,0,1],[0,0,1,1,1,1],[0,0,0,0,0,0]]
GPIO.setwarnings(False)
# Define constants
servo1 = 13
servo2 = 19
servo3 = 4
servo4 = 18
servo5 = 5
servo6 = 6
# Set up GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(servo1, GPIO.OUT)
GPIO.setup(servo2, GPIO.OUT)
GPIO.setup(servo3, GPIO.OUT)
GPIO.setup(servo4, GPIO.OUT)
GPIO.setup(servo5, GPIO.OUT)
GPIO.setup(servo6, GPIO.OUT)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
s1 = GPIO.PWM(servo1, 50) # GPIO 13 for PWM with 50Hz
s1.start(12.5)
s2 = GPIO.PWM(servo2, 50) # GPIO 19 for PWM with 50Hz
s2.start(12.5)
s3 = GPIO.PWM(servo3, 50) # GPIO 22 for PWM with 50Hz
s3.start(12.5)
s4 = GPIO.PWM(servo4, 50) # GPIO 18 for PWM with 50Hz
s4.start(9.8)
s5 = GPIO.PWM(servo5, 50) # GPIO 5 for PWM with 50Hz
s5.start(9.8)
s6 = GPIO.PWM(servo6, 50) # GPIO 6 for PWM with 50Hz
s6.start(9.8)
flag = False
step = False
prev_step = False
mode = True
def GPIO27_callback(channel): # Start - Stop function
global flag
if(flag == False):
flag = True
elif(flag == True):
flag = False
s1.stop()
s2.stop()
s3.stop()
s4.stop()
s5.stop()
s6.stop()
def GPIO23_callback(channel): # Next Step
global step
step=True
def GPIO17_callback(channel): # toggle mode
global mode
if(mode == False):
mode = True
elif(mode == True):
mode = False
def GPIO22_callback(channel): # previous step
global prev_step
prev_step=True
GPIO.add_event_detect(27,GPIO.FALLING, callback=GPIO27_callback, bouncetime=300)
GPIO.add_event_detect(23,GPIO.FALLING, callback=GPIO23_callback, bouncetime=300)
GPIO.add_event_detect(17,GPIO.FALLING, callback=GPIO17_callback, bouncetime=300)
GPIO.add_event_detect(22,GPIO.FALLING, callback=GPIO22_callback, bouncetime=300)
camera = PiCamera() #initialise an object o
def image():
print("camera working\n\n")
#camera = PiCamera() #initialise an object of class picamera
if os.path.isfile("/home/pi/Project/image.PNG"):
os.remove("/home/pi/Project/image.PNG")
#print("FILE REMOVED")
camera.start_preview() #use this object to start camera
time.sleep(1) #delay
camera.capture('image.PNG') #capture image and save as file image.jpg
camera.stop_preview() #use this object to stop camera preview
img = cv2.imread('image.PNG')
img = img[80:1100, 300:1350]
# RGB to Greyscale
img = cv2.medianBlur(img,5)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
noise_removal = cv2.bilateralFilter(gray,9,75,75)
th3 = cv2.adaptiveThreshold(noise_removal,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY,11,2)
#cv2.imshow("Transfered", open_out)
cv2.imwrite('gray_final.PNG',th3)
text = pytesseract.image_to_string(th3,lang='eng')
print(text)
return text
def motor_run(motor_array):
time.sleep(0.2)
if(motor_array[5] == 1):
s1.ChangeDutyCycle(9.8)
if(motor_array[1] == 1):
s2.ChangeDutyCycle(9.8)
if(motor_array[3] == 1):
s3.ChangeDutyCycle(9.8)
if(motor_array[2] == 1):
s4.ChangeDutyCycle(12.5)
if(motor_array[4] == 1):
s5.ChangeDutyCycle(12.5)
if(motor_array[0] == 1):
s6.ChangeDutyCycle(12.5)
time.sleep(0.2)
s1.ChangeDutyCycle(12.5)
s2.ChangeDutyCycle(12.5)
s3.ChangeDutyCycle(12.5)
s4.ChangeDutyCycle(9.8)
s5.ChangeDutyCycle(9.8)
s6.ChangeDutyCycle(9.8)
def character_conversion(alpha):
print(alpha)
if(ord(alpha) >= 48 and ord(alpha) <= 57):
b = ord(alpha) - 48 # for numerals
elif(ord(alpha)>64 and ord(alpha)<91):
b = ord(alpha) - 55 # for alphabets
elif(ord(alpha) == 33):
b = 36 # for !
elif(ord(alpha) == 39):
b = 37
elif(ord(alpha) == 44):
b = 38
elif(ord(alpha) == 45):
b = 39
elif(ord(alpha) == 46):
b = 40
elif(ord(alpha) == 63):
b = 41
elif(ord(alpha) == 35):
b = 42
else:
b = 43
print(motor_array[b])
motor_run(motor_array[b])
return
# ==================Main Function======================
while(True):
print("press 27 to start")
while(flag == False):
time.sleep(0.02)
text = image() # Converts image to text
#for i in range(len(text)):
i=0
while(i < len(text)):
if(flag == False):
GPIO.cleanup()
break;
if(mode == True):
while(step==False):
if(prev_step == True):
break;
if(flag==False):
break;
time.sleep(0.002)
if(flag == False):
GPIO.cleanup()
break;
if(prev_step==True):
character_conversion(text[i-1])
prev_step=False
while(step == False):
time.sleep(0.2)
character_conversion(text[i])
step = False
elif(mode == False):
character_conversion(text[i])
i = i+1
time.sleep(1)
if(flag == False):
GPIO.cleanup()
break;
flag=False