|
|
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 = (0, 0, 255) |
|
|
label = "Partially" |
|
|
elif cls == 2: |
|
|
color = (255, 0, 0) |
|
|
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=1 |
|
|
) |
|
|
|
|
|
|
|
|
annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB) |
|
|
|
|
|
return annotated_image_rgb |
|
|
|
|
|
|
|
|
examples = [["cacao_1.png"], ["cacao_2.jpg"]] |
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=detectar_objetos, |
|
|
inputs=gr.Image(label="Upload Image"), |
|
|
outputs=gr.Image(label="Image with Detected Objects"), |
|
|
title="Fermentation Level Classification for Cocoa Beans", |
|
|
examples=examples, |
|
|
description=""" |
|
|
**Cacao Classes According to NTC1252:2021:** |
|
|
- **a) Well-fermented:** Optimal fermentation process. |
|
|
- **b) Partially fermented:** Incomplete fermentation process. |
|
|
- **c) Non-fermented:** Lack of adequate fermentation. |
|
|
|
|
|
**Explanation According to NTC1252:2021:** Here you can explain how the NTC1252:2021 norm applies to the classification of fermentation levels. |
|
|
""" |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
iface.launch() |
|
|
|
|
|
|