Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from collections import Counter
|
| 5 |
+
from time import time
|
| 6 |
+
import tkinter.filedialog
|
| 7 |
+
from tkinter import *
|
| 8 |
+
import sys
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
def k_nearest_neighbors(predict, k):
|
| 12 |
+
distances = []
|
| 13 |
+
for image in training_data:
|
| 14 |
+
distances.append([np.linalg.norm(image[0] - predict), image[1]]) # calcul de distance euclidienne
|
| 15 |
+
distances.sort()
|
| 16 |
+
votes = [i[1] for i in distances[:k]]
|
| 17 |
+
votes = ''.join(str(e) for e in votes)
|
| 18 |
+
votes = votes.replace(',', '')
|
| 19 |
+
votes = votes.replace(' ', '')
|
| 20 |
+
result = Counter(votes).most_common(1)[0][0]
|
| 21 |
+
return result
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def test():
|
| 25 |
+
start = time()
|
| 26 |
+
correct = 0
|
| 27 |
+
total = 0
|
| 28 |
+
skipped = 0
|
| 29 |
+
for i in range(len(x_test)+1):
|
| 30 |
+
try:
|
| 31 |
+
prediction = k_nearest_neighbors(x_test[i], 5)
|
| 32 |
+
if int(prediction) == y_test[i]:
|
| 33 |
+
correct += 1
|
| 34 |
+
total += 1
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print('An exception occured')
|
| 37 |
+
skipped += 1
|
| 38 |
+
accuracy = correct/total
|
| 39 |
+
end = time()
|
| 40 |
+
print(end-start)
|
| 41 |
+
print(accuracy)
|
| 42 |
+
|
| 43 |
+
def ia_handler(image):
|
| 44 |
+
pred = k_nearest_neighbors(img, 10)
|
| 45 |
+
if pred == 0:
|
| 46 |
+
return 'It\'s a coin'
|
| 47 |
+
return 'It\'s a banknote'
|
| 48 |
+
|
| 49 |
+
def main():
|
| 50 |
+
if len(sys.argv) > 1 and sys.argv[1] == '--cli':
|
| 51 |
+
root = Tk()
|
| 52 |
+
root.withdraw()
|
| 53 |
+
root.update()
|
| 54 |
+
filename = tkinter.filedialog.askopenfilename(title="Ouvrir fichier", filetypes=[('all files', '.*')]) # sélectionner la photo
|
| 55 |
+
src = cv2.imread(cv2.samples.findFile(filename), cv2.IMREAD_COLOR) # charger la photo
|
| 56 |
+
root.destroy()
|
| 57 |
+
img = resize_img(src)
|
| 58 |
+
pred = k_nearest_neighbors(img, 10)
|
| 59 |
+
if pred == '0':
|
| 60 |
+
print('Coin')
|
| 61 |
+
else:
|
| 62 |
+
print('Banknote')
|
| 63 |
+
else:
|
| 64 |
+
iface = gr.Interface(fn=ia_handler, inputs="image", outputs="text")
|
| 65 |
+
iface.launch()
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def resize_img(img):
|
| 69 |
+
dim = (150, 150)
|
| 70 |
+
new_img = cv2.resize(img, dim)
|
| 71 |
+
return new_img
|
| 72 |
+
|
| 73 |
+
if __name__=="__main__":
|
| 74 |
+
coin_datadir_train = '../coins-dataset/classified/train'
|
| 75 |
+
coin_datadir_test = '../coins-dataset/classified/test'
|
| 76 |
+
note_datadir_train = '../banknote-dataset/classified/train'
|
| 77 |
+
note_datadir_test = '../banknote-dataset/classified/test'
|
| 78 |
+
|
| 79 |
+
categories = ['1c', '2c', '5c', '10c', '20c', '50c', '1e', '2e', '5e', '10e', '20e', '50e']
|
| 80 |
+
coin_index = 8
|
| 81 |
+
|
| 82 |
+
training_data = []
|
| 83 |
+
|
| 84 |
+
for category in categories[:coin_index]:
|
| 85 |
+
path = os.path.join(coin_datadir_train, category)
|
| 86 |
+
label = 0
|
| 87 |
+
for img in os.listdir(path):
|
| 88 |
+
img_array = cv2.imread(os.path.join(path, img))
|
| 89 |
+
training_data.append([img_array, label])
|
| 90 |
+
|
| 91 |
+
for category in categories[coin_index:]:
|
| 92 |
+
path = os.path.join(note_datadir_train, category)
|
| 93 |
+
label = 1
|
| 94 |
+
for img in os.listdir(path):
|
| 95 |
+
img_array = resize_img(cv2.imread(os.path.join(path, img)))
|
| 96 |
+
training_data.append([img_array, label])
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
testing_data = []
|
| 100 |
+
|
| 101 |
+
for category in categories[:coin_index]:
|
| 102 |
+
path = os.path.join(coin_datadir_test, category)
|
| 103 |
+
label = 0
|
| 104 |
+
for img in os.listdir(path):
|
| 105 |
+
img_array = cv2.imread(os.path.join(path, img))
|
| 106 |
+
testing_data.append([img_array, label])
|
| 107 |
+
|
| 108 |
+
for category in categories[coin_index:]:
|
| 109 |
+
path = os.path.join(note_datadir_test, category)
|
| 110 |
+
label = 1
|
| 111 |
+
for img in os.listdir(path):
|
| 112 |
+
img_array = resize_img(cv2.imread(os.path.join(path, img)))
|
| 113 |
+
testing_data.append([img_array, label])
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
x_train = []
|
| 117 |
+
y_train = []
|
| 118 |
+
|
| 119 |
+
for features, label in training_data:
|
| 120 |
+
x_train.append(features)
|
| 121 |
+
y_train.append(label)
|
| 122 |
+
|
| 123 |
+
x_train = np.array(x_train)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
x_test = []
|
| 127 |
+
y_test = []
|
| 128 |
+
|
| 129 |
+
for features, label in testing_data:
|
| 130 |
+
x_test.append(features)
|
| 131 |
+
y_test.append(label)
|
| 132 |
+
|
| 133 |
+
x_test = np.array(x_test)
|
| 134 |
+
main()
|