arturevs commited on
Commit
7ea11ff
·
1 Parent(s): e0f5958

dealing with png

Browse files
Files changed (2) hide show
  1. app.py +15 -9
  2. requirements.txt +1 -1
app.py CHANGED
@@ -30,6 +30,9 @@ def load_cnn():
30
 
31
  # Função para pré-processar a imagem e extrair características
32
  def extract_features(image, cnn_model):
 
 
 
33
  transform = transforms.Compose([
34
  transforms.Resize((256, 256)),
35
  transforms.ToTensor(),
@@ -51,15 +54,18 @@ cnn_model = load_cnn()
51
  # Upload da imagem
52
  uploaded_file = st.file_uploader("Escolha uma imagem...", type=["jpg", "jpeg", "png"])
53
  if uploaded_file is not None:
54
- image = Image.open(uploaded_file)
55
- st.image(image, caption="Imagem enviada", use_column_width=True)
 
56
 
57
- # Extrair características da imagem usando a CNN
58
- image_features = extract_features(image, cnn_model)
59
 
60
- # Fazer a previsão
61
- prediction = model.predict(image_features)
62
- predicted_class = label_encoder.inverse_transform(prediction)[0]
63
 
64
- # Exibir o resultado
65
- st.write(f"Classificação: **{predicted_class}**")
 
 
 
30
 
31
  # Função para pré-processar a imagem e extrair características
32
  def extract_features(image, cnn_model):
33
+ # Converter a imagem para RGB (remover canal alfa se existir)
34
+ image = image.convert("RGB")
35
+
36
  transform = transforms.Compose([
37
  transforms.Resize((256, 256)),
38
  transforms.ToTensor(),
 
54
  # Upload da imagem
55
  uploaded_file = st.file_uploader("Escolha uma imagem...", type=["jpg", "jpeg", "png"])
56
  if uploaded_file is not None:
57
+ try:
58
+ image = Image.open(uploaded_file)
59
+ st.image(image, caption="Imagem enviada", use_column_width=True)
60
 
61
+ # Extrair características da imagem usando a CNN
62
+ image_features = extract_features(image, cnn_model)
63
 
64
+ # Fazer a previsão
65
+ prediction = model.predict(image_features)
66
+ predicted_class = label_encoder.inverse_transform(prediction)[0]
67
 
68
+ # Exibir o resultado
69
+ st.write(f"Classificação: **{predicted_class}**")
70
+ except Exception as e:
71
+ st.error(f"Erro ao processar a imagem: {e}")
requirements.txt CHANGED
@@ -9,4 +9,4 @@ matplotlib==3.7.2
9
  streamlit==1.27.0
10
  joblib==1.3.2
11
  huggingface_hub==0.19.4
12
- Pillow>=7.1.0,<10 # Versão compatível com todos os pacotes
 
9
  streamlit==1.27.0
10
  joblib==1.3.2
11
  huggingface_hub==0.19.4
12
+ Pillow==10.0.0