File size: 3,038 Bytes
f113e60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import os
import tkinter as tk
from PIL import Image, ImageTk

root_path = "data_label"

# Create folders for labeled images
os.makedirs(f"{root_path}/left", exist_ok=True)
os.makedirs(f"{root_path}/right", exist_ok=True)
os.makedirs(f"{root_path}/straight", exist_ok=True)

# Load video
video_path = "collect_data/1.mp4"  # Change this to your video file
cap = cv2.VideoCapture(video_path)

frame_count = 0
current_frame = None
highlight_label = None  # Biến để lưu nhãn đang được chọn

# Read first frame
def read_frame():
    global current_frame, frame_count
    ret, frame = cap.read()
    if ret:
        current_frame = frame
        frame_count += 1
        show_frame()
    else:
        print("Video ended.")
        cap.release()
        root.quit()

# Save frame to respective folder
def save_frame(label):
    global highlight_label
    if current_frame is not None:
        frame_resized = cv2.resize(current_frame, (640, 480))
        filename = f"{label}/{frame_count}.jpg"
        cv2.imwrite(filename, frame_resized)
        highlight_label = label  # Cập nhật nhãn đang chọn
        update_label_highlight()
        read_frame()

# Xử lý sự kiện nhấn phím
def key_press(event):
    if event.char == '1':
        save_frame("left")
    elif event.char == '2':
        save_frame("right")
    elif event.char == '3':
        save_frame("straight")

# Hiển thị hình ảnh từ video lên GUI
def show_frame():
    frame_rgb = cv2.cvtColor(current_frame, cv2.COLOR_BGR2RGB)
    frame_resized = cv2.resize(frame_rgb, (640, 480))
    img = Image.fromarray(frame_resized)
    img = ImageTk.PhotoImage(img)
    panel.config(image=img)
    panel.image = img

# Cập nhật giao diện khi nhấn nút
def update_label_highlight():
    global highlight_label
    if highlight_label == "left":
        label_status.config(text="Selected: LEFT", bg="red")
    elif highlight_label == "right":
        label_status.config(text="Selected: RIGHT", bg="blue")
    elif highlight_label == "straight":
        label_status.config(text="Selected: STRAIGHT", bg="green")

    # Tạo hiệu ứng biến mất sau 500ms
    root.after(500, reset_label_highlight)

# Reset màu nền sau khi chọn label
def reset_label_highlight():
    label_status.config(text="Press 1, 2, 3 to label", bg="white")

# Setup GUI
root = tk.Tk()
root.title("Autonomous Car Label Tool")

# Set window size to match frame size
root.geometry("640x580")  # Tăng chiều cao để thêm thông báo trạng thái

# Add instructions label
instructions = tk.Label(root, text="Press: 1 for Left | 2 for Right | 3 for Straight", font=("Arial", 12))
instructions.pack()

# Add label status indicator
label_status = tk.Label(root, text="Press 1, 2, 3 to label", font=("Arial", 14, "bold"), bg="white", width=30)
label_status.pack(pady=5)

# Panel để hiển thị hình ảnh
panel = tk.Label(root)
panel.pack()

# Bind keyboard events
root.bind('<Key>', key_press)

# Start processing
read_frame()
root.mainloop()