root-sajjan
commited on
update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,47 @@
|
|
1 |
-
from fastapi import FastAPI, File, UploadFile
|
2 |
-
from fastapi.responses import JSONResponse
|
3 |
-
from fastapi.middleware.cors import CORSMiddleware
|
4 |
-
from PIL import Image
|
5 |
-
import io
|
6 |
-
|
7 |
-
from pathlib import Path
|
8 |
-
from model import YOLOModel
|
9 |
-
import shutil
|
10 |
-
|
11 |
-
yolo = YOLOModel()
|
12 |
-
|
13 |
-
UPLOAD_FOLDER = Path("./uploads")
|
14 |
-
UPLOAD_FOLDER.mkdir(exist_ok=True)
|
15 |
-
|
16 |
-
app = FastAPI()
|
17 |
-
|
18 |
-
@app.post("/upload")
|
19 |
-
async def upload_image(image: UploadFile = File(...)):
|
20 |
-
# print(f'\n\t\tUPLOADED!!!!')
|
21 |
-
try:
|
22 |
-
file_path = UPLOAD_FOLDER / image.filename
|
23 |
-
with file_path.open("wb") as buffer:
|
24 |
-
shutil.copyfileobj(image.file, buffer)
|
25 |
-
# print(f'Starting to pass into model, {file_path}')
|
26 |
-
# Perform YOLO inference
|
27 |
-
predictions = yolo.predict(str(file_path))
|
28 |
-
|
29 |
-
# Clean up uploaded file
|
30 |
-
file_path.unlink() # Remove file after processing
|
31 |
-
return JSONResponse(content={"items": predictions})
|
32 |
-
|
33 |
-
|
34 |
-
except Exception as e:
|
35 |
-
return JSONResponse(content={"error": str(e)}, status_code=500)
|
36 |
-
|
37 |
-
# code to accept the localhost to get images from
|
38 |
-
app.add_middleware(
|
39 |
-
CORSMiddleware,
|
40 |
-
allow_origins=["http://192.168.56.1:3000", "http://192.168.56.1:3001"],
|
41 |
-
allow_methods=["*"],
|
42 |
-
allow_headers=["*"],
|
43 |
-
)
|
44 |
-
|
45 |
-
if __name__ == "__main__":
|
46 |
-
import uvicorn
|
47 |
-
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True)
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
|
7 |
+
from pathlib import Path
|
8 |
+
from model import YOLOModel
|
9 |
+
import shutil
|
10 |
+
|
11 |
+
yolo = YOLOModel()
|
12 |
+
|
13 |
+
UPLOAD_FOLDER = Path("./uploads")
|
14 |
+
UPLOAD_FOLDER.mkdir(exist_ok=True)
|
15 |
+
|
16 |
+
app = FastAPI()
|
17 |
+
|
18 |
+
@app.post("/upload")
|
19 |
+
async def upload_image(image: UploadFile = File(...)):
|
20 |
+
# print(f'\n\t\tUPLOADED!!!!')
|
21 |
+
try:
|
22 |
+
file_path = UPLOAD_FOLDER / image.filename
|
23 |
+
with file_path.open("wb") as buffer:
|
24 |
+
shutil.copyfileobj(image.file, buffer)
|
25 |
+
# print(f'Starting to pass into model, {file_path}')
|
26 |
+
# Perform YOLO inference
|
27 |
+
predictions = yolo.predict(str(file_path))
|
28 |
+
print(f'\n\n\n{predictions}\n\n\ \n\t\t\t\tare predictions')
|
29 |
+
# Clean up uploaded file
|
30 |
+
file_path.unlink() # Remove file after processing
|
31 |
+
return JSONResponse(content={"items": predictions})
|
32 |
+
|
33 |
+
|
34 |
+
except Exception as e:
|
35 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
36 |
+
|
37 |
+
# code to accept the localhost to get images from
|
38 |
+
app.add_middleware(
|
39 |
+
CORSMiddleware,
|
40 |
+
allow_origins=["http://192.168.56.1:3000", "http://192.168.56.1:3001"],
|
41 |
+
allow_methods=["*"],
|
42 |
+
allow_headers=["*"],
|
43 |
+
)
|
44 |
+
|
45 |
+
if __name__ == "__main__":
|
46 |
+
import uvicorn
|
47 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True)
|