-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathread_images.py
88 lines (68 loc) · 2.17 KB
/
read_images.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 11 22:44:41 2019
@author: Gireesh Sundaram
"""
import cv2
import numpy as np
import math
import pandas as pd
import codecs
from configuration import window_height, window_width, window_shift, MPoolLayers_H
#%%
vec_per_window = window_width / math.pow(2, MPoolLayers_H)
#%%
#reading the class files
data = {}
with codecs.open("Data\\class.txt", 'r', encoding='utf-8') as cF:
data = cF.read().split('\n')
#%%
def returnClasses(string):
text = list(string)
text = ["<SPACE>"] + ["<SPACE>" if x==" " else x for x in text] + ["<SPACE>"]
classes = [data.index(x) if x in data else 2 for x in text]
return classes
#%%
infile = pd.read_csv(".\\Data\\list.csv")
#%%
inputList = []
seqLens = []
targetList = []
for record in range(0, len(infile)):
path = infile["Path"][record]
annotation = infile["Annotation"][record]
#reading images from the path
print("Reading file: " + path)
image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
h, w = np.shape(image)
print(np.shape(image))
#setting factor if the image is greater than the window height
if (h > window_height): factor = window_height/h
else: factor = 1
#resizing the image to the specified window height
image = cv2.resize(image, None, fx=factor, fy=factor, interpolation = cv2.INTER_CUBIC)
h, w = np.shape(image)
featureSet = []
winId = 0
while True:
s = winId * window_shift
e = s + window_width
if e > w:
break
wnd = image[:h, s:e]
featureSet.append(wnd)
winId = winId + 1
inputList.append(featureSet)
#taking the sequence length
winId = 0
wpd = 0
while True:
s = winId * window_shift
e = s + window_width
if e > w:
sl = (winId+1) * vec_per_window
seqLens.append(sl)
break
winId = winId + 1
#taking the text annotations from the csv file and converting to labels
targetList.append(returnClasses(annotation))