-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
63 lines (48 loc) · 2.09 KB
/
app.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
from flask import Flask, request, render_template
from dotenv import load_dotenv
import os
from predict import predict_image_object_detection
from helpers import get_total
app = Flask(__name__)
# Import the API keys and other invironment variables
load_dotenv()
PROJECT_NUMBER = os.getenv('PROJECT_NUMBER')
LOCATION = os.getenv('LOCATION')
ENDPOINT_ID = os.getenv('ENDPOINT_ID')
API_ENDPOINT = os.getenv('API_ENDPOINT')
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
image_file = request.files['image']
if image_file:
file_path = "./static/images/" + image_file.filename
image_file.save(file_path)
# Make a prediction using the model endpoint
prediction = predict_image_object_detection(
project=PROJECT_NUMBER,
endpoint_id=ENDPOINT_ID,
location=LOCATION,
api_endpoint=API_ENDPOINT,
filename=file_path
)
# Process prediction here
boxes = prediction['bboxes']
labels = prediction['displayNames']
scores = prediction['confidences']
scores = [f"{score:.2f}" for score in scores]
total_amount = get_total(labels)
img_display_width = 400
# Convert normalized coordinates to pixel coordinates
boxes_pixels = []
for box in boxes:
box_pixels = [] # xMin, xMax, yMin, yMax
for x in box:
box_pixels.append(x * img_display_width)
boxes_pixels.append(box_pixels)
# Zip the boxes, labels, and scores together
zip_predictions = zip(boxes_pixels, labels, scores)
# Render template with prediction
return render_template('index.html', predictions=zip_predictions, image_path=file_path, img_width=img_display_width, total_amount=total_amount )
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)