abdullahsajid commited on
Commit
1967515
·
verified ·
1 Parent(s): 5814783

Upload 4 files

Browse files
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, jsonify, request
2
+ from flask_cors import CORS
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ import base64
6
+ import io
7
+ import torch
8
+ import torch.nn as nn
9
+ import torch.nn.functional as F
10
+ import torchvision.models as models
11
+ from PIL import Image
12
+ from torchvision import transforms
13
+ from ultralytics import YOLO
14
+
15
+
16
+ from PIL import Image
17
+ app = Flask(__name__)
18
+ CORS(app)
19
+
20
+
21
+ idx_to_class_resnet50 = {0 : "Genuine" , 1:'Printed Paper' , 2 : 'Replayed'}
22
+ idx_to_class_yolo9 = idx_to_class_yolo9 = {0: 'Genuine', 1: 'Printed Paper', 2: 'Replayed', 3: 'Paper Mask'}
23
+ idx_to_class_resnet50_celeba = {0 : "Genuine" , 1:'Printed Paper' , 2 : 'Paper Cut',3:'Replayed',4:'3D Mask'}
24
+ transform_data_resnet50=transforms.Compose([
25
+ transforms.Resize(size=(224,224)),
26
+ transforms.ToTensor()
27
+ ])
28
+
29
+ transform_data_resnet50_celeba=transforms.Compose([
30
+ transforms.ToTensor(),
31
+ transforms.Resize((224,224), antialias=True)
32
+ ])
33
+
34
+ model_resnet50 = models.resnet50(weights=False)
35
+ num_classes = 3
36
+ model_resnet50.fc = nn.Linear(model_resnet50.fc.in_features, num_classes)
37
+ model_resnet50.load_state_dict(torch.load('resnet50_pytorch_rose_weights.pth',map_location=torch.device('cpu')))
38
+ model_resnet50.eval()
39
+
40
+ model_resnet50_celeba = models.resnet50(weights=False)
41
+ num_classes = 5
42
+ model_resnet50_celeba.fc = nn.Linear(model_resnet50_celeba.fc.in_features, num_classes)
43
+ model_resnet50_celeba.load_state_dict(torch.load('resnet50_model_weights_celeba.pth',map_location=torch.device('cpu')))
44
+ model_resnet50_celeba.eval()
45
+
46
+ model_yolo9 = YOLO('yolo9_best.pt')
47
+ print('Models Loaded Successfully')
48
+
49
+ @app.route('/')
50
+ def home():
51
+ return "Welcome to the Flask API!"
52
+
53
+
54
+ @app.route('/api/data', methods=['GET'])
55
+ def get_data():
56
+ img = plt.imread('test1.jpeg')
57
+ img_arr = np.array(img)
58
+ pil_img = Image.fromarray(img_arr.astype(np.uint8))
59
+ buffered = io.BytesIO()
60
+ pil_img.save(buffered, format="JPEG")
61
+ img_str = base64.b64encode(buffered.getvalue()).decode()
62
+
63
+ data = {
64
+ 'message': 'Hello, World!',
65
+ 'items': [1, 2, 3, 4, 5],
66
+ 'image': img_str
67
+ }
68
+ return jsonify(data)
69
+
70
+ @app.route('/api/data', methods=['POST'])
71
+ def post_data():
72
+ data = request.json
73
+ base64_image = data['imageData']
74
+ filename = data.get('filename', 'image.jpg')
75
+ image_data = base64.b64decode(base64_image)
76
+ image = Image.open(io.BytesIO(image_data)).convert('RGB')
77
+ image.save(filename)
78
+ if data['model']=='resnet':
79
+ transform_img = transform_data_resnet50(image).unsqueeze(0)
80
+ with torch.no_grad():
81
+ pred = model_resnet50(transform_img)
82
+ probabilities = F.softmax(pred[0], dim=0)
83
+ cat = torch.argmax(pred[0]).item()
84
+ prob = round((probabilities[cat] * 100).item(),2)
85
+ name = idx_to_class_resnet50[cat]
86
+ elif data['model']=='resnet50':
87
+ transform_img = transform_data_resnet50_celeba(image).unsqueeze(0)
88
+ with torch.no_grad():
89
+ pred = model_resnet50_celeba(transform_img)
90
+ probabilities = F.softmax(pred[0], dim=0)
91
+ cat = torch.argmax(pred[0]).item()
92
+ prob = round((probabilities[cat] * 100).item(),2)
93
+ name = idx_to_class_resnet50_celeba[cat]
94
+ else:
95
+ results = model_yolo9(image)
96
+ name = 'not detectable'
97
+ prob = 0.00
98
+ for result in results[0].boxes:
99
+ cls = int(result.cls.item())
100
+ name = idx_to_class_yolo9[cls]
101
+ prob = round(result.conf.item() * 100,2)
102
+
103
+ response = {
104
+ 'message': 'Data received!',
105
+ 'your_base64': data['imageData'],
106
+ 'class' : name,
107
+ 'prob' : prob
108
+ }
109
+ return jsonify(response), 201
110
+
111
+ if __name__ == '__main__':
112
+ app.run(debug=True,port = 5000)
resnet50_model_weights_celeba.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:426540175980acb7aac2d61cc89a41abab9f10244efccf40ae6d11f07e08edb0
3
+ size 94395630
resnet50_pytorch_rose_weights.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b4479730c838e12351b22add968e84b8dbe4480a0131f650c152106d96c0e478
3
+ size 94381514
yolo9_best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2a288ba20d964edc8a694ca2ae2705f87a51508a47f982eb35e4fd65507aa9b8
3
+ size 51548923