-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52f673d
commit b87d5f6
Showing
52 changed files
with
680 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
# similarity-detection-model- | ||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import argumentor | ||
p = Augmentor.Pipeline("/path/to/images") |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# import logging | ||
# import sys | ||
# import numpy as np | ||
# import pandas as pd | ||
# import mlops.ml_verify as mas | ||
# for name in logging.Logger.manager.loggerDict.keys(): | ||
# logging.getLogger(name).setLevel(logging.CRITICAL) | ||
|
||
# import os | ||
# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' | ||
# from tkinter.tix import IMAGE | ||
from ocr import ankocr | ||
|
||
|
||
# image= 'subject2.jpeg' | ||
# if(mas.check.check_similarity(image,'adhatest3.jpg')>0.70): | ||
# # print("similar") | ||
# t=ankocr.get_text(image) | ||
# # print(t) | ||
# if('GOVERNMENT OF INDIA' in t): | ||
# print("all test passed") | ||
# else: | ||
# print('text not found') | ||
# else: | ||
# t=ankocr.get_text(image) | ||
# # print(t) | ||
# if('GOVERNMENT OF INDIA' in t): | ||
# print('similarity test fail but text found') | ||
# else: | ||
# print('all test fail') | ||
|
||
|
||
from test import tester | ||
|
||
|
||
|
||
print(tester.check_jee_allot('ALLOTMENT LETTER.pdf')) | ||
# print(tester.check_jee_allot('200310484280_AdmitCard.pdf')) | ||
# ankocr.delete('save.png') | ||
# print(tester.check('testadha2.jpg')) | ||
# print(tester.check_pan('riya_pan.jpeg')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import tensorflow as tf | ||
tf.test.is_built_with_cuda() | ||
tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from fastapi import FastAPI, File, UploadFile | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.post("/files/") | ||
async def create_file(file: bytes | None = File(default=None)): | ||
if not file: | ||
return {"message": "No file sent"} | ||
else: | ||
return {"file_size": len(file)} | ||
|
||
|
||
@app.post("/uploadfile/") | ||
async def create_upload_file(file: UploadFile | None = None): | ||
if not file: | ||
return {"message": "No upload file sent"} | ||
else: | ||
return {"filename": file.filename} | ||
|
||
|
||
|
||
@app.get("/") | ||
def model(file){ | ||
|
||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from mlverify2 import cnn_verify | ||
import os | ||
img_dir = '../data' | ||
img_path_list = ['adhar.jpg'] | ||
index = 0 | ||
img_path = os.path.join(img_dir, img_path_list[index]) | ||
model_name = 'vgg16' | ||
|
||
print(cnn_verify.make_prediction(model_name, img_path)) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import os | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
from keras.applications.vgg16 import VGG16 | ||
from keras.applications.resnet import ResNet50 | ||
from keras.applications.vgg16 import VGG16 | ||
import tensorflow as tf | ||
from keras.models import Model | ||
from keras.models import model_from_json | ||
from keras.utils import load_model | ||
from keras.preprocessing import image | ||
from keras.preprocessing.image import ImageDataGenerator | ||
from keras.applications.imagenet_utils import preprocess_input, decode_predictions | ||
|
||
import cv2 | ||
from IPython.display import HTML, display | ||
from glob import glob | ||
from PIL import Image | ||
import time | ||
|
||
class cnn_verify: | ||
def create_folder(folder_name): | ||
# """ Create folder if there is not | ||
# Args: | ||
# folder_name: String, folder name | ||
# Returns: | ||
# None | ||
# """ | ||
if not os.path.isdir(f"../models/{folder_name}"): | ||
os.makedirs(f"../models/{folder_name}") | ||
print(f"Folder '../models/{folder_name}' created") | ||
|
||
def load_model(model_name, include_top=True): | ||
# """ Load pre-trained Keras model | ||
# Args: | ||
# model_name: String, name of model to load | ||
# include_top: String, the model is buildt with 'feature learning block' + 'classification block' | ||
# Returns: | ||
# model: Keras model instance | ||
# """ | ||
available_models = ['vgg16', 'resnet50'] | ||
selected_model = 'vgg16' | ||
|
||
if selected_model in available_models: | ||
# Load a Keras instance | ||
try: | ||
if model_name == 'vgg16': | ||
model = VGG16(weights='imagenet', include_top=include_top) | ||
elif model_name == 'resnet50': | ||
model = ResNet50(weights='imagenet', include_top=include_top) | ||
print(f">> '{model.name}' model successfully loaded!") | ||
except: | ||
print(f">> Error while loading model '{selected_model}'") | ||
|
||
# Wrong selected model | ||
else: | ||
print(f">> Error: there is no '{selected_model}' in {available_models}") | ||
|
||
return model | ||
|
||
def get_img_size_model(model = load_model('vgg16', include_top=True)): | ||
# """Returns image size for image processing to be used in the model | ||
# Args: | ||
# model: Keras model instance | ||
# Returns: | ||
# img_size_model: Tuple of integers, image size | ||
# """ | ||
model_name = model | ||
if model_name == "vgg16": | ||
img_size_model = (224, 224) | ||
elif model_name == "resnet50": | ||
img_size_model = (224, 224) | ||
else: | ||
img_size_model = (224, 224) | ||
print("Warning: model name unknown. Default image size: {}".format(img_size_model)) | ||
|
||
return img_size_model | ||
|
||
|
||
def get_layername_feature_extraction(model = load_model('vgg16', include_top=True)): | ||
# """ Return the name of last layer for feature extraction | ||
# Args: | ||
# model: Keras model instance | ||
# Returns: | ||
# layername_feature_extraction: String, name of the layer for feature extraction | ||
# """ | ||
model_name = model.name | ||
if model_name == "vgg16": | ||
layername_feature_extraction = 'fc2' | ||
elif model_name == "resnet50": | ||
layername_feature_extraction = 'predictions' | ||
else: | ||
layername_feature_extraction = '' | ||
print("Warning: model name unknown. Default layername: '{}'".format(layername_feature_extraction)) | ||
|
||
return layername_feature_extraction | ||
|
||
|
||
def get_layers_list(model): | ||
# """Get a list of layers from a model | ||
# Args: | ||
# model: Keras model instance | ||
# Returns: | ||
# layers_list: List of string of layername | ||
# """ | ||
layers_list = [] | ||
for i in range(len(model.layers)): | ||
layer = model.layers[i] | ||
layers_list.append(layer.name) | ||
|
||
return layers_list | ||
|
||
def image_processing(img_array): | ||
# """ Preprocess image to be used in a keras model instance | ||
# Args: | ||
# img_array: Numpy array of an image which will be predicte | ||
# Returns: | ||
# processed_img = Numpy array which represents the processed image | ||
# """ | ||
# Expand the shape | ||
img = np.expand_dims(img_array, axis=0) | ||
|
||
# Convert image from RGB to BGR (each color channel is zero-centered with respect to the ImageNet dataset, without scaling) | ||
processed_img = preprocess_input(img) | ||
|
||
return processed_img | ||
|
||
|
||
|
||
def make_prediction(model, img_path, display_img=False): | ||
"""Make a prediction of an image by passing in the model | ||
Args: | ||
model: Keras model instance used to do the classification. | ||
img_path: String to the image path which will be predicted | ||
display_img: Boolean to decid to show image. Defaults to True | ||
Returns: | ||
predicted_class: String which represents the class of the predicted image from ImageNet | ||
""" | ||
img_type = os.path.basename(img_path).split(".")[0] | ||
|
||
# Image processing | ||
img_size_model = cnn_verify.get_img_size_model(model) | ||
img = image.load_img(img_path, target_size=img_size_model) | ||
img_arr = np.array(img) | ||
img_ = cnn_verify.image_processing(img_arr) | ||
|
||
preds = model.predict(img_) | ||
top_preds = decode_predictions(preds) | ||
predicted_class = top_preds[0][0][1] | ||
predicted_class_percentage = top_preds[0][0][2]*100 | ||
|
||
# print('Top 5 predictions:') | ||
for i in range(len(top_preds[0])): | ||
print(" {} >>>>> {}".format(i+1, top_preds[0][i])) | ||
|
||
if display_img: | ||
print() | ||
plt.axis('off') | ||
plt.title("({}) Original image: {}\nPrediction: {} ({:.2f}%)".format(model.name, img_type, predicted_class, predicted_class_percentage)) | ||
plt.imshow(img_arr) | ||
|
||
return predicted_class |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from ml_verify import check | ||
print(check.check_similarity('random2.jpeg', 'testadha2.jpg')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import tensorflow as tf | ||
import tensorflow_hub as hub | ||
import numpy as np | ||
import os | ||
import pandas as pd | ||
import matplotlib.pyplot as plt | ||
import base64 | ||
from PIL import Image | ||
import io | ||
import math | ||
from math import sqrt | ||
|
||
|
||
|
||
|
||
global embed | ||
embed = hub.KerasLayer(os.getcwd()) | ||
|
||
|
||
class TensorVector(object): | ||
|
||
def __init__(self, FileName=None): | ||
self.FileName = FileName | ||
|
||
def process(self): | ||
|
||
img = tf.io.read_file(self.FileName) | ||
img = tf.io.decode_jpeg(img, channels=3) | ||
img = tf.image.resize_with_pad(img, 224, 224) | ||
img = tf.image.convert_image_dtype(img,tf.float32)[tf.newaxis, ...] | ||
features = embed(img) | ||
feature_set = np.squeeze(features) | ||
return list(feature_set) | ||
|
||
class check: | ||
def convertBase64(FileName): | ||
""" | ||
Return the Numpy array for a image | ||
""" | ||
with open(FileName, "rb") as f: | ||
data = f.read() | ||
|
||
res = base64.b64encode(data) | ||
|
||
base64data = res.decode("UTF-8") | ||
|
||
imgdata = base64.b64decode(base64data) | ||
|
||
image = Image.open(io.BytesIO(imgdata)) | ||
|
||
return np.array(image) | ||
|
||
def cosineSim(a1,a2): | ||
sum = 0 | ||
suma1 = 0 | ||
sumb1 = 0 | ||
for i,j in zip(a1, a2): | ||
suma1 += i * i | ||
sumb1 += j*j | ||
sum += i*j | ||
cosine_sim = sum / ((sqrt(suma1))*(sqrt(sumb1))) | ||
return cosine_sim | ||
|
||
|
||
def jaccard_similarity(list1, list2): | ||
intersection = len(list(set(list1).intersection(list2))) | ||
union = (len(list1) + len(list2)) - intersection | ||
return float(intersection) / union | ||
|
||
def average(x): | ||
assert len(x) > 0 | ||
return float(sum(x)) / len(x) | ||
|
||
def pearson_def(x, y): | ||
assert len(x) == len(y) | ||
n = len(x) | ||
assert n > 0 | ||
avg_x = check.average(x) | ||
avg_y = check.average(y) | ||
diffprod = 0 | ||
xdiff2 = 0 | ||
ydiff2 = 0 | ||
for idx in range(n): | ||
xdiff = x[idx] - avg_x | ||
ydiff = y[idx] - avg_y | ||
diffprod += xdiff * ydiff | ||
xdiff2 += xdiff * xdiff | ||
ydiff2 += ydiff * ydiff | ||
|
||
return diffprod / math.sqrt(xdiff2 * ydiff2) | ||
|
||
|
||
|
||
|
||
def check_similarity(img1, img2): | ||
# img1 = convertBase64(img1) | ||
# img2 = convertBase64(img2) | ||
helper1=TensorVector(img1) | ||
helper2=TensorVector(img2) | ||
vector = helper1.process() | ||
vector2 = helper2.process() | ||
|
||
similarity = (check.jaccard_similarity(vector, vector2) + check.cosineSim(vector, vector2) + check.pearson_def(vector, vector2)) / 3 | ||
# print(check.jaccard_similarity(vector, vector2)) | ||
# print(check.cosineSim(vector, vector2)) | ||
# print(check.pearson_def(vector, vector2)) | ||
# img1 = TensorVector(img1).process() | ||
# img2 = TensorVector(img2).process() | ||
|
||
return similarity |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.