-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
90 lines (64 loc) · 2.37 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
81
82
83
84
85
86
87
88
89
90
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List, Optional
import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
# import the image module
from PIL import Image
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
# Instantiates a client
client = vision.ImageAnnotatorClient()
# Change this
host_url = "http://127.0.0.1:8000/"
class Vertex(BaseModel):
x : float
y : float
class BoundingRectangle(BaseModel):
vertices : List[Vertex]
class ImageWithBoundingBox(BaseModel):
image_url : str
rectangles : List[BoundingRectangle]
file_list = []
for i in range(8):
# The name of the image file to annotate
createdFileName = f'static/test_file_{i}.jpeg'
file_name = os.path.abspath(createdFileName)
file_list.append(file_name)
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.mount("/static", StaticFiles(directory="static"), name="static")
parent_dir_path = os.path.dirname(os.path.realpath(__file__))
@app.get('/')
def index():
return FileResponse(parent_dir_path + '/static/frontend/index.html')
@app.get("/getStadium/{ticket_id}", response_model = ImageWithBoundingBox)
def root(ticket_id):
# Get the image associated with the ticket_id and store it in the image_number
image_number = int(ticket_id[0])
path = file_list[image_number]
bounding_labels = {"Person", "People", "Crowd"}
with open(path, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
with Image.open(path) as pImage:
width, height = pImage.size
objects = client.object_localization(
image=image, max_results=100).localized_object_annotations
response = ImageWithBoundingBox(image_url = host_url + "static/test_file_" + str(image_number) + ".jpeg", rectangles = [])
for object_ in objects:
if object_.name in bounding_labels:
bounding_rectangle = BoundingRectangle(vertices = [])
for v in object_.bounding_poly.normalized_vertices:
bounding_rectangle.vertices.append(Vertex(x = v.x, y = v.y))
response.rectangles.append(bounding_rectangle)
return response