/
main.py
86 lines (71 loc) · 3.04 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import numpy as np
import cv2
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from PIL import Image
import PIL.ImageOps
import os, ssl, time
#Fetching the data
X = np.load('image.npz')['arr_0']
y = pd.read_csv("labels.csv")["labels"]
print(pd.Series(y).value_counts())
classes = ['A', 'B', 'C', 'D', 'E','F', 'G', 'H', 'I', 'J', "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
nclasses = len(classes)
#Splitting the data and scaling it
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 9, train_size = 3500, test_size = 500)
#scaling the features
X_train_scaled = X_train/255.0
X_test_scaled = X_test/255.0
#Fitting the training data into the model
clf = LogisticRegression(solver = 'saga', multi_class = 'multinomial').fit(X_train_scaled, y_train)
#calculating the accuracy of the model
y_pred = clf.predict(X_test_scaled)
accuracy = accuracy_score(y_test, y_pred)
print(accuracy)
#starting the camera
cap = cv2.VideoCapture(0)
print("Video Capturing : Onn")
while(True):
#capture frame-by-frame
try:
ret, frame = cap.read()
print("Inside the loop")
#our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#drawing a box in the center of the video
height, width = gray.shape
upper_left = (int(width / 2 - 56), int(height / 2 - 56))
bottom_right = (int(width / 2 + 56), int(height / 2 + 56))
cv2.rectangle(gray, upper_left, bottom_right, (0, 255, 0), 2)
#to only consider the area inside the box for detecting the digit
#roi = Region Of Interest
roi = gray[upper_left[1]:bottom_right[1], upper_left[0]:bottom_right[0]]
#Converting cv2 image to pil format
im_pil = Image.fromarray(roi)
#convert to grayscale image - 'L' format means each pixel is
#represented by a single value from 0 to 255
image_bw = im_pil.convert('L')
image_bw_resized = image_bw.resize((28,28), Image.ANTIALIAS)
image_bw_resized_inverted = PIL.ImageOps.invert(image_bw_resized)
pixel_filter = 20
min_pixel = np.percentile(image_bw_resized_inverted, pixel_filter)
image_bw_resized_inverted_scaled = np.clip(image_bw_resized_inverted-min_pixel, 0, 255)
max_pixel = np.max(image_bw_resized_inverted)
image_bw_resized_inverted_scaled = np.asarray(image_bw_resized_inverted_scaled)/max_pixel
test_sample = np.array(image_bw_resized_inverted_scaled).reshape(1,784)
test_pred = clf.predict(test_sample)
print("Predicted class is : ", test_pred)
#display the resulting frame
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except Exception as e:
pass
#when everything done, release the capture
cap.release()
cv2.destroyAllWindows()