Ashegh-Sad-Warrior
commited on
Commit
•
87956be
1
Parent(s):
f846ba5
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
|
@@ -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,13 +44,19 @@ colors = {
|
|
44 |
7: (255, 165, 0), # نارنجی
|
45 |
8: (0, 128, 0), # سبز تیره
|
46 |
9: (128, 128, 0), # زیتونی
|
47 |
-
10: (0, 255, 0), #
|
48 |
11: (0, 128, 128), # سبز نفتی
|
49 |
12: (0, 0, 128), # نیوی
|
50 |
13: (75, 0, 130), # ایندیگو
|
51 |
14: (199, 21, 133) # رز متوسط
|
52 |
}
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
# تابع برای تشخیص اشیاء در تصاویر
|
55 |
def detect_and_draw_image(input_image):
|
56 |
try:
|
@@ -58,20 +64,16 @@ def detect_and_draw_image(input_image):
|
|
58 |
input_image_np = np.array(input_image)
|
59 |
print("Image converted to NumPy array.")
|
60 |
|
61 |
-
# تبدیل تصویر به BGR برای OpenCV
|
62 |
-
image_cv = cv2.cvtColor(input_image_np, cv2.COLOR_RGB2BGR)
|
63 |
-
print("Image converted to OpenCV format.")
|
64 |
-
|
65 |
# اجرای مدل روی تصویر با استفاده از آرایه NumPy
|
66 |
-
results = model.predict(source=
|
67 |
print("Model prediction completed.")
|
68 |
|
69 |
# دسترسی به نتایج OBB
|
70 |
-
if hasattr(results[0], '
|
71 |
-
obb_results = results[0].
|
72 |
print("Accessed obb_results.")
|
73 |
else:
|
74 |
-
print("No '
|
75 |
obb_results = None
|
76 |
|
77 |
# بررسی وجود جعبههای شناسایی شده
|
@@ -85,34 +87,37 @@ def detect_and_draw_image(input_image):
|
|
85 |
return input_image, df
|
86 |
|
87 |
counts = {}
|
88 |
-
|
89 |
-
for obb, conf, cls in zip(obb_results.data.cpu().numpy(), obb_results.conf.cpu().numpy(), obb_results.cls.cpu().numpy()):
|
90 |
-
x_center, y_center, width, height, rotation = obb[:5]
|
91 |
-
class_id = int(cls)
|
92 |
-
confidence = float(conf)
|
93 |
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
box_points = cv2.boxPoints(rect)
|
97 |
box_points = np.int0(box_points)
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
100 |
print(f"Drawn OBB for class_id {class_id} with confidence {confidence}.")
|
101 |
|
102 |
# رسم برچسب
|
103 |
label_en, label_fa = class_names.get(class_id, ('unknown', 'ناشناخته'))
|
104 |
-
|
105 |
-
(int(x_center), int(y_center)),
|
106 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2, cv2.LINE_AA)
|
107 |
|
108 |
# شمارش اشیاء
|
109 |
counts[label_en] = counts.get(label_en, 0) + 1
|
110 |
|
111 |
-
# تبدیل تصویر به RGB برای Gradio
|
112 |
-
image_rgb = cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB)
|
113 |
-
output_image = Image.fromarray(image_rgb)
|
114 |
-
print("Image converted back to RGB for Gradio.")
|
115 |
-
|
116 |
# ایجاد DataFrame برای نمایش نتایج
|
117 |
df = pd.DataFrame({
|
118 |
'Label (English)': list(counts.keys()),
|
@@ -121,7 +126,7 @@ def detect_and_draw_image(input_image):
|
|
121 |
})
|
122 |
print("DataFrame created.")
|
123 |
|
124 |
-
return
|
125 |
|
126 |
except Exception as e:
|
127 |
print(f"Error in detect_and_draw_image: {e}")
|
@@ -156,36 +161,48 @@ def detect_and_draw_video(video_path):
|
|
156 |
print(f"Model prediction completed for frame {frame_count}.")
|
157 |
|
158 |
# دسترسی به نتایج OBB
|
159 |
-
if hasattr(results[0], '
|
160 |
-
obb_results = results[0].
|
161 |
print("Accessed obb_results for frame.")
|
162 |
else:
|
163 |
print("No 'obb' attribute found in results[0] for frame.")
|
164 |
obb_results = None
|
165 |
|
166 |
if obb_results is not None and len(obb_results.data) > 0:
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
rect = ((x_center, y_center), (width, height), rotation * 180.0 / np.pi)
|
174 |
box_points = cv2.boxPoints(rect)
|
175 |
box_points = np.int0(box_points)
|
176 |
-
|
177 |
-
|
|
|
|
|
|
|
|
|
178 |
print(f"Drawn OBB for class_id {class_id} with confidence {confidence} in frame {frame_count}.")
|
179 |
|
180 |
# رسم برچسب
|
181 |
label_en, label_fa = class_names.get(class_id, ('unknown', 'ناشناخته'))
|
182 |
-
|
183 |
-
(int(x_center), int(y_center)),
|
184 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
|
185 |
|
186 |
# شمارش اشیاء
|
187 |
overall_counts[label_en] = overall_counts.get(label_en, 0) + 1
|
188 |
|
|
|
|
|
|
|
189 |
frames.append(frame)
|
190 |
print(f"Frame {frame_count} processed.")
|
191 |
|
|
|
3 |
|
4 |
import cv2
|
5 |
from ultralytics import YOLO
|
6 |
+
from PIL import Image, ImageDraw, ImageFont
|
7 |
import gradio as gr
|
8 |
import pandas as pd
|
9 |
import numpy as np
|
|
|
32 |
14: ('swimming pool', 'استخر شنا')
|
33 |
}
|
34 |
|
35 |
+
# رنگها برای هر کلاس (RGB برای PIL)
|
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: (0, 255, 0), # سبز روشن برای class_id=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 |
input_image_np = np.array(input_image)
|
65 |
print("Image converted to NumPy array.")
|
66 |
|
|
|
|
|
|
|
|
|
67 |
# اجرای مدل روی تصویر با استفاده از آرایه NumPy
|
68 |
+
results = model.predict(source=input_image_np, conf=0.3)
|
69 |
print("Model prediction completed.")
|
70 |
|
71 |
# دسترسی به نتایج OBB
|
72 |
+
if hasattr(results[0], 'boxes') and results[0].boxes is not None:
|
73 |
+
obb_results = results[0].boxes
|
74 |
print("Accessed obb_results.")
|
75 |
else:
|
76 |
+
print("No 'boxes' attribute found in results[0].")
|
77 |
obb_results = None
|
78 |
|
79 |
# بررسی وجود جعبههای شناسایی شده
|
|
|
87 |
return input_image, df
|
88 |
|
89 |
counts = {}
|
90 |
+
draw = ImageDraw.Draw(input_image)
|
|
|
|
|
|
|
|
|
91 |
|
92 |
+
# پردازش نتایج و رسم جعبهها
|
93 |
+
for box in obb_results:
|
94 |
+
# دسترسی به دادههای جعبه
|
95 |
+
try:
|
96 |
+
x_center, y_center, width, height, rotation = box.xywh.tolist()
|
97 |
+
except:
|
98 |
+
x_center, y_center, width, height, rotation = box.tolist()[:5]
|
99 |
+
class_id = int(box.cls.tolist()[0]) if hasattr(box, 'cls') else 0
|
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 |
+
# تبدیل نقاط به لیست برای رسم در PIL
|
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 |
+
draw.text((int(x_center), int(y_center)), f'{label_en}: {confidence:.2f}', fill=colors.get(class_id, (0, 255, 0)), font=font)
|
|
|
|
|
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 |
})
|
127 |
print("DataFrame created.")
|
128 |
|
129 |
+
return input_image, df
|
130 |
|
131 |
except Exception as e:
|
132 |
print(f"Error in detect_and_draw_image: {e}")
|
|
|
161 |
print(f"Model prediction completed for frame {frame_count}.")
|
162 |
|
163 |
# دسترسی به نتایج OBB
|
164 |
+
if hasattr(results[0], 'boxes') and results[0].boxes is not None:
|
165 |
+
obb_results = results[0].boxes
|
166 |
print("Accessed obb_results for frame.")
|
167 |
else:
|
168 |
print("No 'obb' attribute found in results[0] for frame.")
|
169 |
obb_results = None
|
170 |
|
171 |
if obb_results is not None and len(obb_results.data) > 0:
|
172 |
+
# تبدیل فریم به PIL Image برای رسم
|
173 |
+
frame_pil = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
174 |
+
draw = ImageDraw.Draw(frame_pil)
|
175 |
+
|
176 |
+
for box in obb_results:
|
177 |
+
try:
|
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 |
+
# تبدیل نقاط به لیست برای رسم در PIL
|
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 |
+
draw.text((int(x_center), int(y_center)), f"{label_en}: {confidence:.2f}", fill=colors.get(class_id, (0, 255, 0)), font=font)
|
|
|
|
|
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 |
|