SalmanAboAraj commited on
Commit
ffb4efb
·
verified ·
1 Parent(s): fd08f6b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sklearn.datasets import fetch_openml
2
+ from sklearn.preprocessing import StandardScaler
3
+ from sklearn.model_selection import train_test_split
4
+ import gradio as gr
5
+ import requests
6
+ from sklearn import svm, metrics
7
+ from sklearn.linear_model import LogisticRegression
8
+ from sklearn.metrics import confusion_matrix
9
+ import numpy as np
10
+ import cv2
11
+
12
+ X, y = fetch_openml("mnist_784", version=1, return_X_y=True, as_frame=False)
13
+ scaler = StandardScaler()
14
+ X = scaler.fit_transform(X)
15
+
16
+ X_train_img, X_test_img, y_train_img, y_test_img = train_test_split(
17
+ X, y, test_size=0.2, shuffle=True)
18
+
19
+
20
+ # Create a classifier
21
+ project_classifier = svm.SVC(kernel='linear',probability=True)
22
+ project_classifier.fit(X_train_img, y_train_img)
23
+
24
+ predicted = project_classifier.predict(X_test_img)
25
+
26
+ def inference(img):
27
+ labels =["0","1", "2","3", "4","5", "6","7", "8","9"]
28
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
29
+ H,W = 28,28
30
+ img = cv2.resize(img, (H, W))
31
+ ret,img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
32
+ img = np.array(img)
33
+ img = img.astype("float32")
34
+ img = img.flatten()
35
+ img = img.reshape(1,H*W)
36
+ img = scaler.transform(img)
37
+ pred = project_classifier.predict_proba(img).flatten()
38
+ dictionary = dict(zip(labels, map(float, pred)))
39
+ return dictionary
40
+
41
+
42
+ nbr_top_classes = 3
43
+ gr.Interface(fn=inference,
44
+ inputs=gr.inputs.Image(source="upload"), # source= "upload" "webcam" "canvas"
45
+ outputs=gr.outputs.Label(num_top_classes=nbr_top_classes),
46
+ theme="darkdefault").launch(share=True) #, debug=True Use in Colab