StoneSeller commited on
Commit
dc009a7
ยท
verified ยท
1 Parent(s): 18666db

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -0
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()