Ashegh-Sad-Warrior
commited on
Commit
•
8e3a911
1
Parent(s):
d56e998
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@
|
|
3 |
|
4 |
import cv2
|
5 |
from ultralytics import YOLO
|
6 |
-
from PIL import Image
|
7 |
import gradio as gr
|
8 |
import pandas as pd
|
9 |
import numpy as np
|
@@ -11,7 +11,7 @@ import tempfile
|
|
11 |
import os
|
12 |
|
13 |
# بارگذاری مدل آموزشدیده شما
|
14 |
-
model = YOLO('
|
15 |
|
16 |
# تعریف نام کلاسها به انگلیسی و فارسی
|
17 |
class_names = {
|
@@ -32,7 +32,7 @@ class_names = {
|
|
32 |
14: ('swimming pool', 'استخر شنا')
|
33 |
}
|
34 |
|
35 |
-
# رنگها برای هر کلاس (
|
36 |
colors = {
|
37 |
0: (255, 0, 0), # قرمز
|
38 |
1: (0, 255, 0), # سبز
|
@@ -44,19 +44,12 @@ colors = {
|
|
44 |
7: (255, 165, 0), # نارنجی
|
45 |
8: (0, 128, 0), # سبز تیره
|
46 |
9: (128, 128, 0), # زیتونی
|
47 |
-
10: (
|
48 |
11: (0, 128, 128), # سبز نفتی
|
49 |
12: (0, 0, 128), # نیوی
|
50 |
13: (75, 0, 130), # ایندیگو
|
51 |
14: (199, 21, 133) # رز متوسط
|
52 |
}
|
53 |
-
|
54 |
-
# فونت برای نوشتن برچسبها
|
55 |
-
try:
|
56 |
-
font = ImageFont.truetype("arial.ttf", 15)
|
57 |
-
except IOError:
|
58 |
-
font = ImageFont.load_default()
|
59 |
-
|
60 |
# تابع برای تشخیص اشیاء در تصاویر
|
61 |
def detect_and_draw_image(input_image):
|
62 |
try:
|
@@ -64,8 +57,12 @@ def detect_and_draw_image(input_image):
|
|
64 |
input_image_np = np.array(input_image)
|
65 |
print("Image converted to NumPy array.")
|
66 |
|
67 |
-
#
|
68 |
-
|
|
|
|
|
|
|
|
|
69 |
print("Model prediction completed.")
|
70 |
|
71 |
# دسترسی به نتایج OBB
|
@@ -87,37 +84,34 @@ def detect_and_draw_image(input_image):
|
|
87 |
return input_image, df
|
88 |
|
89 |
counts = {}
|
90 |
-
draw = ImageDraw.Draw(input_image)
|
91 |
-
|
92 |
# پردازش نتایج و رسم جعبهها
|
93 |
-
for
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
confidence = float(box.conf.tolist()[0]) if hasattr(box, 'conf') else 0.0
|
101 |
-
|
102 |
-
# محاسبه مختصات گوشهها با استفاده از OpenCV
|
103 |
-
rect = ((x_center, y_center), (width, height), rotation * 180.0 / np.pi)
|
104 |
box_points = cv2.boxPoints(rect)
|
105 |
box_points = np.int0(box_points)
|
106 |
-
|
107 |
-
|
108 |
-
box_points = [(int(point[0]), int(point[1])) for point in box_points]
|
109 |
-
|
110 |
-
# رسم جعبه
|
111 |
-
draw.polygon(box_points, outline=colors.get(class_id, (0, 255, 0)))
|
112 |
print(f"Drawn OBB for class_id {class_id} with confidence {confidence}.")
|
113 |
|
114 |
# رسم برچسب
|
115 |
label_en, label_fa = class_names.get(class_id, ('unknown', 'ناشناخته'))
|
116 |
-
|
|
|
|
|
117 |
|
118 |
# شمارش اشیاء
|
119 |
counts[label_en] = counts.get(label_en, 0) + 1
|
120 |
|
|
|
|
|
|
|
|
|
|
|
121 |
# ایجاد DataFrame برای نمایش نتایج
|
122 |
df = pd.DataFrame({
|
123 |
'Label (English)': list(counts.keys()),
|
@@ -126,7 +120,7 @@ def detect_and_draw_image(input_image):
|
|
126 |
})
|
127 |
print("DataFrame created.")
|
128 |
|
129 |
-
return
|
130 |
|
131 |
except Exception as e:
|
132 |
print(f"Error in detect_and_draw_image: {e}")
|
@@ -169,40 +163,28 @@ def detect_and_draw_video(video_path):
|
|
169 |
obb_results = None
|
170 |
|
171 |
if obb_results is not None and len(obb_results.data) > 0:
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
x_center, y_center, width, height, rotation = box.xywh.tolist()
|
179 |
-
except:
|
180 |
-
x_center, y_center, width, height, rotation = box.tolist()[:5]
|
181 |
-
class_id = int(box.cls.tolist()[0]) if hasattr(box, 'cls') else 0
|
182 |
-
confidence = float(box.conf.tolist()[0]) if hasattr(box, 'conf') else 0.0
|
183 |
-
|
184 |
-
# محاسبه مختصات گوشهها با استفاده از OpenCV
|
185 |
rect = ((x_center, y_center), (width, height), rotation * 180.0 / np.pi)
|
186 |
box_points = cv2.boxPoints(rect)
|
187 |
box_points = np.int0(box_points)
|
188 |
-
|
189 |
-
|
190 |
-
box_points = [(int(point[0]), int(point[1])) for point in box_points]
|
191 |
-
|
192 |
-
# رسم جعبه
|
193 |
-
draw.polygon(box_points, outline=colors.get(class_id, (0, 255, 0)))
|
194 |
print(f"Drawn OBB for class_id {class_id} with confidence {confidence} in frame {frame_count}.")
|
195 |
|
196 |
# رسم برچسب
|
197 |
label_en, label_fa = class_names.get(class_id, ('unknown', 'ناشناخته'))
|
198 |
-
|
|
|
|
|
199 |
|
200 |
# شمارش اشیاء
|
201 |
overall_counts[label_en] = overall_counts.get(label_en, 0) + 1
|
202 |
|
203 |
-
# تبدیل فریم به آرایه NumPy برای ذخیره
|
204 |
-
frame = cv2.cvtColor(np.array(frame_pil), cv2.COLOR_RGB2BGR)
|
205 |
-
|
206 |
frames.append(frame)
|
207 |
print(f"Frame {frame_count} processed.")
|
208 |
|
|
|
3 |
|
4 |
import cv2
|
5 |
from ultralytics import YOLO
|
6 |
+
from PIL import Image
|
7 |
import gradio as gr
|
8 |
import pandas as pd
|
9 |
import numpy as np
|
|
|
11 |
import os
|
12 |
|
13 |
# بارگذاری مدل آموزشدیده شما
|
14 |
+
model = YOLO('/content/best.pt') # یا '/content/best.pt' بر اساس مدل مورد نظر شما
|
15 |
|
16 |
# تعریف نام کلاسها به انگلیسی و فارسی
|
17 |
class_names = {
|
|
|
32 |
14: ('swimming pool', 'استخر شنا')
|
33 |
}
|
34 |
|
35 |
+
# رنگها برای هر کلاس (BGR برای OpenCV)
|
36 |
colors = {
|
37 |
0: (255, 0, 0), # قرمز
|
38 |
1: (0, 255, 0), # سبز
|
|
|
44 |
7: (255, 165, 0), # نارنجی
|
45 |
8: (0, 128, 0), # سبز تیره
|
46 |
9: (128, 128, 0), # زیتونی
|
47 |
+
10: (128, 0, 0), # سرخ کلید
|
48 |
11: (0, 128, 128), # سبز نفتی
|
49 |
12: (0, 0, 128), # نیوی
|
50 |
13: (75, 0, 130), # ایندیگو
|
51 |
14: (199, 21, 133) # رز متوسط
|
52 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
# تابع برای تشخیص اشیاء در تصاویر
|
54 |
def detect_and_draw_image(input_image):
|
55 |
try:
|
|
|
57 |
input_image_np = np.array(input_image)
|
58 |
print("Image converted to NumPy array.")
|
59 |
|
60 |
+
# تبدیل تصویر به BGR برای OpenCV
|
61 |
+
image_cv = cv2.cvtColor(input_image_np, cv2.COLOR_RGB2BGR)
|
62 |
+
print("Image converted to OpenCV format.")
|
63 |
+
|
64 |
+
# اجرای مدل روی تصویر با استفاده از آرایه NumPy
|
65 |
+
results = model.predict(source=image_cv, conf=0.3)
|
66 |
print("Model prediction completed.")
|
67 |
|
68 |
# دسترسی به نتایج OBB
|
|
|
84 |
return input_image, df
|
85 |
|
86 |
counts = {}
|
|
|
|
|
87 |
# پردازش نتایج و رسم جعبهها
|
88 |
+
for obb, conf, cls in zip(obb_results.data.cpu().numpy(), obb_results.conf.cpu().numpy(), obb_results.cls.cpu().numpy()):
|
89 |
+
x_center, y_center, width, height, rotation = obb[:5]
|
90 |
+
class_id = int(cls)
|
91 |
+
confidence = float(conf)
|
92 |
+
|
93 |
+
# رسم جعبه چرخان با استفاده از OpenCV
|
94 |
+
rect = ((x_center, y_center), (width, height), rotation * 180.0 / np.pi) # تبدیل رادیان به درجه
|
|
|
|
|
|
|
|
|
95 |
box_points = cv2.boxPoints(rect)
|
96 |
box_points = np.int0(box_points)
|
97 |
+
color = colors.get(class_id, (0, 255, 0))
|
98 |
+
cv2.drawContours(image_cv, [box_points], 0, color, 2)
|
|
|
|
|
|
|
|
|
99 |
print(f"Drawn OBB for class_id {class_id} with confidence {confidence}.")
|
100 |
|
101 |
# رسم برچسب
|
102 |
label_en, label_fa = class_names.get(class_id, ('unknown', 'ناشناخته'))
|
103 |
+
cv2.putText(image_cv, f'{label_en}: {confidence:.2f}',
|
104 |
+
(int(x_center), int(y_center)),
|
105 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2, cv2.LINE_AA)
|
106 |
|
107 |
# شمارش اشیاء
|
108 |
counts[label_en] = counts.get(label_en, 0) + 1
|
109 |
|
110 |
+
# تبدیل تصویر به RGB برای Gradio
|
111 |
+
image_rgb = cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB)
|
112 |
+
output_image = Image.fromarray(image_rgb)
|
113 |
+
print("Image converted back to RGB for Gradio.")
|
114 |
+
|
115 |
# ایجاد DataFrame برای نمایش نتایج
|
116 |
df = pd.DataFrame({
|
117 |
'Label (English)': list(counts.keys()),
|
|
|
120 |
})
|
121 |
print("DataFrame created.")
|
122 |
|
123 |
+
return output_image, df
|
124 |
|
125 |
except Exception as e:
|
126 |
print(f"Error in detect_and_draw_image: {e}")
|
|
|
163 |
obb_results = None
|
164 |
|
165 |
if obb_results is not None and len(obb_results.data) > 0:
|
166 |
+
for obb, conf, cls in zip(obb_results.data.cpu().numpy(), obb_results.conf.cpu().numpy(), obb_results.cls.cpu().numpy()):
|
167 |
+
x_center, y_center, width, height, rotation = obb[:5]
|
168 |
+
class_id = int(cls)
|
169 |
+
confidence = float(conf)
|
170 |
+
|
171 |
+
# رسم جعبه چرخان با استفاده از OpenCV
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
rect = ((x_center, y_center), (width, height), rotation * 180.0 / np.pi)
|
173 |
box_points = cv2.boxPoints(rect)
|
174 |
box_points = np.int0(box_points)
|
175 |
+
color = colors.get(class_id, (0, 255, 0))
|
176 |
+
cv2.drawContours(frame, [box_points], 0, color, 2)
|
|
|
|
|
|
|
|
|
177 |
print(f"Drawn OBB for class_id {class_id} with confidence {confidence} in frame {frame_count}.")
|
178 |
|
179 |
# رسم برچسب
|
180 |
label_en, label_fa = class_names.get(class_id, ('unknown', 'ناشناخته'))
|
181 |
+
cv2.putText(frame, f"{label_en}: {confidence:.2f}",
|
182 |
+
(int(x_center), int(y_center)),
|
183 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
|
184 |
|
185 |
# شمارش اشیاء
|
186 |
overall_counts[label_en] = overall_counts.get(label_en, 0) + 1
|
187 |
|
|
|
|
|
|
|
188 |
frames.append(frame)
|
189 |
print(f"Frame {frame_count} processed.")
|
190 |
|