raj999 commited on
Commit
d2a4567
·
verified ·
1 Parent(s): 200d3a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -2
app.py CHANGED
@@ -13,12 +13,72 @@ import tempfile
13
  from inference import split_image_from_dataframe
14
  from datetime import datetime
15
  from predict import extract_features, predict_similarity, compare_features, extract_features_cp
 
 
 
 
 
 
16
 
 
 
 
17
 
 
 
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- import rasterio
21
- import geopandas as gpd
22
 
23
  model = main.deepforest()
24
  model.use_release()
 
13
  from inference import split_image_from_dataframe
14
  from datetime import datetime
15
  from predict import extract_features, predict_similarity, compare_features, extract_features_cp
16
+ import cv2
17
+ from PIL import Image
18
+ import os
19
+ import numpy as np
20
+ import urllib.request
21
+ import glob
22
 
23
+ # intake library and plugin
24
+ # import intake
25
+ # from intake_zenodo_fetcher import download_zenodo_files_for_entry
26
 
27
+ # geospatial libraries
28
+ import geopandas as gpd
29
 
30
+ from rasterio.transform import from_origin
31
+ import rasterio.features
32
+
33
+ import fiona
34
+
35
+ from shapely.geometry import shape, mapping, box
36
+ from shapely.geometry.multipolygon import MultiPolygon
37
+
38
+ # machine learning libraries
39
+ from detectron2 import model_zoo
40
+ from detectron2.engine import DefaultPredictor
41
+ from detectron2.utils.visualizer import Visualizer, ColorMode
42
+ from detectron2.config import get_cfg
43
+ from detectron2.engine import DefaultTrainer
44
+ # define the URL to retrieve the model
45
+ fn = 'model_final.pth'
46
+ url = f'https://zenodo.org/record/5515408/files/{fn}?download=1'
47
+
48
+ urllib.request.urlretrieve(url, config['model'] + '/' + fn)
49
+
50
+ # import geoviews.tile_sources as gts
51
+
52
+ # import hvplot.pandas
53
+ # import hvplot.xarray
54
+
55
+ # hv.extension('bokeh', width=100)
56
+ cfg = get_cfg()
57
+
58
+ # if you want to make predictions using a CPU, run the following line. If using GPU, hash it out.
59
+ cfg.MODEL.DEVICE='cuda'
60
+
61
+ # model and hyperparameter selection
62
+ cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_101_FPN_3x.yaml"))
63
+ cfg.DATALOADER.NUM_WORKERS = 2
64
+ cfg.SOLVER.IMS_PER_BATCH = 2
65
+ cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1
66
+
67
+ ### path to the saved pre-trained model weights
68
+ cfg.MODEL.WEIGHTS = config['model'] + '/model_final.pth'
69
+
70
+ # set confidence threshold at which we predict
71
+ cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.15
72
+
73
+ #### Settings for predictions using detectron config
74
+
75
+ predictor = DefaultPredictor(cfg)
76
+ outputs = predictor(im)
77
+ v = Visualizer(im[:, :, ::-1], scale=1.5, instance_mode=ColorMode.IMAGE_BW) # remove the colors of unsegmented pixels
78
+ v = v.draw_instance_predictions(outputs["instances"].to("cpu"))
79
+ image = cv2.cvtColor(v.get_image()[:, :, :], cv2.COLOR_BGR2RGB)
80
+ st.image(image, caption='Segmented Panoramic Image Detecttree', channels ='RGB', use_column_width=True)
81
 
 
 
82
 
83
  model = main.deepforest()
84
  model.use_release()