Skip to content

Instantly share code, notes, and snippets.

@emadehsan
Created January 29, 2020 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emadehsan/32d1c40ae16aaf206e65619b7813e5af to your computer and use it in GitHub Desktop.
Save emadehsan/32d1c40ae16aaf206e65619b7813e5af to your computer and use it in GitHub Desktop.
import IPython
import time
import sys
import numpy as np
import cv2
import base64
import logging
from google.colab import output
from PIL import Image
from io import BytesIO
def data_uri_to_img(uri):
"""convert base64 image to numpy array"""
try:
image = base64.b64decode(uri.split(',')[1], validate=True)
# make the binary image, a PIL image
image = Image.open(BytesIO(image))
# convert to numpy array
image = np.array(image, dtype=np.uint8);
return image
except Exception as e:
logging.exception(e);print('\n')
return None
def run_algo(imgB64):
"""
in Colab, run_algo function gets invoked by the JavaScript,
that sends N images every second, one at a time.
params:
image: image
"""
image = data_uri_to_img(imgB64)
if image is None:
print("At run_algo(): image is None.")
return
try:
# Run detection
results = model.detect([image], verbose=1)
# Visualize results
r = results[0]
visualize.display_instances(
image,
r['rois'],
r['masks'],
r['class_ids'],
class_names,
r['scores']
)
except Exception as e:
logging.exception(e)
print('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment