Skip to content

Commit

Permalink
Docs : Modify files & README
Browse files Browse the repository at this point in the history
  • Loading branch information
dlatjgus0612 authored and soyoonjeong committed Mar 30, 2024
1 parent 81f5f99 commit e399ea0
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 63 deletions.
106 changes: 81 additions & 25 deletions data/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@



# 🔊 Voice2Face-Data



<img src="https://img.shields.io/badge/Python-3776AB?style=for-the-badge&logo=Python&logoColor=white"> <img src="https://img.shields.io/badge/opencv-5C3EE8?style=for-the-badge&logo=opencv&logoColor=white"> <img src="https://img.shields.io/badge/git-F05032?style=for-the-badge&logo=git&logoColor=white"> <img src="https://img.shields.io/badge/NCP-03C75A?style=for-the-badge&logo=Naver&logoColor=white"> <img src="https://img.shields.io/badge/Linux-FCC624?style=for-the-badge&logo=Linux&logoColor=white"> <img src="https://img.shields.io/badge/Selenium-43B02A?style=for-the-badge&logo=Selenium&logoColor=white"> <img src="https://img.shields.io/badge/Numpy-013243?style=for-the-badge&logo=Numpy&logoColor=white"> <img src="https://img.shields.io/badge/Pytorch-EE4C2C?style=for-the-badge&logo=Pytorch&logoColor=white"> <img src="https://img.shields.io/badge/FFmpeg-007808?style=for-the-badge&logo=FFmpeg&logoColor=white">



## Project Structure

```
data
┣ audio
┃ ┣ audio_check_dB.py
┃ ┣ audio_crop.py
┃ ┣ audio_dB_crop.py
┃ ┗ audio_wav_cropping.py
┣ crawling
┃ ┣ crawling_detect.py
Expand All @@ -29,54 +36,103 @@ data
┣ video
┃ ┣ video_clipimage.py
┃ ┗ video_download.py
README.md
README.md
┗ requirements.txt
```
## Usage



#### audio
- `audio_check_dB.py`: 특정 dB 값을 확인하여 사람의 음성 여부를 판별하는 스크립트입니다.
- `audio_dB_crop.py`: 오디오 파일에서 인간의 목소리 세그먼트를 추출하고 감지된 음성 세그먼트를 포함하는 새로운 오디오 파일을 10초로 자르는 스크립트입니다.
- `audio_wav_cropping.py`: JSON POINT에 맞춰 오디오를 자르는 스크립트입니다.

#### crawling

- `crawling_detect.py`: 비디오 클립에서 얼굴과 오디오를 감지하고 세분화하는 스크립트입니다.
- `crawling_rename_video.py`: 'download' 폴더에서 비디오 이름과 CSV의 인덱스를 맞추는 스크립트입니다.
- `crawling_select_csv.py`: 주어진 CSV 파일에서 YouTube ID를 찾아 해당하는 파일 이름에서 정보를 추출하고, 이 정보를 새로운 CSV 파일에 저장하는 간단한 데이터 처리 작업을 수행하는 스크립트입니다.
- `crawling_urlsave.py`: Selenium을 사용하여 YouTube 크롤링을 수행하여 약 162개의 비디오에 대한 이름, 제목 및 URL 정보를 Youtube_search_df.csv에 저장하는 스크립트입니다.
- `crawling_videosave.py`: '`crawling_urlsave.py`'를 통해 얻은 URL에서 비디오를 다운로드하는 스크립트입니다. 비디오는 'download' 폴더에 저장됩니다.

#### image

- `image_clipseg2.py`: CLIPSeg 모델을 사용하여 텍스트 프롬프트를 기반으로 이미지 세분화를 수행하는 스크립트입니다. 이미지를 불러와 텍스트 프롬프트로 처리하고, 식별된 객체를 기반으로 세분화된 이미지를 생성합니다.
- `image_face_frame.py`: 비디오에서 사람의 얼굴이 정면이고, 눈을 뜨고 있을 때 캡쳐하고 배경을 제거하는 스크립트입니다.

#### relabel

- `relabel_detect_getframe.py`: 주어진 비디오에서 얼굴을 감지하고, 감지된 얼굴에 대해 성별과 연령을 추정하여 화면에 표시하고, 일정한 간격으로 프레임을 캡처하여 이미지 파일로 저장하는 기능을 수행합니다.
- `relabel_select_csv.py`: 데이터 경로에서 YouTube ID를 추출하고, 파일 이름에서 필요한 정보를 추출하여 새로운 CSV 파일에 저장하는 스크립트입니다.
- `relabel_Vox_age.py`: 이미지 폴더에서 이미지들을 읽어와 각 이미지의 나이를 예측하고, 가장 흔한 나이 그룹을 세서 출력하고, 그 결과를 CSV 파일에 저장하는 작업을 수행합니다.

#### video

- `video_clipimage.py`: 주어진 이미지에서 얼굴을 감지하고, 감지된 얼굴 영역을 사각형으로 표시한 후 해당 얼굴을 256x256 크기로 조정하여 저장하는 작업을 수행합니다.
- `video_download.py`: 주요 기능은 주어진 YouTube 비디오 링크에서 비디오를 다운로드하고, 다운로드한 비디오를 mp4 또는 mp3 형식으로 변환하는 스크립트입니다.

#### total
- `total_audio_video_image.py`: 오디오, 비디오 및 이미지와 관련된 작업을 총 수행하는 스크립트입니다.
- `total_origin_remove.py`: 데이터 경로에서 원본 파일을 제거하는 스크립트입니다.




## Getting Started


### Setting up Vitual Enviornment


1. Initialize and update the server
```
su -
source .bashrc
```

2. Create and Activate a virtual environment in the project directory
```
```
conda create -n env python=3.8
conda activate env
```
su -
4. To deactivate and exit the virtual environment, simply run:
source .bashrc
```
deactivate
```
```

### Install Requirements


2. Create and Activate a virtual environment in the project directory



To Insall the necessary packages liksted in `requirements.txt`, run the following command while your virtual environment is activated:
```
pip install -r requirements.txt
conda create -n env python=3.8
conda activate env
```

## Usage

#### Training
4. To deactivate and exit the virtual environment, simply run:

To train the model with your custom dataset, set the appropriate directories for the training images and model saving, then run the training script.

```
python train.py --data_dir /path/to/images --model_dir /path/to/model
deactivate
```

#### Inference


### Install Requirements



For generating predictions with a trained model, provide directories for evaluation data, the trained model, and output, then run the inference script.
To Install the necessary packages liksted in `requirements.txt`, run the following command while your virtual environment is activated:

```
python inference.py --data_dir /path/to/images --model_dir /path/to/model --output_dir /path/to/model
pip install -r requirements.txt
```


File renamed without changes.
52 changes: 42 additions & 10 deletions data/crawling/crawling_select_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,72 @@
from argparse import ArgumentParser

def parse_args():
"""
Command-line arguments parser.
Returns:
argparse.Namespace: Parsed arguments.
"""
parser = ArgumentParser()

# Conventional args
parser.add_argument('--csv_file', type=str, default='output_test.csv')
parser.add_argument('--data_path', type=str, default='origin/video')
parser.add_argument('--save_csv', type=str, default='new_output.csv')
parser.add_argument('--csv_file', type=str, default='output_test.csv',
help="Path to the CSV file containing data.")
parser.add_argument('--data_path', type=str, default='origin/video',
help="Path to the directory containing files and folders.")
parser.add_argument('--save_csv', type=str, default='new_output.csv',
help="Path to save the new CSV file.")

args = parser.parse_args()

return args


def list_files_and_folders(data_path):
"""
List files and folders in the given directory path.
Args:
data_path (str): Path to the directory.
Returns:
list or None: List of files and folders if directory exists, otherwise None.
"""
if os.path.isdir(data_path):
items = os.listdir(data_path)
return items
else:
return None

def main(csv_file, data_path, save_csv):
"""
Main function to process data.
Args:
csv_file (str): Path to the CSV file.
data_path (str): Path to the directory containing files and folders.
save_csv (str): Path to save the new CSV file.
"""
# Read CSV file
csv_data = pd.read_csv(csv_file, header=None)

# Get list of files and folders in the data path
youtube_ids = list_files_and_folders(data_path)

# Process each YouTube ID
for youtube_id in youtube_ids:
# Filter rows corresponding to the YouTube ID
filtered_df = csv_data[csv_data[0].astype(str).str.contains(youtube_id)]
first_row = filtered_df.iloc[0:1]

# Extract information from file name
file_name = list_files_and_folders(os.path.join(data_path, youtube_id))[0]
file_name_list = file_name.split("_")
first_row[4] = file_name_list[0]
first_row[5] = file_name_list[1]

# Add extracted information as new columns
first_row[4] = file_name_list[0]
first_row[5] = file_name_list[1]

# Save to new CSV file
first_row.to_csv(save_csv, mode="a", index=False, header=False)


if __name__ == '__main__':
args = parse_args()
main(**args.__dict__)
main(**args.__dict__)
18 changes: 18 additions & 0 deletions data/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
opencv-python
rembg
librosa
numpy
matplotlib
soundfile
pydub
pandas
moviepy
face_recognition
selenium
requests
beautifulsoup4
pytube
transformers
Pillow
torch
facenet-pytorch
66 changes: 38 additions & 28 deletions data/video/video_clipimage.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
import cv2

face_cascade = cv2.CascadeClassifier('code/file/haarcascade_frontalface_default.xml')

# 이미지 읽기
img = cv2.imread('code/file/image.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 얼굴 감지
faces = face_cascade.detectMultiScale(gray, 1.3, 5)

# 각 얼굴에 대해 반복
for (x, y, w, h) in faces:
# 얼굴 영역에 사각형 그리기
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)

# 얼굴 영역을 256x256 크기로 조정
face_crop = cv2.resize(img[y:y+h, x:x+w], (256, 256))
def detect_and_save_faces(image_path, output_path):
"""Detects faces in the input image and saves them as 256x256 pixel images.
# 얼굴 이미지 저장
cv2.imwrite('code/file/face_detected_256x256.png', face_crop)

# 이미지 보여주기
cv2.imshow('Image view', img)

# 'q' 키를 누를 때까지 대기
while cv2.waitKey(0) & 0xFF != ord('q'):
pass

cv2.destroyAllWindows()

Args:
image_path (str): Path to the input image file.
output_path (str): Path to save the detected face images.
"""
# Load the pre-trained face cascade classifier
face_cascade = cv2.CascadeClassifier('code/file/haarcascade_frontalface_default.xml')

# Read the input image
img = cv2.imread(image_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.3, 5)

# Iterate over each detected face
for (x, y, w, h) in faces:
# Draw rectangle around the face
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)

# Resize the face region to 256x256 pixels
face_crop = cv2.resize(img[y:y+h, x:x+w], (256, 256))

# Save the face image
cv2.imwrite(output_path, face_crop)

# Display the image
cv2.imshow('Image view', img)

# Wait for 'q' key to be pressed
while cv2.waitKey(0) & 0xFF != ord('q'):
pass

cv2.destroyAllWindows()

# Example usage
detect_and_save_faces('code/file/image.png', 'code/file/face_detected_256x256.png')
25 changes: 25 additions & 0 deletions data/video/video_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,38 @@ class Download:
'''
파일을 변환하기 위해선 ffmpeg란 프로그램을 별도로 설치해 컴퓨터 환경변수 설정을 마쳐야 함.
'''

def __init__(self, link):
'''
Download 클래스의 생성자입니다.
Args:
link (str): 유튜브 영상의 링크.
'''
# link 인자는 GUI에서 입력된 값을 받을 때 사용
# 컴퓨터 이용자명을 받아서 다운로드 폴더를 기본 폴더로 지정
self.parent_dir = f"/Users/{getuser()}/Documents/voice2face-data/code/file"
self.yt = YouTube(link)

def getVideoName(self):
'''(GUI 버전) 비디오 이름을 내보내는 함수'''
'''
유튜브 비디오의 제목을 반환하는 함수입니다.
Returns:
str: 비디오의 제목.
'''
name = self.yt.title
return name

def downloadMp3(self):
'''mp3 파일로 다운로드하는 함수'''
'''
mp3 형식으로 비디오를 다운로드하는 함수입니다.
Returns:
str: 다운로드한 mp3 파일의 이름.
'''
# mp4 형태지만 영상 없이 소리만 있는 파일 다운로드
stream = self.yt.streams.filter(only_audio=True).first()
stream.download(self.parent_dir)
Expand All @@ -37,6 +56,12 @@ def downloadMp3(self):

def downloadMp4(self):
'''mp4 파일로 다운로드하는 함수'''
'''
mp4 형식으로 비디오를 다운로드하는 함수입니다.
Returns:
str: 다운로드한 mp4 파일의 이름.
'''
audio = self.downloadMp3() # mp3 파일 다운로드
video = self.yt.streams.filter(adaptive=True, file_extension='mp4').first() # 비디오 객체 가져오기
print(video)
Expand Down

0 comments on commit e399ea0

Please sign in to comment.