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
|
| 6 |
+
from PIL import Image, ImageDraw
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import pandas as pd
|
| 9 |
+
|
| 10 |
+
# بارگذاری مدل آموزشدیده شما
|
| 11 |
+
model = YOLO('/content/yolo11n-obb.pt')
|
| 12 |
+
|
| 13 |
+
# تعریف نام کلاسها به انگلیسی و فارسی
|
| 14 |
+
class_names = {
|
| 15 |
+
0: ('plane', 'هواپیما'),
|
| 16 |
+
1: ('ship', 'کشتی'),
|
| 17 |
+
2: ('storage tank', 'مخزن ذخیره'),
|
| 18 |
+
3: ('baseball diamond', 'زمین بیسبال'),
|
| 19 |
+
4: ('tennis court', 'زمین تنیس'),
|
| 20 |
+
5: ('basketball court', 'زمین بسکتبال'),
|
| 21 |
+
6: ('ground track field', 'زمین دو و میدانی'),
|
| 22 |
+
7: ('harbor', 'بندرگاه'),
|
| 23 |
+
8: ('bridge', 'پل'),
|
| 24 |
+
9: ('large vehicle', 'خودرو بزرگ'),
|
| 25 |
+
10: ('small vehicle', 'خودرو کوچک'),
|
| 26 |
+
11: ('helicopter', 'هلیکوپتر'),
|
| 27 |
+
12: ('roundabout', 'میدان'),
|
| 28 |
+
13: ('soccer ball field', 'زمین فوتبال'),
|
| 29 |
+
14: ('swimming pool', 'استخر شنا')
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
# تابع برای تشخیص اشیاء در تصاویر
|
| 33 |
+
def detect_and_draw_image(input_image):
|
| 34 |
+
results = model(input_image)
|
| 35 |
+
|
| 36 |
+
# بررسی کردن اینکه آیا اشیاء شناسایی شده اند یا خیر
|
| 37 |
+
if not results or results[0].boxes is None or not results[0].boxes.boxes:
|
| 38 |
+
# اگر هیچ شیء شناسایی نشده باشد
|
| 39 |
+
df = pd.DataFrame({
|
| 40 |
+
'Label (English)': [],
|
| 41 |
+
'Label (Persian)': [],
|
| 42 |
+
'Object Count': []
|
| 43 |
+
})
|
| 44 |
+
return input_image, df
|
| 45 |
+
|
| 46 |
+
detections = results[0].boxes.boxes # دسترسی به نتایج در YOLOv8
|
| 47 |
+
|
| 48 |
+
draw = ImageDraw.Draw(input_image)
|
| 49 |
+
counts = {}
|
| 50 |
+
for detection in detections:
|
| 51 |
+
xmin, ymin, xmax, ymax, conf, class_id = detection.cpu().numpy()
|
| 52 |
+
label_en, label_fa = class_names.get(int(class_id), ('unknown', 'ناشناخته'))
|
| 53 |
+
counts[label_en] = counts.get(label_en, 0) + 1
|
| 54 |
+
|
| 55 |
+
draw.rectangle([(xmin, ymin), (xmax, ymax)], outline="red", width=2)
|
| 56 |
+
draw.text((xmin, ymin), f"{label_en}: {conf:.2f}", fill="white")
|
| 57 |
+
|
| 58 |
+
df = pd.DataFrame({
|
| 59 |
+
'Label (English)': list(counts.keys()),
|
| 60 |
+
'Label (Persian)': [class_names.get(k, ('unknown', 'ناشناخته'))[1] for k in counts.keys()],
|
| 61 |
+
'Object Count': list(counts.values())
|
| 62 |
+
})
|
| 63 |
+
|
| 64 |
+
return input_image, df
|
| 65 |
+
|
| 66 |
+
# تابع برای تشخیص اشیاء در ویدئوها
|
| 67 |
+
def detect_and_draw_video(video_path):
|
| 68 |
+
cap = cv2.VideoCapture(video_path)
|
| 69 |
+
frames = []
|
| 70 |
+
overall_counts = {}
|
| 71 |
+
seen_objects = [] # لیست برای دنبال کردن اشیاء شناسایی شده
|
| 72 |
+
|
| 73 |
+
while cap.isOpened():
|
| 74 |
+
ret, frame = cap.read()
|
| 75 |
+
if not ret:
|
| 76 |
+
break
|
| 77 |
+
|
| 78 |
+
frame = cv2.resize(frame, (640, 480))
|
| 79 |
+
results = model(frame)
|
| 80 |
+
detections = results[0].boxes.boxes
|
| 81 |
+
|
| 82 |
+
if detections is not None:
|
| 83 |
+
detections = detections.cpu().numpy()
|
| 84 |
+
for detection in detections:
|
| 85 |
+
xmin, ymin, xmax, ymax, conf, class_id = detection
|
| 86 |
+
label_en, label_fa = class_names.get(int(class_id), ('unknown', 'ناشناخته'))
|
| 87 |
+
current_object = (label_en, int(xmin), int(ymin), int(xmax), int(ymax))
|
| 88 |
+
|
| 89 |
+
# بررسی وجود شیء در لیست seen_objects
|
| 90 |
+
if not any(existing[0] == label_en and
|
| 91 |
+
(existing[1] < xmax and existing[3] > xmin and
|
| 92 |
+
existing[2] < ymax and existing[4] > ymin) for existing in seen_objects):
|
| 93 |
+
seen_objects.append(current_object)
|
| 94 |
+
overall_counts[label_en] = overall_counts.get(label_en, 0) + 1
|
| 95 |
+
|
| 96 |
+
# رسم مستطیل و نام شیء بر روی فریم
|
| 97 |
+
cv2.rectangle(frame, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (255, 0, 0), 2)
|
| 98 |
+
cv2.putText(frame, f"{label_en}: {conf:.2f}", (int(xmin), int(ymin) - 10),
|
| 99 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
|
| 100 |
+
|
| 101 |
+
frames.append(frame)
|
| 102 |
+
|
| 103 |
+
cap.release()
|
| 104 |
+
|
| 105 |
+
output_path = 'output.mp4'
|
| 106 |
+
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), 20.0, (640, 480))
|
| 107 |
+
|
| 108 |
+
for frame in frames:
|
| 109 |
+
out.write(frame)
|
| 110 |
+
out.release()
|
| 111 |
+
|
| 112 |
+
# ایجاد DataFrame برای ذخیره نتایج
|
| 113 |
+
df = pd.DataFrame({
|
| 114 |
+
'Label (English)': list(overall_counts.keys()),
|
| 115 |
+
'Label (Persian)': [class_names.get(k, ('unknown', 'ناشناخته'))[1] for k in overall_counts.keys()],
|
| 116 |
+
'Object Count': list(overall_counts.values())
|
| 117 |
+
})
|
| 118 |
+
|
| 119 |
+
return output_path, df
|
| 120 |
+
|
| 121 |
+
# رابط کاربری تصویر
|
| 122 |
+
image_interface = gr.Interface(
|
| 123 |
+
fn=detect_and_draw_image,
|
| 124 |
+
inputs=gr.Image(type="pil", label="بارگذاری تصویر"),
|
| 125 |
+
outputs=[gr.Image(type="pil"), gr.Dataframe(label="تعداد اشیاء")],
|
| 126 |
+
title="تشخیص اشیاء در تصاویر هوایی",
|
| 127 |
+
description="یک تصویر هوایی بارگذاری کنید تا اشیاء شناسایی شده و تعداد آنها را ببینید.",
|
| 128 |
+
examples=['/content/example/images/areial_car.jpg', '/content/example/images/arieal_car_1.jpg'] # مسیر نمونههای خود را وارد کنید
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
# رابط کاربری ویدئو
|
| 132 |
+
video_interface = gr.Interface(
|
| 133 |
+
fn=detect_and_draw_video,
|
| 134 |
+
inputs=gr.Video(label="بارگذاری ویدئو"),
|
| 135 |
+
outputs=[gr.Video(label="ویدئوی پردازش شده"), gr.Dataframe(label="تعداد اشیاء")],
|
| 136 |
+
title="تشخیص اشیاء در ویدئوها",
|
| 137 |
+
description="یک ویدئو بارگذاری کنید تا اشیاء شناسایی شده و تعداد آنها را ببینید.",
|
| 138 |
+
examples=['/content/example_video1.mp4', '/content/example_video2.mp4'] # مسیر نمونههای خود را وارد کنید
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
# اجرای برنامه با استفاده از رابط کاربری تبدار
|
| 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
|