Spaces:
Sleeping
Sleeping
Update app.py
Browse filesinit 2 rev new file
app.py
CHANGED
|
@@ -1,121 +1,219 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
<div class="upload-section">
|
| 17 |
-
<h2>Upload Image</h2>
|
| 18 |
-
<div class="file-upload">
|
| 19 |
-
<input type="file" id="fileInput" accept="image/*">
|
| 20 |
-
<label for="fileInput">Choose File</label>
|
| 21 |
-
</div>
|
| 22 |
-
<button id="uploadPredict" disabled>Analyze Upload</button>
|
| 23 |
-
</div>
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
<h2>Capture Image</h2>
|
| 31 |
-
<video id="video" autoplay playsinline></video>
|
| 32 |
-
<canvas id="canvas" style="display: none;"></canvas>
|
| 33 |
-
<button id="capture">Capture Image</button>
|
| 34 |
-
</div>
|
| 35 |
-
</div>
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
</div>
|
| 43 |
-
<div id="result" class="result-container"></div>
|
| 44 |
-
</div>
|
| 45 |
-
</div>
|
| 46 |
-
</div>
|
| 47 |
-
<script>
|
| 48 |
-
const video = document.getElementById('video');
|
| 49 |
-
const canvas = document.getElementById('canvas');
|
| 50 |
-
const preview = document.getElementById('preview');
|
| 51 |
-
const captureBtn = document.getElementById('capture');
|
| 52 |
-
const predictBtn = document.getElementById('predict');
|
| 53 |
-
const result = document.getElementById('result');
|
| 54 |
-
const fileInput = document.getElementById('fileInput');
|
| 55 |
-
const uploadPredict = document.getElementById('uploadPredict');
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
});
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
if (file) {
|
| 67 |
-
const reader = new FileReader();
|
| 68 |
-
reader.onload = (e) => {
|
| 69 |
-
preview.src = e.target.result;
|
| 70 |
-
preview.style.display = 'block';
|
| 71 |
-
uploadPredict.disabled = false;
|
| 72 |
-
predictBtn.disabled = true;
|
| 73 |
-
};
|
| 74 |
-
reader.readAsDataURL(file);
|
| 75 |
-
}
|
| 76 |
-
});
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
fileInput.value = '';
|
| 88 |
-
});
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
await predict(formData);
|
| 118 |
-
});
|
| 119 |
-
</script>
|
| 120 |
-
</body>
|
| 121 |
-
</html>
|
|
|
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|