Instantly share code, notes, and snippets.

Embed
What would you like to do?
Performance Analysis of Bytes IO vs File IO
"""
Usage:
>>> ipython
>>> %load perf_bytes_io.py
>>> <ENTER>
"""
import logging
import tempfile
import cv2
import requests
url = 'https://chaudhary.page.link/test-zomato-img' # A jpeg image
response_object = requests.get(url)
def log(s):
# print(s)
pass
def file_io():
import logging
import os
import tempfile
import cv2
import requests
def download_image(url):
#logging.info('Downloading image from url: %s', url[:100])
#response_object = requests.get(url)
file_descriptor, filename = tempfile.mkstemp(prefix='image-', suffix='.jpg')
logging.info('Saving file: %s', filename)
with open(file_descriptor, mode='wb') as f:
f.write(response_object.content)
return filename
url = 'https://chaudhary.page.link/test-zomato-img'
image_path = download_image(url)
img = cv2.imread(image_path)
resized_img = cv2.resize(img, (299, 299))
# preprocess(resized_image)
# prediction_score = model.predict(resized_img)
os.remove(image_path)
log(resized_img.shape)
def bytes_io():
from io import BytesIO
import cv2
import numpy as np
import requests
url = 'https://chaudhary.page.link/test-zomato-img'
#response_object = requests.get(url)
image_data = BytesIO(response_object.content)
file_bytes = np.asarray(bytearray(image_data.read()), dtype=np.uint8)
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
image_data.close()
resized_img = cv2.resize(img, (299, 299))
# preprocess(resized_image)
# prediction_score = model.predict(resized_img)
log(resized_img.shape)
def direct_decode():
import cv2
import numpy as np
import requests
url = 'https://chaudhary.page.link/test-zomato-img'
#response_object = requests.get(url)
file_bytes = np.asarray(bytearray(response_object.content), dtype=np.uint8)
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
resized_img = cv2.resize(img, (299, 299))
# preprocess(resized_image)
# prediction_score = model.predict(resized_img)
log(resized_img.shape)
%timeit file_io()
%timeit bytes_io()
%timeit direct_decode()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment