-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputerVision5.py
More file actions
212 lines (168 loc) · 7.06 KB
/
ComputerVision5.py
File metadata and controls
212 lines (168 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from torchvision import transforms, models
from PIL import Image, ImageTk
from ultralytics import YOLO
import customtkinter as ctk
import numpy as np
import threading
import torch
import time
import cv2
class VisionDashboard:
def __init__(self):
# Configuración principal de la ventana
self.root = ctk.CTk()
self.root.title("Dashboard de Visión Computacional")
self.root.geometry("1920x1080")
# Cargar modelos
self.load_models()
# Configurar la interfaz
self.setup_ui()
# Inicializar la captura de video
self.cap = cv2.VideoCapture(0)
# Variables de control
self.running = True
self.current_frame = None
self.current_mode = "classification" # Modo inicial
# Iniciar hilos de procesamiento
self.start_processing_threads()
def load_models(self):
# Modelo de clasificación (ResNet50)
self.classification_model = models.resnet50(pretrained=True)
self.classification_model.eval()
# Modelo de detección (YOLOv8)
self.detection_model = YOLO('yolov8n.pt')
# Modelo de segmentación (YOLOv8-seg)
self.segmentation_model = YOLO('yolov8n-seg.pt')
# Transformaciones para clasificación
self.transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
def setup_ui(self):
# Configurar grid
self.root.grid_rowconfigure(1, weight=1)
self.root.grid_columnconfigure(0, weight=1)
# Frame para el menú
menu_frame = ctk.CTkFrame(self.root)
menu_frame.grid(row=0, column=0, sticky="ew", padx=10, pady=5)
# Botones del menú
modes = [
("Clasificación", "classification"),
("Detección de Objetos", "detection"),
("Segmentación", "segmentation"),
("Seguimiento de Objetos", "tracking")
]
for i, (text, mode) in enumerate(modes):
btn = ctk.CTkButton(
menu_frame,
text=text,
command=lambda m=mode: self.change_mode(m)
)
btn.pack(side="left", padx=5, pady=5)
# Frame principal para el video
self.main_frame = ctk.CTkFrame(self.root)
self.main_frame.grid(row=1, column=0, padx=10, pady=5, sticky="nsew")
# Canvas para el video
self.main_canvas = ctk.CTkCanvas(self.main_frame)
self.main_canvas.pack(expand=True, fill="both", padx=5, pady=5)
# Label para resultados de clasificación
self.result_label = ctk.CTkLabel(self.main_frame, text="")
self.result_label.pack(pady=5)
def change_mode(self, mode):
self.current_mode = mode
self.result_label.configure(text="")
def start_processing_threads(self):
# Hilo para captura de video
self.video_thread = threading.Thread(target=self.update_video)
self.video_thread.daemon = True
self.video_thread.start()
# Hilo para procesamiento
self.processing_thread = threading.Thread(target=self.process_frame)
self.processing_thread.daemon = True
self.processing_thread.start()
def update_video(self):
while self.running:
ret, frame = self.cap.read()
if ret:
self.current_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
time.sleep(0.03) # ~30 FPS
def process_frame(self):
tracker = cv2.TrackerCSRT_create()
bbox = None
while self.running:
if self.current_frame is not None:
frame = self.current_frame.copy()
processed_frame = None
if self.current_mode == "classification":
# Clasificación
image = Image.fromarray(frame)
input_tensor = self.transform(image)
input_batch = input_tensor.unsqueeze(0)
with torch.no_grad():
output = self.classification_model(input_batch)
_, predicted = torch.max(output, 1)
self.result_label.configure(text=f"Clase: {predicted.item()}")
processed_frame = frame
elif self.current_mode == "detection":
# Detección
results = self.detection_model(frame)
processed_frame = results[0].plot()
elif self.current_mode == "segmentation":
# Segmentación
results = self.segmentation_model(frame)
processed_frame = results[0].plot()
elif self.current_mode == "tracking":
# Seguimiento
if bbox is None:
results = self.detection_model(frame)
if len(results[0].boxes) > 0:
box = results[0].boxes[0].xyxy[0].numpy()
bbox = (int(box[0]), int(box[1]),
int(box[2] - box[0]), int(box[3] - box[1]))
tracker.init(frame, bbox)
else:
success, bbox = tracker.update(frame)
if success:
x, y, w, h = [int(v) for v in bbox]
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
processed_frame = frame
if processed_frame is not None:
self.update_main_display(Image.fromarray(processed_frame))
time.sleep(0.1)
def update_main_display(self, image):
# Obtener el tamaño del canvas
canvas_width = self.main_canvas.winfo_width()
canvas_height = self.main_canvas.winfo_height()
# Calcular el tamaño manteniendo la relación de aspecto
img_width, img_height = image.size
aspect_ratio = img_width / img_height
if canvas_width / canvas_height > aspect_ratio:
new_height = canvas_height
new_width = int(canvas_height * aspect_ratio)
else:
new_width = canvas_width
new_height = int(canvas_width / aspect_ratio)
# Redimensionar la imagen
resized_image = image.resize((new_width, new_height))
photo = ImageTk.PhotoImage(resized_image)
# Centrar la imagen en el canvas
x_center = (canvas_width - new_width) // 2
y_center = (canvas_height - new_height) // 2
self.main_canvas.delete("all")
self.main_canvas.create_image(x_center, y_center, anchor="nw", image=photo)
self.main_canvas.image = photo
def run(self):
self.root.mainloop()
def cleanup(self):
self.running = False
self.cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
app = VisionDashboard()
try:
app.run()
finally:
app.cleanup()