khanhromvn commited on
Commit
ca0c0af
·
1 Parent(s): ce3d949

feat: ✨ Thêm ứng dụng Gradio để nhận diện tướng AOV

Browse files

- Tạo giao diện Gradio cho phép người dùng tải ảnh lên và nhận diện tướng AOV sử dụng YOLOv8.
- Tích hợp model YOLOv8 để thực hiện việc nhận diện và hiển thị kết quả trên ảnh.
- Thêm file requirements.txt để liệt kê các thư viện cần thiết cho ứng dụng.

Files changed (3) hide show
  1. app.py +106 -0
  2. models/aov_herodetector_v1.pt +3 -0
  3. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ from ultralytics import YOLO
5
+ import os
6
+ from PIL import Image
7
+
8
+ # Load model
9
+ model_path = "models/aov_herodetector_v1.pt"
10
+ try:
11
+ model = YOLO(model_path)
12
+ print(f"Model loaded successfully from {model_path}")
13
+ except Exception as e:
14
+ print(f"Error loading model: {e}")
15
+ model = None
16
+
17
+ def detect_heroes(image):
18
+ """
19
+ Detect AOV heroes in the uploaded image
20
+ """
21
+ if model is None:
22
+ return None, "Model not loaded properly"
23
+
24
+ try:
25
+ # Convert PIL Image to numpy array if needed
26
+ if isinstance(image, Image.Image):
27
+ image = np.array(image)
28
+
29
+ # Run inference
30
+ results = model(image)
31
+
32
+ # Get the first result
33
+ result = results[0]
34
+
35
+ # Plot results on image
36
+ annotated_image = result.plot()
37
+
38
+ # Convert BGR to RGB (OpenCV uses BGR, but gradio expects RGB)
39
+ annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
40
+
41
+ # Get detection info
42
+ detection_info = ""
43
+ if len(result.boxes) > 0:
44
+ detection_info = f"Detected {len(result.boxes)} heroes:\n"
45
+ for i, box in enumerate(result.boxes):
46
+ conf = box.conf[0].item()
47
+ cls = int(box.cls[0].item())
48
+ class_name = model.names[cls] if cls < len(model.names) else f"Class_{cls}"
49
+ detection_info += f"- {class_name}: {conf:.2f}\n"
50
+ else:
51
+ detection_info = "No heroes detected"
52
+
53
+ return annotated_image, detection_info
54
+
55
+ except Exception as e:
56
+ return None, f"Error during detection: {str(e)}"
57
+
58
+ # Create Gradio interface
59
+ with gr.Blocks(title="AOV Hero Detector", theme=gr.themes.Soft()) as demo:
60
+ gr.Markdown("# 🎮 Arena of Valor Hero Detector")
61
+ gr.Markdown("Upload an image to detect AOV heroes using YOLOv8 model")
62
+
63
+ with gr.Row():
64
+ with gr.Column():
65
+ input_image = gr.Image(
66
+ label="Upload Image",
67
+ type="pil",
68
+ height=400
69
+ )
70
+ detect_btn = gr.Button("🔍 Detect Heroes", variant="primary")
71
+
72
+ with gr.Column():
73
+ output_image = gr.Image(
74
+ label="Detection Result",
75
+ height=400
76
+ )
77
+ detection_text = gr.Textbox(
78
+ label="Detection Info",
79
+ lines=5,
80
+ max_lines=10
81
+ )
82
+
83
+ # Examples (optional - you can add sample images)
84
+ gr.Examples(
85
+ examples=[], # Add paths to example images if you have them
86
+ inputs=input_image,
87
+ label="Example Images"
88
+ )
89
+
90
+ # Event handlers
91
+ detect_btn.click(
92
+ fn=detect_heroes,
93
+ inputs=input_image,
94
+ outputs=[output_image, detection_text]
95
+ )
96
+
97
+ # Auto-detect when image is uploaded
98
+ input_image.change(
99
+ fn=detect_heroes,
100
+ inputs=input_image,
101
+ outputs=[output_image, detection_text]
102
+ )
103
+
104
+ # Launch the app
105
+ if __name__ == "__main__":
106
+ demo.launch()
models/aov_herodetector_v1.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6a80e681d5aade95cd5b7d30e150cfc81e6737ab363ab5863dd41358b84cb0c1
3
+ size 52025867
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio
2
+ ultralytics
3
+ opencv-python
4
+ pillow
5
+ numpy
6
+ torch
7
+ torchvision