Currencydetect
import RPi.GPIO as GPIO
import time
from utils import cv2, read_img
from gtts import gTTS
import os
import pygame
import time
from gtts import gTTS
from mutagen.mp3 import MP3
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
IR = 17
SW = 23
GPIO.setup(IR, GPIO.IN)
GPIO.setup(SW, GPIO.IN)
def Play(text):
myobj = gTTS(text=text, lang='en', slow =False)
myobj.save("voice.mp3")
print('\n--------------Playing------------\n')
song = MP3("voice.mp3")
pygame.mixer.init()
pygame.mixer.music.load('voice.mp3')
pygame.mixer.music.play()
time.sleep(song.info.length)
pygame.quit()
def helper(filename):
# from matplotlib import pyplot as plt
max_val = 8
max_pt = -1
max_kp = 0
orb = cv2.ORB_create(nfeatures=2500)
# orb is an alternative to SIFT
#test_img = read_img('files/test_100_2.jpg')
#test_img = read_img('files/test_50_2.jpg')
#test_img = read_img('files/500test.jpg')
#test_img = read_img('files/test_100_3.jpg')
#test_img = read_img('files/10test.jpg')
test_img = read_img(filename)
# resizing must be dynamic
# original = resize_img(test_img, 0.4)
# display('original', original)
# keypoints and descriptors
# (kp1, des1) = orb.detectAndCompute(test_img, None)
(kp1, des1) = orb.detectAndCompute(test_img, None)
training_set = os.listdir('currency')
for i in range(0, len(training_set)):
# train image
try:
print(i)
train_img = cv2.imread('currency/'+training_set[i])
(kp2, des2) = orb.detectAndCompute(train_img, None)
# brute force matcher
bf = cv2.BFMatcher()
all_matches = bf.knnMatch(des1, des2, k=2)
good = []
# give an arbitrary number -> 0.789
# if good -> append to list of good matches
for (m, n) in all_matches:
if m.distance < 0.80 * n.distance:
good.append([m])
if len(good) > max_val:
max_val = len(good)
max_pt = i
max_kp = kp2
print(i, ' ', 'currency/'+training_set[i], ' ', len(good))
except:
continue
print(max_val)
if max_val > 50:
print('currency/'+training_set[max_pt])
print('good matches ', max_val)
train_img = cv2.imread('currency/'+training_set[max_pt])
img3 = cv2.drawMatchesKnn(test_img, kp1, train_img, max_kp, good, 4)
note = training_set[max_pt].split('(')[0]
note = int(note.replace(' ', ''))
print('\nDetected denomination: Rs. ', note)
return note
else:
print('No Matches')
return "-1"
import cv2
amount = 0
print('Starting...')
while True:
if GPIO.input(IR) == False:
print("Inserting")
time.sleep(5)
vs = cv2.VideoCapture(0)
print("Capturing")
counting = 0
while True:
ret, img = vs.read()
counting +=1
cv2.imshow('img', img)
if cv2.waitKey(100) & 0xFF == ord('q'):
vs.release()
cv2.destroyAllWindows()
break
if counting > 50:
cv2.imwrite('frame.jpg', img)
amt = helper('frame.jpg')
if amt != '-1':
amount += amt
Play(f'detected amount is {amt}')
vs.release()
cv2.destroyAllWindows()
break
print('Amount ', amount)
if GPIO.input(SW) == False:
print("Total amount is ", amount)
Play(f'total amount is {amount} rupees')
amount = 0
time.sleep(1)