|
|
import gradio as gr |
|
|
from ultralytics import YOLO |
|
|
import cv2 |
|
|
import numpy as np |
|
|
from PIL import Image |
|
|
|
|
|
|
|
|
model = YOLO('./best.pt') |
|
|
|
|
|
|
|
|
def detectar_objetos(image): |
|
|
|
|
|
image_bgr = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR) |
|
|
|
|
|
|
|
|
results = model.predict(source=image_bgr, conf=0.25, save=False) |
|
|
|
|
|
|
|
|
annotated_image = image_bgr.copy() |
|
|
for box in results[0].boxes: |
|
|
|
|
|
x1, y1, x2, y2 = map(int, box.xyxy[0]) |
|
|
cls = int(box.cls) |
|
|
|
|
|
|
|
|
if cls == 0: |
|
|
color = (0, 255, 0) |
|
|
label = "Well" |
|
|
elif cls == 1: |
|
|
color = (255, 0, 0) |
|
|
label = "Partially" |
|
|
elif cls == 2: |
|
|
color = (0, 0, 255) |
|
|
label = "Bad" |
|
|
else: |
|
|
color = (255, 255, 255) |
|
|
label = f"Class {cls}" |
|
|
|
|
|
|
|
|
cv2.rectangle(annotated_image, (x1, y1), (x2, y2), color, thickness=2) |
|
|
cv2.putText( |
|
|
annotated_image, label, (x1, y1 - 10), |
|
|
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, thickness=0 |
|
|
) |
|
|
|
|
|
|
|
|
annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB) |
|
|
|
|
|
return annotated_image_rgb |
|
|
|
|
|
|
|
|
css = """ |
|
|
.app-container { background-color: white; } |
|
|
.title-container { |
|
|
background-color: white; |
|
|
display: flex; |
|
|
align-items: center; |
|
|
justify-content: center; |
|
|
height: 300px; |
|
|
font-size: 24px; |
|
|
font-weight: bold; |
|
|
text-align: center; |
|
|
} |
|
|
.centered-image { |
|
|
display: block; |
|
|
margin: auto; |
|
|
} |
|
|
""" |
|
|
|
|
|
|
|
|
examples = [["cacao_1.png"], ["cacao_2.jpg"]] |
|
|
|
|
|
with gr.Blocks(css=css) as demo: |
|
|
|
|
|
with gr.Row(): |
|
|
gr.Image(value="Cacaotin.png", width=300, height=300, elem_id="centered-image") |
|
|
gr.Markdown("<div class='title-container'>Fermentation Level Classification for Cocoa Beans</div>") |
|
|
|
|
|
|
|
|
gr.Markdown("<center><a href='https://github.com/kebincontreras/cocoa_beans_interfaces' target='_blank'><button style='background-color: #007bff; color: white; padding: 10px 20px; border: none; border-radius: 5px; font-size: 16px;'>View on GitHub</button></a></center>") |
|
|
|
|
|
|
|
|
with gr.Row(): |
|
|
img_input = gr.Image(label="Upload Image") |
|
|
img_output = gr.Image(label="Image with Detected Objects") |
|
|
|
|
|
|
|
|
btn_detectar = gr.Button("Detect Objects") |
|
|
btn_detectar.click(detectar_objetos, inputs=img_input, outputs=img_output) |
|
|
|
|
|
|
|
|
gr.Markdown(""" |
|
|
**Cacao Classes According to NTC1252:2021:** |
|
|
- **a) Well-fermented:** |
|
|
- **b) Partially fermented:** |
|
|
- **c) Non-fermented:** |
|
|
""") |
|
|
gr.Image(value="cacao.png", label="a) Well-fermented, b) Partially fermented, c) Non-fermented") |
|
|
|
|
|
|
|
|
gr.Markdown("**Explanation According to NTC1252:2021:** Here you can explain how the NTC1252:2021 norm applies to the classification of fermentation levels.") |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |
|
|
|