Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
```python
|
| 2 |
+
from flask import Flask, render_template, request, jsonify
|
| 3 |
+
import os
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# Define the HTML template for the Tree Counter page
|
| 10 |
+
@app.route('/')
|
| 11 |
+
def index():
|
| 12 |
+
return render_template('index.html')
|
| 13 |
+
|
| 14 |
+
# Define the API endpoint for uploading images
|
| 15 |
+
@app.route('/upload_image', methods=['POST'])
|
| 16 |
+
def upload_image():
|
| 17 |
+
image = request.files['image']
|
| 18 |
+
image.save('image.jpg')
|
| 19 |
+
img = Image.open('image.jpg')
|
| 20 |
+
width, height = img.size
|
| 21 |
+
if width > 1024 or height > 1024:
|
| 22 |
+
return jsonify({'message': 'Image is too large. Please crop it.'})
|
| 23 |
+
else:
|
| 24 |
+
# Simulate tree detection using YOLO8 model
|
| 25 |
+
# Replace this with actual model implementation
|
| 26 |
+
trees = np.random.randint(0, 100)
|
| 27 |
+
return jsonify({'message': f'Number of Trees: {trees}'})
|
| 28 |
+
|
| 29 |
+
# Define the API endpoint for cropping images
|
| 30 |
+
@app.route('/crop_image', methods=['POST'])
|
| 31 |
+
def crop_image():
|
| 32 |
+
image = request.files['image']
|
| 33 |
+
image.save('image.jpg')
|
| 34 |
+
img = Image.open('image.jpg')
|
| 35 |
+
width, height = img.size
|
| 36 |
+
# Simulate image cropping
|
| 37 |
+
# Replace this with actual image cropping implementation
|
| 38 |
+
cropped_img = img.crop((0, 0, 1024, 1024))
|
| 39 |
+
cropped_img.save('cropped_image.jpg')
|
| 40 |
+
return jsonify({'message': 'Image cropped successfully'})
|
| 41 |
+
|
| 42 |
+
if __name__ == '__main__':
|
| 43 |
+
app.run(debug=True)
|
| 44 |
+
```
|