Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,9 +2,9 @@ import gradio as gr
|
|
| 2 |
import cv2
|
| 3 |
from ultralytics import YOLO
|
| 4 |
import os
|
| 5 |
-
|
| 6 |
|
| 7 |
-
# 1. Khởi tạo mô hình
|
| 8 |
model = YOLO('yolov8n.pt')
|
| 9 |
|
| 10 |
def process_video(video_path):
|
|
@@ -28,7 +28,7 @@ def process_video(video_path):
|
|
| 28 |
# Vạch đếm ở 60% chiều cao màn hình
|
| 29 |
line_y = int(height * 0.6)
|
| 30 |
|
| 31 |
-
# File tạm trung gian (
|
| 32 |
temp_output = 'temp_raw.mp4'
|
| 33 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 34 |
out = cv2.VideoWriter(temp_output, fourcc, fps, (width, height))
|
|
@@ -38,7 +38,7 @@ def process_video(video_path):
|
|
| 38 |
if not ret:
|
| 39 |
break
|
| 40 |
|
| 41 |
-
# Thực hiện Tracking
|
| 42 |
results = model.track(frame, classes=vehicle_class_ids, persist=True, verbose=False)
|
| 43 |
|
| 44 |
# Vẽ vạch đếm màu đỏ
|
|
@@ -57,11 +57,11 @@ def process_video(video_path):
|
|
| 57 |
cx = int((x1 + x2) / 2)
|
| 58 |
cy = int((y1 + y2) / 2)
|
| 59 |
|
| 60 |
-
# Vẽ khung và tâm xe
|
| 61 |
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (255, 255, 0), 2)
|
| 62 |
cv2.circle(frame, (cx, cy), 5, (0, 255, 0), -1)
|
| 63 |
|
| 64 |
-
# Logic đếm qua vạch
|
| 65 |
if obj_id in track_history and obj_id not in counted_ids:
|
| 66 |
prev_cy = track_history[obj_id]
|
| 67 |
if (prev_cy < line_y and cy >= line_y) or (prev_cy > line_y and cy <= line_y):
|
|
@@ -71,7 +71,7 @@ def process_video(video_path):
|
|
| 71 |
|
| 72 |
track_history[obj_id] = cy
|
| 73 |
|
| 74 |
-
# --- BẢNG THỐNG KÊ
|
| 75 |
total = sum(vehicle_count.values())
|
| 76 |
overlay = frame.copy()
|
| 77 |
box_w, box_h = 250, 160
|
|
@@ -92,19 +92,18 @@ def process_video(video_path):
|
|
| 92 |
cap.release()
|
| 93 |
out.release()
|
| 94 |
|
| 95 |
-
# --- CHUYỂN ĐỔI SANG CHUẨN H.264 ĐỂ XEM TRỰC TIẾP
|
| 96 |
final_output = 'result_display.mp4'
|
| 97 |
try:
|
| 98 |
-
#
|
| 99 |
-
clip =
|
| 100 |
clip.write_videofile(final_output, codec="libx264", audio=False)
|
| 101 |
clip.close()
|
| 102 |
-
# Xóa file tạm cho nhẹ bộ nhớ server
|
| 103 |
if os.path.exists(temp_output):
|
| 104 |
os.remove(temp_output)
|
| 105 |
except Exception as e:
|
| 106 |
print(f"Lỗi encode video: {e}")
|
| 107 |
-
return temp_output
|
| 108 |
|
| 109 |
return final_output
|
| 110 |
|
|
@@ -114,7 +113,7 @@ interface = gr.Interface(
|
|
| 114 |
inputs=gr.Video(label="Tải video của bạn lên"),
|
| 115 |
outputs=gr.Video(label="Kết quả đếm xe (Xem trực tiếp)"),
|
| 116 |
title="🚗 Hệ thống Đếm Xe Thông Minh (YOLOv8)",
|
| 117 |
-
description="
|
| 118 |
)
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import cv2
|
| 3 |
from ultralytics import YOLO
|
| 4 |
import os
|
| 5 |
+
from moviepy.editor import VideoFileClip
|
| 6 |
|
| 7 |
+
# 1. Khởi tạo mô hình
|
| 8 |
model = YOLO('yolov8n.pt')
|
| 9 |
|
| 10 |
def process_video(video_path):
|
|
|
|
| 28 |
# Vạch đếm ở 60% chiều cao màn hình
|
| 29 |
line_y = int(height * 0.6)
|
| 30 |
|
| 31 |
+
# File tạm trung gian (định dạng mp4v)
|
| 32 |
temp_output = 'temp_raw.mp4'
|
| 33 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 34 |
out = cv2.VideoWriter(temp_output, fourcc, fps, (width, height))
|
|
|
|
| 38 |
if not ret:
|
| 39 |
break
|
| 40 |
|
| 41 |
+
# Thực hiện Tracking
|
| 42 |
results = model.track(frame, classes=vehicle_class_ids, persist=True, verbose=False)
|
| 43 |
|
| 44 |
# Vẽ vạch đếm màu đỏ
|
|
|
|
| 57 |
cx = int((x1 + x2) / 2)
|
| 58 |
cy = int((y1 + y2) / 2)
|
| 59 |
|
| 60 |
+
# Vẽ khung và tâm xe
|
| 61 |
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (255, 255, 0), 2)
|
| 62 |
cv2.circle(frame, (cx, cy), 5, (0, 255, 0), -1)
|
| 63 |
|
| 64 |
+
# Logic đếm qua vạch
|
| 65 |
if obj_id in track_history and obj_id not in counted_ids:
|
| 66 |
prev_cy = track_history[obj_id]
|
| 67 |
if (prev_cy < line_y and cy >= line_y) or (prev_cy > line_y and cy <= line_y):
|
|
|
|
| 71 |
|
| 72 |
track_history[obj_id] = cy
|
| 73 |
|
| 74 |
+
# --- BẢNG THỐNG KÊ (Thiết kế gốc của bạn) ---
|
| 75 |
total = sum(vehicle_count.values())
|
| 76 |
overlay = frame.copy()
|
| 77 |
box_w, box_h = 250, 160
|
|
|
|
| 92 |
cap.release()
|
| 93 |
out.release()
|
| 94 |
|
| 95 |
+
# --- CHUYỂN ĐỔI SANG CHUẨN H.264 ĐỂ XEM TRỰC TIẾP ---
|
| 96 |
final_output = 'result_display.mp4'
|
| 97 |
try:
|
| 98 |
+
# Chuyển đổi định dạng để trình duyệt web có thể Play trực tiếp
|
| 99 |
+
clip = VideoFileClip(temp_output)
|
| 100 |
clip.write_videofile(final_output, codec="libx264", audio=False)
|
| 101 |
clip.close()
|
|
|
|
| 102 |
if os.path.exists(temp_output):
|
| 103 |
os.remove(temp_output)
|
| 104 |
except Exception as e:
|
| 105 |
print(f"Lỗi encode video: {e}")
|
| 106 |
+
return temp_output
|
| 107 |
|
| 108 |
return final_output
|
| 109 |
|
|
|
|
| 113 |
inputs=gr.Video(label="Tải video của bạn lên"),
|
| 114 |
outputs=gr.Video(label="Kết quả đếm xe (Xem trực tiếp)"),
|
| 115 |
title="🚗 Hệ thống Đếm Xe Thông Minh (YOLOv8)",
|
| 116 |
+
description="Nhận diện và đếm số lượng xe qua vạch. Kết quả xem được trực tiếp trên web.",
|
| 117 |
)
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|