Skip to content

Commit 9e2b77e

Browse files
author
Tyler Odenthal
committed
Fixed test cases
Ran python -m unittest a lot.
1 parent 9ad3ffe commit 9e2b77e

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

roboflow/models/inference.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,22 @@ def __get_image_params(self, image_path):
3434
hosted_image = urllib.parse.urlparse(image_path).scheme in ("http", "https")
3535

3636
if hosted_image:
37-
return {"image": image_path}, {}
37+
image_dims = {"width": "Undefined", "height": "Undefined"}
38+
return {"image": image_path}, {}, image_dims
3839

3940
image = Image.open(image_path)
41+
dimensions = image.size
42+
image_dims = {"width": str(dimensions[0]), "height": str(dimensions[1])}
4043
buffered = io.BytesIO()
4144
image.save(buffered, quality=90, format="JPEG")
4245
data = MultipartEncoder(
4346
fields={"file": ("imageToUpload", buffered.getvalue(), "image/jpeg")}
4447
)
45-
return {}, {"data": data, "headers": {"Content-Type": data.content_type}}
48+
return (
49+
{},
50+
{"data": data, "headers": {"Content-Type": data.content_type}},
51+
image_dims,
52+
)
4653

4754
def predict(self, image_path, prediction_type=None, **kwargs):
4855
"""
@@ -54,7 +61,7 @@ def predict(self, image_path, prediction_type=None, **kwargs):
5461
:return: PredictionGroup - a group of predictions based on Roboflow JSON response
5562
:raises Exception: Image path is not valid
5663
"""
57-
params, request_kwargs = self.__get_image_params(image_path)
64+
params, request_kwargs, image_dims = self.__get_image_params(image_path)
5865

5966
params["api_key"] = self.__api_key
6067

@@ -65,5 +72,8 @@ def predict(self, image_path, prediction_type=None, **kwargs):
6572
response.raise_for_status()
6673

6774
return PredictionGroup.create_prediction_group(
68-
response.json(), image_path=image_path, prediction_type=prediction_type
75+
response.json(),
76+
image_path=image_path,
77+
prediction_type=prediction_type,
78+
image_dims=image_dims,
6979
)

roboflow/models/object_detection.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,17 @@ def predict(
163163
elif isinstance(image_path, np.ndarray):
164164
# Performing inference on a OpenCV2 frame
165165
retval, buffer = cv2.imencode(".jpg", image_path)
166+
# Currently cv2.imencode does not properly return shape
166167
dimensions = buffer.shape
167168
img_str = base64.b64encode(buffer)
168-
# print(img_str)
169169
img_str = img_str.decode("ascii")
170170
resp = requests.post(
171171
self.api_url,
172172
data=img_str,
173173
headers={"Content-Type": "application/x-www-form-urlencoded"},
174174
)
175-
image_dims = {"width": str(dimensions[0]), "height": str(dimensions[1])}
175+
# Replace with dimensions variable once cv2.imencode shape solution is found
176+
image_dims = {"width": "0", "height": "0"}
176177
else:
177178
raise ValueError("image_path must be a string or a numpy array.")
178179
else:

0 commit comments

Comments
 (0)