Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
import pandas as pd
|
| 8 |
+
from collections import Counter
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
# ๋ชจ๋ธ ๋ก๋
|
| 12 |
+
model = YOLO("best.pt")
|
| 13 |
+
|
| 14 |
+
# ํด๋์ค๋ณ ์์ ์ ์
|
| 15 |
+
class_colors = {
|
| 16 |
+
'freshripe': (50, 205, 50),
|
| 17 |
+
'freshunripe': (173, 255, 47),
|
| 18 |
+
'overripe': (255, 165, 0),
|
| 19 |
+
'ripe': (0, 128, 0),
|
| 20 |
+
'rotten': (128, 0, 0),
|
| 21 |
+
'unripe': (255, 255, 0)
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
def predict_image(image):
|
| 25 |
+
if image is None:
|
| 26 |
+
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์."
|
| 27 |
+
|
| 28 |
+
# ์ด๋ฏธ์ง ๋ณต์ฌ๋ณธ ์์ฑ
|
| 29 |
+
img = image.copy()
|
| 30 |
+
h, w = img.shape[:2]
|
| 31 |
+
|
| 32 |
+
# ๋ชจ๋ธ ์์ธก
|
| 33 |
+
results = model.predict(
|
| 34 |
+
source=img,
|
| 35 |
+
imgsz=640, # ๊ธฐ๋ณธ ์ด๋ฏธ์ง ํฌ๊ธฐ
|
| 36 |
+
conf=0.5, # ์ ๋ขฐ๋ ์๊ณ๊ฐ
|
| 37 |
+
save=False,
|
| 38 |
+
verbose=False
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
result = results[0]
|
| 42 |
+
result_img = result.plot() # ๋ชจ๋ธ์ด ๊ทธ๋ฆฐ ์์ธก ๊ฒฐ๊ณผ
|
| 43 |
+
|
| 44 |
+
# ๊ฒฐ๊ณผ ๋ถ์
|
| 45 |
+
boxes = result.boxes.data.cpu().numpy()
|
| 46 |
+
cls_names = result.names
|
| 47 |
+
|
| 48 |
+
class_counts = Counter()
|
| 49 |
+
result_text = "์์ธก ๊ฒฐ๊ณผ:\n"
|
| 50 |
+
|
| 51 |
+
# matplotlib ๊ทธ๋ฆผ ์์ฑ
|
| 52 |
+
fig, ax = plt.subplots(1, figsize=(12, 8))
|
| 53 |
+
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 54 |
+
ax.imshow(img_rgb)
|
| 55 |
+
ax.set_title("๋ฐ๋๋ ์ต์ ๋ถ๋ฅ ๊ฒฐ๊ณผ")
|
| 56 |
+
|
| 57 |
+
for box in boxes:
|
| 58 |
+
x1, y1, x2, y2, conf, cls_id = box
|
| 59 |
+
pred_class = cls_names[int(cls_id)]
|
| 60 |
+
class_counts[pred_class] += 1
|
| 61 |
+
|
| 62 |
+
# ๊ฒฐ๊ณผ ํ
์คํธ ์ถ๊ฐ
|
| 63 |
+
result_text += f"- ํด๋์ค: {pred_class}, ํ๋ฅ : {conf:.2f}\n"
|
| 64 |
+
result_text += f" ์์น: [{int(x1)}, {int(y1)}, {int(x2)}, {int(y2)}]\n"
|
| 65 |
+
|
| 66 |
+
# ๋ฐ์ค ๊ทธ๋ฆฌ๊ธฐ
|
| 67 |
+
rect = plt.Rectangle((x1, y1), x2-x1, y2-y1,
|
| 68 |
+
fill=False,
|
| 69 |
+
edgecolor=tuple(c/255 for c in class_colors.get(pred_class, (255, 0, 0))),
|
| 70 |
+
linewidth=2)
|
| 71 |
+
ax.add_patch(rect)
|
| 72 |
+
|
| 73 |
+
# ๋ ์ด๋ธ ํ
์คํธ ์ถ๊ฐ
|
| 74 |
+
label = f"{pred_class} {conf:.2f}"
|
| 75 |
+
ax.text(x1, y1-5, label,
|
| 76 |
+
color='white',
|
| 77 |
+
fontsize=10,
|
| 78 |
+
bbox=dict(facecolor=tuple(c/255 for c in class_colors.get(pred_class, (255, 0, 0))),
|
| 79 |
+
alpha=0.7,
|
| 80 |
+
pad=2))
|
| 81 |
+
|
| 82 |
+
ax.axis('off')
|
| 83 |
+
|
| 84 |
+
# ํด๋์ค๋ณ ๊ฐ์ ํฉ๊ณ ์ถ๊ฐ
|
| 85 |
+
result_text += "\nํด๋์ค๋ณ ๊ฐ์:\n"
|
| 86 |
+
for cls_name, count in class_counts.items():
|
| 87 |
+
result_text += f"- {cls_name}: {count}๊ฐ\n"
|
| 88 |
+
|
| 89 |
+
if len(boxes) == 0:
|
| 90 |
+
result_text += "\n๋ฐ๋๋๊ฐ ๊ฐ์ง๋์ง ์์์ต๋๋ค."
|
| 91 |
+
|
| 92 |
+
# ๊ทธ๋ฆผ์ ์ด๋ฏธ์ง๋ก ๋ณํ
|
| 93 |
+
plt.tight_layout()
|
| 94 |
+
# ๊ทธ๋ฆผ์ ๋ฉ๋ชจ๋ฆฌ์์ ์ด๋ฏธ์ง๋ก ์ ์ฅ
|
| 95 |
+
fig.canvas.draw()
|
| 96 |
+
vis_img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
|
| 97 |
+
vis_img = vis_img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
|
| 98 |
+
plt.close(fig)
|
| 99 |
+
|
| 100 |
+
return vis_img, result_text
|
| 101 |
+
|
| 102 |
+
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
| 103 |
+
with gr.Blocks(title="๋ฐ๋๋ ์ต์ ๋ถ๋ฅ๊ธฐ") as demo:
|
| 104 |
+
gr.Markdown("# ๐ ๋ฐ๋๋ ์ต์ ๋ถ๋ฅ๊ธฐ")
|
| 105 |
+
gr.Markdown("๋ฐ๋๋ ์ฌ์ง์ ์
๋ก๋ํ๋ฉด ์ต์ ์ ๋๋ฅผ ๋ถ์ํด ๋๋ฆฝ๋๋ค.")
|
| 106 |
+
|
| 107 |
+
with gr.Row():
|
| 108 |
+
with gr.Column():
|
| 109 |
+
input_image = gr.Image(type="numpy")
|
| 110 |
+
submit_btn = gr.Button("๋ถ์ํ๊ธฐ")
|
| 111 |
+
|
| 112 |
+
with gr.Column():
|
| 113 |
+
output_image = gr.Image(type="numpy")
|
| 114 |
+
output_text = gr.Textbox(label="๋ถ์ ๊ฒฐ๊ณผ", lines=10)
|
| 115 |
+
|
| 116 |
+
submit_btn.click(
|
| 117 |
+
fn=predict_image,
|
| 118 |
+
inputs=input_image,
|
| 119 |
+
outputs=[output_image, output_text]
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# ์์ ์ด๋ฏธ์ง ์ถ๊ฐ (์์ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ค๋ฉด)
|
| 123 |
+
if os.path.exists("examples"):
|
| 124 |
+
example_images = [f"examples/{f}" for f in os.listdir("examples") if f.endswith(('.jpg', '.jpeg', '.png'))]
|
| 125 |
+
if example_images:
|
| 126 |
+
gr.Examples(
|
| 127 |
+
examples=example_images,
|
| 128 |
+
inputs=input_image
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
demo.launch()
|