Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,94 @@
|
|
1 |
-
streamlit
|
2 |
-
|
3 |
-
numpy
|
4 |
-
pandas
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
from paddleocr import PaddleOCR
|
6 |
+
|
7 |
+
# Função OCR com PaddleOCR
|
8 |
+
def ocr_with_paddle(img):
|
9 |
+
ocr = PaddleOCR(lang='en', use_angle_cls=True)
|
10 |
+
result = ocr.ocr(img, cls=True)
|
11 |
+
|
12 |
+
finaltext = ''
|
13 |
+
for line in result:
|
14 |
+
for word in line:
|
15 |
+
finaltext += ' ' + word[1][0]
|
16 |
+
return finaltext
|
17 |
+
|
18 |
+
# Função para categorizar despesa (exemplo simples)
|
19 |
+
def categorize_expense(text):
|
20 |
+
if "gas" in text.lower():
|
21 |
+
return "Combustível"
|
22 |
+
elif "restaurant" in text.lower():
|
23 |
+
return "Alimentação"
|
24 |
+
elif "shopping" in text.lower():
|
25 |
+
return "Compras"
|
26 |
+
else:
|
27 |
+
return "Outros"
|
28 |
+
|
29 |
+
# Função para processar a imagem e salvar as informações
|
30 |
+
def process_image(image, method):
|
31 |
+
if method == "PaddleOCR":
|
32 |
+
text = ocr_with_paddle(image)
|
33 |
+
else:
|
34 |
+
raise ValueError("Método OCR não suportado")
|
35 |
+
|
36 |
+
category = categorize_expense(text)
|
37 |
+
return f"Texto reconhecido: {text}\nCategoria: {category}"
|
38 |
+
|
39 |
+
# Função para adicionar projeto
|
40 |
+
def add_project(name, budget, items):
|
41 |
+
project = {
|
42 |
+
"name": name,
|
43 |
+
"budget": budget,
|
44 |
+
"items": items,
|
45 |
+
"spent": 0,
|
46 |
+
"balance": budget
|
47 |
+
}
|
48 |
+
projects.append(project)
|
49 |
+
return f"Projeto {name} adicionado com sucesso."
|
50 |
+
|
51 |
+
# Função para adicionar item ao projeto
|
52 |
+
def add_item_to_project(project_name, item_name, item_cost):
|
53 |
+
for project in projects:
|
54 |
+
if project["name"] == project_name:
|
55 |
+
project["items"].append({"name": item_name, "cost": item_cost})
|
56 |
+
project["balance"] -= item_cost
|
57 |
+
return f"Item {item_name} adicionado ao projeto {project_name}. Saldo atual: {project['balance']}"
|
58 |
+
return "Projeto não encontrado."
|
59 |
+
|
60 |
+
# Inicializando a lista de projetos
|
61 |
+
projects = []
|
62 |
+
|
63 |
+
# Interface Streamlit
|
64 |
+
st.title("Controle Financeiro com OCR")
|
65 |
+
|
66 |
+
# Tab para OCR
|
67 |
+
with st.expander("OCR"):
|
68 |
+
st.header("Processar Imagem")
|
69 |
+
uploaded_image = st.file_uploader("Upload da imagem", type=["jpg", "jpeg", "png"])
|
70 |
+
method = st.radio("Método OCR", ["PaddleOCR"])
|
71 |
+
if uploaded_image is not None:
|
72 |
+
image = cv2.imdecode(np.fromstring(uploaded_image.read(), np.uint8), cv2.IMREAD_COLOR)
|
73 |
+
if st.button("Processar Imagem"):
|
74 |
+
result = process_image(image, method)
|
75 |
+
st.text(result)
|
76 |
+
|
77 |
+
# Tab para Projetos
|
78 |
+
with st.expander("Projetos"):
|
79 |
+
st.header("Gerenciar Projetos")
|
80 |
+
project_name = st.text_input("Nome do Projeto")
|
81 |
+
project_budget = st.number_input("Orçamento do Projeto", min_value=0.0)
|
82 |
+
project_items = st.text_input("Itens do Projeto (separados por vírgula e espaço)")
|
83 |
+
if st.button("Adicionar Projeto"):
|
84 |
+
items_list = [item.strip() for item in project_items.split(',')]
|
85 |
+
result = add_project(project_name, project_budget, items_list)
|
86 |
+
st.text(result)
|
87 |
+
|
88 |
+
st.header("Adicionar Item ao Projeto")
|
89 |
+
project_select = st.selectbox("Selecione o Projeto", [p["name"] for p in projects])
|
90 |
+
item_name = st.text_input("Nome do Item")
|
91 |
+
item_cost = st.number_input("Custo do Item", min_value=0.0)
|
92 |
+
if st.button("Adicionar Item"):
|
93 |
+
result = add_item_to_project(project_select, item_name, item_cost)
|
94 |
+
st.text(result)
|