Spaces:
Build error
Build error
Upload 9 files
Browse files- .gitattributes +2 -0
- README.md +9 -13
- app.py +47 -0
- model.keras +3 -0
- model/saved_model.pb +3 -0
- model/variables/variables.data-00000-of-00001 +3 -0
- model/variables/variables.index +0 -0
- requirements.txt +70 -0
- train.py +37 -0
- weights.weights.h5 +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
model.keras filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
model/variables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,14 +1,10 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
license: mit
|
| 11 |
-
short_description: allows users to draw handwritten numbers and receive predict
|
| 12 |
-
---
|
| 13 |
|
| 14 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
+
# Number Detect
|
| 2 |
+
Draw a Number - BrainiaC©
|
| 3 |
+
|
| 4 |
+
## Install
|
| 5 |
+
```
|
| 6 |
+
$ sudo pip install -r requirements.txt
|
| 7 |
+
$ streamlit run app.py
|
| 8 |
+
```
|
| 9 |
+

|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__author__ = "Marlon"
|
| 2 |
+
__Cop__ = "BrainiaC©"
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2
|
| 6 |
+
import os
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from streamlit_drawable_canvas import st_canvas
|
| 9 |
+
from tensorflow.keras.models import load_model
|
| 10 |
+
|
| 11 |
+
st.title('Detector Numérico - BrainiaC©')
|
| 12 |
+
|
| 13 |
+
# Caminho do modelo salvo corretamente
|
| 14 |
+
model_path = os.path.join(os.path.dirname(__file__), 'model.keras') # Certifique-se de salvar como .keras
|
| 15 |
+
weights_path = "weights.weights.h5" # Certifique-se de salvar os pesos corretamente
|
| 16 |
+
|
| 17 |
+
# Carregar o modelo e os pesos
|
| 18 |
+
model = load_model(model_path)
|
| 19 |
+
model.load_weights(weights_path)
|
| 20 |
+
|
| 21 |
+
st.write("###### Author - Marlon Sousa")
|
| 22 |
+
st.write("###### [Blog](https://marlonsousa.medium.com)")
|
| 23 |
+
|
| 24 |
+
st.markdown('''
|
| 25 |
+
Tente Desenhar um Número!
|
| 26 |
+
''')
|
| 27 |
+
|
| 28 |
+
canvas_result = st_canvas(
|
| 29 |
+
fill_color='#000000',
|
| 30 |
+
stroke_width=20,
|
| 31 |
+
stroke_color='#FFFFFF',
|
| 32 |
+
background_color='#000000',
|
| 33 |
+
width=300,
|
| 34 |
+
height=300,
|
| 35 |
+
key='canvas'
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if canvas_result.image_data is not None:
|
| 39 |
+
img = canvas_result.image_data.astype('uint8') # Garante que a imagem seja uint8
|
| 40 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGBA2GRAY) # Converte para escala de cinza
|
| 41 |
+
img = cv2.resize(img, (28, 28)) # Redimensiona para 28x28 pixels
|
| 42 |
+
img = img / 255.0 # Normaliza os valores dos pixels
|
| 43 |
+
img = img.reshape(1, 28, 28, 1) # Adiciona dimensão extra para o modelo
|
| 44 |
+
|
| 45 |
+
if st.button('Predict'):
|
| 46 |
+
val = model.predict(img)
|
| 47 |
+
st.write(f"""# Resultado: {np.argmax(val[0])}""")
|
model.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:208b6d7829622431d22bc9907c3830ffcbbe84bf3356aa7300a8f6d102f52523
|
| 3 |
+
size 3044681
|
model/saved_model.pb
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f180e23cdbab12643fd342979b807ad348d961b76b6656cfee4a3f66f5e58f44
|
| 3 |
+
size 121529
|
model/variables/variables.data-00000-of-00001
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0231210ee975fe5fd2c55fcfc0e9dcc6a3d080baca998a02b40c114f4ccbe0ae
|
| 3 |
+
size 3018295
|
model/variables/variables.index
ADDED
|
Binary file (1.84 kB). View file
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
absl-py==2.1.0
|
| 2 |
+
altair==5.5.0
|
| 3 |
+
astunparse==1.6.3
|
| 4 |
+
attrs==25.1.0
|
| 5 |
+
blinker==1.9.0
|
| 6 |
+
cachetools==5.5.2
|
| 7 |
+
certifi==2025.1.31
|
| 8 |
+
charset-normalizer==3.4.1
|
| 9 |
+
click==8.1.8
|
| 10 |
+
contourpy==1.3.1
|
| 11 |
+
cycler==0.12.1
|
| 12 |
+
flatbuffers==25.2.10
|
| 13 |
+
fonttools==4.56.0
|
| 14 |
+
gast==0.6.0
|
| 15 |
+
gitdb==4.0.12
|
| 16 |
+
GitPython==3.1.44
|
| 17 |
+
google-pasta==0.2.0
|
| 18 |
+
grpcio==1.70.0
|
| 19 |
+
h5py==3.13.0
|
| 20 |
+
idna==3.10
|
| 21 |
+
Jinja2==3.1.5
|
| 22 |
+
jsonschema==4.23.0
|
| 23 |
+
jsonschema-specifications==2024.10.1
|
| 24 |
+
keras==3.8.0
|
| 25 |
+
kiwisolver==1.4.8
|
| 26 |
+
libclang==18.1.1
|
| 27 |
+
Markdown==3.7
|
| 28 |
+
markdown-it-py==3.0.0
|
| 29 |
+
MarkupSafe==3.0.2
|
| 30 |
+
matplotlib==3.10.0
|
| 31 |
+
mdurl==0.1.2
|
| 32 |
+
ml-dtypes==0.4.1
|
| 33 |
+
namex==0.0.8
|
| 34 |
+
narwhals==1.28.0
|
| 35 |
+
numpy==2.0.2
|
| 36 |
+
opencv-python==4.11.0.86
|
| 37 |
+
opt_einsum==3.4.0
|
| 38 |
+
optree==0.14.0
|
| 39 |
+
packaging==24.2
|
| 40 |
+
pandas==2.2.3
|
| 41 |
+
pillow==11.1.0
|
| 42 |
+
protobuf==5.29.3
|
| 43 |
+
pyarrow==19.0.1
|
| 44 |
+
pydeck==0.9.1
|
| 45 |
+
Pygments==2.19.1
|
| 46 |
+
pyparsing==3.2.1
|
| 47 |
+
python-dateutil==2.9.0.post0
|
| 48 |
+
pytz==2025.1
|
| 49 |
+
referencing==0.36.2
|
| 50 |
+
requests==2.32.3
|
| 51 |
+
rich==13.9.4
|
| 52 |
+
rpds-py==0.23.1
|
| 53 |
+
six==1.17.0
|
| 54 |
+
smmap==5.0.2
|
| 55 |
+
streamlit==1.42.2
|
| 56 |
+
streamlit-drawable-canvas==0.9.3
|
| 57 |
+
tenacity==9.0.0
|
| 58 |
+
tensorboard==2.18.0
|
| 59 |
+
tensorboard-data-server==0.7.2
|
| 60 |
+
tensorflow==2.18.0
|
| 61 |
+
tensorflow-io-gcs-filesystem==0.37.1
|
| 62 |
+
termcolor==2.5.0
|
| 63 |
+
toml==0.10.2
|
| 64 |
+
tornado==6.4.2
|
| 65 |
+
typing_extensions==4.12.2
|
| 66 |
+
tzdata==2025.1
|
| 67 |
+
urllib3==2.3.0
|
| 68 |
+
watchdog==6.0.0
|
| 69 |
+
Werkzeug==3.1.3
|
| 70 |
+
wrapt==1.17.2
|
train.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
|
| 5 |
+
# Carregar dataset corretamente
|
| 6 |
+
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
|
| 7 |
+
|
| 8 |
+
# Normalizar imagens (0 a 1) e adicionar dimensão extra para (28,28,1)
|
| 9 |
+
x_train = x_train.astype("float32") / 255.0
|
| 10 |
+
x_test = x_test.astype("float32") / 255.0
|
| 11 |
+
|
| 12 |
+
x_train = np.expand_dims(x_train, axis=-1) # De (60000, 28, 28) para (60000, 28, 28, 1)
|
| 13 |
+
x_test = np.expand_dims(x_test, axis=-1)
|
| 14 |
+
|
| 15 |
+
# Criar modelo
|
| 16 |
+
model = tf.keras.Sequential([
|
| 17 |
+
tf.keras.layers.Flatten(input_shape=(28, 28, 1)),
|
| 18 |
+
tf.keras.layers.Dense(300, activation='relu'),
|
| 19 |
+
tf.keras.layers.Dropout(0.2),
|
| 20 |
+
tf.keras.layers.Dense(50, activation='relu'),
|
| 21 |
+
tf.keras.layers.Dropout(0.3),
|
| 22 |
+
tf.keras.layers.Dense(10, activation='softmax')
|
| 23 |
+
])
|
| 24 |
+
|
| 25 |
+
# Compilar modelo
|
| 26 |
+
model.compile(loss='sparse_categorical_crossentropy',
|
| 27 |
+
optimizer=tf.keras.optimizers.Adam(0.0003),
|
| 28 |
+
metrics=['accuracy'])
|
| 29 |
+
|
| 30 |
+
# Treinar modelo
|
| 31 |
+
model.fit(x_train, y_train, batch_size=32, epochs=20, validation_data=(x_test, y_test))
|
| 32 |
+
|
| 33 |
+
# Salvar no formato correto para Keras 3
|
| 34 |
+
model.save("model.keras") # Antes era "model", agora usa ".keras"
|
| 35 |
+
|
| 36 |
+
# Salvar pesos separadamente, se necessário
|
| 37 |
+
model.save_weights("weights.weights.h5")
|
weights.weights.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:467015ccba5dbeb2dd5d08d2f3252b5df54c0b09fa5710a1ddc4573b9969612b
|
| 3 |
+
size 3040632
|