File size: 1,544 Bytes
77e1798
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import streamlit as st
from PIL import Image
import numpy as np
from tensorflow.keras.models import load_model

model = load_model('yolo8_model.h5')

def detect_trees(image):
    # Preprocess the image
    img = np.array(image)
    img = img / 255.0

    # Make predictions
    predictions = model.predict(img)

    # Process the predictions
    trees = []
    for prediction in predictions:
        x, y, w, h, confidence = prediction
        if confidence > 0.5:
            trees.append((x, y, w, h))

    return trees

def main():
    st.title('Tree Counter')
    st.write('Upload an image to count the trees')

    col1, col2, col3 = st.columns([1, 1, 1])
    with col1:
        st.button('Detect Trees')
    with col2:
        st.button('Crop Image')
    with col3:
        st.button('Reset')

    uploaded_file = st.file_uploader('Choose an image', type=['jpg', 'png', 'jpeg'])
    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        st.image(image, caption='Uploaded Image')

        width, height = image.size
        if width > 1024 or height > 1024:
            if st.button('Crop Image'):
                # Crop the image
                cropped_image = image.crop((100, 100, 300, 300))
                st.image(cropped_image, caption='Cropped Image')
        else:
            if st.button('Detect Trees'):
                trees = detect_trees(image)
                st.write(f'Number of Trees: {len(trees)}')

    st.write('Google Earth Integration')
    st.map()

if __name__ == '__main__':
    main()