diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f539ddc --- /dev/null +++ b/Makefile @@ -0,0 +1,56 @@ +# Makefile + +# Shell +SHELL := /bin/bash +#Project +PROJECT = "image_pro" +#Version +RELEASE = "0.0.1" +# Path to source directory +PROJECTPATH := . +# OS machine +OS_TYPE = 'uname -a' +# CPP EXTENSIONS +CPP_EXT = ".cpp" + +# SETUP +SETUPFILE = "setup.py" + +# VENV + +_virtualenv: + # create virtual env + virtualenv _virtualenv + _virtualenv/bin/pip install --upgrade pip + _virtualenv/bin/pip install --upgrade setuptools + +_use_env: + # use virtual env + source _virtualenv/bin/activate + +##### PYTHON + +prebuild: + @echo "Preparing build" + @echo "Installing requirements" + pip install -r requirements.txt + +setup: + echo "Setup..." + sudo python setup.py install + +test: + echo "Test" + python setup.py test + +clean: + rm -f MANIFEST + rm -rf build dist + +deactivate_env: + deactivate + + +all: prebuild setup + +.PHONY: clean \ No newline at end of file diff --git a/image_pro/__init__.py b/image_pro/__init__.py new file mode 100644 index 0000000..ab28e3f --- /dev/null +++ b/image_pro/__init__.py @@ -0,0 +1 @@ +#NOTE \ No newline at end of file diff --git a/image_pro/parameters.py b/image_pro/parameters.py new file mode 100644 index 0000000..e61d3f4 --- /dev/null +++ b/image_pro/parameters.py @@ -0,0 +1,7 @@ +UR_DATA_PATH = "./train/*/*.jpg" +UR_PRED_PATH = "./test1/test1/*.jpg" +UR_OPTION = "gray" +UR_TEST_SIZE = 1000 +T_HEIGHT = 256 +T_WIDTH = 256 +T_Channnels = 1 \ No newline at end of file diff --git a/image_pro/tfRecord/Tfrecord.py b/image_pro/tfRecord/Tfrecord.py new file mode 100644 index 0000000..fff4917 --- /dev/null +++ b/image_pro/tfRecord/Tfrecord.py @@ -0,0 +1,171 @@ +import os +import cv2 +import sys +import glob +import numpy as np +import tensorflow as tf + +from random import shuffle, seed +from image_pro.utils_data.contours.contours import draw_all_contours + + +# This function returns the different paths and corresponding labels +def shuffling_data(data_path, labels): + c = list(zip(data_path, labels)) + shuffle(c) + #NOTE: data and labeld are tuples. Care about immutability + data, labeled = zip(*c) #NOTE:ith element in data correpand to ith label elmt + + return data, labeled + +def read_paths_treat(ur_path, test_size): + pathname = os.path.dirname(ur_path) + #Reading the path of each data(ex: image) and then extracting the labels from path + data_path = glob.glob(ur_path) + '''labels = [int(i.split('.')[0][-1]) for i in data_path]'''#xray + #NOTE: labels and labels_num: modify split method to get labels// conditions "cat" or "dog" + labels = [i.split('\\')[-1] for i in data_path] #Cat vs Dog + labels_num = [0 if "cat" in i else 1 for i in labels] #Cat vs Dog + len_d = len(labels_num) + k = int(len_d/2) + a = int((len_d-test_size)/2) + b = int(len_d-(test_size/2)) + data_path_test, labels_test = data_path[a : k], labels_num[a:k] + data_path_test = data_path_test + data_path[b:] + labels_test = labels_test + labels_num[b:] + data_path_train,labels_train = (data_path[0:a]), (labels_num[0:a]) + data_path_train = data_path_train + data_path[k:b] + labels_train = labels_train + labels_num[k:b] + data_path_test, labels_test = shuffling_data(data_path_test, labels_test) + data_path_train, labels_train = shuffling_data(data_path_train, labels_train) + return data_path_test, labels_test,data_path_train, labels_train + + +def split_data(training_prop,test_prop, data, labels, dev_prop=0): + ''' + - training_prop is required .numerical float < 1 + - dev_prop is optional. Numerical float < 0.3 + - test_prop is required. Numerical float < 0.3 + - data and labels are lists + ''' + #NOTE: data is a list of paths of each image + + training_data = data[0:int(training_prop*len(data))] + training_labels = labels[0:int(training_prop*len(labels))] + + if dev_prop != 0: + dev_data = data[int(training_prop*len(data)):int((1-dev_prop)*len(data))] + dev_labels = labels[int(training_prop*len(labels)):int((1-dev_prop)*len(labels))] + test_data = data[int((1-dev_prop)*len(data)):] + test_labels = labels[int((1-dev_prop)*len(labels)):] + return training_data,training_labels, dev_data, dev_labels, test_data, test_labels + + else: + test_data = data[int((1-test_prop)*len(data)):] + test_labels = labels[int((1-test_prop)*len(labels)):] + return training_data, training_labels, test_data, test_labels + +def load(ur_path, option): + + if option =="gray": + img = cv2.imread(ur_path,cv2.IMREAD_GRAYSCALE) + elif option =="unchanged": + img = cv2.imread(ur_path, cv2.IMREAD_UNCHANGED) + else: + img = cv2.imread(ur_path,cv2.IMREAD_COLOR) + return img + + +# Load image + +def load_image(data, s_width, s_height, option): + + #cv2 load data from data (value of one path) + img_load = load(data, UR_OPTION) + '''img_after = cv2.cvtColor(img_load, cv2.COLOR_BGR2GRAY)''' + img= cv2.resize(img_cont, dsize=(s_width, s_height), interpolation=cv2.INTER_CUBIC) + img_f = img.tostring() + + return img_f + + +def _int64_feature(value): + + return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) + + + +def _bytes_feature(value): + + return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) + + + +def get_tfrecord_file(ur_path, test_size, s_width, s_height,option, *x_filename,): + + #NOTE:data is a tuple (list) of paths of each image + #NOTE: data can come from the dev-set or test-set + #NOTE: Same for labeled + + test_img, test_label, train_img, train_label = read_paths_treat(ur_path, test_size) + file_path = x_filename[0] + #Open a TFRecordWriter + + writer = tf.python_io.TFRecordWriter(file_path) + for i in range(len(train_img)): + new_img = load_image(train_img[i], s_width, s_height,option) + new_label = train_label[i] + new_feature = { 'height': _int64_feature(s_height), + 'width' : _int64_feature(s_width), + 'label': _int64_feature(new_label), + 'image': _bytes_feature(tf.compat.as_bytes(new_img)) + } + + #NOTE: PLease refer to this url for defenirtion: https://github.com/tensorflow/tensorflow/blob/r1.7/tensorflow/core/example/example.proto + example = tf.train.Example(features=tf.train.Features(feature=new_feature)) + writer.write(example.SerializeToString()) + + writer.close() + sys.stdout.flush() + file_path = x_filename[1] + writer = tf.python_io.TFRecordWriter(file_path) + + for j in range(len(test_img)): + + new_img = load_image(test_img[j], s_width, s_height,option) + new_label = test_label[j] + new_feature = { 'height': _int64_feature(s_height), + 'width' : _int64_feature(s_width), + 'label': _int64_feature(new_label), + 'image': _bytes_feature(tf.compat.as_bytes(new_img)) + } + + #NOTE: PLease refer to this url for def: https://github.com/tensorflow/tensorflow/blob/r1.7/tensorflow/core/example/example.proto + example = tf.train.Example(features=tf.train.Features(feature=new_feature)) + writer.write(example.SerializeToString()) + + writer.close() + sys.stdout.flush() + return 0 + + +def get_tfrecord_pred(ur_path, s_width, s_height,option, x_filename): + pathname = os.path.dirname(ur_path) + data_path = glob.glob(ur_path) + file_path = x_filename + writer = tf.python_io.TFRecordWriter(file_path) + for i in range(len(data_path)): + new_img = load_image(data_path[i], s_width, s_height,option) + new_feature = { 'height': _int64_feature(s_height), + 'width' : _int64_feature(s_width), + 'image': _bytes_feature(tf.compat.as_bytes(new_img)) + } + example = tf.train.Example(features=tf.train.Features(feature=new_feature)) + writer.write(example.SerializeToString()) + writer.close() + sys.stdout.flush() + return 0 + + +'''get_tfrecord_pred(UR_PRED_PATH, 256, 256,UR_OPTION, "tfpred.tfrecords")''' +get_tfrecord_file(UR_DATA_PATH, UR_TEST_SIZE, 128, 128,UR_OPTION, "tftrain.tfrecords", "tftest.tfrecords") \ No newline at end of file diff --git a/image_pro/tfRecord/__init__.py b/image_pro/tfRecord/__init__.py new file mode 100644 index 0000000..6f492a6 --- /dev/null +++ b/image_pro/tfRecord/__init__.py @@ -0,0 +1 @@ +#NOTE diff --git a/image_pro/tfRecord/decode.py b/image_pro/tfRecord/decode.py new file mode 100644 index 0000000..13e113a --- /dev/null +++ b/image_pro/tfRecord/decode.py @@ -0,0 +1,117 @@ +import os +import numpy as np +import tensorflow as tf +import matplotlib.pyplot as plt + +#NOTE: refer to this url: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/how_tos/reading_data/fully_connected_reader.py + +def tfrec_data_input_fn(filenames, num_epochs=1, batch_size=16, shuffle=False): + + def _input_fn(): + def _parse_record(tf_record): + features = { + 'image': tf.FixedLenFeature([], dtype=tf.string), + 'label': tf.FixedLenFeature([], dtype=tf.int64) + } + record = tf.parse_single_example(tf_record, features) + + image_raw = tf.decode_raw(record['image'], tf.uint8) + label = tf.one_hot(tf.cast(record['label'], tf.int32), depth=2) + + def _normalize(image_x): + """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" + image = tf.cast(image_x, tf.float32) * (1. / 255)-0.5 + return image + + image_raw = _normalize(image_raw) + '''image_raw = tf.reshape(image_raw, (32,32,3))'''#For vizualizing + return { 'image': image_raw}, label + + # For TF dataset blog post, see https://developers.googleblog.com/2017/09/introducing-tensorflow-datasets.html + dataset = tf.data.TFRecordDataset(filenames) + dataset = dataset.map(_parse_record) + + dataset = dataset.repeat(num_epochs) + dataset = dataset.batch(batch_size) + + iterator = dataset.make_initializable_iterator() + '''features, labels = iterator.get_next()''' + + return iterator + + return _input_fn + + +def read_file(x_filename, x_capacity=800): + record_iterator = tf.python_io.tf_record_iterator(path=x_filename) + + example = tf.train.Example() + for str_rec in record_iterator: + example.ParseFromString(str_rec) + height = int(example.features.feature['height'] + .int64_list + .value[0]) + + width = int(example.features.feature['width'] + .int64_list + .value[0]) + + + img_string = (example.features.feature['image'] + .bytes_list + .value[0]) + + label = (example.features.feature['label'].int64_list.value[0]) + + + return 0 + + +def tfrec_data_catvdog(filenames, num_epochs=1, batch_size=16): + + def _input_fn(): + def _parse_record(tf_record): + features = { + 'image': tf.FixedLenFeature([], dtype=tf.string), + } + record = tf.parse_single_example(tf_record, features) + + image_raw = tf.decode_raw(record['image'], tf.uint8) + + def _normalize(image): + """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" + image = tf.cast(image, tf.float32) * (1. / 255)-0.5 + return image + + image_n = _normalize(image_raw) + return { 'image': image_n } + + # For TF dataset blog post, see https://developers.googleblog.com/2017/09/introducing-tensorflow-datasets.html + dataset = tf.data.TFRecordDataset(filenames) + dataset = dataset.map(_parse_record) + + '''dataset = dataset.repeat(num_epochs)''' + '''dataset = dataset.batch(batch_size)''' + + iterator = dataset.make_one_shot_iterator() + features= iterator.get_next() + + return features + + return _input_fn + + +'''read_file("./tftest.tfrecords") + +tfrec_dev_input_fn = tfrec_data_input_fn(["tftest.tfrecords"]) +features, labels = tfrec_dev_input_fn() + +with tf.Session() as sess: + for step in range(2): + img, label = sess.run([features['image'], labels]) + img = np.reshape(img, (16,64,64,3)) + + for i in range(16): + plt.imshow(img[i]) + plt.show()''' + \ No newline at end of file diff --git a/image_pro/utils_data/__init__.py b/image_pro/utils_data/__init__.py new file mode 100644 index 0000000..ab28e3f --- /dev/null +++ b/image_pro/utils_data/__init__.py @@ -0,0 +1 @@ +#NOTE \ No newline at end of file diff --git a/image_pro/utils_data/colors/__init__.py b/image_pro/utils_data/colors/__init__.py new file mode 100644 index 0000000..ab28e3f --- /dev/null +++ b/image_pro/utils_data/colors/__init__.py @@ -0,0 +1 @@ +#NOTE \ No newline at end of file diff --git a/image_pro/utils_data/colors/colors.py b/image_pro/utils_data/colors/colors.py new file mode 100644 index 0000000..a209e53 --- /dev/null +++ b/image_pro/utils_data/colors/colors.py @@ -0,0 +1,117 @@ +import cv2 + +############# +#Color change +############# +#Changing color space: +def change_color_gray(frame): + change = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + return change + +def change_color_hsv(frame): + change = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) + return change + +def change_color_rgb(frame): + change = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) + return change + +#Object tracking: using a specified range of color +#Ex: lower_blue = np.array([110,50,50]); upper_blue = np.array([130,255,255]) +#lower_col & upper_col have to be numpy arrays + +def track_color(frame, lower_col, upper_col): + hsv = change_color_hsv(frame) + mask = cv2.inRange(hsv, lower_col, upper_col) + img = cv2.bitwise_and(frame,frame,mask=mask) + return img + +############# +#Thresholding +############# + +#Images processed in this part must be grayscale +#Ex: if x>value_min --> white else black +# value_aft is given if pixel > value_thr + +def thresh_bin(frame, value_thr, value_aft): + _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_BINARY) + return img + +def thresh_bin_inv(frame, value_thr, value_aft): + _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_BINARY_INV) + return img + +def thresh_truc(frame, value_thr, value_aft): + _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_TRUNC) + return img + +def thresh_tozero(frame, value_thr, value_aft): + _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_TOZERO) + return img + +def thresh_tozero_inv(frame, value_thr, value_aft): + _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_TOZERO_INV) + return img + +#Adaptive thresholding +#block_h: means block size height, block_w means block size width + +##With binary thresholding + +def thresh_adapgauss_bin(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ + cv2.THRESH_BINARY, block_h, block_w ) + + return img + +def thresh_adapgauss_bininv(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ + cv2.THRESH_BINARY_INV, block_h, block_w ) + + return img + +def thresh_adapgauss_tunc(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ + cv2.THRESH_TRUNC, block_h, block_w ) + + return img + +def thresh_adapgauss_toz(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ + cv2.THRESH_TOZERO, block_h, block_w ) + + return img + +def thresh_adapgauss_tozinv(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ + cv2.THRESH_TOZERO_INV, block_h, block_w ) + + return img + +def thresh_adapmean_bin(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_MEAN_C,\ + cv2.THRESH_BINARY, block_h,block_w) + return img + +def thresh_adapmean_trunc(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_MEAN_C,\ + cv2.THRESH_TRUNC, block_w, block_h) + return img + +def thresh_adapmean_toz(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_MEAN_C,\ + cv2.THRESH_TOZERO, block_h, block_w) + return img + +def thresh_adapmean_tozinv(frame, value_aft, block_h, block_w): + img = cv2.adaptiveThreshold(frame, value_aft, cv2.ADAPTIVE_THRESH_MEAN_C,\ + cv2.THRESH_TOZERO_INV, block_h, block_w) + return img + + +################# +#Otsu thresholding +################## + +#TODO: ret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) \ No newline at end of file diff --git a/image_pro/utils_data/contours/__init__.py b/image_pro/utils_data/contours/__init__.py new file mode 100644 index 0000000..ab28e3f --- /dev/null +++ b/image_pro/utils_data/contours/__init__.py @@ -0,0 +1 @@ +#NOTE \ No newline at end of file diff --git a/image_pro/utils_data/contours/contours.py b/image_pro/utils_data/contours/contours.py new file mode 100644 index 0000000..e503be6 --- /dev/null +++ b/image_pro/utils_data/contours/contours.py @@ -0,0 +1,126 @@ +import numpy as np +import cv2 +'''from ..colors.colors import thresh_bin''' +import matplotlib.pyplot as plt + +#################### +# Canny contours +#################### +def canny_cont(frame, threch1, threch2): + img_save = frame + edges = cv2.Canny(frame, threch1,threch2) + return edges + + +################### +# Thresholding +#################### +def thresh_bin(frame, value_thr, value_aft): + _ , img = cv2.threshold(frame, value_thr, value_aft, cv2.THRESH_BINARY) + return img + +def find_contours(frame, approx=False): + tresh = thresh_bin(frame, 70, 255) + if approx: + image, contours, hierarchy = cv2.findContours(tresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) + else: + image, contours, hierarchy = cv2.findContours(tresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) + + return image, contours, hierarchy + + #image: source image + # contours: contour retrieval mode + # hierarchy: contour approximation method + +def draw_all_contours(frame): + save_img = frame + tresh, contours, _ = find_contours(frame) + img = cv2.drawContours(tresh, contours, -1,(0,255)) + return img, save_img + +def draw_contour(frame, pos): + #pos is an int begining from 0 + save_img = frame + tresh, contours, _ = find_contours(frame) + image = cv2.drawContours(tresh, contours, pos, (0,255)) + return image, save_img + +def draw_contour_approx(frame): + save_img = frame + tresh, contours, _ = find_contours(frame, approx=True) + image = cv2.drawContours(tresh, contours, -1, (0,255)) + return image, save_img + +#################### +# Contours Features +#################### + +def get_moment(frame, index): + _, contours, _ = find_contours(frame, approx=True) + cnt = contours[index] + M = cv2.moments(cnt) + return M #M is a dict of all the moments + + +def get_contour_area(contour): + area = cv2.contourArea(contour) + return area + +def get_perimeter(contour): + perimeter = cv2.arcLength(contour) + return perimeter + +def get_approx_cont(contour): + epsilon = 0.1*cv2.arcLength(contour,True) + approx = cv2.approxPolyDP(contour,epsilon,True) + return approx + + +#################### +# Bounding Rectangle +#################### +def bounding_rec(frame): + _, contours,_ = find_contours(frame, approx=True) + image = frame + for c in contours: + x,y,w,h = cv2.boundingRect(c) + approx = get_approx_cont(c) + if len(approx) >= 4: + image = cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0)) + + return image + +def draw_rec(frame, index): + _,contours,_ = find_contours(frame, approx=True) + cnt = contours + rect = cv2.minAreaRect(cnt[0]) + box = cv2.boxPoints(rect) + box = np.int0(box) + image = cv2.drawContours(frame, [box],0,(0,0,255),2) + return image + + +######################## +# Contours Properties +######################## +def get_ratio(contour): + x,y,w,h = cv2.boundingRect(contour) + ratio = float(w)/h + return ratio + +def get_extent(contour): + area = get_contour_area(contour) + x,y,w,h = cv2.boundingRect(cnt) + rect_area = w*h + extent = float(area)/rect_area + return extent + +def mask_pix(frame_gray): + _,contours,_ = find_contours(frame_gray) + mask = np.zeros(frame_gray.shape,np.uint8) + cv2.drawContours(mask,contours,0,255) + pixelpoints = cv2.findNonZero(mask) + return pixelpoints + +#Aspect ratio: It is the ratio of width to height of bounding rect of the object + diff --git a/image_pro/utils_data/geotransf/__init__.py b/image_pro/utils_data/geotransf/__init__.py new file mode 100644 index 0000000..ab28e3f --- /dev/null +++ b/image_pro/utils_data/geotransf/__init__.py @@ -0,0 +1 @@ +#NOTE \ No newline at end of file diff --git a/image_pro/utils_data/geotransf/geotransf.py b/image_pro/utils_data/geotransf/geotransf.py new file mode 100644 index 0000000..9c9b2fb --- /dev/null +++ b/image_pro/utils_data/geotransf/geotransf.py @@ -0,0 +1,81 @@ +import cv2 +import numpy as np +import matplotlib.pyplot as plt + +#Let's consider M the translation matrix. Given a translation vector (tx,ty), +# the matrix is defined as follows: M = [1 0 tx] +# [0 1 ty] + +################## +# Translation +################## +def translate(frame, tx, ty): + dim = frame.shape + M = np.float32([[1,0,tx],[0,1,ty]]) + img = cv2.warpAffine(frame, M, dim) + return img + +################ +# Rotation +################ +#See reference: http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_geometric_transformations/py_geometric_transformations.html +#Angle is in degree +def rotate(frame, angle): + dim = (int(frame.shape[0]/2), int(frame.shape[1]/2)) + M = cv2.getRotationMatrix2D(dim, angle, 1) #Last argument is for scale + img = cv2.warpAffine(frame, M, dim) + return img + +####################### +# Affine transformation +####################### +# For this func , we need three points from the original image +# And the corresponding points in the output image +#list1: list of tuples of input image: 3 tuples// Same for list2 (output image) +def affine(frame, list1, list2): + '''assert type(list1) == list and len(list1)==3''' + col,row,_ = frame.shape + sub1, sub2 = [], [] + for i in list1: + sub1.append([i[0],i[1]]) + for j in list2: + sub2.append([j[0], j[1]]) + ps1 = np.float32(sub1) + ps2 = np.float32(sub2) + M = cv2.getAffineTransform(ps1,ps2) + img = cv2.warpAffine(frame, M, dsize=(col,row)) + return img + + + +####################### +# Perspective transformation +####################### + +def perspec(frame, list1, list2): + '''assert type(list1) == list and len(list1)==4''' + col,row,_ = frame.shape + sub1, sub2 = [], [] + for i in list1: + sub1.append([i[0],i[1]]) + for j in list2: + sub2.append([j[0], j[1]]) + ps1 = np.float32(sub1) + ps2 = np.float32(sub2) + M = cv2.getPerspectiveTransform(ps1,ps2) + img = cv2.warpPerspective(frame, M, dsize=(col,row)) + + return img + +'''a = cv2.imread("C:/Users/User12/Desktop/Work/train/train/cat.0.jpg", cv2.IMREAD_UNCHANGED) +b = perspec(a, [(50,50),(300,50),(50,300), (300,300)], [(0,0),(250,0),(0,250),(250,250)]) +plt.imshow(b) +plt.show()''' + + +####################### +# Morphological transformation +####################### +#TODO: + + diff --git a/image_pro/utils_data/histogram/__init__.py b/image_pro/utils_data/histogram/__init__.py new file mode 100644 index 0000000..d3f5a12 --- /dev/null +++ b/image_pro/utils_data/histogram/__init__.py @@ -0,0 +1 @@ + diff --git a/image_pro/utils_data/histogram/histogram.py b/image_pro/utils_data/histogram/histogram.py new file mode 100644 index 0000000..7ef0af8 --- /dev/null +++ b/image_pro/utils_data/histogram/histogram.py @@ -0,0 +1,16 @@ +from matplotlib import pyplot as plt +import cv2 +import numpy as np + +def plot_histogram(frame, color='b', channels=[0], mask=None, histosize=[256], ranges=[0,256]): + histr = cv2.calcHist([frame], channels, mask, histosize, ranges) + plt.plot(histr, color=color) + plt.show() + +def plot_bgr_histogram(frame, mask=None, histosize=[256], ranges=[0, 256]): + color = ('b','g','r') + for i, col in enumerate(color): + histr = cv2.calcHist([frame], [i], mask, [256], ranges) + plt.plot(histr, color=col) + plt.xlim([0,256]) + plt.show() diff --git a/image_pro/utils_data/norma/__init__.py b/image_pro/utils_data/norma/__init__.py new file mode 100644 index 0000000..ab28e3f --- /dev/null +++ b/image_pro/utils_data/norma/__init__.py @@ -0,0 +1 @@ +#NOTE \ No newline at end of file diff --git a/image_pro/utils_data/norma/norma.py b/image_pro/utils_data/norma/norma.py new file mode 100644 index 0000000..e6cd714 --- /dev/null +++ b/image_pro/utils_data/norma/norma.py @@ -0,0 +1,22 @@ +import tensorflow as tf +import cv2 + +def normalize(image, label): + """Convert `image` from [0, 255] -> [-0.5, 0.5] floats.""" + image = tf.cast(image, tf.float32) * (1. / 255) - 0.5 + return image, label + +#################### +# Image Gradient +#################### + +def laplacian(frame): + img = cv2.Laplacian(frame, cv2.CV_64F) + return img + +def sobelX(frame, kernel_size): + img = cv2.Sobel(frame, cv2.CV_64F, 1, 0, ksize=kernel_size) + return img + +def sobelY(frame, kernel_size): + img = cv2.Sobel(frame, cv2.CV_64F, 0,1,ksize=kernel_size) \ No newline at end of file diff --git a/image_pro/utils_data/zooming/__init__.py b/image_pro/utils_data/zooming/__init__.py new file mode 100644 index 0000000..ab28e3f --- /dev/null +++ b/image_pro/utils_data/zooming/__init__.py @@ -0,0 +1 @@ +#NOTE \ No newline at end of file diff --git a/image_pro/utils_data/zooming/zoom.py b/image_pro/utils_data/zooming/zoom.py new file mode 100644 index 0000000..ab28e3f --- /dev/null +++ b/image_pro/utils_data/zooming/zoom.py @@ -0,0 +1 @@ +#NOTE \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..eb13a03 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +tensorflow +opencv-python +numpy +matplotlib diff --git a/CNN-Xray.py b/scripts/CNN-Xray.py similarity index 100% rename from CNN-Xray.py rename to scripts/CNN-Xray.py diff --git a/scripts/NN2-Xray.py b/scripts/NN2-Xray.py new file mode 100644 index 0000000..c2164b1 --- /dev/null +++ b/scripts/NN2-Xray.py @@ -0,0 +1,128 @@ +import tensorflow as tf +import numpy as np +from tfRecord.decode_tfrecord import tfrec_data_input_fn, tfrec_data_catvdog +import matplotlib.pyplot as plt + +T_HEIGHT = 256 +T_WIDTH = 256 +T_Channnels = 1 +TRAIN_FILE = 'tftrain.tfrecords' +TEST_FILE = 'tftest.tfrecords' + +learning_rate = 0.1 +lambda_r = 0.01 +num_steps = 1500 +num_epoch = 3 +batch_size = 16 +display_step = 10 + +n_hidden_1 = 160 # 1st layer number of neurons +n_hidden_2 = 160 # 2nd layer number of neurons + +num_input = T_Channnels*T_HEIGHT*T_WIDTH # data input (img shape: 64*64) +num_classes = 2 # + +# tf Graph input +X = tf.placeholder("float", [None, num_input]) +Y = tf.placeholder(tf.int64, [None, num_classes]) + +# Store layers weight & bias +weights = { + 'h1': tf.Variable(tf.random_normal([num_input, n_hidden_1])), + 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), + 'out': tf.Variable(tf.random_normal([n_hidden_2, num_classes])) +} +tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, weights['h1']) +tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, weights['h2']) +tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, weights['out']) +biases = { + 'b1': tf.Variable(tf.random_normal([n_hidden_1])), + 'b2': tf.Variable(tf.random_normal([n_hidden_2])), + 'out': tf.Variable(tf.random_normal([num_classes])) +} + + +# Create model +def neural_net(x): + + layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) + z1 = tf.nn.tanh(layer_1) + + layer_2 = tf.add(tf.matmul(z1, weights['h2']), biases['b2']) + + z2 = tf.nn.relu(layer_2) + out_layer = tf.matmul(z2, weights['out']) + biases['out'] + return out_layer + +# Construct model +logits = neural_net(X) +prediction = tf.nn.softmax(logits) + +# Define loss and optimizer + +loss_op = tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=logits) +regul = tf.contrib.layers.l2_regularizer(scale=0.1) +reg_variables = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) +reg_term = tf.contrib.layers.apply_regularization(regul, reg_variables) #Regularization term +loss_op = tf.reduce_mean(loss_op + reg_term) +optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) +train_op = optimizer.minimize(loss_op) + +# Evaluate model +correct_pred = tf.equal(tf.argmax(prediction,1), tf.argmax(Y, 1)) +accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) +#### + + + + + +tfrec_dev_input_fn = tfrec_data_input_fn(["tftrain.tfrecords"], num_epochs=num_epoch, shuffle=True) +iterator = tfrec_dev_input_fn() + +# Initialize the variables (i.e. assign their default value) +init = tf.global_variables_initializer() + +# Start training +with tf.Session() as sess: + sess.run(init) + for ep in range(1, num_epoch+1): + # Run the initializer + sess.run(iterator.initializer) + features, labels = iterator.get_next() + + img, label = sess.run([features['image'], labels]) + for step in range(1,1+num_steps): + '''for i in range(16): + print(label[i]) + plt.imshow(img[i].reshape(256,256)) + plt.show()''' + # Run optimization op (backprop) + sess.run(train_op, feed_dict={X: img, Y: label}) + + if step % display_step == 0 or step == 1: + # Calculate batch loss and accuracy + loss, acc = sess.run([loss_op, accuracy], feed_dict={X: img, + Y: label}) + print("Step " + str(step) + ", Minibatch Loss= " + \ + "{:.4f}".format(loss) + ", Training Accuracy= " + \ + "{:.3f}".format(acc)) + print("Optimization finished") + + + tfrec_dev_input_fn = tfrec_data_input_fn(["tftest.tfrecords"], batch_size=1000) + iterator = tfrec_dev_input_fn() + sess.run(iterator.initializer) + features, labels = iterator.get_next() + img, label = sess.run([features['image'], labels]) + print("Testing Accuracy:", \ + sess.run(accuracy, feed_dict={X: img, + Y: label})) + + + tfrec_dcatvdog= tfrec_data_catvdog(["tfpred.tfrecords"]) + a = tfrec_dcatvdog() + for i in range(750): + img = sess.run([a['image']]) + print("prediction :", \ + sess.run(prediction, feed_dict={X:img})) diff --git a/scripts/NN3-Xray..py b/scripts/NN3-Xray..py new file mode 100644 index 0000000..5b80f0b --- /dev/null +++ b/scripts/NN3-Xray..py @@ -0,0 +1,122 @@ +import tensorflow as tf +import numpy as np +from tensorflow.examples.tutorials.mnist import input_data +mnist = input_data.read_data_sets('MNIST_data', one_hot=True) +import matplotlib.pyplot as plt + +T_HEIGHT = 28 +T_WIDTH = 28 +T_Channnels = 1 +TRAIN_FILE = 'tftrain.tfrecords' +TEST_FILE = 'tftest.tfrecords' + +learning_rate = 0.1 +lambda_r = 0.01 +num_steps = 1500 +num_epoch = 3 +batch_size = 16 +display_step = 10 + +n_hidden_1 = 160 # 1st layer number of neurons +n_hidden_2 = 160 # 2nd layer number of neurons + +num_input = T_Channnels*T_HEIGHT*T_WIDTH # data input (img shape: 64*64) +num_classes = 10 # + +# tf Graph input +X = tf.placeholder("float", [None, num_input]) +Y = tf.placeholder(tf.int64, [None, num_classes]) + +# Store layers weight & bias +weights = { + 'h1': tf.Variable(tf.random_normal([num_input, n_hidden_1])), + 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), + 'out': tf.Variable(tf.random_normal([n_hidden_2, num_classes])) +} +tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, weights['h1']) +tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, weights['h2']) +tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, weights['out']) +biases = { + 'b1': tf.Variable(tf.random_normal([n_hidden_1])), + 'b2': tf.Variable(tf.random_normal([n_hidden_2])), + 'out': tf.Variable(tf.random_normal([num_classes])) +} + + +# Create model +def neural_net(x): + + layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) + z1 = tf.nn.tanh(layer_1) + + layer_2 = tf.add(tf.matmul(z1, weights['h2']), biases['b2']) + + z2 = tf.nn.relu(layer_2) + out_layer = tf.matmul(z2, weights['out']) + biases['out'] + return out_layer + +# Construct model +logits = neural_net(X) +prediction = tf.nn.softmax(logits) + +# Define loss and optimizer + +loss_op = tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=logits) +regul = tf.contrib.layers.l2_regularizer(scale=0.1) +reg_variables = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) +reg_term = tf.contrib.layers.apply_regularization(regul, reg_variables) #Regularization term +loss_op = tf.reduce_mean(loss_op + reg_term) +optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) +train_op = optimizer.minimize(loss_op) + +# Evaluate model +correct_pred = tf.equal(tf.argmax(prediction,1), tf.argmax(Y, 1)) +accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) +#### + + +# Initialize the variables (i.e. assign their default value) +init = tf.global_variables_initializer() + +# Start training +with tf.Session() as sess: + sess.run(init) + # Run the initializer + '''img, label = sess.run([features['image'], labels])''' + batch = mnist.train.next_batch(batch_size) + print(batch[0][1].shape) + for step in range(1,1+num_steps): + + '''for i in range(16): + print(label[i]) + plt.imshow(img[i].reshape(256,256)) + plt.show()''' + # Run optimization op (backprop) + + '''sess.run(train_op, feed_dict={X: batch[0], Y: batch[1]}) + + if step % display_step == 0 or step == 1: + # Calculate batch loss and accuracy + loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch[0], + Y: batch[1]}) + print("Step " + str(step) + ", Minibatch Loss= " + \ + "{:.4f}".format(loss) + ", Training Accuracy= " + \ + "{:.3f}".format(acc)) + print("Optimization finished") + + + + print("Testing Accuracy:", \ + sess.run(accuracy, feed_dict={X: mnist.test.images, + Y: mnist.test.labels})) + + + tfrec_dcatvdog= tfrec_data_catvdog(["tfpred.tfrecords"]) + a = tfrec_dcatvdog() + for i in range(750): + img = sess.run([a['image']]) + print("prediction :", \ + sess.run(prediction, feed_dict={X:img})) + + print(sess.run([weights['h1'], biases['b1'], weights['h2'], biases['b2']])) + print(sess.run([weights['out'],biases['out']]))''' \ No newline at end of file diff --git a/Tfrecord2.py b/scripts/Tfrecord2.py similarity index 100% rename from Tfrecord2.py rename to scripts/Tfrecord2.py diff --git a/decode_tfrecord.py b/scripts/decode_tfrecord.py similarity index 100% rename from decode_tfrecord.py rename to scripts/decode_tfrecord.py diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..58145ae --- /dev/null +++ b/setup.py @@ -0,0 +1,27 @@ +from setuptools import setup, find_packages + + +setup( + name='image_pro', + version='0.0.1', + description='Image processing and save into Tfrecords', + author='', + author_email='hilalyamine@gmail.com/alaa.el.bouchti@gmail.com', + license='Open', + packages=find_packages(include=['image_pro','image_pro.*','tfRecord', 'utils_data']), + packages_dir={'tfrecord':'tfRecord', + 'tfrecord.tfrecord':'tfRecord/tfrecord.py', + 'tfrecord.decode':'tfRecord/decode.py', + 'utils': 'utils_data', + 'utils.colors': 'utils_data/colors', + 'utils.colors.colors': 'utils_data/colors/colors.py', + 'utils.contours':'utils_data/contours', + 'utils.contours.contours':'utils_data/contours/contours.py', + 'utils.geotransf':'utils_data/geotransf', + 'utils.geotransf.geotransf':'utils_data/geotransf/geotransf.py', + 'utils.norma':'utils_data/norma', + 'utils.norma.norma':'utils_data/contours/norma.py', + 'utils.visualize':'utils_data/contours', + 'utils.visualize.visu':'utils_data/visualize/visu.py' + }, +) \ No newline at end of file