File size: 8,805 Bytes
6ad6df4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75f8177
 
 
cbed291
 
 
6ad6df4
 
cbed291
6ad6df4
cbed291
6ad6df4
 
3344dd3
 
 
6ad6df4
bb5a804
6ad6df4
 
 
 
 
 
7827df9
 
b5325ff
724f73d
15064f4
3344dd3
 
 
 
d187eed
3344dd3
6ad6df4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126357b
 
 
6ad6df4
 
 
 
 
 
 
 
 
 
0b26aed
 
 
 
126357b
0b26aed
d262340
0b26aed
d6586ba
b1cdef5
0b26aed
 
b1cdef5
af08cf0
6ad6df4
cbed291
 
 
6ad6df4
44338f2
 
6ad6df4
cbed291
8d83de8
cbed291
6ad6df4
cbed291
6ad6df4
cbed291
 
6ad6df4
cbed291
1cff2f2
df522bc
cbed291
 
 
 
 
 
 
 
 
6ad6df4
cbed291
 
6ad6df4
cbed291
 
 
6ad6df4
cbed291
 
 
 
6ad6df4
cbed291
 
6ad6df4
cbed291
 
6ad6df4
cbed291
6ad6df4
cbed291
 
 
 
6ad6df4
cbed291
6ad6df4
cbed291
 
 
 
 
6ad6df4
cbed291
 
 
 
6ad6df4
cbed291
6ad6df4
abba874
cbed291
6ad6df4
cbed291
6ad6df4
cbed291
6ad6df4
cbed291
7dd0b84
cbed291
 
 
 
 
 
 
 
 
 
 
 
 
 
0b26aed
 
0a0867b
0b26aed
 
 
a29eb3f
cbed291
 
 
 
 
 
 
 
 
 
 
abba874
cbed291
 
 
 
 
 
 
 
44338f2
 
cbed291
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abba874
cbed291
 
 
 
9452e2d
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 13 16:58:28 2021

"""

import time
import streamlit as st
import tensorflow as tf
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
import numpy as np
import pandas as pd


from absl import app, flags, logging
from absl.flags import FLAGS
import cv2

from models import (YoloV3, YoloV3Tiny)
from dataset import transform_images
from utils import draw_outputs
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.patches import Rectangle

from geotiff import GeoTiff
from PIL import Image

import os
import sys

import requests



image = load_img('trees.jpg')
image = img_to_array(image).astype('float')/255

st.image(image)

c1, c2, c3 = st.columns([0.2, 0.6, 0.2])

file_str=c2.text_input(label='URL to GeoTIFF file', value='')
if file_str:
    filepath = 'example.tif'
    file=filepath
    #file = wget.download(file_str, out=filepath)
    #file = wget.download(file_str)
    r = requests.get(file_str, allow_redirects=True)
    open(filepath, 'wb').write(r.content)

    
    geo_tiff = GeoTiff(filepath)

    # the original crs code
    # geo_tiff.crs_code
    # the current crs code
    # geo_tiff.as_crs
    # the shape of the tiff
    # geo_tiff.tif_shape
    # the bounding box in the as_crs CRS
    # geo_tiff.tif_bBox
    # the bounding box as WGS 84
    # geo_tiff.tif_bBox_wgs_84
    # the bounding box in the as_crs converted coordinates
    # geo_tiff.tif_bBox_converted

    i = geo_tiff.tif_shape[1]
    j = geo_tiff.tif_shape[0]
    # in the as_crs coords
    # geo_tiff.get_coords(i, j)
    # in WGS 84 coords
    print('Koordinaten')
    print(geo_tiff.get_wgs_84_coords(i, j))
    print(geo_tiff.get_wgs_84_coords(0, 0))

    # degrees per Pixel in x-direction
    deg_pixel_x = (geo_tiff.get_wgs_84_coords(i, j)[
                   0]-geo_tiff.get_wgs_84_coords(0, 0)[0])/(i, -j)[0]
    deg_pixel_y = (geo_tiff.get_wgs_84_coords(i, j)[
                   1]-geo_tiff.get_wgs_84_coords(0, 0)[1])/(i, -j)[1]

    start_x = geo_tiff.get_wgs_84_coords(0, 0)[0]
    start_y = geo_tiff.get_wgs_84_coords(i, j)[1]

    
    #print(start_x, start_y)
    #print(deg_pixel_x,deg_pixel_y )
    #print('_'*50 + ' Ende '+ '_'*50)

    #size = 416

    #area_box = [(start_x+int(i/size/2)*deg_pixel_x*size, start_y+int(j/size/2)*deg_pixel_y*size), (start_x+int(i/size/2) *
    #                                                     deg_pixel_x*size+size*deg_pixel_x, start_y+int(j/size/2)*deg_pixel_y*size+size*deg_pixel_y)]

    #array = geo_tiff.read_box(area_box.copy())
    

    size=(416, 416)

    Image.MAX_IMAGE_PIXELS = 10000000000
    with Image.open(file) as im:
        im.thumbnail(size)

    gloabl_image = c2.image(im)
    #gloabl_image = c2.image(array/255)    

    threshold = c2.text_input(
        label='Detection threshold: Reduce to detect more trees, increase to remove duplicates', value=0.3)
    button = c2.button('Start detecting defect trees')

    my_bar = c2.progress(0)

    if button == True:
        size = 416

        FLAGS(sys.argv)
        flags.DEFINE_string('classes', 'trees_simple.names', 'path to classes file')
        flags.DEFINE_string('weights', 'checkpoints/trees_all.tf',
                            'path to weights file')
        flags.DEFINE_boolean('tiny', False, 'yolov3 or yolov3-tiny')
        flags.DEFINE_integer('size', 416, 'resize images to')
        flags.DEFINE_string('video', './data/video.mp4',
                            'path to video file or number for webcam)')
        flags.DEFINE_string('output', './data/video2.mp4', 'path to output video')
        flags.DEFINE_string('output_format', 'XVID',
                            'codec used in VideoWriter when saving video to file')
        flags.DEFINE_integer('num_classes', 5, 'number of classes in the model')

        flags.DEFINE_float('yolo_iou_threshold', 0.5, 'iou threshold')
        flags.DEFINE_float('yolo_score_threshold', float(threshold), 'score threshold')

        physical_devices = tf.config.experimental.list_physical_devices('GPU')
        for physical_device in physical_devices:
            tf.config.experimental.set_memory_growth(physical_device, True)

        if FLAGS.tiny:
            yolo = YoloV3Tiny(classes=FLAGS.num_classes)
        else:
            yolo = YoloV3(classes=FLAGS.num_classes)

        yolo.load_weights(FLAGS.weights)
        logging.info('weights loaded')

        class_names = [c.strip() for c in open(FLAGS.classes).readlines()]
        logging.info('classes loaded')

        times = []

        try:
            vid = cv2.VideoCapture(int(FLAGS.video))
        except:
            vid = cv2.VideoCapture(FLAGS.video)

        out = None

        images = []
        bboxes_x_found = []
        bboxes_y_found = []
        classes_found = []
        scores_found = []

        fig = plt.figure()
        canvas = FigureCanvasAgg()
        ax = fig.add_subplot()
        ax.axis('off')

        imgg = st.image([], width=300)

        z2 = pd.DataFrame(np.ones((0, 4)), columns=['Class', 'Certainty', 'Longitude', 'Lattitude'])
        datafr = c2.dataframe(data=z2)

        for m in (range(int(i/size))):

            my_bar.progress(int((m+1)/int(i/size)*100))

            for n in range(int(j/size)):

                area_box = [(start_x+m*deg_pixel_x*size, start_y+n*deg_pixel_y*size), (start_x+m *
                                                                                       deg_pixel_x*size+size*deg_pixel_x, start_y+n*deg_pixel_y*size+size*deg_pixel_y)]

                array = geo_tiff.read_box(area_box.copy())

                img = array

                # img_in = np.arra([img[:, :, :3], img[:, :, :3], img[:, :, :3]])
                img_in = tf.expand_dims(img[:, :, :3], 0)
                img_in = transform_images(img_in, FLAGS.size)

                t1 = time.time()
                boxes, scores, classes, nums = yolo.predict(img_in, verbose=False)

                
                #print('image min max:', img.min(), img.max(), img.shape)

                #images.append(img.astype('float')/255)
                #imgg.image(images, width=230)
                

                if nums > 0:

                    ax.cla()
                    ax.imshow(im)
                    rect = Rectangle((m, n), 416, 416, linewidth=2,
                                     edgecolor='r', facecolor='none')

                    ax.add_patch(rect)
                    ax.draw(canvas.get_renderer())
                    im = np.array(canvas.buffer_rgba())

                    # gloabl_image.image(im)

                    t2 = time.time()
                    times.append(t2-t1)
                    times = times[-20:]
                    img = draw_outputs(img, (boxes, scores, classes, nums), class_names)
                    img = cv2.putText(img, "Time: {:.2f}ms".format(sum(times)/len(times)*1000), (0, 30),
                                      cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2)

                    images.append(img/255)
                    imgg.image(images, width=230)

                    for ind in range(nums[0]):
                        classes_found.append(class_names[int(classes[0][ind])])
                        scores_found.append(np.array(scores[0][ind]))
                        bboxes_x_found.append(
                            np.array(boxes[0][ind][0]*deg_pixel_x*size+start_x+m*deg_pixel_x*size))
                        bboxes_y_found.append(
                            np.array(boxes[0][ind][1]*deg_pixel_y*size+start_y+n*deg_pixel_y*size))

                     # plt.imshow(img)
                     # plt.show()
                if len(classes_found) != 0:

                    classes_found_np = np.array(classes_found).reshape(-1, 1).astype('str')
                    bboxes_x_found_np = np.array(bboxes_x_found).reshape(-1, 1)
                    bboxes_y_found_np = np.array(bboxes_y_found).reshape(-1, 1)
                    scores_found_np = np.array(scores_found).reshape(-1, 1).astype('float64')

                    found = np.concatenate((classes_found_np, scores_found_np,
                                            bboxes_x_found_np, bboxes_y_found_np), axis=1)
                    # np.savetxt(r'C:\Users\alfa\Desktop\Python\Baum Projekt Labels\found trees.txt',
                    #            found, fmt=['%s', '%.0f', '%.7f', '%.7f'])

                    z2 = found

                    z2 = pd.DataFrame(
                        z2, columns=['Class', 'Certainty', 'Longitude', 'Lattitude'])

                    datafr.dataframe(data=z2)
        if len(classes_found) != 0:
            z3 = z2.to_csv()
            st.download_button('Download *.csv file', z3)