-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
80 lines (63 loc) · 2.79 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import matplotlib.pyplot as plt
from ultralytics import YOLO
from PIL import Image
from typing import List, Any
from fastapi import FastAPI, UploadFile, File
from fastapi.responses import FileResponse
from pathlib import Path
import os
import requests
app = FastAPI()
# Define endpoint for home page that accepts images
@app.get("/")
async def read_index():
return FileResponse('Upload_for_Detection.html')
# Define endpoint for making box predictions using Web UI
@app.post("/YOLO_Box_Prediction_Website/")
def predict_uploaded_image(file: UploadFile):
try:
# Upload and open the image transmitted via POST in a file based on its name
file_name = file.filename
with open(file_name, "wb") as f:
f.write(file.file.read())
# Obtain box detection result
best_model_source = 'best.pt'
yolov8_box_detection = YOLO(best_model_source)
results = yolov8_box_detection(file_name)
# Plot the results on image and display it
im_array = results[0].plot() # plot a BGR numpy array of predictions
im = Image.fromarray(im_array[..., ::-1]) # RGB PIL image
im.save('example_output.jpg') # save image
output_path = Path("example_output.jpg")
return FileResponse(output_path)
except Exception as e:
return {"message": e.args}
# Define endpoint for making box predictions programatically
@app.post("/YOLO_Box_Prediction_Service/")
async def predict_uploaded_image(file: UploadFile):
try:
# Upload the image transmitted via POST in a file based on its name
file_name = file.filename
with open(file_name, "wb") as f:
f.write(await file.read())
# Obtain box detection result
best_model_source = 'best.pt'
yolov8_box_detection = YOLO(best_model_source)
results = yolov8_box_detection(file_name)
# Plot the results on image and display it
im_array = results[0].plot() # plot a BGR numpy array of predictions
im = Image.fromarray(im_array[..., ::-1]) # RGB PIL image
im.save('example_output.jpg') # save image
output_path = Path("example_output.jpg")
# Get metadata about the file
file_name = os.path.basename(output_path)
file_size = os.path.getsize(output_path)
# Create a dictionary containing file metadata
file_metadata = {
"file_name": file_name,
"file_size": file_size
}
# Return JSON response containing file metadata along with the file data
return {"file_metadata": file_metadata, "file_data": FileResponse(output_path)}
except Exception as e:
return {"message": e.args}