Spaces:
Runtime error
Runtime error
Commit
·
a4f2b8a
1
Parent(s):
a143940
migrating project
Browse files- app.py +69 -0
- images/Conso.jpg +0 -0
- images/Incom.jpg +0 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio
|
| 2 |
+
import cv2
|
| 3 |
+
from sklearn.naive_bayes import BernoulliNB
|
| 4 |
+
import pickle
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# multiclass_model = pickle.load(open('models/MulticlassModel_200x200', 'rb'))
|
| 8 |
+
ensemble_model = pickle.load(open('models/EnsembleModels_200x200', 'rb'))
|
| 9 |
+
|
| 10 |
+
examples = ["images/Conso.jpg", "images/Incom.jpg"]
|
| 11 |
+
|
| 12 |
+
def preprocess(img):
|
| 13 |
+
img = cv2.resize(img, (200,200))
|
| 14 |
+
img = cv2.adaptiveThreshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
|
| 15 |
+
img = np.reshape(img, (1,200*200))/255
|
| 16 |
+
return img
|
| 17 |
+
|
| 18 |
+
def predict(img):
|
| 19 |
+
img = preprocess(img)
|
| 20 |
+
categories = {
|
| 21 |
+
"Inco": 2,
|
| 22 |
+
"Teac": 1,
|
| 23 |
+
"Cons": 0,
|
| 24 |
+
"Publ": 4,
|
| 25 |
+
"Econ": 3,
|
| 26 |
+
"Reaf": 5}
|
| 27 |
+
|
| 28 |
+
proba = np.zeros((6))
|
| 29 |
+
for key in categories.keys():
|
| 30 |
+
proba[categories[key]] = ensemble_model[key].predict_proba(img)[:,0]
|
| 31 |
+
|
| 32 |
+
return proba
|
| 33 |
+
|
| 34 |
+
def generate_results(proba):
|
| 35 |
+
categories = [
|
| 36 |
+
"DLC",
|
| 37 |
+
"TLF",
|
| 38 |
+
"IDR",
|
| 39 |
+
"EHD",
|
| 40 |
+
"PLSF",
|
| 41 |
+
"REAF",
|
| 42 |
+
"UNKNOWN"]
|
| 43 |
+
|
| 44 |
+
scores = [0,0,0,0,0,0,0]
|
| 45 |
+
|
| 46 |
+
choice = np.where(proba == np.amin(proba))[0]
|
| 47 |
+
|
| 48 |
+
if len(choice)>1:
|
| 49 |
+
choice = 6
|
| 50 |
+
scores[int(choice)] = 1
|
| 51 |
+
|
| 52 |
+
results = dict(zip(categories, scores))
|
| 53 |
+
return results
|
| 54 |
+
|
| 55 |
+
def inference(img):
|
| 56 |
+
proba = predict(img)
|
| 57 |
+
results = generate_results(proba)
|
| 58 |
+
return results
|
| 59 |
+
|
| 60 |
+
demo = gradio.Interface(
|
| 61 |
+
fn=inference,
|
| 62 |
+
inputs=gradio.Image(),
|
| 63 |
+
outputs=gradio.Label(),
|
| 64 |
+
title='Document Classification',
|
| 65 |
+
description='Loan Document Classification Using A Naive Bayes Classifier Ensemble',
|
| 66 |
+
article='This demo was built as part of demo for a student project.\n\n\n#BuiltAtMetis',
|
| 67 |
+
examples=examples)
|
| 68 |
+
|
| 69 |
+
demo.launch()
|
images/Conso.jpg
ADDED
|
images/Incom.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
opencv-python
|
| 2 |
+
sklearn
|