Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#app7.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from torchvision import transforms
|
| 6 |
+
from ultralyticsplus import YOLO, render_result
|
| 7 |
+
# import matplotlib.pyplot as plt
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
torch.hub.download_url_to_file(
|
| 11 |
+
'https://i.postimg.cc/g2xGJ4Qs/NSTA-Test-IMG-3276.jpg', 'NSTA.jpg')
|
| 12 |
+
torch.hub.download_url_to_file(
|
| 13 |
+
'https://i.postimg.cc/BZCSwj2T/NSTB-Test-IMG-1472.jpg', 'NSTB.jpg')
|
| 14 |
+
torch.hub.download_url_to_file(
|
| 15 |
+
'https://i.postimg.cc/yYY1q7Tw/NSTC-Test-IMG-0118.jpg', 'NSTC.jpg')
|
| 16 |
+
torch.hub.download_url_to_file(
|
| 17 |
+
'https://i.postimg.cc/zD9ZQX6z/KCCA-Test-IMG-3555.jpg', 'KCCA.jpg')
|
| 18 |
+
torch.hub.download_url_to_file(
|
| 19 |
+
'https://i.postimg.cc/vZLPXP7L/KCCB-Test-IMG-3733.jpg', 'KCCB.jpg')
|
| 20 |
+
torch.hub.download_url_to_file(
|
| 21 |
+
'https://i.postimg.cc/BZFYqFmF/KCCC-Test-IMG-3892.jpg', 'KCCC.jpg')
|
| 22 |
+
|
| 23 |
+
def detect_objects(image_path, selected_model):
|
| 24 |
+
# Open the image file and resize it
|
| 25 |
+
image = Image.open(image_path)
|
| 26 |
+
resized_image = image.resize((1024, 768))
|
| 27 |
+
|
| 28 |
+
#default
|
| 29 |
+
# model_path = ('MvitHYF/v8mvitcocoaseed2024')
|
| 30 |
+
|
| 31 |
+
# Load the model
|
| 32 |
+
nstcurrentmodel = "NST Model"
|
| 33 |
+
kcccurrentmodel = "KCC Model"
|
| 34 |
+
currentmodel = str(selected_model)
|
| 35 |
+
nstcurrentmodel = "NST Model"
|
| 36 |
+
if currentmodel == nstcurrentmodel:
|
| 37 |
+
print("this is nst model")
|
| 38 |
+
#best of NST Model
|
| 39 |
+
#model_path = ('code/runs/train45/best.pt')
|
| 40 |
+
model_path = ('MvitHYF/v8mvitcocoaseed2024')
|
| 41 |
+
elif currentmodel == kcccurrentmodel:
|
| 42 |
+
print("this is kcc model")
|
| 43 |
+
#model_path = ('code/runs/kcc/v8/train83/best.pt')
|
| 44 |
+
model_path = ('MvitHYF/kccv8mvitcocoaseed2024')
|
| 45 |
+
|
| 46 |
+
#model_path = ('code/runs/train45/best.pt')
|
| 47 |
+
# model_path = ('MvitHYF/v8mvitcocoaseed2024')
|
| 48 |
+
model = YOLO(model_path)
|
| 49 |
+
# model = YOLO('MvitHYF/v8mvitcocoaseed2024')
|
| 50 |
+
|
| 51 |
+
# Set model parameters
|
| 52 |
+
model.overrides['conf'] = 'null' # NMS confidence threshold
|
| 53 |
+
model.overrides['iou'] = 0.70 # NMS IoU threshold
|
| 54 |
+
model.overrides['agnostic_nms'] = True # NMS class-agnostic
|
| 55 |
+
model.overrides['max_det'] = 1000 # maximum number of detections per image
|
| 56 |
+
|
| 57 |
+
# Perform inference
|
| 58 |
+
results = model.predict(resized_image)
|
| 59 |
+
|
| 60 |
+
#debug check count
|
| 61 |
+
# print("see")
|
| 62 |
+
# print(results)
|
| 63 |
+
cls = results[0].boxes.cls
|
| 64 |
+
# print(cls)
|
| 65 |
+
strcls = str(cls)
|
| 66 |
+
# print(type(strcls))
|
| 67 |
+
# print(strcls)
|
| 68 |
+
count_classa = strcls.count('0')
|
| 69 |
+
# print('Count of classA:', count_classa)
|
| 70 |
+
count_classb = strcls.count('1')
|
| 71 |
+
# print('Count of classB', count_classb)
|
| 72 |
+
count_classc = strcls.count('2')
|
| 73 |
+
# print('Count of classC:', count_classc)
|
| 74 |
+
intcount_classa = int(count_classa)
|
| 75 |
+
intcount_classb = int(count_classb)
|
| 76 |
+
intcount_classc = int(count_classc)
|
| 77 |
+
total = intcount_classa + intcount_classb + intcount_classc
|
| 78 |
+
# print("end see")
|
| 79 |
+
|
| 80 |
+
# gr.Image(label="Pie Graph")
|
| 81 |
+
# Format the output to print the counts
|
| 82 |
+
output_counts = f"Totoal cocoa seeds: {total}\nClass A: {count_classa} seeds\nClass B: {count_classb} seeds\nClass C: {count_classc} seeds"
|
| 83 |
+
|
| 84 |
+
# Render results
|
| 85 |
+
render = render_result(model=model, image=resized_image, result=results[0])
|
| 86 |
+
|
| 87 |
+
print("Selected model:", selected_model)
|
| 88 |
+
|
| 89 |
+
#return render, output_counts, plotbar
|
| 90 |
+
return render, output_counts, "You have selected the " + str(selected_model)
|
| 91 |
+
|
| 92 |
+
#csspath = 'code/yolov8newultlt/gradio.css'
|
| 93 |
+
|
| 94 |
+
with gr.Blocks(theme='ParityError/LimeFace') as demo:
|
| 95 |
+
|
| 96 |
+
with gr.Row():
|
| 97 |
+
with gr.Column():
|
| 98 |
+
gr.Interface(fn=detect_objects,
|
| 99 |
+
inputs=[gr.Image(type="filepath", label="Upload an Image"), gr.Dropdown(choices=["NST Model", "KCC Model"])],
|
| 100 |
+
outputs=[gr.Image(type="filepath", label="Result"), gr.Textbox(label="Detection Counts"), gr.Textbox(label="Selected Model")],
|
| 101 |
+
title="YOLOv8 Cocoa Seed Classification",
|
| 102 |
+
description="Upload an image to detect objects using YOLO.",
|
| 103 |
+
#html = gr.HTML(value="<p>This is another paragraph123.</p>"),
|
| 104 |
+
examples = [["NSTA.jpg"],
|
| 105 |
+
["NSTB.jpg"],
|
| 106 |
+
["NSTC.jpg"],
|
| 107 |
+
["KCCA.jpg"],
|
| 108 |
+
["KCCB.jpg"],
|
| 109 |
+
["KCCC.jpg"]],
|
| 110 |
+
cache_examples = bool(False)
|
| 111 |
+
#css=csspath,
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# with gr.Row(): #original Column
|
| 115 |
+
# with gr.Row(): #original Column
|
| 116 |
+
# example = [[gr.Image(value="NSTA.jpg", interactive = bool(True)), gr.Markdown(value='**label 5**')],
|
| 117 |
+
# [gr.Image(value="NSTB.jpg", interactive = bool(True)), gr.Markdown(value='**label 5**')],
|
| 118 |
+
# [gr.Image(value="NSTC.jpg", interactive = bool(True)), gr.Markdown(value='**label 5**')],
|
| 119 |
+
# [gr.Image(value="KCCA.jpg", interactive = bool(True)), gr.Markdown(value='**label 5**')],
|
| 120 |
+
# [gr.Image(value="KCCB.jpg", interactive = bool(True)), gr.Markdown(value='**label 5**')],
|
| 121 |
+
# [gr.Image(value="KCCC.jpg", interactive = bool(True)), gr.Markdown(value='**label 5**')]]
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
with gr.Row():
|
| 125 |
+
with gr.Row():
|
| 126 |
+
# gr.HTML(value="<b>Class A</b> <p>Class A is the best from all 3 classes. It have the best of physical appreance eg. shape, size, texture</p>"),
|
| 127 |
+
gr.HTML(value="<p> NSTA NSTB NTSC KCCA KCCB KCCC</p> <dl> <dt><b>Class A</b></dt> <dd>Class A is the best from all 3 classes. It have the best of physical appreance eg. shape, size, texture</dd> </dl> <dt><b>Class B</b></dt> <dd>Class B most of the cocoa seed have physical appreance similar to class A. <br> But the size must me smaller and texture is not smmoth as class A</dd> <dt><b>Class C</b></dt> <dd>Class C is the worst from all 3 classes. Its the smallest, rough texter and have a irregular shape </dd> </dl></dl>")
|
| 128 |
+
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
demo.queue().launch(share=True)
|