이준호

feat: pad_data 추가

<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="pytest" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/dataset.iml" filepath="$PROJECT_DIR$/.idea/dataset.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="7a60ea4e-0e66-4fb3-9f90-7b3a4b35774b" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/change.py" beforeDir="false" afterPath="$PROJECT_DIR$/change.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Python Script" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectId" id="1s952kK9rpTSw6LkimeqRC1dJQ8" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/file" />
</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/file" />
<recent name="C:\Users\이준호\Desktop\nomad\dataset\juno\data" />
</key>
</component>
<component name="RunManager" selected="Python.change">
<configuration name="change" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="dataset" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/change.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="main" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="dataset" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/main.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Python.change" />
<item itemvalue="Python.main" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="7a60ea4e-0e66-4fb3-9f90-7b3a4b35774b" name="Default Changelist" comment="" />
<created>1620271759246</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1620271759246</updated>
</task>
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
</project>
\ No newline at end of file
{
"CurrentProjectSetting": null
}
\ No newline at end of file
{
"ExpandedNodes": [
""
],
"PreviewInSolutionExplorer": false
}
\ No newline at end of file
No preview for this file type
No preview for this file type
## Capstone Design: customData
### 🌈 customData 소개
```
customData는 0 ~ 9 범위의 숫자로 이루어진 데이터셋 입니다.
데이터를 수집한 방법은 다음과 같습니다
```
- iPad를 이용하여 `0 ~ 9` 범위의 숫자를 사람들에게 직접 기록을 부탁하였습니다.
- `25~27살` 남자 5명, `20~24살` 여자 5명에게 데이터를 받았습니다.
- 받은 데이터를 가공하여 저장하고, 거기에 `blur` `color` `synthesis` 를 적용하였습니다.
- 현재 숫자당 약 1,200 여개의 데이터가 존재합니다.
<br/>
### 👨🏻‍💻 OpenCV 를 이용한 customDataset 만들기
1. 배경색 변경하기
```python
def coloring(num, source)
```
2. 이미지에 noise 추가하기 (blur 처리 )
```python
def bluring(num, source)
```
3. 다른 이미지와 합성하기(원고지 등을 합성하여 배경으로 사용)
```python
def synthesis(num, source, background)
```
<br/>
### 🙆🏻‍♂️ How to use
- `filename` 원본 이미지 경로를 기입합니다.
- 배경 합성시엔, `background` 원본 배경 이미지 경로를 기입합니다.
- `change.py` 파일을 실행합니다.
```python
python3 change.py # 를 통해 실행합니다
```
import numpy as np
import matplotlib.pyplot as plt
import cv2
import os
background = ' ' # put background.png's location
def bluring(num, source):
img = cv2.imread(source)
kernel = np.ones((5, 5), np.float32)/25
blur = cv2.filter2D(img, -1, kernel)
cv2.imshow("blur", blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
# cv2.imwrite(f"./juno/bluring/{num}/{source.split('/')[4].split('.')[0]}.png", blur)
def coloring(num, source):
img = cv2.imread(source)
yuv_img = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
cv2.imshow('yuv img', yuv_img) # color rgb -> bgr
cv2.waitKey(0)
cv2.destroyAllWindows()
# color_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# cv2.imshow('color img', color_img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# cv2.imwrite(f"./juno/coloring/{num}/{source.split('/')[4].split('.')[0]}.png", yuv_img)
def rotate(num, source):
image = cv2.imread(source)
# 행과 열 정보만 저장합니다.
height, width = image.shape[:2]
M = cv2.getRotationMatrix2D((width / 2, height / 2), 90, 0.5)
dst = cv2.warpAffine(image, M, (width, height))
result = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)
cv2.imshow('rotate', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
# cv2.imwrite(f"./juno/rotate/{num}/{source.split('/')[4].split('.')[0]}.png", result)
def synthesis(num, source, background):
background_image = cv2.imread(background)
background_resize = cv2.resize(background_image, dsize=(28, 28), interpolation=cv2.INTER_AREA)
# image resize 확인
cv2.imshow('background_resize', background_resize)
cv2.waitKey(0)
# image load
source_image = cv2.imread(source)
result = cv2.add(source_image, background_resize)
cv2.imshow('synthesis',result)
cv2.waitKey(0)
cv2.destroyAllWindows()
# cv2.imwrite(f"./juno/synthesis6/{num}/{source.split('/')[4].split('.')[0]}.png", result)
for i in range(10):
file_dir = f"./juno/data/{i}"
file_list = os.listdir(file_dir)
for file in file_list:
pass
# coloring(i, f"{file_dir}/{file}")
# bluring(i, f"{file_dir}/{file}")
# synthesis(i, f"{file_dir}/{file}", background)
import cv2
import os
import numpy as np
filenames = ['13.jpg', '14.jpg', '15.jpg']
def generate_dataset(filename):
src = cv2.imread(filename, cv2.IMREAD_GRAYSCALE) # 그레이 스케일로 변환
src = cv2.resize(src, (int(src.shape[1] / 5), int(src.shape[0] / 5))) # 이미지의 크기가 워낙 크기 때문에 5분의1로 줄여서 확인
cv2.imshow('gray', src)
k = cv2.waitKey(0)
cv2.destroyAllWindows()
src = src[0:src.shape[0] - 10, 15:src.shape[1] - 25] # 양 옆의 노이즈를 제거
ret, binary = cv2.threshold(src, 170, 255, cv2.THRESH_BINARY_INV) # 영상 이진화
cv2.imshow('binary', binary)
k = cv2.waitKey(0)
cv2.destroyAllWindows()
# binary = cv2.morphologyEx(binary , cv2.MORPH_OPEN , cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2,2)), iterations = 2)
# cv2.imshow('binary',binary)
# k = cv2.waitKey(0)
# cv2.destroyAllWindows()
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 외곽선 검출
color = cv2.cvtColor(binary, cv2.COLOR_GRAY2BGR) # 이진화 이미지를 color이미지로 복사(확인용)
cv2.drawContours(color, contours, -1, (0, 255, 0), 3) # 초록색으로 외곽선을 그려준다.
# 리스트연산을 위해 초기변수 선언
bR_arr = []
digit_arr = []
digit_arr2 = []
count = 0
# 검출한 외곽선에 사각형을 그려서 배열에 추가
for i in range(len(contours)):
bin_tmp = binary.copy()
x, y, w, h = cv2.boundingRect(contours[i])
bR_arr.append([x, y, w, h])
bR_arr = sorted(bR_arr, key=lambda num: num[0], reverse=False)
print(bR_arr[:5])
print(len(bR_arr))
# 작은 노이즈데이터 버림,사각형그리기, 10개씩 리스트로 다시 묶어서 저장
for x, y, w, h in bR_arr:
tmp_y = bin_tmp[y - 2:y + h + 2, x - 2:x + w + 2].shape[0]
tmp_x = bin_tmp[y - 2:y + h + 2, x - 2:x + w + 2].shape[1]
if tmp_x and tmp_y > 10:
count += 1
cv2.rectangle(color, (x - 2, y - 2), (x + w + 2, y + h + 2), (0, 0, 255), 1)
digit_arr.append(bin_tmp[y - 2:y + h + 2, x - 2:x + w + 2])
if count == 10:
digit_arr2.append(digit_arr)
digit_arr = []
count = 0
cv2.imshow('contours', color)
k = cv2.waitKey(0)
cv2.destroyAllWindows()
for i in range(0, len(digit_arr2)):
for j in range(len(digit_arr2[i])):
count += 1
if i == 0: # 1일 경우 비율 유지를 위해 마스크를 만들어 그위에 얹어줌
width = digit_arr2[i][j].shape[1]
height = digit_arr2[i][j].shape[0]
tmp = (height - width) / 2
mask = np.zeros((height, height))
mask[0:height, int(tmp):int(tmp) + width] = digit_arr2[i][j]
digit_arr2[i][j] = cv2.resize(mask, (28, 28))
else:
digit_arr2[i][j] = cv2.resize(digit_arr2[i][j], (28, 28))
# if i == 9 : i = -1
cv2.imwrite(f"./juno/data/{i}/" + str(i) + '_' + str(j) + f"_{filename.split('.')[0]}.png", digit_arr2[i][j])
for filename in filenames:
generate_dataset(filename)
# def createdataset(directory): #sklearn사용을 위해 데이터세트를 생성
# files = os.listdir(directory)
# x = []
# y = []
# for file in files:
# attr_x = cv2.imread(directory+file, cv2.IMREAD_GRAYSCALE)
# attr_x = attr_x.flatten()
# attr_y = file[0]
# x.append(attr_x)
# y.append(attr_y)
#
# x = np.array(x)
# y = np.array(y)
# return x , y
#
# train_dir = './juno/data/'
# train_x ,train_y = createdataset(train_dir)
# test_dir = './juno/test/'
# test_x , test_y = createdataset(test_dir)