44import urllib .request
55import warnings
66
7- import cv2
8- import matplotlib .image as mpimg
9- import matplotlib .pyplot as plt
10- import numpy as np
117import requests
12- from matplotlib import patches
138from PIL import Image
149
1510from roboflow .config import (
@@ -29,6 +24,8 @@ def plot_image(image_path):
2924 :param image_path: path of image to be plotted (can be hosted or local)
3025 :return:
3126 """
27+ import matplotlib .pyplot as plt
28+
3229 validate_image_path (image_path )
3330 try :
3431 img = Image .open (image_path )
@@ -52,6 +49,8 @@ def plot_annotation(axes, prediction=None, stroke=1, transparency=60, colors=Non
5249 :param transparency: alpha transparency of masks for semantic overlays
5350 :return:
5451 """
52+ from matplotlib import patches
53+
5554 # Object Detection annotation
5655
5756 colors = {} if colors is None else colors
@@ -88,6 +87,8 @@ def plot_annotation(axes, prediction=None, stroke=1, transparency=60, colors=Non
8887 polygon = patches .Polygon (points , linewidth = stroke , edgecolor = stroke_color , facecolor = "none" )
8988 axes .add_patch (polygon )
9089 elif prediction ["prediction_type" ] == SEMANTIC_SEGMENTATION_MODEL :
90+ import matplotlib .image as mpimg
91+
9192 encoded_mask = prediction ["segmentation_mask" ]
9293 mask_bytes = io .BytesIO (base64 .b64decode (encoded_mask ))
9394 mask = mpimg .imread (mask_bytes , format = "JPG" )
@@ -121,6 +122,9 @@ def json(self):
121122 return self .json_prediction
122123
123124 def __load_image (self ):
125+ import cv2
126+ import numpy as np
127+
124128 if "http://" in self .image_path :
125129 req = urllib .request .urlopen (self .image_path )
126130 arr = np .asarray (bytearray (req .read ()), dtype = np .uint8 )
@@ -131,6 +135,8 @@ def __load_image(self):
131135 return cv2 .imread (self .image_path )
132136
133137 def plot (self , stroke = 1 ):
138+ import matplotlib .pyplot as plt
139+
134140 # Exception to check if image path exists
135141 validate_image_path (self ["image_path" ])
136142 _ , axes = plot_image (self ["image_path" ])
@@ -146,6 +152,9 @@ def save(self, output_path="predictions.jpg", stroke=2, transparency=60):
146152 :param stroke: line width to use when drawing rectangles and polygons
147153 :param transparency: alpha transparency of masks for semantic overlays
148154 """
155+ import cv2
156+ import numpy as np
157+
149158 image = self .__load_image ()
150159 stroke_color = (255 , 0 , 0 )
151160
@@ -302,6 +311,8 @@ def add_prediction(self, prediction=None):
302311 self .predictions .append (prediction )
303312
304313 def plot (self , stroke = 1 ):
314+ import matplotlib .pyplot as plt
315+
305316 if len (self ) > 0 :
306317 validate_image_path (self .base_image_path )
307318 _ , axes = plot_image (self .base_image_path )
@@ -311,6 +322,9 @@ def plot(self, stroke=1):
311322 plt .show ()
312323
313324 def __load_image (self ):
325+ import cv2
326+ import numpy as np
327+
314328 # Check if it is a hosted image and open image as needed
315329 if "http://" in self .base_image_path or "https://" in self .base_image_path :
316330 req = urllib .request .urlopen (self .base_image_path )
@@ -322,6 +336,9 @@ def __load_image(self):
322336 return cv2 .imread (self .base_image_path )
323337
324338 def save (self , output_path = "predictions.jpg" , stroke = 2 ):
339+ import cv2
340+ import numpy as np
341+
325342 # Load image based on image path as an array
326343 image = self .__load_image ()
327344 stroke_color = (255 , 0 , 0 )
0 commit comments