SalmanAboAraj commited on
Commit
3a4d95a
·
verified ·
1 Parent(s): fcc8b1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -20
app.py CHANGED
@@ -2,13 +2,11 @@ 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)
@@ -16,31 +14,28 @@ X = scaler.fit_transform(X)
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
 
 
 
 
2
  from sklearn.preprocessing import StandardScaler
3
  from sklearn.model_selection import train_test_split
4
  import gradio as gr
 
 
 
 
5
  import numpy as np
6
  import cv2
7
+ from sklearn import svm
8
 
9
+ # Load and preprocess data
10
  X, y = fetch_openml("mnist_784", version=1, return_X_y=True, as_frame=False)
11
  scaler = StandardScaler()
12
  X = scaler.fit_transform(X)
 
14
  X_train_img, X_test_img, y_train_img, y_test_img = train_test_split(
15
  X, y, test_size=0.2, shuffle=True)
16
 
 
17
  # Create a classifier
18
+ project_classifier = svm.SVC(kernel='linear', probability=True)
19
  project_classifier.fit(X_train_img, y_train_img)
20
 
 
 
21
  def inference(img):
22
+ labels = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
23
  img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
24
+ H, W = 28, 28
25
  img = cv2.resize(img, (H, W))
26
+ ret, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
27
+ img = img.astype("float32").flatten().reshape(1, -1)
 
 
 
28
  img = scaler.transform(img)
29
  pred = project_classifier.predict_proba(img).flatten()
30
  dictionary = dict(zip(labels, map(float, pred)))
31
  return dictionary
32
 
33
+ # Set up Gradio interface
34
  nbr_top_classes = 3
35
+ iface = gr.Interface(fn=inference,
36
+ inputs=gr.Image(source="upload"), # source= "upload" "webcam" "canvas"
37
+ outputs=gr.Label(num_top_classes=nbr_top_classes),
38
+ theme="darkdefault")
39
+
40
+ # Launch the interface
41
+ iface.launch(share=True)