Upload 4 files
Browse files- README.md +39 -0
- README.pdf +0 -0
- label.py +109 -0
- requirements.txt +1 -0
README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# README
|
| 2 |
+
|
| 3 |
+
## 运行环境
|
| 4 |
+
|
| 5 |
+
```bash
|
| 6 |
+
conda create -n label python=3.10
|
| 7 |
+
conda activate label
|
| 8 |
+
pip install -r requirements.txt
|
| 9 |
+
```
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
## 视频类型描述
|
| 14 |
+
|
| 15 |
+
| 类型 | 描述 | |
|
| 16 |
+
| :--: | :--------------------------: | ---- |
|
| 17 |
+
| 0 | 相机旋转,主体静止 | |
|
| 18 |
+
| 1 | 相机大幅旋转,主体有一定运动 | |
|
| 19 |
+
| 2 | 除以上两类之外 | |
|
| 20 |
+
|
| 21 |
+
## 代码使用方法
|
| 22 |
+
|
| 23 |
+
在`label.py`中将`main`函数中`batch_file`变量赋值为需要标注的视频文件夹名字
|
| 24 |
+
|
| 25 |
+
在终端中运行以下命令,将自动生成与标注文件夹同名的csv文件
|
| 26 |
+
|
| 27 |
+
```bash
|
| 28 |
+
python label.py
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
若环境配置正确,将自动播放视频,在命令行中将输出视频的名字以及标注进度,在播放过程中可以按下`q`中断播放直接开始进行标注
|
| 32 |
+
|
| 33 |
+
在标注环节,输入0, 1, 2,将自动在csv文件中修改对应文件的label,并播放下一个视频;
|
| 34 |
+
|
| 35 |
+
输入`r`,将重新播放视频;
|
| 36 |
+
|
| 37 |
+
输入`w`将关闭程序,下次将从第一个未标注的视频开始播放
|
| 38 |
+
|
| 39 |
+
标注结束后,将csv文件回收即可
|
README.pdf
ADDED
|
Binary file (89.9 kB). View file
|
|
|
label.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import csv
|
| 3 |
+
import cv2
|
| 4 |
+
|
| 5 |
+
VIDEO_EXTENSIONS = [".mp4", ".webm", ".mkv"]
|
| 6 |
+
|
| 7 |
+
def list_video_files(folder_path):
|
| 8 |
+
return [f for f in os.listdir(folder_path) if os.path.splitext(f)[1].lower() in VIDEO_EXTENSIONS]
|
| 9 |
+
|
| 10 |
+
def load_csv(csv_path):
|
| 11 |
+
with open(csv_path, 'r', newline='', encoding='utf-8') as csv_file:
|
| 12 |
+
reader = csv.reader(csv_file)
|
| 13 |
+
next(reader) # Skip header
|
| 14 |
+
return list(reader)
|
| 15 |
+
|
| 16 |
+
def save_csv(csv_path, data):
|
| 17 |
+
with open(csv_path, 'w', newline='', encoding='utf-8') as csv_file:
|
| 18 |
+
writer = csv.writer(csv_file)
|
| 19 |
+
writer.writerow(["filename", "label"])
|
| 20 |
+
writer.writerows(data)
|
| 21 |
+
|
| 22 |
+
def find_first_unlabeled(data):
|
| 23 |
+
for i, row in enumerate(data):
|
| 24 |
+
if row[1] == '-1':
|
| 25 |
+
return i
|
| 26 |
+
return -1
|
| 27 |
+
|
| 28 |
+
def create_label_csv(folder_path, csv_file_path):
|
| 29 |
+
video_files = list_video_files(folder_path)
|
| 30 |
+
if not video_files:
|
| 31 |
+
print(f"No video files found in {folder_path}")
|
| 32 |
+
return
|
| 33 |
+
|
| 34 |
+
folder_name = os.path.basename(folder_path)
|
| 35 |
+
csv_file_path = os.path.join(csv_file_path, f"{folder_name}.csv")
|
| 36 |
+
|
| 37 |
+
with open(csv_file_path, mode='w', newline='', encoding='utf-8') as csv_file:
|
| 38 |
+
writer = csv.writer(csv_file)
|
| 39 |
+
writer.writerow(["filename", "label"])
|
| 40 |
+
for video_file in video_files:
|
| 41 |
+
writer.writerow([video_file, -1])
|
| 42 |
+
print(f"CSV file created: {csv_file_path}")
|
| 43 |
+
|
| 44 |
+
def play_video(video_path):
|
| 45 |
+
cap = cv2.VideoCapture(video_path)
|
| 46 |
+
if not cap.isOpened():
|
| 47 |
+
print(f"Failed to open video: {video_path}")
|
| 48 |
+
return
|
| 49 |
+
|
| 50 |
+
print("Press 'q' to end view.")
|
| 51 |
+
while True:
|
| 52 |
+
ret, frame = cap.read()
|
| 53 |
+
if not ret:
|
| 54 |
+
break
|
| 55 |
+
cv2.imshow(os.path.basename(video_path), frame)
|
| 56 |
+
if cv2.waitKey(25) & 0xFF == ord('q'):
|
| 57 |
+
break
|
| 58 |
+
|
| 59 |
+
cap.release()
|
| 60 |
+
cv2.destroyAllWindows()
|
| 61 |
+
|
| 62 |
+
def label_videos(folder_path, csv_file_path):
|
| 63 |
+
csv_path = os.path.join(csv_file_path, f"{os.path.basename(folder_path)}.csv")
|
| 64 |
+
if not os.path.exists(csv_path):
|
| 65 |
+
print(f"CSV file not found: {csv_path}, creating...")
|
| 66 |
+
create_label_csv(folder_path, csv_file_path)
|
| 67 |
+
|
| 68 |
+
data = load_csv(csv_path)
|
| 69 |
+
total_videos = len(data)
|
| 70 |
+
unlabeled_index = find_first_unlabeled(data)
|
| 71 |
+
|
| 72 |
+
if unlabeled_index == -1:
|
| 73 |
+
print("All videos are labeled.")
|
| 74 |
+
return
|
| 75 |
+
|
| 76 |
+
while unlabeled_index < total_videos:
|
| 77 |
+
video_name, label = data[unlabeled_index]
|
| 78 |
+
video_path = os.path.join(folder_path, video_name)
|
| 79 |
+
|
| 80 |
+
print(f"Current video: {video_name}")
|
| 81 |
+
print(f"Progress: {unlabeled_index + 1}/{total_videos} ({(unlabeled_index + 1) / total_videos * 100:.2f}%)")
|
| 82 |
+
|
| 83 |
+
play_video(video_path)
|
| 84 |
+
|
| 85 |
+
while True:
|
| 86 |
+
label_input = input("Enter label (0, 1, 2), 'w' to exit, 'r' to replay: ")
|
| 87 |
+
if label_input in ['0', '1', '2']:
|
| 88 |
+
data[unlabeled_index][1] = label_input
|
| 89 |
+
save_csv(csv_path, data)
|
| 90 |
+
print(f"Label {label_input} saved for {video_name}.")
|
| 91 |
+
break
|
| 92 |
+
elif label_input == 'w':
|
| 93 |
+
print("Exiting")
|
| 94 |
+
return
|
| 95 |
+
elif label_input == 'r':
|
| 96 |
+
print(f"Replaying {video_name}")
|
| 97 |
+
play_video(video_path)
|
| 98 |
+
else:
|
| 99 |
+
print("Invalid input. Please enter 0, 1, 2, 'w' or 'r'.")
|
| 100 |
+
|
| 101 |
+
unlabeled_index = find_first_unlabeled(data)
|
| 102 |
+
if unlabeled_index == -1:
|
| 103 |
+
print("All videos are labeled.")
|
| 104 |
+
break
|
| 105 |
+
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
batch_folder = "./batch_0"
|
| 108 |
+
csv_file_path = './'
|
| 109 |
+
label_videos(batch_folder, csv_file_path)
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
opencv-python>=4.5.5
|