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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -63
app.py CHANGED
@@ -1,71 +1,30 @@
1
- from flask import Flask, render_template, request, jsonify
2
- import os
3
  from PIL import Image
4
  import numpy as np
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():
15
- return render_template('index.html')
16
-
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)
 
1
+ import streamlit as st
 
2
  from PIL import Image
3
  import numpy as np
4
 
5
+ st.title("Tree Counter App")
6
 
7
+ # Upload image via Streamlit
8
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
 
9
 
10
+ if uploaded_file is not None:
11
+ # Open the image
12
+ img = Image.open(uploaded_file)
13
+ st.image(img, caption="Uploaded Image.", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ # Check image dimensions
16
+ width, height = img.size
17
+ if width > 1024 or height > 1024:
18
+ st.error("Image is too large. Please crop it.")
19
+ else:
20
+ # Simulate tree detection using random number
21
+ trees = np.random.randint(0, 100)
22
+ st.success(f"Number of Trees: {trees}")
23
 
24
+ # Image cropping functionality
25
+ if uploaded_file is not None and st.button("Crop Image to 1024x1024"):
26
+ cropped_img = img.crop((0, 0, min(1024, img.width), min(1024, img.height)))
27
+ st.image(cropped_img, caption="Cropped Image.", use_column_width=True)
28
+ cropped_img.save("cropped_image.jpg")
29
+ st.success("Image cropped successfully.")
 
 
 
 
 
 
 
 
30