Update app.py
Browse files
app.py
CHANGED
@@ -10,8 +10,14 @@ import numpy as np
|
|
10 |
import tempfile
|
11 |
import os
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
# بارگذاری مدل آموزشدیده شما
|
14 |
-
model = YOLO('
|
15 |
|
16 |
# تعریف نام کلاسها به انگلیسی و فارسی
|
17 |
class_names = {
|
@@ -44,12 +50,13 @@ 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 |
def detect_and_draw_image(input_image):
|
55 |
try:
|
@@ -57,12 +64,8 @@ def detect_and_draw_image(input_image):
|
|
57 |
input_image_np = np.array(input_image)
|
58 |
print("Image converted to NumPy array.")
|
59 |
|
60 |
-
#
|
61 |
-
|
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
|
@@ -95,12 +98,12 @@ def detect_and_draw_image(input_image):
|
|
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(
|
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(
|
104 |
(int(x_center), int(y_center)),
|
105 |
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2, cv2.LINE_AA)
|
106 |
|
@@ -108,7 +111,7 @@ def detect_and_draw_image(input_image):
|
|
108 |
counts[label_en] = counts.get(label_en, 0) + 1
|
109 |
|
110 |
# تبدیل تصویر به RGB برای Gradio
|
111 |
-
image_rgb = cv2.cvtColor(
|
112 |
output_image = Image.fromarray(image_rgb)
|
113 |
print("Image converted back to RGB for Gradio.")
|
114 |
|
@@ -253,4 +256,4 @@ video_interface = gr.Interface(
|
|
253 |
|
254 |
# اجرای برنامه با استفاده از رابط کاربری تبدار
|
255 |
app = gr.TabbedInterface([image_interface, video_interface], ["تشخیص تصویر", "تشخیص ویدئو"])
|
256 |
-
app.launch(
|
|
|
10 |
import tempfile
|
11 |
import os
|
12 |
|
13 |
+
# بررسی وجود فایل مدل
|
14 |
+
if os.path.exists('best.pt'):
|
15 |
+
print("Model file found.")
|
16 |
+
else:
|
17 |
+
print("Model file not found. Please upload 'best.pt' to the Space.")
|
18 |
+
|
19 |
# بارگذاری مدل آموزشدیده شما
|
20 |
+
model = YOLO('best.pt') # مسیر مدل را به صورت نسبی تنظیم کنید
|
21 |
|
22 |
# تعریف نام کلاسها به انگلیسی و فارسی
|
23 |
class_names = {
|
|
|
50 |
7: (255, 165, 0), # نارنجی
|
51 |
8: (0, 128, 0), # سبز تیره
|
52 |
9: (128, 128, 0), # زیتونی
|
53 |
+
10: (0, 255, 0), # سبز روشن برای class_id=10
|
54 |
11: (0, 128, 128), # سبز نفتی
|
55 |
12: (0, 0, 128), # نیوی
|
56 |
13: (75, 0, 130), # ایندیگو
|
57 |
14: (199, 21, 133) # رز متوسط
|
58 |
}
|
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 (RGB)
|
68 |
+
results = model.predict(source=input_image_np, conf=0.3)
|
|
|
|
|
|
|
|
|
69 |
print("Model prediction completed.")
|
70 |
|
71 |
# دسترسی به نتایج OBB
|
|
|
98 |
box_points = cv2.boxPoints(rect)
|
99 |
box_points = np.int0(box_points)
|
100 |
color = colors.get(class_id, (0, 255, 0))
|
101 |
+
cv2.drawContours(input_image_np, [box_points], 0, color, 2)
|
102 |
print(f"Drawn OBB for class_id {class_id} with confidence {confidence}.")
|
103 |
|
104 |
# رسم برچسب
|
105 |
label_en, label_fa = class_names.get(class_id, ('unknown', 'ناشناخته'))
|
106 |
+
cv2.putText(input_image_np, f'{label_en}: {confidence:.2f}',
|
107 |
(int(x_center), int(y_center)),
|
108 |
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2, cv2.LINE_AA)
|
109 |
|
|
|
111 |
counts[label_en] = counts.get(label_en, 0) + 1
|
112 |
|
113 |
# تبدیل تصویر به RGB برای Gradio
|
114 |
+
image_rgb = cv2.cvtColor(input_image_np, cv2.COLOR_BGR2RGB)
|
115 |
output_image = Image.fromarray(image_rgb)
|
116 |
print("Image converted back to RGB for Gradio.")
|
117 |
|
|
|
256 |
|
257 |
# اجرای برنامه با استفاده از رابط کاربری تبدار
|
258 |
app = gr.TabbedInterface([image_interface, video_interface], ["تشخیص تصویر", "تشخیص ویدئو"])
|
259 |
+
app.launch(debog=True)
|