generated from CodeChefVIT/template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpreprocessing.py
57 lines (52 loc) · 1.75 KB
/
preprocessing.py
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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
import string
def func(path):
img = cv2.imread(path)
img=cv2.resize(img,(300, 300))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),2)
th3 = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,2)
ret, res = cv2.threshold(th3, 70, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
return res
if not os.path.exists("Sign2"):
os.makedirs("Sign2")
if not os.path.exists("Sign2/train"):
os.makedirs("Sign2/train")
if not os.path.exists("Sign2/test"):
os.makedirs("Sign2/test")
l=[]
path="Sign/train"
path2="Sign2/train"
for (dirpath,dirnames,filenames) in os.walk(path):
dirnames.sort()
for dirname in dirnames:
for (direcpath,direcnames,files) in os.walk(path+"/"+dirname):
if not os.path.exists(path2+"/"+dirname):
os.makedirs(path2+"/"+dirname)
for file in files:
if dirname not in l:
l.append(dirname)
actual_path=path+"/"+dirname+"/"+file #path of each image
actual_path2=path2+"/"+dirname+"/"+file
bw_img=func(actual_path)
cv2.imwrite(actual_path2,bw_img)
path="Sign/test"
path2="Sign2/test"
for (dirpath,dirnames,filenames) in os.walk(path):
dirnames.sort()
for dirname in dirnames:
for (direcpath,direcnames,files) in os.walk(path+"/"+dirname):
if not os.path.exists(path2+"/"+dirname):
os.makedirs(path2+"/"+dirname)
for file in files:
if dirname not in l:
l.append(dirname)
actual_path=path+"/"+dirname+"/"+file #path of each image
actual_path2=path2+"/"+dirname+"/"+file
bw_img=func(actual_path)
cv2.imwrite(actual_path2,bw_img)
print("Done")