Hudda commited on
Commit
40719aa
·
verified ·
1 Parent(s): bda21b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -20
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.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)
 
 
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)