Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
```python
|
| 2 |
from flask import Flask, render_template, request, jsonify
|
| 3 |
import os
|
| 4 |
from PIL import Image
|
|
@@ -6,6 +5,10 @@ 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():
|
|
@@ -14,30 +17,55 @@ def index():
|
|
| 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.
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
if __name__ == '__main__':
|
| 43 |
-
app.run(debug=True)
|
|
|
|
|
|
|
| 1 |
from flask import Flask, render_template, request, jsonify
|
| 2 |
import os
|
| 3 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
+
# Ensure uploads directory exists
|
| 9 |
+
if not os.path.exists('uploads'):
|
| 10 |
+
os.makedirs('uploads')
|
| 11 |
+
|
| 12 |
# Define the HTML template for the Tree Counter page
|
| 13 |
@app.route('/')
|
| 14 |
def index():
|
|
|
|
| 17 |
# Define the API endpoint for uploading images
|
| 18 |
@app.route('/upload_image', methods=['POST'])
|
| 19 |
def upload_image():
|
| 20 |
+
if 'image' not in request.files:
|
| 21 |
+
return jsonify({'message': 'No file part'}), 400
|
| 22 |
+
|
| 23 |
image = request.files['image']
|
| 24 |
+
if image.filename == '':
|
| 25 |
+
return jsonify({'message': 'No selected file'}), 400
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
# Save the uploaded image
|
| 29 |
+
image_path = os.path.join('uploads', 'image.jpg')
|
| 30 |
+
image.save(image_path)
|
| 31 |
+
|
| 32 |
+
# Open and process the image
|
| 33 |
+
img = Image.open(image_path)
|
| 34 |
+
width, height = img.size
|
| 35 |
+
if width > 1024 or height > 1024:
|
| 36 |
+
return jsonify({'message': 'Image is too large. Please crop it.'}), 400
|
| 37 |
+
else:
|
| 38 |
+
# Simulate tree detection using YOLO8 model
|
| 39 |
+
# Replace this with actual model implementation
|
| 40 |
+
trees = np.random.randint(0, 100)
|
| 41 |
+
return jsonify({'message': f'Number of Trees: {trees}'})
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return jsonify({'message': f'Error processing image: {str(e)}'}), 500
|
| 44 |
|
| 45 |
# Define the API endpoint for cropping images
|
| 46 |
@app.route('/crop_image', methods=['POST'])
|
| 47 |
def crop_image():
|
| 48 |
+
if 'image' not in request.files:
|
| 49 |
+
return jsonify({'message': 'No file part'}), 400
|
| 50 |
+
|
| 51 |
image = request.files['image']
|
| 52 |
+
if image.filename == '':
|
| 53 |
+
return jsonify({'message': 'No selected file'}), 400
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
# Save the uploaded image
|
| 57 |
+
image_path = os.path.join('uploads', 'image.jpg')
|
| 58 |
+
image.save(image_path)
|
| 59 |
+
|
| 60 |
+
# Open and crop the image
|
| 61 |
+
img = Image.open(image_path)
|
| 62 |
+
cropped_img = img.crop((0, 0, min(1024, img.width), min(1024, img.height)))
|
| 63 |
+
|
| 64 |
+
# Save the cropped image
|
| 65 |
+
cropped_img.save(os.path.join('uploads', 'cropped_image.jpg'))
|
| 66 |
+
return jsonify({'message': 'Image cropped successfully'})
|
| 67 |
+
except Exception as e:
|
| 68 |
+
return jsonify({'message': f'Error cropping image: {str(e)}'}), 500
|
| 69 |
|
| 70 |
if __name__ == '__main__':
|
| 71 |
+
app.run(debug=True)
|