piyush3 commited on
Commit
53c6c4a
·
verified ·
1 Parent(s): 353d4c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -204
app.py CHANGED
@@ -1,219 +1,63 @@
1
- from fastapi import FastAPI, File, UploadFile, HTTPException, WebSocket, WebSocketDisconnect
2
- from fastapi.middleware.cors import CORSMiddleware
3
  import os
4
  import tempfile
5
- from numpy import asarray
6
- from ultralytics import YOLO
7
  import base64
8
- import shutil
9
- from fastapi.responses import JSONResponse
10
  import json
11
- import cv2
12
- import mediapipe as mp
13
- import pandas as pd
14
- import numpy as np
15
- import multiprocessing
16
-
17
- app = FastAPI()
18
- app.add_middleware(
19
- CORSMiddleware,
20
- allow_origins=["*"],
21
- allow_credentials=True,
22
- allow_methods=["*"],
23
- allow_headers=["*"],
24
- )
25
-
26
- distance_value = multiprocessing.Value('i', 0)
27
 
28
- def start_lidar_process():
29
- import lidar_dist
30
- process = multiprocessing.Process(target=lidar_dist.lidar_process, args=(distance_value,))
31
- process.start()
32
- return process
33
 
34
- lidar_process = start_lidar_process()
35
- def get_single_image_name(directory_path):
36
- image_names = [file for file in os.listdir(directory_path) if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif'))]
37
- return image_names[0]
38
 
39
- mp_face_mesh = mp.solutions.face_mesh
40
- face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1)
41
- data = pd.read_csv('/home/chait/Desktop/Proj/backend/data1.csv')
42
-
43
- osmf = YOLO('/home/chait/Desktop/Proj/backend/best.pt', task="classify")
44
- calculus = YOLO('/home/chait/Desktop/Proj/backend/calculus.pt', task="classify")
45
- calculus_inst = YOLO('/home/chait/Desktop/Proj/backend/calculus-inst.pt', task="segment")
46
- gingivitis = YOLO('/home/chait/Desktop/Proj/backend/gingivitis.pt', task="classify")
47
- phenotype = YOLO('/home/chait/Desktop/Proj/backend/phenotype.pt', task="classify")
48
-
49
- @app.get("/")
50
- def read_root():
51
- return {"Hello": "World"}
52
-
53
- @app.post('/opening')
54
- def opening(file: UploadFile = None):
55
- if file is None:
56
- raise HTTPException(status_code=400, detail="No file provided")
57
- try:
58
- if file:
59
- print(f"Lidar Distance: {distance_value.value}")
60
- with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
61
- temp_file.write(file.file.read())
62
- image = cv2.imread(temp_file.name)
63
- image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
64
- results = face_mesh.process(image)
65
- if results.multi_face_landmarks:
66
- for face_landmarks in results.multi_face_landmarks:
67
- lower_lip_center = (
68
- int(face_landmarks.landmark[13].x * image.shape[1]),
69
- int(face_landmarks.landmark[13].y * image.shape[0])
70
- )
71
- upper_lip_center = (
72
- int(face_landmarks.landmark[14].x * image.shape[1]),
73
- int(face_landmarks.landmark[14].y * image.shape[0])
74
- )
75
- cv2.circle(image, lower_lip_center, 2, (0, 255, 0), -1)
76
- cv2.circle(image, upper_lip_center, 2, (0, 255, 0), -1)
77
- dist = np.linalg.norm(np.array(upper_lip_center) - np.array(lower_lip_center))
78
- if dist > 2:
79
- index = (data["Lidar Distance"] - distance_value.value).abs().idxmin()
80
- multiplication_factor = data.iloc[index]["Multiplication Factor"]
81
- actual_length = dist * multiplication_factor * 10
82
- text = f"Actual Distance: {actual_length} cm"
83
- cv2.putText(image, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
84
- else:
85
- text = "Mouth is Closed"
86
- cv2.putText(image, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
87
- image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
88
- _, buffer = cv2.imencode('.jpg', image)
89
- encoded_image = base64.b64encode(buffer).decode('utf-8')
90
- os.remove(temp_file.name)
91
 
92
- result = {'status': 'success', 'generatedImage': encoded_image, 'opening': actual_length}
93
- return JSONResponse(content=result)
94
- except Exception as e:
95
- raise HTTPException(status_code=500, detail=str(e))
96
 
97
- @app.post('/osmf')
98
- async def osmf_detection(file: UploadFile = None):
99
- if file is None:
100
- raise HTTPException(status_code=400, detail="No file provided")
101
- if file:
102
- print(file.filename.lower())
103
- try:
104
- if file:
105
- with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
106
- temp_file.write(file.file.read())
107
- results = osmf.predict(source=temp_file.name, conf=0.2, save=True)
108
- predict = osmf(temp_file.name)
109
- js = predict[0].tojson()
110
- predict_dict = json.loads(js)
111
- name = predict_dict[0]["name"]
112
- confidence = predict_dict[0]["confidence"]
113
- os.remove(temp_file.name)
114
- image_name = get_single_image_name('/home/chait/Desktop/runs/classify/predict')
115
- with open('/home/chait/Desktop/runs/classify/predict/'+image_name, "rb") as image_file:
116
- encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
117
- result = {'status': 'success', 'generatedImage': encoded_image, 'class': name, 'conf': confidence}
118
- shutil.rmtree('/home/chait/Desktop/runs')
119
- return JSONResponse(content=result)
120
- else:
121
- return{'status': 'error with file'}
122
- except Exception as e:
123
- raise HTTPException(status_code=500, detail=str(e))
124
 
125
- @app.post('/calculus')
126
- async def calculus_class(file: UploadFile = None):
127
- if file is None:
128
- raise HTTPException(status_code=400, detail="No file provided")
129
- if file:
130
- print(file.filename.lower())
131
  try:
132
- if file:
133
- with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
134
- temp_file.write(file.file.read())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
- predict = calculus(temp_file.name)
137
- js = predict[0].tojson()
138
- predict_dict = json.loads(js)
139
- name = predict_dict[0]["name"]
140
- confidence = predict_dict[0]["confidence"]
141
- predict[0].save('/home/chait/Desktop/Proj/backend/calculus-class.jpg')
142
- if name == "Calculus":
143
- instance = calculus_inst(source=temp_file.name)
144
- instance[0].save('/home/chait/Desktop/Proj/backend/calculus-inst.jpg')
145
- with open('/home/chait/Desktop/Proj/backend/calculus-inst.jpg', "rb") as image_file:
146
- encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
147
- result = { 'status': 'success', 'generatedImage': encoded_image, 'class': name, 'conf': confidence}
148
- os.remove('./calculus-inst.jpg')
149
- else:
150
- with open('/home/chait/Desktop/Proj/backend/calculus-class.jpg', "rb") as image_file:
151
- encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
152
- result = {'status': 'success', 'generatedImage': encoded_image, 'class': name, 'conf': confidence}
153
- os.remove('/home/chait/Desktop/Proj/backend/calculus-class.jpg')
154
- os.remove(temp_file.name)
155
- return JSONResponse(content=result)
156
- else:
157
- return{'status': 'error with file'}
158
  except Exception as e:
159
- raise HTTPException(status_code=500, detail=str(e))
 
 
 
160
 
161
- @app.post('/gingivitis')
162
- async def gingivitis_class(file: UploadFile = None):
163
- if file is None:
164
- raise HTTPException(status_code=400, detail="No file provided")
165
- if file:
166
- print(file.filename.lower())
167
- try:
168
- if file:
169
- with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
170
- temp_file.write(file.file.read())
171
- results = gingivitis.predict(source=temp_file.name, conf=0.2, save=True)
172
- predict = gingivitis(temp_file.name)
173
- js = predict[0].tojson()
174
- predict_dict = json.loads(js)
175
- name = predict_dict[0]["name"]
176
- confidence = predict_dict[0]["confidence"]
177
- os.remove(temp_file.name)
178
- image_name = get_single_image_name('/home/chait/Desktop/runs/classify/predict')
179
- with open('/home/chait/Desktop/runs/classify/predict/'+image_name, "rb") as image_file:
180
- encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
181
- result = {'status': 'success', 'generatedImage': encoded_image, 'class': name, 'conf': confidence}
182
- shutil.rmtree('/home/chait/Desktop/runs')
183
- return JSONResponse(content=result)
184
- else:
185
- return{'status': 'error with file'}
186
- except Exception as e:
187
- raise HTTPException(status_code=500, detail=str(e))
188
-
189
- @app.post('/phenotype')
190
- async def phenotype_class(file: UploadFile = None):
191
- if file is None:
192
- raise HTTPException(status_code=400, detail="No file provided")
193
- if file:
194
- print(file.filename.lower())
195
- try:
196
- if file:
197
- with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
198
- temp_file.write(file.file.read())
199
- results = phenotype.predict(source=temp_file.name, conf=0.5, save=True)
200
- predict = phenotype(temp_file.name)
201
- js = predict[0].tojson()
202
- predict_dict = json.loads(js)
203
- name = predict_dict[0]["name"]
204
- confidence = predict_dict[0]["confidence"]
205
- os.remove(temp_file.name)
206
- image_name = get_single_image_name('/home/chait/Desktop/runs/classify/predict')
207
- with open('/home/chait/Desktop/runs/classify/predict/'+image_name, "rb") as image_file:
208
- encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
209
- result = {'status': 'success', 'generatedImage': encoded_image, 'class': name, 'conf': confidence}
210
- shutil.rmtree('/home/chait/Desktop/runs')
211
- return JSONResponse(content=result)
212
- else:
213
- return{'status': 'error with file'}
214
- except Exception as e:
215
- raise HTTPException(status_code=500, detail=str(e))
216
 
217
- if __name__ == "__main__":
218
- import uvicorn
219
- uvicorn.run(app)
 
1
+ from flask import Flask, render_template, request, jsonify
2
+ from ultralytics import YOLO
3
  import os
4
  import tempfile
 
 
5
  import base64
 
 
6
  import json
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ app = Flask(__name__)
 
 
 
 
9
 
10
+ # Load OSMF model
11
+ osmf = YOLO('best.pt', task="classify")
 
 
12
 
13
+ @app.route('/')
14
+ def home():
15
+ return render_template('index.html')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ @app.route('/predict', methods=['POST'])
18
+ def predict():
19
+ if 'image' not in request.files:
20
+ return jsonify({'error': 'No image provided'})
21
 
22
+ file = request.files['image']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
 
 
 
 
 
 
24
  try:
25
+ # Create temporary file
26
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
27
+ file.save(temp_file.name)
28
+ temp_path = temp_file.name
29
+
30
+ # Process image
31
+ predict = osmf(temp_path)
32
+ results = predict[0].to_json() # Using the new recommended method
33
+ predict_dict = json.loads(results)
34
+
35
+ name = predict_dict[0]["name"]
36
+ confidence = float(predict_dict[0]["confidence"]) * 100
37
+
38
+ # Close file handle and remove
39
+ try:
40
+ os.close(temp_file.fileno())
41
+ except:
42
+ pass
43
+
44
+ try:
45
+ os.unlink(temp_path)
46
+ except:
47
+ pass
48
 
49
+ return jsonify({
50
+ 'class': name,
51
+ 'confidence': f"{confidence:.2f}%",
52
+ 'status': 'success'
53
+ })
54
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  except Exception as e:
56
+ return jsonify({
57
+ 'error': str(e),
58
+ 'status': 'error'
59
+ }), 500
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
+ if __name__ == '__main__':
63
+ app.run(debug=True)