Skip to content

Commit 9ad3ffe

Browse files
author
Tyler Odenthal
committed
Solved image_dims issue in object_detection.py
Was able to pass variables from object_detection.py instead of generating in prediction.py
1 parent 75ac51b commit 9ad3ffe

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

roboflow/models/object_detection.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def predict(
146146
if not hosted:
147147
if type(image_path) is str:
148148
image = Image.open(image_path).convert("RGB")
149+
dimensions = image.size
149150
# Create buffer
150151
buffered = io.BytesIO()
151152
image.save(buffered, format="PNG")
@@ -158,9 +159,11 @@ def predict(
158159
data=img_str,
159160
headers={"Content-Type": "application/x-www-form-urlencoded"},
160161
)
162+
image_dims = {"width": str(dimensions[0]), "height": str(dimensions[1])}
161163
elif isinstance(image_path, np.ndarray):
162164
# Performing inference on a OpenCV2 frame
163165
retval, buffer = cv2.imencode(".jpg", image_path)
166+
dimensions = buffer.shape
164167
img_str = base64.b64encode(buffer)
165168
# print(img_str)
166169
img_str = img_str.decode("ascii")
@@ -169,11 +172,13 @@ def predict(
169172
data=img_str,
170173
headers={"Content-Type": "application/x-www-form-urlencoded"},
171174
)
175+
image_dims = {"width": str(dimensions[0]), "height": str(dimensions[1])}
172176
else:
173177
raise ValueError("image_path must be a string or a numpy array.")
174178
else:
175179
# Create API URL for hosted image (slightly different)
176180
self.api_url += "&image=" + urllib.parse.quote_plus(image_path)
181+
image_dims = {"width": "0", "height": "0"}
177182
# POST to the API
178183
resp = requests.get(self.api_url)
179184

@@ -184,6 +189,7 @@ def predict(
184189
resp.json(),
185190
image_path=image_path,
186191
prediction_type=OBJECT_DETECTION_MODEL,
192+
image_dims=image_dims,
187193
)
188194
# Returns base64 encoded Data
189195
elif self.format == "image":

roboflow/util/prediction.py

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -473,53 +473,34 @@ def json(self):
473473
return prediction_group_json
474474

475475
@staticmethod
476-
def create_prediction_group(json_response, image_path, prediction_type):
476+
def create_prediction_group(json_response, image_path, prediction_type, image_dims):
477477
"""
478478
Method to create a prediction group based on the JSON Response
479479
480480
:param prediction_type:
481481
:param json_response: Based on Roboflow JSON Response from Inference API
482482
:param model:
483483
:param image_path:
484+
:param image_dims:
484485
:return:
485486
"""
486487
prediction_list = []
487488

488-
# LOADING PHOTO TAKES TIME - PASS VARIABLES IF POSSIBLE
489-
image_loaded = Image.open(image_path)
490-
dimensions = image_loaded.size
491-
# LOADING PHOTO TAKES TIME - PASS VARIABLES IF POSSIBLE
492-
493489
if prediction_type in [OBJECT_DETECTION_MODEL, INSTANCE_SEGMENTATION_MODEL]:
494490
for prediction in json_response["predictions"]:
495491
prediction = Prediction(
496492
prediction, image_path, prediction_type=prediction_type
497493
)
498494
prediction_list.append(prediction)
499-
if "image" not in json_response:
500-
json_response["image"] = {
501-
"width": dimensions[0],
502-
"height": dimensions[1],
503-
}
504-
img_dims = json_response["image"]
495+
img_dims = image_dims
505496
elif prediction_type == CLASSIFICATION_MODEL:
506497
prediction = Prediction(json_response, image_path, prediction_type)
507498
prediction_list.append(prediction)
508-
if "image" not in json_response:
509-
json_response["image"] = {
510-
"width": dimensions[0],
511-
"height": dimensions[1],
512-
}
513-
img_dims = json_response["image"]
499+
img_dims = image_dims
514500
elif prediction_type == SEMANTIC_SEGMENTATION_MODEL:
515501
prediction = Prediction(json_response, image_path, prediction_type)
516502
prediction_list.append(prediction)
517-
if "image" not in json_response:
518-
json_response["image"] = {
519-
"width": dimensions[0],
520-
"height": dimensions[1],
521-
}
522-
img_dims = json_response["image"]
503+
img_dims = image_dims
523504

524505
# Seperate list and return as a prediction group
525506
return PredictionGroup(img_dims, image_path, *prediction_list)

0 commit comments

Comments
 (0)