JairoDanielMT commited on
Commit
573eccc
1 Parent(s): 839e2e2

Deployment of Bananas Quality App v2 in Hugging Face Space

Browse files
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torchvision.transforms as transforms
4
+ import cv2
5
+ import numpy as np
6
+ from PIL import Image
7
+ import gradio as gr
8
+
9
+ # Preprocesamiento de im谩genes
10
+ transform = transforms.Compose([
11
+ transforms.Resize((512, 512)),
12
+ transforms.ToTensor(),
13
+ transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
14
+ ])
15
+
16
+ class CNN(nn.Module):
17
+ def __init__(self):
18
+ super(CNN, self).__init__()
19
+ self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1)
20
+ self.relu = nn.ReLU()
21
+ self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
22
+ self.fc1 = nn.Linear(32 * 256 * 256, 128)
23
+ self.fc2 = nn.Linear(128, 4) # 4 clases: baja, regular, excelente, mala
24
+
25
+ def forward(self, x):
26
+ x = self.conv1(x)
27
+ x = self.relu(x)
28
+ x = self.maxpool(x)
29
+ x = x.view(x.size(0), -1)
30
+ x = self.fc1(x)
31
+ x = self.relu(x)
32
+ x = self.fc2(x)
33
+ return x
34
+
35
+ # Configurar dispositivo en CPU
36
+ device = torch.device('cpu')
37
+
38
+ # Cargar el modelo previamente guardado
39
+ model = CNN().to(device)
40
+ model.load_state_dict(torch.load('calidadplatano.pth', map_location=device))
41
+ model.eval()
42
+
43
+ # Funci贸n para clasificar la imagen de entrada
44
+ def classify_image(input_image):
45
+ input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
46
+ input_image = Image.fromarray(input_image)
47
+ input_image = transform(input_image).unsqueeze(0).to(device)
48
+ output = model(input_image)
49
+ probabilities = torch.softmax(output, dim=1).squeeze().detach().cpu().numpy()
50
+ class_labels = ['baja', 'regular', 'excelente', 'mala']
51
+ predicted_class = class_labels[np.argmax(probabilities)]
52
+ confidence = probabilities[np.argmax(probabilities)]
53
+ return predicted_class, confidence
54
+
55
+ # Definir la interfaz gr谩fica de usuario
56
+ inputs = gr.inputs.Image()
57
+ outputs = gr.outputs.Label(num_top_classes=1)
58
+ examples=[["imagenesDeEjemplos/1.webp"],["imagenesDeEjemplos/2.webp"],["imagenesDeEjemplos/3.webp"],["imagenesDeEjemplos/4.webp"],["imagenesDeEjemplos/5.webp"]]
59
+
60
+ def process_image(input_image):
61
+ predicted_class, confidence = classify_image(input_image)
62
+ return predicted_class + " (" + str(round(confidence * 100, 2)) + "%)"
63
+
64
+ title = "Clasificaci贸n de calidad de pl谩tanos"
65
+ description = "Carga una imagen de pl谩tano y obt茅n la clasificaci贸n de calidad."
66
+ iface = gr.Interface(fn=process_image, inputs=inputs, outputs=outputs, title=title, description=description,examples=examples)
67
+ iface.launch()
calidadplatano.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:554cdad36dab1919556a7a87fc766101846ce18cc43184bc9b84f166246b61f2
3
+ size 1073750287
imagenesDeEjemplos/1.webp ADDED
imagenesDeEjemplos/2.webp ADDED
imagenesDeEjemplos/3.webp ADDED
imagenesDeEjemplos/4.webp ADDED
imagenesDeEjemplos/5.webp ADDED
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch
2
+ torchvision
3
+ numpy
4
+ Pillow
5
+ gradio
6
+ opencv-python