|
|
|
|
|
|
|
import cv2 |
|
from ultralytics import YOLO |
|
from PIL import Image, ImageDraw |
|
import gradio as gr |
|
import pandas as pd |
|
|
|
|
|
model = YOLO('/content/yolo11n-obb.pt') |
|
|
|
|
|
class_names = { |
|
0: ('plane', 'هواپیما'), |
|
1: ('ship', 'کشتی'), |
|
2: ('storage tank', 'مخزن ذخیره'), |
|
3: ('baseball diamond', 'زمین بیسبال'), |
|
4: ('tennis court', 'زمین تنیس'), |
|
5: ('basketball court', 'زمین بسکتبال'), |
|
6: ('ground track field', 'زمین دو و میدانی'), |
|
7: ('harbor', 'بندرگاه'), |
|
8: ('bridge', 'پل'), |
|
9: ('large vehicle', 'خودرو بزرگ'), |
|
10: ('small vehicle', 'خودرو کوچک'), |
|
11: ('helicopter', 'هلیکوپتر'), |
|
12: ('roundabout', 'میدان'), |
|
13: ('soccer ball field', 'زمین فوتبال'), |
|
14: ('swimming pool', 'استخر شنا') |
|
} |
|
|
|
|
|
def detect_and_draw_image(input_image): |
|
results = model(input_image) |
|
|
|
|
|
if not results or results[0].boxes is None or not results[0].boxes.boxes: |
|
|
|
df = pd.DataFrame({ |
|
'Label (English)': [], |
|
'Label (Persian)': [], |
|
'Object Count': [] |
|
}) |
|
return input_image, df |
|
|
|
detections = results[0].boxes.boxes |
|
|
|
draw = ImageDraw.Draw(input_image) |
|
counts = {} |
|
for detection in detections: |
|
xmin, ymin, xmax, ymax, conf, class_id = detection.cpu().numpy() |
|
label_en, label_fa = class_names.get(int(class_id), ('unknown', 'ناشناخته')) |
|
counts[label_en] = counts.get(label_en, 0) + 1 |
|
|
|
draw.rectangle([(xmin, ymin), (xmax, ymax)], outline="red", width=2) |
|
draw.text((xmin, ymin), f"{label_en}: {conf:.2f}", fill="white") |
|
|
|
df = pd.DataFrame({ |
|
'Label (English)': list(counts.keys()), |
|
'Label (Persian)': [class_names.get(k, ('unknown', 'ناشناخته'))[1] for k in counts.keys()], |
|
'Object Count': list(counts.values()) |
|
}) |
|
|
|
return input_image, df |
|
|
|
|
|
def detect_and_draw_video(video_path): |
|
cap = cv2.VideoCapture(video_path) |
|
frames = [] |
|
overall_counts = {} |
|
seen_objects = [] |
|
|
|
while cap.isOpened(): |
|
ret, frame = cap.read() |
|
if not ret: |
|
break |
|
|
|
frame = cv2.resize(frame, (640, 480)) |
|
results = model(frame) |
|
detections = results[0].boxes.boxes |
|
|
|
if detections is not None: |
|
detections = detections.cpu().numpy() |
|
for detection in detections: |
|
xmin, ymin, xmax, ymax, conf, class_id = detection |
|
label_en, label_fa = class_names.get(int(class_id), ('unknown', 'ناشناخته')) |
|
current_object = (label_en, int(xmin), int(ymin), int(xmax), int(ymax)) |
|
|
|
|
|
if not any(existing[0] == label_en and |
|
(existing[1] < xmax and existing[3] > xmin and |
|
existing[2] < ymax and existing[4] > ymin) for existing in seen_objects): |
|
seen_objects.append(current_object) |
|
overall_counts[label_en] = overall_counts.get(label_en, 0) + 1 |
|
|
|
|
|
cv2.rectangle(frame, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (255, 0, 0), 2) |
|
cv2.putText(frame, f"{label_en}: {conf:.2f}", (int(xmin), int(ymin) - 10), |
|
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) |
|
|
|
frames.append(frame) |
|
|
|
cap.release() |
|
|
|
output_path = 'output.mp4' |
|
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), 20.0, (640, 480)) |
|
|
|
for frame in frames: |
|
out.write(frame) |
|
out.release() |
|
|
|
|
|
df = pd.DataFrame({ |
|
'Label (English)': list(overall_counts.keys()), |
|
'Label (Persian)': [class_names.get(k, ('unknown', 'ناشناخته'))[1] for k in overall_counts.keys()], |
|
'Object Count': list(overall_counts.values()) |
|
}) |
|
|
|
return output_path, df |
|
|
|
|
|
image_interface = gr.Interface( |
|
fn=detect_and_draw_image, |
|
inputs=gr.Image(type="pil", label="بارگذاری تصویر"), |
|
outputs=[gr.Image(type="pil"), gr.Dataframe(label="تعداد اشیاء")], |
|
title="تشخیص اشیاء در تصاویر هوایی", |
|
description="یک تصویر هوایی بارگذاری کنید تا اشیاء شناسایی شده و تعداد آنها را ببینید.", |
|
examples=['/content/example/images/areial_car.jpg', '/content/example/images/arieal_car_1.jpg'] |
|
) |
|
|
|
|
|
video_interface = gr.Interface( |
|
fn=detect_and_draw_video, |
|
inputs=gr.Video(label="بارگذاری ویدئو"), |
|
outputs=[gr.Video(label="ویدئوی پردازش شده"), gr.Dataframe(label="تعداد اشیاء")], |
|
title="تشخیص اشیاء در ویدئوها", |
|
description="یک ویدئو بارگذاری کنید تا اشیاء شناسایی شده و تعداد آنها را ببینید.", |
|
examples=['/content/example_video1.mp4', '/content/example_video2.mp4'] |
|
) |
|
|
|
|
|
app = gr.TabbedInterface([image_interface, video_interface], ["تشخیص تصویر", "تشخیص ویدئو"]) |
|
app.launch(debug=True, share=True) |
|
|
|
|
|
|