Toggle navigation
Toggle navigation
This project
Loading...
Sign in
2021-1-capstone-design2
/
2017110276
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
이혜리
2021-06-13 20:04:31 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
40c5651eff58e8bfc1e8eb53799c4377b57fc6c8
40c5651e
1 parent
56d24bfd
code
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
100 additions
and
0 deletions
ConvLSTM.ipynb
video_to_image_sequence.ipynb
ConvLSTM.ipynb
0 → 100644
View file @
40c5651
{
"nbformat"
:
4
,
"nbformat_minor"
:
0
,
"metadata"
:{
"colab"
:{
"name"
:
"ConvLSTM.ipynb"
,
"provenance"
:[],
"collapsed_sections"
:[],
"authorship_tag"
:
"ABX9TyMXox37jjcWOTMtoI++NxCW"
},
"kernelspec"
:{
"display_name"
:
"Python 3"
,
"name"
:
"python3"
},
"language_info"
:{
"name"
:
"python"
},
"accelerator"
:
"GPU"
},
"cells"
:[{
"cell_type"
:
"code"
,
"metadata"
:{
"id"
:
"KChrGBiCjFJF"
,
"executionInfo"
:{
"status"
:
"ok"
,
"timestamp"
:
1623580255557
,
"user_tz"
:
-540
,
"elapsed"
:
2437
,
"user"
:{
"displayName"
:
"이혜리[학생](소프트웨어융합대학 컴퓨터공학과)"
,
"photoUrl"
:
""
,
"userId"
:
"16579190241035414660"
}}},
"source"
:[
"import numpy as np
\n
"
,
"import cv2
\n
"
,
"import matplotlib.pyplot as plt
\n
"
,
"import os
\n
"
,
"import random
\n
"
,
"from math import ceil
\n
"
,
"from tqdm import tqdm_notebook
\n
"
,
"import tensorflow as tf
\n
"
,
"from keras.models import Sequential
\n
"
,
"from keras.layers import Dense
\n
"
,
"from keras.layers import Flatten
\n
"
,
"from keras.layers import Dropout
\n
"
,
"from keras.layers import LSTM
\n
"
,
"from keras.layers import TimeDistributed
\n
"
,
"from keras.layers import ConvLSTM2D
\n
"
,
"from keras.utils.np_utils import to_categorical
\n
"
,
"from keras.models import load_model"
],
"execution_count"
:
3
,
"outputs"
:[]},{
"cell_type"
:
"code"
,
"metadata"
:{
"id"
:
"pRoXlH-ejqaV"
,
"executionInfo"
:{
"status"
:
"ok"
,
"timestamp"
:
1623580304615
,
"user_tz"
:
-540
,
"elapsed"
:
361
,
"user"
:{
"displayName"
:
"이혜리[학생](소프트웨어융합대학 컴퓨터공학과)"
,
"photoUrl"
:
""
,
"userId"
:
"16579190241035414660"
}}},
"source"
:[
"BASE_PATH = './Capston2'
\n
"
,
"DATASET_PATH = BASE_PATH+ '/data'"
],
"execution_count"
:
6
,
"outputs"
:[]},{
"cell_type"
:
"markdown"
,
"metadata"
:{
"id"
:
"MW6_oaLWFhXH"
},
"source"
:[
"### Image sequence를 numpy로 만들기"
]},{
"cell_type"
:
"markdown"
,
"metadata"
:{
"id"
:
"27d5FeyVFyD4"
},
"source"
:[
"1. 각 image sequence의 data directory path 가져오고 train, test dataset 나누기"
]},{
"cell_type"
:
"code"
,
"metadata"
:{
"id"
:
"WCxaqiIAkiJ8"
},
"source"
:[
"data_dir_path_list = []
\n
"
,
"for top, dir, f in os.walk(DATASET_PATH):
\n
"
,
" if len(dir) == 0:
\n
"
,
" data_dir_path_list.append(top)
\n
"
,
"
\n
"
,
"random.shuffle(data_dir_path_list)
\n
"
,
"
\n
"
,
"tr_ratio = 0.8
\n
"
,
"point = ceil(len(data_dir_path_list) * tr_ratio)
\n
"
,
"train_data_dir_path = data_dir_path_list[:point]
\n
"
,
"test_data_dir_path = data_dir_path_list[point:]
\n
"
,
"
\n
"
,
"# train_data_dir_path_numpy = np.array(train_data_dir_path)
\n
"
,
"# test_data_dir_path_numpy = np.array(test_data_dir_path)
\n
"
,
"# np.save(BASE_PATH + '/train_dir_path', train_data_dir_path_numpy)
\n
"
,
"# np.save(BASE_PATH + '/test_dir_path', test_data_dir_path_numpy)"
],
"execution_count"
:
null
,
"outputs"
:[]},{
"cell_type"
:
"markdown"
,
"metadata"
:{
"id"
:
"Q7IcRpY-98LU"
},
"source"
:[
"2. image sequence를 5D tensor로 만들어서 저장"
]},{
"cell_type"
:
"code"
,
"metadata"
:{
"id"
:
"3inQO4yQnOfp"
},
"source"
:[
"def get_image_sequence(data_dir_path_list, typ):
\n
"
,
" idx = {'fight':0 , 'swoon':1, 'normal':2}
\n
"
,
" dataset = []
\n
"
,
" label = []
\n
"
,
" for base_path in tqdm_notebook(data_dir_path_list):
\n
"
,
" tmp = []
\n
"
,
" im_list = sorted(os.listdir(base_path))
\n
"
,
" for i in im_list:
\n
"
,
" path = os.path.join(base_path,i)
\n
"
,
" img = cv2.imread(path)
\n
"
,
" img = cv2.resize(img, dsize=(0, 0), fx=0.045, fy=0.045, interpolation=cv2.INTER_AREA)
\n
"
,
" tmp.append(np.array(img))
\n
"
,
" if len(tmp) == 20:
\n
"
,
" dataset.append(np.array(tmp))
\n
"
,
" label.append(idx[base_path.split('/')[-3]])
\n
"
,
"
\n
"
,
" dataset = np.array(dataset)
\n
"
,
" label = to_categorical(label)
\n
"
,
"
\n
"
,
" np.save('./drive/MyDrive/Capston2/{}_img_seq'.format(typ), dataset)
\n
"
,
" np.save('./drive/MyDrive/Capston2/{}_label'.format(typ), label)
\n
"
,
"
\n
"
,
"get_image_sequence(train_data_dir_path, 'train')
\n
"
,
"get_image_sequence(test_data_dir_path, 'test')"
],
"execution_count"
:
null
,
"outputs"
:[]},{
"cell_type"
:
"markdown"
,
"metadata"
:{
"id"
:
"McUXDRXE-LHR"
},
"source"
:[
"### ConvLSTM model"
]},{
"cell_type"
:
"code"
,
"metadata"
:{
"id"
:
"HWfLFz7g4cac"
},
"source"
:[
"# data load
\n
"
,
"train_img = np.load(BASE_PATH+'/train_img_seq.npy', allow_pickle = True)
\n
"
,
"train_label = np.load(BASE_PATH+'/train_label.npy', allow_pickle = True)
\n
"
,
"test_img = np.load(BASE_PATH+'/test_img_seq.npy', allow_pickle = True)
\n
"
,
"test_label = np.load(BASE_PATH+'/test_label.npy', allow_pickle = True)"
],
"execution_count"
:
null
,
"outputs"
:[]},{
"cell_type"
:
"code"
,
"metadata"
:{
"colab"
:{
"base_uri"
:
"https://localhost:8080/"
},
"id"
:
"YoOX36je18Bn"
,
"executionInfo"
:{
"status"
:
"ok"
,
"timestamp"
:
1623509935114
,
"user_tz"
:
-540
,
"elapsed"
:
263
,
"user"
:{
"displayName"
:
"이혜리[학생](소프트웨어융합대학 컴퓨터공학과)"
,
"photoUrl"
:
""
,
"userId"
:
"16579190241035414660"
}},
"outputId"
:
"3e639a0d-dc89-43dd-bec6-eedf404c6f93"
},
"source"
:[
"print(train_img.shape)
\n
"
,
"print(train_label.shape)
\n
"
,
"print(test_img.shape)
\n
"
,
"print(test_label.shape)"
],
"execution_count"
:
null
,
"outputs"
:[{
"output_type"
:
"stream"
,
"text"
:[
"(1994, 20, 97, 173, 3)
\n
"
,
"(1994, 3)
\n
"
,
"(401, 20, 97, 173, 3)
\n
"
,
"(401, 3)
\n
"
],
"name"
:
"stdout"
}]},{
"cell_type"
:
"code"
,
"metadata"
:{
"id"
:
"xt6UbAh03aRj"
},
"source"
:[
"# fit and evaluate a model
\n
"
,
"def evaluate_model(trainX, trainy, testX, testy):
\n
"
,
" epochs, batch_size = 10, 20
\n
"
,
" n_steps = 20
\n
"
,
" n_outputs = trainy.shape[1] # 3
\n
"
,
"
\n
"
,
" # define model
\n
"
,
" model = Sequential()
\n
"
,
" model.add(ConvLSTM2D(filters=32, kernel_size=(3,3), activation='relu', data_format =
\"
channels_last
\"
, input_shape=(n_steps, trainX.shape[2], trainX.shape[3], 3)))
\n
"
,
" model.add(Dropout(0.5))
\n
"
,
" model.add(Flatten())
\n
"
,
" model.add(Dense(100, activation='relu'))
\n
"
,
" model.add(Dense(n_outputs, activation='softmax'))
\n
"
,
"
\n
"
,
" model.summary()
\n
"
,
"
\n
"
,
" model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy', tf.keras.metrics.Recall(), tf.keras.metrics.Precision()])
\n
"
,
" # fit network
\n
"
,
" model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=1, validation_split=0.15, shuffle=True)
\n
"
,
" # model save
\n
"
,
" model.save(BASE_PATH+'/convLSTM.h5')
\n
"
,
"
\t
# evaluate model
\n
"
,
" accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=1)
\n
"
,
" return accuracy
\n
"
,
"
\n
"
,
"acc = evaluate_model(train_img, train_label, test_img, test_label)
\n
"
,
"print('test accuracy: %.2f%%', % (acc[1]*100))"
],
"execution_count"
:
null
,
"outputs"
:[]},{
"cell_type"
:
"markdown"
,
"metadata"
:{
"id"
:
"QCjP5lXs_Loe"
},
"source"
:[
"### 데이터 시험해보기"
]},{
"cell_type"
:
"code"
,
"metadata"
:{
"colab"
:{
"base_uri"
:
"https://localhost:8080/"
,
"height"
:
942
,
"output_embedded_package_id"
:
"1IWQL7D1th7TXlyWnXlZ5a6wDWOo7I5Wr"
},
"id"
:
"yGoPognn4hC3"
,
"executionInfo"
:{
"status"
:
"ok"
,
"timestamp"
:
1623503736985
,
"user_tz"
:
-540
,
"elapsed"
:
15093
,
"user"
:{
"displayName"
:
"이혜리[학생](소프트웨어융합대학 컴퓨터공학과)"
,
"photoUrl"
:
""
,
"userId"
:
"16579190241035414660"
}},
"outputId"
:
"c4e5ee26-6e99-4ccb-9fb7-145b614d1ba5"
},
"source"
:[
"model = load_model(BASE_PATH+'/convLSTM.h5')
\n
"
,
"img_number = 400
\n
"
,
"sc = model.evaluate(test_img[img_number:img_number+1], test_label[img_number:img_number+1], verbose=0)
\n
"
,
"print(
\"
%s: %.2f%%
\"
% (model.metrics_names[1], sc[1]*100))
\n
"
,
"print(
\"
실제 label:
\"
, test_label[img_number])
\n
"
,
"
\n
"
,
"fig=plt.figure(figsize=(27, 13))
\n
"
,
"rows = 4
\n
"
,
"cols = 5
\n
"
,
"for i in range(20):
\n
"
,
" im = cv2.resize(test_img[img_number][i], dsize=(320, 180), interpolation=cv2.INTER_LINEAR)
\n
"
,
" fig.add_subplot(rows, cols, i+1)
\n
"
,
" plt.imshow(im)
\n
"
,
"
\n
"
,
"plt.tight_layout()
\n
"
,
"plt.show()"
],
"execution_count"
:
null
,
"outputs"
:[{
"output_type"
:
"display_data"
,
"data"
:{
"text/plain"
:
"Output hidden; open in https://colab.research.google.com to view."
},
"metadata"
:{}}]}]}
\ No newline at end of file
video_to_image_sequence.ipynb
0 → 100644
View file @
40c5651
{
"cells"
:
[
{
"cell_type"
:
"code"
,
"execution_count"
:
1
,
"metadata"
:
{},
"outputs"
:
[],
"source"
:
[
"import os
\n
"
,
"import random
\n
"
,
"import cv2
\n
"
,
"from PIL import Image"
]
},
{
"cell_type"
:
"code"
,
"execution_count"
:
2
,
"metadata"
:
{},
"outputs"
:
[],
"source"
:
[
"action = 'fight'
\n
"
,
"base_dir = '/mnt/c/Users/hyerilee/Downloads/{}'.format(action)
\n
"
,
"file_path = {}"
]
},
{
"cell_type"
:
"code"
,
"execution_count"
:
13
,
"metadata"
:
{},
"outputs"
:
[],
"source"
:
[
"for (root ,dirs, files) in os.walk(base_dir):
\n
"
,
" if len(dirs) == 0 and len(files) != 0:
\n
"
,
" if root not in file_path:
\n
"
,
" file_path[root] = files"
]
},
{
"cell_type"
:
"code"
,
"execution_count"
:
75
,
"metadata"
:
{},
"outputs"
:
[],
"source"
:
[
"def getFrame(sec, saveFilePath, count):
\n
"
,
" vidcap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
\n
"
,
" hasFrames,img = vidcap.read()
\n
"
,
" if hasFrames:
\n
"
,
" img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
\n
"
,
" im_pil = Image.fromarray(img) # wsl에서 cv2.imwirte 안돼서 하는 작업. cv2 to pillow
\n
"
,
" if os.path.exists(saveFilePath) and count==1: #동일한 시작지점을 또 capture하는 경우 skip
\n
"
,
" return False
\n
"
,
" if not os.path.exists(saveFilePath):
\n
"
,
" os.makedirs(saveFilePath)
\n
"
,
" im_pil.save(saveFilePath+'/'+'{:03d}'.format(count)+
\"
.jpg
\"
)
\n
"
,
"# cv2.imwrite(saveFilePath+'{:03d}'.format(count)+
\"
.jpg
\"
, image) # save frame as JPG file
\n
"
,
" return hasFrames
\n
"
,
"
\n
"
,
"starts = [i for i in range(180,291)] #가능시작지점
\n
"
,
"for (file_dir, file_list) in file_path.items():
\n
"
,
" for video_file in file_list:
\n
"
,
" video_path = file_dir + '/' + video_file
\n
"
,
" vidcap = cv2.VideoCapture(video_path)
\n
"
,
" for i in range(0,10):
\n
"
,
" sec = random.choice(starts)
\n
"
,
" start_sec = sec
\n
"
,
" frameRate = 0.5 # it will capture image in each 1 second
\n
"
,
" cnt = 1
\n
"
,
" success = getFrame(sec, video_path[:-4]+'_start'+str(start_sec)+'s', cnt)
\n
"
,
" while success:
\n
"
,
" cnt += 1
\n
"
,
" sec += frameRate
\n
"
,
" success = getFrame(sec, video_path[:-4]+'_start'+str(start_sec)+'s', cnt)
\n
"
,
" if cnt >= 20:
\n
"
,
" break"
]
}
],
"metadata"
:
{
"kernelspec"
:
{
"display_name"
:
"Python 3"
,
"language"
:
"python"
,
"name"
:
"python3"
},
"language_info"
:
{
"codemirror_mode"
:
{
"name"
:
"ipython"
,
"version"
:
3
},
"file_extension"
:
".py"
,
"mimetype"
:
"text/x-python"
,
"name"
:
"python"
,
"nbconvert_exporter"
:
"python"
,
"pygments_lexer"
:
"ipython3"
,
"version"
:
"3.6.13"
}
},
"nbformat"
:
4
,
"nbformat_minor"
:
4
}
Please
register
or
login
to post a comment