Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkurKumarShukla authored Aug 29, 2022
1 parent 52f673d commit b87d5f6
Show file tree
Hide file tree
Showing 52 changed files with 680 additions and 1 deletion.
Binary file added 200310484280_AdmitCard.pdf
Binary file not shown.
Binary file added ALLOTMENT LETER.pdf
Binary file not shown.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
# similarity-detection-model-
Binary file added __pycache__/main.cpython-310.pyc
Binary file not shown.
Binary file added __pycache__/ocr.cpython-310.pyc
Binary file not shown.
Binary file added __pycache__/svefy.cpython-310.pyc
Binary file not shown.
Binary file added __pycache__/test.cpython-310.pyc
Binary file not shown.
2 changes: 2 additions & 0 deletions addharec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import argumentor
p = Augmentor.Pipeline("/path/to/images")
Binary file added adhar/20191018_074403.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added adhar/73a8158ee961472799e57e72b1868f54.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added adhar/Aadhar-Card.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added adhar/Aadhar_Card.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added adhar/Screenshot_20220218-123153-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added adhar/download (1).jfif
Binary file not shown.
Binary file added adhar/download.jfif
Binary file not shown.
Binary file added adhar/f99b3efadece9bde10e251760cc71b1c.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added adhar/ffa5e80e5c547686632c7bc9746fee87.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added adhar/hqdefault.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added adhar/id-1505369822.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added adhatest3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added allotletter.pdf
Binary file not shown.
Binary file added bhalu.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions check.py
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'))
3 changes: 3 additions & 0 deletions gpu.py
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)
Binary file added jee allot.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions main.py
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 added mlcnn/__pycache__/mlverify2.cpython-310.pyc
Binary file not shown.
9 changes: 9 additions & 0 deletions mlcnn/check.py
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))
Binary file added mlcnn/data/adhar.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
165 changes: 165 additions & 0 deletions mlcnn/mlverify2.py
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 added mlops/__pycache__/ml_verify.cpython-310.pyc
Binary file not shown.
2 changes: 2 additions & 0 deletions mlops/check.py
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'))
110 changes: 110 additions & 0 deletions mlops/ml_verify.py
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 added mlops/saved_model.pb
Binary file not shown.
Binary file added mlops/test.jpeg
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 added mlops/variables/variables.data-00000-of-00001
Binary file not shown.
Binary file added mlops/variables/variables.index
Binary file not shown.
Loading

0 comments on commit b87d5f6

Please sign in to comment.