Spaces:
Running
Running
File size: 2,338 Bytes
7e19f9a 499884f |
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 |
import plantvision
import requests
from io import BytesIO
import pickle as pkl
from flask import Flask, render_template, request, session, jsonify, url_for
from PIL import Image
import os
import time
import random
from pathlib import Path
THIS_FOLDER = Path(__file__).parent.resolve()
app = Flask(__name__)
app.secret_key = 'pi-33pp-co-sk-33'
app.template_folder = os.path.abspath(f'{THIS_FOLDER}/web/templates')
app.static_folder = os.path.abspath(f'{THIS_FOLDER}/web/static')
print(app.static_folder)
flowerLayers = None
leafLayers = None
fruitLayers = None
@app.route('/')
def home():
return render_template('index.html')
@app.route('/guess', methods=['POST'])
def guess():
global flowerLayers, leafLayers, fruitLayers
if request.method == 'POST':
print('Thinking...')
img = request.files.get('uploaded-image')
feature = request.form.get('feature')
tensor = plantvision.processImage(img, feature)
predictions = plantvision.see(tensor, feature, 6)
with open(f'{THIS_FOLDER}/resources/speciesNameToKey.pkl','rb') as f:
speciesNameToKey = pkl.load(f)
with open(f'{THIS_FOLDER}/resources/speciesNameToVernacular.pkl','rb') as f:
speciesNameToVernacular = pkl.load(f)
with open(f'{THIS_FOLDER}/resources/{feature}speciesIndexDict.pkl','rb') as f:
speciesNameToIndex = pkl.load(f)
urls = []
predicted_image_urls = []
for p in predictions:
key = speciesNameToKey[p]
img = speciesNameToIndex[p]
query = ''
for i in p.split(' '):
query += i
query += '+'
urls.append(f'https://www.google.com/search?q={query[:-1]}')
predicted_image_urls.append(f"https://storage.googleapis.com/bmllc-images-bucket/images/img{img}.jpeg")
names = []
for p in predictions:
try:
names.append(speciesNameToVernacular[p])
except:
names.append(p)
response = {
'names': names,
'species': predictions,
'predictions': urls,
'images': predicted_image_urls
}
return jsonify(response)
if __name__ == '__main__':
app.run(port=7860,host='0.0.0.0',debug=True)
|