MohammadRezaHalakoo
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -125,7 +125,134 @@ image_interface = gr.Interface(
|
|
| 125 |
outputs=[gr.Image(type="pil"), gr.Dataframe(label="تعداد اشیاء")],
|
| 126 |
title="تشخیص اشیاء در تصاویر هوایی",
|
| 127 |
description="یک تصویر هوایی بارگذاری کنید تا اشیاء شناسایی شده و تعداد آنها را ببینید.",
|
| 128 |
-
examples=['
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
)
|
| 130 |
|
| 131 |
# رابط کاربری ویدئو
|
|
@@ -142,5 +269,23 @@ video_interface = gr.Interface(
|
|
| 142 |
app = gr.TabbedInterface([image_interface, video_interface], ["تشخیص تصویر", "تشخیص ویدئو"])
|
| 143 |
app.launch(debug=True, share=True)
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
# * Running on local URL: http://127.0.0.1:7860
|
| 146 |
# * Running on public URL: https://c0bd255e573eb86c80.gradio.live
|
|
|
|
| 125 |
outputs=[gr.Image(type="pil"), gr.Dataframe(label="تعداد اشیاء")],
|
| 126 |
title="تشخیص اشیاء در تصاویر هوایی",
|
| 127 |
description="یک تصویر هوایی بارگذاری کنید تا اشیاء شناسایی شده و تعداد آنها را ببینید.",
|
| 128 |
+
examples=['# !pip install ultralytics
|
| 129 |
+
# !pip install gradio
|
| 130 |
+
|
| 131 |
+
import cv2
|
| 132 |
+
from ultralytics import YOLO
|
| 133 |
+
from PIL import Image, ImageDraw
|
| 134 |
+
import gradio as gr
|
| 135 |
+
import pandas as pd
|
| 136 |
+
|
| 137 |
+
# بارگذاری مدل آموزشدیده شما
|
| 138 |
+
model = YOLO('/content/yolo11n-obb.pt')
|
| 139 |
+
|
| 140 |
+
# تعریف نام کلاسها به انگلیسی و فارسی
|
| 141 |
+
class_names = {
|
| 142 |
+
0: ('plane', 'هواپیما'),
|
| 143 |
+
1: ('ship', 'کشتی'),
|
| 144 |
+
2: ('storage tank', 'مخزن ذخیره'),
|
| 145 |
+
3: ('baseball diamond', 'زمین بیسبال'),
|
| 146 |
+
4: ('tennis court', 'زمین تنیس'),
|
| 147 |
+
5: ('basketball court', 'زمین بسکتبال'),
|
| 148 |
+
6: ('ground track field', 'زمین دو و میدانی'),
|
| 149 |
+
7: ('harbor', 'بندرگاه'),
|
| 150 |
+
8: ('bridge', 'پل'),
|
| 151 |
+
9: ('large vehicle', 'خودرو بزرگ'),
|
| 152 |
+
10: ('small vehicle', 'خودرو کوچک'),
|
| 153 |
+
11: ('helicopter', 'هلیکوپتر'),
|
| 154 |
+
12: ('roundabout', 'میدان'),
|
| 155 |
+
13: ('soccer ball field', 'زمین فوتبال'),
|
| 156 |
+
14: ('swimming pool', 'استخر شنا')
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
# تابع برای تشخیص اشیاء در تصاویر
|
| 160 |
+
def detect_and_draw_image(input_image):
|
| 161 |
+
results = model(input_image)
|
| 162 |
+
|
| 163 |
+
# بررسی کردن اینکه آیا اشیاء شناسایی شده اند یا خیر
|
| 164 |
+
if not results or results[0].boxes is None or not results[0].boxes.boxes:
|
| 165 |
+
# اگر هیچ شیء شناسایی نشده باشد
|
| 166 |
+
df = pd.DataFrame({
|
| 167 |
+
'Label (English)': [],
|
| 168 |
+
'Label (Persian)': [],
|
| 169 |
+
'Object Count': []
|
| 170 |
+
})
|
| 171 |
+
return input_image, df
|
| 172 |
+
|
| 173 |
+
detections = results[0].boxes.boxes # دسترسی به نتایج در YOLOv8
|
| 174 |
+
|
| 175 |
+
draw = ImageDraw.Draw(input_image)
|
| 176 |
+
counts = {}
|
| 177 |
+
for detection in detections:
|
| 178 |
+
xmin, ymin, xmax, ymax, conf, class_id = detection.cpu().numpy()
|
| 179 |
+
label_en, label_fa = class_names.get(int(class_id), ('unknown', 'ناشناخته'))
|
| 180 |
+
counts[label_en] = counts.get(label_en, 0) + 1
|
| 181 |
+
|
| 182 |
+
draw.rectangle([(xmin, ymin), (xmax, ymax)], outline="red", width=2)
|
| 183 |
+
draw.text((xmin, ymin), f"{label_en}: {conf:.2f}", fill="white")
|
| 184 |
+
|
| 185 |
+
df = pd.DataFrame({
|
| 186 |
+
'Label (English)': list(counts.keys()),
|
| 187 |
+
'Label (Persian)': [class_names.get(k, ('unknown', 'ناشناخته'))[1] for k in counts.keys()],
|
| 188 |
+
'Object Count': list(counts.values())
|
| 189 |
+
})
|
| 190 |
+
|
| 191 |
+
return input_image, df
|
| 192 |
+
|
| 193 |
+
# تابع برای تشخیص اشیاء در ویدئوها
|
| 194 |
+
def detect_and_draw_video(video_path):
|
| 195 |
+
cap = cv2.VideoCapture(video_path)
|
| 196 |
+
frames = []
|
| 197 |
+
overall_counts = {}
|
| 198 |
+
seen_objects = [] # لیست برای دنبال کردن اشیاء شناسایی شده
|
| 199 |
+
|
| 200 |
+
while cap.isOpened():
|
| 201 |
+
ret, frame = cap.read()
|
| 202 |
+
if not ret:
|
| 203 |
+
break
|
| 204 |
+
|
| 205 |
+
frame = cv2.resize(frame, (640, 480))
|
| 206 |
+
results = model(frame)
|
| 207 |
+
detections = results[0].boxes.boxes
|
| 208 |
+
|
| 209 |
+
if detections is not None:
|
| 210 |
+
detections = detections.cpu().numpy()
|
| 211 |
+
for detection in detections:
|
| 212 |
+
xmin, ymin, xmax, ymax, conf, class_id = detection
|
| 213 |
+
label_en, label_fa = class_names.get(int(class_id), ('unknown', 'ناشناخته'))
|
| 214 |
+
current_object = (label_en, int(xmin), int(ymin), int(xmax), int(ymax))
|
| 215 |
+
|
| 216 |
+
# بررسی وجود شیء در لیست seen_objects
|
| 217 |
+
if not any(existing[0] == label_en and
|
| 218 |
+
(existing[1] < xmax and existing[3] > xmin and
|
| 219 |
+
existing[2] < ymax and existing[4] > ymin) for existing in seen_objects):
|
| 220 |
+
seen_objects.append(current_object)
|
| 221 |
+
overall_counts[label_en] = overall_counts.get(label_en, 0) + 1
|
| 222 |
+
|
| 223 |
+
# رسم مستطیل و نام شیء بر روی فریم
|
| 224 |
+
cv2.rectangle(frame, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (255, 0, 0), 2)
|
| 225 |
+
cv2.putText(frame, f"{label_en}: {conf:.2f}", (int(xmin), int(ymin) - 10),
|
| 226 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
|
| 227 |
+
|
| 228 |
+
frames.append(frame)
|
| 229 |
+
|
| 230 |
+
cap.release()
|
| 231 |
+
|
| 232 |
+
output_path = 'output.mp4'
|
| 233 |
+
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), 20.0, (640, 480))
|
| 234 |
+
|
| 235 |
+
for frame in frames:
|
| 236 |
+
out.write(frame)
|
| 237 |
+
out.release()
|
| 238 |
+
|
| 239 |
+
# ایجاد DataFrame برای ذخیره نتایج
|
| 240 |
+
df = pd.DataFrame({
|
| 241 |
+
'Label (English)': list(overall_counts.keys()),
|
| 242 |
+
'Label (Persian)': [class_names.get(k, ('unknown', 'ناشناخته'))[1] for k in overall_counts.keys()],
|
| 243 |
+
'Object Count': list(overall_counts.values())
|
| 244 |
+
})
|
| 245 |
+
|
| 246 |
+
return output_path, df
|
| 247 |
+
|
| 248 |
+
# رابط کاربری تصویر
|
| 249 |
+
image_interface = gr.Interface(
|
| 250 |
+
fn=detect_and_draw_image,
|
| 251 |
+
inputs=gr.Image(type="pil", label="بارگذاری تصویر"),
|
| 252 |
+
outputs=[gr.Image(type="pil"), gr.Dataframe(label="تعداد اشیاء")],
|
| 253 |
+
title="تشخیص اشیاء در تصاویر هوایی",
|
| 254 |
+
description="یک تصویر هوایی بارگذاری کنید تا اشیاء شناسایی شده و تعداد آنها را ببینید.",
|
| 255 |
+
examples=['Examples/images/areial_car.jpg', '/Examples/images/arieal_car_1.jpg','Examples/images/t.jpg'] # مسیر نمونههای خود را وارد کنید
|
| 256 |
)
|
| 257 |
|
| 258 |
# رابط کاربری ویدئو
|
|
|
|
| 269 |
app = gr.TabbedInterface([image_interface, video_interface], ["تشخیص تصویر", "تشخیص ویدئو"])
|
| 270 |
app.launch(debug=True, share=True)
|
| 271 |
|
| 272 |
+
# * Running on local URL: http://127.0.0.1:7860
|
| 273 |
+
# * Running on public URL: https://c0bd255e573eb86c80.gradio.live/areial_car.jpg', '/content/example/images/arieal_car_1.jpg'] # مسیر نمونههای خود را وارد کنید
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
# رابط کاربری ویدئو
|
| 277 |
+
video_interface = gr.Interface(
|
| 278 |
+
fn=detect_and_draw_video,
|
| 279 |
+
inputs=gr.Video(label="بارگذاری ویدئو"),
|
| 280 |
+
outputs=[gr.Video(label="ویدئوی پردازش شده"), gr.Dataframe(label="تعداد اشیاء")],
|
| 281 |
+
title="تشخیص اشیاء در ویدئوها",
|
| 282 |
+
description="یک ویدئو بارگذاری کنید تا اشیاء شناسایی شده و تعداد آنها را ببینید.",
|
| 283 |
+
examples=['Examples/video/city.mp4', 'Examples/video/airplane.mp4'] # مسیر نمونههای خود را وارد کنید
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
# اجرای برنامه با استفاده از رابط کاربری تبدار
|
| 287 |
+
app = gr.TabbedInterface([image_interface, video_interface], ["تشخیص تصویر", "تشخیص ویدئو"])
|
| 288 |
+
app.launch(debug=True, share=True)
|
| 289 |
+
|
| 290 |
# * Running on local URL: http://127.0.0.1:7860
|
| 291 |
# * Running on public URL: https://c0bd255e573eb86c80.gradio.live
|