-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
70 lines (52 loc) · 1.5 KB
/
Copy pathpreprocess.py
File metadata and controls
70 lines (52 loc) · 1.5 KB
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
# python3
# -*- coding: utf-8 -*-
#
# ===============================================
# Author: Scott A. Soifer
# Email: sas2412@columbia.edu
# Email: soifer00@gmail.com
# Date Created: Sun May 1 6:26:34 EST 2022
# ======================================================
import tensorflow as tf
print(tf.__version__)
from tensorflow.keras import layers
from tensorflow.keras import Model
from tensorflow.keras import metrics
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import pandas as pd
import csv
from datetime import datetime
import numpy as np
import pathlib
import os
from sklearn.model_selection import train_test_split
def get_label(path_name, offset=0):
start_i = path_name.rfind('/')+1
end_i = path_name.find('_')
label = int(path_name[start_i:end_i])
label -= offset
return label
def load_glyph_img(path_name):
img = tf.io.read_file(path_name)
img = tf.image.decode_png(img, channels=3)
img = tf.image.rgb_to_grayscale(img)
img = tf.squeeze(img)
img = tf.cast(img, tf.float32)
img /= 255.0 # normalize pixels to 0,1
return img
def load_glyphs(path):
imgs = []
labels = []
for path_name in os.listdir(path):
if 'png' in path_name:
img = load_glyph_img(path+path_name)
label = get_label(path_name, offset=ord('א'))
imgs.append(img)
labels.append(label)
imgs = np.array(imgs)
labels = np.array(labels)
return labels, imgs
if __name__=="__main__":
pass