krishanwalia30 commited on
Commit
f3f94da
·
verified ·
1 Parent(s): 678249d

Upload 3 files

Browse files
Files changed (3) hide show
  1. best.pt +3 -0
  2. flaskapp.py +152 -0
  3. requirements.txt +5 -0
best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b1c7e7aa260c6f1e34b1c8c0d4c3bdc35b6f40f75f59e3cf593ba58aaff690d3
3
+ size 6253849
flaskapp.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ from flask import Flask, request, jsonify, render_template
3
+ import os
4
+ from flask_cors import CORS, cross_origin
5
+ # -
6
+ # import streamlit as st
7
+ from ultralytics import YOLO
8
+ import numpy as np
9
+ from PIL import Image
10
+ import requests
11
+ from io import BytesIO
12
+ import cv2
13
+ import io
14
+
15
+ def calculate_score(results):
16
+ labels = {0: u'bathtub', 1: u'c', 2: u'geyser', 3: u'mirror', 4: u'showerhead', 5: u'sink', 6: u'toilet', 7: u'towel', 8: u'washbasin', 9: u'wc', 10: u'none'}
17
+ scores = {0: 70, # Bathtub
18
+ 1: 50, # 'c' idk wtf is this
19
+ 2: 60, # Geyser is imp
20
+ 3: 80, # Mirrors are op
21
+ 4: 60, # Showerhead is ok, but not imp when shitting
22
+ 5: 90, # Sink is a S+
23
+ 6: 100, # Not imp
24
+ 7: 40, # Towels
25
+ 8: 80, # Washbasin
26
+ 9: 100, # 'wc'
27
+ 10: 0} # 'none'
28
+ score = 0
29
+ for key, value in labels.items():
30
+ if value in results:
31
+ score = score + scores[key]
32
+
33
+ score = (score*100.0)/730.0
34
+ return score
35
+
36
+ app = Flask(__name__)
37
+ CORS(app)
38
+
39
+
40
+ def decodeImage(imgstring, fileName):
41
+ imgdata = base64.b64decode(imgstring)
42
+ with open(fileName, 'wb') as f:
43
+ f.write(imgdata)
44
+ f.close()
45
+
46
+
47
+ def encodeImageIntoBase64(croppedImagePath):
48
+ with open(croppedImagePath, "rb") as f:
49
+ return base64.b64encode(f.read())
50
+
51
+
52
+ @app.route("/", methods = ['GET'])
53
+ @cross_origin()
54
+ def home():
55
+ html_content = "<h1>SERVER UP!</h1>"
56
+ return html_content, 200, {'Content-Type': 'text/html'}
57
+
58
+
59
+ @app.route('/api/process-images', methods=['POST'])
60
+ def process_images():
61
+ try:
62
+ if 'images' not in request.files:
63
+ return jsonify({"error": "No images uploaded"}), 400
64
+
65
+ images = request.files.getlist('images')
66
+
67
+ model = YOLO('best.pt')
68
+
69
+ for image in images:
70
+ image_file = image
71
+ image_file.save('input.png') # Example: Save image as 'uploaded_image.png'
72
+
73
+
74
+ processed_images = []
75
+ for image in images:
76
+ # Read image file
77
+ img = Image.open(image)
78
+ # Perform processing (example: resizing)
79
+ img_resized = img.resize((100, 100)) # Resize the image to 100x100 (example)
80
+ # Convert processed image to bytes
81
+ buffered = BytesIO()
82
+ img_resized.save(buffered, format="JPEG")
83
+ processed_images.append(buffered.getvalue())
84
+
85
+ return jsonify({"processed_images": processed_images}), 200
86
+ except Exception as e:
87
+ return jsonify({"error": str(e)}), 500
88
+
89
+
90
+ @app.route('/api/process-image', methods=['POST'])
91
+ def process_image():
92
+ try:
93
+ if 'image' not in request.files:
94
+ return jsonify({"error": "No image uploaded"}), 400
95
+
96
+ image_file = request.files['image']
97
+ image_file.save('input.png') # Example: Save image as 'uploaded_image.png'
98
+
99
+ model = YOLO("best.pt")
100
+ Img = Image.open(image_file)
101
+
102
+ results = model(Img)
103
+
104
+ class_id = results[0].boxes.cls.numpy()
105
+ labels = {0: u'bathtub', 1: u'c', 2: u'geyser', 3: u'mirror', 4: u'showerhead', 5: u'sink', 6: u'toilet', 7: u'towel', 8: u'washbasin', 9: u'wc', 10: u'none'}
106
+
107
+ classes = set()
108
+ for i in class_id:
109
+ classes.add(labels[i])
110
+
111
+ return jsonify({"Facilities_Detected": list(classes), "Toilet_Score":calculate_score(classes)}), 200
112
+ except Exception as e:
113
+ return jsonify({"error": str(e)}), 500
114
+
115
+
116
+
117
+ @app.route('/c', methods=['POST'])
118
+ def processor_image():
119
+ # try:
120
+ if 'image' not in request.files:
121
+ return jsonify({"error": "No image uploaded"}), 400
122
+
123
+ image_file = request.files.getlist('image')
124
+ print(image_file)
125
+
126
+ total_list = set()
127
+
128
+ for image in image_file:
129
+
130
+ model = YOLO("best.pt")
131
+ image.save('input.png') # Example: Save image as 'uploaded_image.png'
132
+ # image_file.save('input.png') # Example: Save image as 'uploaded_image.png'
133
+
134
+ Img = Image.open(image)
135
+ results = model(Img)
136
+
137
+ class_id = results[0].boxes.cls.numpy()
138
+ labels = {0: u'bathtub', 1: u'c', 2: u'geyser', 3: u'mirror', 4: u'showerhead', 5: u'sink', 6: u'toilet', 7: u'towel', 8: u'washbasin', 9: u'wc', 10: u'none'}
139
+
140
+ # classes = set()
141
+ for i in class_id:
142
+ total_list.add(labels[i])
143
+
144
+ # total_list.append(classes)
145
+ print(total_list)
146
+ return jsonify({"Facilities_Detected": list(total_list), "Toilet_Score":calculate_score(set(total_list))}), 200
147
+ # except Exception as e:
148
+ # return jsonify({"error": str(e)}), 500
149
+
150
+
151
+
152
+ app.run(host='0.0.0.0', port=1000)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ ultralytics
2
+ # streamlit
3
+ opencv-contrib-python-headless
4
+ flask_cors
5
+ flask