Hudda commited on
Commit
77e1798
·
verified ·
1 Parent(s): f0bebf2

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +57 -0
main.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import numpy as np
4
+ from tensorflow.keras.models import load_model
5
+
6
+ model = load_model('yolo8_model.h5')
7
+
8
+ def detect_trees(image):
9
+ # Preprocess the image
10
+ img = np.array(image)
11
+ img = img / 255.0
12
+
13
+ # Make predictions
14
+ predictions = model.predict(img)
15
+
16
+ # Process the predictions
17
+ trees = []
18
+ for prediction in predictions:
19
+ x, y, w, h, confidence = prediction
20
+ if confidence > 0.5:
21
+ trees.append((x, y, w, h))
22
+
23
+ return trees
24
+
25
+ def main():
26
+ st.title('Tree Counter')
27
+ st.write('Upload an image to count the trees')
28
+
29
+ col1, col2, col3 = st.columns([1, 1, 1])
30
+ with col1:
31
+ st.button('Detect Trees')
32
+ with col2:
33
+ st.button('Crop Image')
34
+ with col3:
35
+ st.button('Reset')
36
+
37
+ uploaded_file = st.file_uploader('Choose an image', type=['jpg', 'png', 'jpeg'])
38
+ if uploaded_file is not None:
39
+ image = Image.open(uploaded_file)
40
+ st.image(image, caption='Uploaded Image')
41
+
42
+ width, height = image.size
43
+ if width > 1024 or height > 1024:
44
+ if st.button('Crop Image'):
45
+ # Crop the image
46
+ cropped_image = image.crop((100, 100, 300, 300))
47
+ st.image(cropped_image, caption='Cropped Image')
48
+ else:
49
+ if st.button('Detect Trees'):
50
+ trees = detect_trees(image)
51
+ st.write(f'Number of Trees: {len(trees)}')
52
+
53
+ st.write('Google Earth Integration')
54
+ st.map()
55
+
56
+ if __name__ == '__main__':
57
+ main()