[go: up one dir, main page]

Menu

[2feb8a]: / face.py  Maximize  Restore  History

Download this file

369 lines (302 with data), 14.1 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import tkinter as tk
from tkinter import filedialog, ttk, messagebox, Toplevel
from PIL import Image, ImageTk, ImageDraw, ImageFont
import cv2
from ultralytics import YOLO
import os
import threading
from datetime import datetime
import torch
import atexit
import shutil
import queue
import sys
import time
# Splash Screen Implementation
def show_splash(root, duration=2):
splash = Toplevel(root)
splash.overrideredirect(True)
splash.geometry("400x300+%d+%d" % (
(root.winfo_screenwidth() - 400) // 2,
(root.winfo_screenheight() - 300) // 2
))
try:
# Path management for bundled app
if getattr(sys, 'frozen', False):
base_path = sys._MEIPASS
else:
base_path = os.path.dirname(__file__)
img_path = os.path.join(base_path, "splash.jpg")
img = Image.open(img_path)
except Exception as e:
# Create default image if splash.jpg not found
img = Image.new('RGB', (400, 300), color=(73, 109, 137))
d = ImageDraw.Draw(img)
d.text((100, 140), "Face Detection App", fill=(255, 255, 255), font=ImageFont.truetype("arial.ttf", 20))
img = img.resize((400, 300))
photo = ImageTk.PhotoImage(img)
label = tk.Label(splash, image=photo)
label.image = photo
label.pack()
splash.after(int(duration*1000), splash.destroy)
return splash
class VideoPlayer:
def __init__(self, parent, video_path):
self.parent = parent
self.video_path = video_path
self.cap = None
self.playing = False
self.fullscreen = False
self.stop_flag = False
self.setup_ui()
def setup_ui(self):
self.main_frame = ttk.Frame(self.parent)
self.main_frame.pack(fill=tk.BOTH, expand=True)
control_frame = ttk.Frame(self.main_frame)
control_frame.pack(fill=tk.X, pady=5)
self.btn_play = ttk.Button(control_frame, text="▶", command=self.toggle_play)
self.btn_play.pack(side=tk.LEFT, padx=5)
self.btn_fullscreen = ttk.Button(control_frame, text="⤢", command=self.toggle_fullscreen)
self.btn_fullscreen.pack(side=tk.LEFT, padx=5)
self.video_label = ttk.Label(self.main_frame)
self.video_label.pack(fill=tk.BOTH, expand=True)
def toggle_play(self):
self.playing = not self.playing
self.btn_play.config(text="⏸" if self.playing else "▶")
if self.playing:
self.stop_flag = False
self.play_video()
else:
self.stop_video()
def play_video(self):
if self.cap is None or not self.cap.isOpened():
self.cap = cv2.VideoCapture(self.video_path)
self.original_width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
self.original_height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
if self.playing and not self.stop_flag:
ret, frame = self.cap.read()
if ret:
max_width = self.parent.winfo_width()
max_height = self.parent.winfo_height()
ratio = min(max_width/self.original_width, max_height/self.original_height)
new_width = int(self.original_width * ratio)
new_height = int(self.original_height * ratio)
frame = cv2.resize(frame, (new_width, new_height))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(frame)
imgtk = ImageTk.PhotoImage(image=img)
self.video_label.config(image=imgtk)
self.video_label.image = imgtk
self.video_label.after(25, self.play_video)
else:
self.stop_video()
def stop_video(self):
self.playing = False
self.stop_flag = True
if self.cap:
self.cap.release()
self.cap = None
self.btn_play.config(text="▶")
def toggle_fullscreen(self):
if self.fullscreen:
self.parent.master.attributes('-fullscreen', False)
self.fullscreen = False
else:
self.parent.master.attributes('-fullscreen', True)
self.fullscreen = True
class FaceDetectionApp:
def __init__(self, root):
self.root = root
self.root.title("Advanced Face Detection")
self.root.state('zoomed')
self.model = None
self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
self.load_model()
self.stop_event = threading.Event()
self.frame_queue = queue.Queue()
self.temp_dir = "temp_frames"
self.output_dir = ""
self.thumbnails = []
self.video_path = ""
self.current_video_player = None
self.total_frames = 0
self.processed_frames = 0
self.progress_var = tk.StringVar()
self.status_var = tk.StringVar()
self.setup_ui()
self.root.after(100, self.process_queue)
atexit.register(self.cleanup)
def load_model(self):
model_path = "yolov8n-face.pt"
if not os.path.exists(model_path):
try:
YOLO("yolov8n.pt").export(format="pt", name="yolov8n-face")
except Exception as e:
messagebox.showerror("Error", f"خطا در دریافت مدل: {str(e)}")
exit()
self.model = YOLO(model_path).to(self.device)
def setup_ui(self):
self.main_paned = ttk.PanedWindow(self.root, orient=tk.HORIZONTAL)
self.main_paned.pack(fill=tk.BOTH, expand=True)
self.gallery_frame = ttk.Frame(self.main_paned)
self.main_paned.add(self.gallery_frame, weight=7)
self.video_container = ttk.Frame(self.main_paned)
self.main_paned.add(self.video_container, weight=3)
control_frame = ttk.Frame(self.gallery_frame)
control_frame.pack(fill=tk.X, pady=5)
self.btn_select = ttk.Button(control_frame, text="انتخاب ویدئو", command=self.select_video)
self.btn_select.pack(side=tk.LEFT, padx=5)
self.btn_process = ttk.Button(control_frame, text="شروع پردازش", command=self.toggle_processing)
self.btn_process.pack(side=tk.LEFT, padx=5)
self.btn_stop = ttk.Button(control_frame, text="توقف", state=tk.DISABLED, command=self.stop_processing)
self.btn_stop.pack(side=tk.LEFT, padx=5)
self.progress_label = ttk.Label(control_frame, textvariable=self.progress_var)
self.progress_label.pack(side=tk.RIGHT, padx=10)
self.canvas = tk.Canvas(self.gallery_frame, bg='#2e2e2e')
self.scrollbar = ttk.Scrollbar(self.gallery_frame, orient="vertical", command=self.canvas.yview)
self.scrollable_frame = ttk.Frame(self.canvas)
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.pack(side="left", fill="both", expand=True)
self.scrollbar.pack(side="right", fill="y")
self.canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")
self.scrollable_frame.bind("<Configure>", self.update_scrollregion)
status_bar = ttk.Label(self.root, textvariable=self.status_var, relief=tk.SUNKEN)
status_bar.pack(side=tk.BOTTOM, fill=tk.X)
self.root.bind("<Configure>", self.on_window_resize)
def on_window_resize(self, event):
self.resize_gallery()
if self.current_video_player:
self.current_video_player.play_video()
def resize_gallery(self):
gallery_width = self.gallery_frame.winfo_width()
thumbnail_size = 200
cols = max(1, gallery_width // (thumbnail_size + 20))
self.arrange_thumbnails(cols)
def arrange_thumbnails(self, cols):
for i, child in enumerate(self.scrollable_frame.winfo_children()):
row = i // cols
col = i % cols
child.grid(row=row, column=col, padx=10, pady=10, sticky='nsew')
def update_scrollregion(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
def select_video(self):
self.video_path = filedialog.askopenfilename(filetypes=[("Video Files", "*.mp4 *.avi *.mov *.mkv")])
if self.video_path:
cap = cv2.VideoCapture(self.video_path)
self.total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
cap.release()
self.status_var.set(f"Selected: {os.path.basename(self.video_path)} | Total frames: {self.total_frames}")
self.progress_var.set("Progress: 0.0% (0/0)")
if self.current_video_player:
self.current_video_player.main_frame.destroy()
self.current_video_player = VideoPlayer(self.video_container, self.video_path)
def toggle_processing(self):
if not hasattr(self, 'processing_thread') or not self.processing_thread.is_alive():
self.start_processing()
else:
self.stop_processing()
def start_processing(self):
if not self.video_path:
messagebox.showwarning("Warning", "Please select a video file first!")
return
self.processed_frames = 0
self.progress_var.set("Progress: 0.0% (0/0)")
self.output_dir = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
os.makedirs(self.output_dir, exist_ok=True)
os.makedirs(self.temp_dir, exist_ok=True)
self.stop_event.clear()
self.btn_process.config(state=tk.DISABLED)
self.btn_stop.config(state=tk.NORMAL)
self.processing_thread = threading.Thread(target=self.process_video, daemon=True)
self.processing_thread.start()
def process_video(self):
cap = cv2.VideoCapture(self.video_path)
frame_count = 0
try:
while cap.isOpened() and not self.stop_event.is_set():
ret, frame = cap.read()
if not ret:
break
results = self.model.predict(frame, conf=0.5, device=self.device, verbose=False)
if len(results[0].boxes) > 0:
timestamp = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000
filepath = os.path.join(self.output_dir, f"{datetime.now().strftime('%Y%m%d_%H%M%S')}_{timestamp:.2f}s.jpg")
cv2.imwrite(filepath, frame)
self.frame_queue.put((frame.copy(), filepath, timestamp))
frame_count += 1
progress_percent = (frame_count / self.total_frames) * 100 if self.total_frames > 0 else 0
progress_text = f"Progress: {progress_percent:.1f}% ({frame_count}/{self.total_frames})"
self.root.after(10, lambda: [
self.progress_var.set(progress_text),
self.status_var.set(progress_text)
])
finally:
cap.release()
self.root.after(10, lambda: [
self.status_var.set(f"Completed! {frame_count}/{self.total_frames} frames processed"),
self.btn_process.config(state=tk.NORMAL),
self.btn_stop.config(state=tk.DISABLED)
])
self.stop_event.set()
def process_queue(self):
try:
while not self.frame_queue.empty():
frame, filepath, timestamp = self.frame_queue.get()
thumbnail = self.create_thumbnail(frame, filepath, timestamp)
frame_label = ttk.Frame(self.scrollable_frame)
img_label = ttk.Label(frame_label, image=thumbnail)
img_label.image = thumbnail
img_label.bind("<Button-1>", lambda e, t=timestamp: self.play_from_timestamp(t))
img_label.grid(row=0, column=0)
time_label = ttk.Label(frame_label, text=f"Time: {timestamp:.2f}s")
time_label.grid(row=1, column=0)
frame_label.grid()
self.resize_gallery()
finally:
self.root.after(100, self.process_queue)
def create_thumbnail(self, frame, filepath, timestamp):
thumbnail = cv2.resize(frame, (200, 200))
img_pil = Image.fromarray(cv2.cvtColor(thumbnail, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(img_pil)
try:
font = ImageFont.truetype("arial.ttf", 16)
except:
font = ImageFont.load_default()
draw.text((10, 10), f"{timestamp:.2f}s", fill=(255, 0, 0), font=font)
img_pil.save(os.path.join(self.temp_dir, os.path.basename(filepath)))
return ImageTk.PhotoImage(img_pil)
def play_from_timestamp(self, timestamp):
if self.current_video_player:
self.current_video_player.stop_video()
self.current_video_player.cap = cv2.VideoCapture(self.video_path)
self.current_video_player.cap.set(cv2.CAP_PROP_POS_MSEC, timestamp * 1000)
self.root.after(100, self.start_playback)
def start_playback(self):
if self.current_video_player:
self.current_video_player.playing = True
self.current_video_player.stop_flag = False
self.current_video_player.btn_play.config(text="⏸")
self.current_video_player.play_video()
def cleanup(self):
if os.path.exists(self.temp_dir):
shutil.rmtree(self.temp_dir)
def stop_processing(self):
self.stop_event.set()
self.btn_process.config(state=tk.NORMAL)
self.btn_stop.config(state=tk.DISABLED)
self.status_var.set("Processing stopped by user")
if __name__ == "__main__":
root = tk.Tk()
root.withdraw() # Hide main window
# Show splash screen
splash = show_splash(root, duration=2)
# Background initialization
def init_app():
# Initialize heavy resources
app = FaceDetectionApp(root)
root.deiconify() # Show main window
splash.destroy()
# Start initialization thread
init_thread = threading.Thread(target=init_app, daemon=True)
init_thread.start()
root.mainloop()