souvikmaji22 commited on
Commit
8dafa03
1 Parent(s): d020d74

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +116 -0
  2. demo.pkl +3 -0
  3. requirements.txt +10 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ import cv2
4
+ import numpy as np
5
+ from skimage.io import imread
6
+ from skimage.transform import resize
7
+ from skimage.feature import hog
8
+ import torch
9
+ import torch.nn as nn
10
+ import torchvision.models as models
11
+ import torchvision.transforms as transforms
12
+ from PIL import Image
13
+ import pickle
14
+
15
+ # Load the pretrained resnet-50 model
16
+ resnet = models.resnet50(pretrained=True)
17
+ resnet = nn.Sequential(*list(resnet.children())[:-1])
18
+ resnet.eval()
19
+
20
+ # Load the demo model
21
+ with open('demo.pkl', 'rb') as f:
22
+ demo = pickle.load(f)
23
+
24
+ # defining some of the important functions that are needed
25
+
26
+ # defining feature extraction for resnet-50
27
+ def extract_features(image, model):
28
+ image_np = np.array(image)
29
+ preprocess = transforms.Compose([
30
+ transforms.ToPILImage(),
31
+ transforms.Resize(256),
32
+ transforms.CenterCrop(224),
33
+ transforms.ToTensor(),
34
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
35
+ ])
36
+ image = preprocess(image_np)
37
+ image = image.unsqueeze(0)
38
+ with torch.no_grad():
39
+ features = model(image)
40
+ features = features.squeeze(0)
41
+ return features
42
+
43
+
44
+ # hog calculation
45
+ def compute_hog(img):
46
+ resized_img = resize(img, (128*4, 64*4))
47
+ fd, hog_image = hog(resized_img, orientations=9, pixels_per_cell=(8, 8),
48
+ cells_per_block=(2, 2), visualize=True,channel_axis=-1)
49
+ return fd
50
+
51
+ def get_pixel(img, center, x, y):
52
+ new_value = 0
53
+ try:
54
+ if img[x][y] >= center:
55
+ new_value = 1
56
+ except:
57
+ pass
58
+ return new_value
59
+
60
+ # calculate lbp
61
+
62
+ def lbp_calculated_pixel(img, x, y):
63
+ center = img[x][y]
64
+ val_ar = []
65
+ val_ar.append(get_pixel(img, center, x-1, y+1))
66
+ val_ar.append(get_pixel(img, center, x, y+1))
67
+ val_ar.append(get_pixel(img, center, x+1, y+1))
68
+ val_ar.append(get_pixel(img, center, x+1, y))
69
+ val_ar.append(get_pixel(img, center, x+1, y-1))
70
+ val_ar.append(get_pixel(img, center, x, y-1))
71
+ val_ar.append(get_pixel(img, center, x-1, y-1))
72
+ val_ar.append(get_pixel(img, center, x-1, y))
73
+
74
+ power_val = [1, 2, 4, 8, 16, 32, 64, 128]
75
+ val = 0
76
+ for i in range(len(val_ar)):
77
+ val += val_ar[i] * power_val[i]
78
+ return val
79
+
80
+ def calcLBP(img):
81
+ height, width, channel = img.shape
82
+ img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
83
+ img_lbp = np.zeros((height, width,3), np.uint8)
84
+ for i in range(0, height):
85
+ for j in range(0, width):
86
+ img_lbp[i, j] = lbp_calculated_pixel(img_gray, i, j)
87
+ hist_lbp = cv2.calcHist([img_lbp], [0], None, [256], [0, 256])
88
+ return hist_lbp.flatten()
89
+
90
+ # Function to infer the class of an image
91
+
92
+ def infer(image_path):
93
+ image = imread(image_path) # reading the image
94
+ hog_feature = compute_hog(image) # hog features
95
+ lbp_feature = calcLBP(image) # lbp features
96
+ cnn_feature = extract_features(image, resnet).numpy() #cnn features
97
+ hog_feature = hog_feature.reshape(-1) # reshaping
98
+ lbp_feature = lbp_feature.reshape(-1) #reshaping
99
+ cnn_feature = cnn_feature.flatten() # cnn features
100
+ combined_feature = np.concatenate((hog_feature, lbp_feature, cnn_feature)) # combining all the features
101
+ prediction = demo.predict([combined_feature])
102
+ return prediction[0]
103
+ # Streamlit code
104
+
105
+ st.title("Face Identification")
106
+ uploaded_file = st.file_uploader("Choose an image...", type=["png"])
107
+
108
+ if uploaded_file is not None:
109
+ with open("temp_image.png", "wb") as f:
110
+ f.write(uploaded_file.getvalue())
111
+ image = Image.open("temp_image.png")
112
+ st.image(image, caption='Uploaded Image', use_column_width=True)
113
+ st.write("")
114
+ st.write("Classifying...")
115
+ prediction = infer("temp_image.png")
116
+ st.write("Prediction:", prediction)
demo.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:332ac3f12fcbf253869c688b99862faec19190e447465b080eaf5a5e049ecd57
3
+ size 4067527
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ opencv-python
2
+ numpy
3
+ scikit-image
4
+ torch
5
+ torchvision
6
+ streamlit
7
+ Pillow
8
+ pickle-mixin
9
+ scikit-learn
10
+