-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (58 loc) · 1.66 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
from ultralytics import YOLO
from PIL import Image
import numpy as np
import cv2
from fastapi import FastAPI
from fastapi.responses import FileResponse
import base64
import requests
app = FastAPI()
model = YOLO("best.pt")
def predict(image):
results = model(image)
image = np.array(image)
return results, image
def to_list(results):
return results[0].boxes.cpu().xywh.tolist()
def overlay_rectangles(image, xyxys):
for xyxy in xyxys:
cv2.rectangle(
image,
(
int(xyxy[0]),
int(xyxy[1]),
int(xyxy[2]),
int(xyxy[3]),
),
(0, 255, 0),
3,
)
img = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_GRAY2RGB))
return img
# results, image = predict(Image.open("test2.jpg"))
# results = to_list(results)
# print("done")
# print("rec")
@app.get("/inference")
async def inference(
center,
zoom=17,
):
print(center)
r = requests.get(
f"https://maps.googleapis.com/maps/api/staticmap?center={center}&zoom={zoom}&scale=1&maptype=satellite&size=1920x1080&key=AIzaSyDEeRLQUVP2qJ1Q1_iiVYIziCdDUPzgqsc",
"res.png",
)
with open("res.png", "wb") as f:
f.write(r.content)
img = Image.open("res.png")
# return FileResponse("res.png")
results, image = predict(img)
results = to_list(results)
overlay_rectangles(image, results).save("overlayed.png")
with open("res.png", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
return {"image": encoded_string, "boxes": results}
@app.get("/")
async def resp():
return {"message": "server up"}