Created
January 29, 2020 15:30
-
-
Save emadehsan/4e779dc87817016dc37609c401d0fdeb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function takePhoto(quality) { | |
// create html elements | |
const div = document.createElement('div'); | |
const video = document.createElement('video'); | |
video.style.display = 'block'; | |
// request the stream. This will ask for Permission to access | |
// a connected Camera/Webcam | |
const stream = await navigator.mediaDevices.getUserMedia({video: true}); | |
// show the HTML elements | |
document.body.appendChild(div); | |
div.appendChild(video); | |
// display the stream | |
video.srcObject = stream; | |
await video.play(); | |
// Resize the output (of Colab Notebook Cell) to fit the video element. | |
google.colab.output | |
.setIfrmeHeight(document.documentElement.scrollHeight, true); | |
// capture 5 frames (for test) | |
for (let i = 0; i < 5; i++) { | |
const canvas = document.createElement('canvas'); | |
canvas.width = video.videoWidth; | |
canvas.height = video.videoHeight; | |
canvas.getContext('2d').drawImage(video, 0, 0); | |
img = canvas.toDataURL('image/jpeg', quality); | |
// Call a python function and send this image | |
google.colab.kernel.invokeFunction('notebook.run_algo', [img], {}); | |
// wait for X miliseconds second, before next capture | |
await new Promise(resolve => setTimeout(resolve, 250)); | |
} | |
stream.getVideoTracks()[0].stop(); // stop video stream | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create model object in inference mode. | |
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) | |
# Load weights trained on MS-COCO | |
model.load_weights(COCO_MODEL_PATH, by_name=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import random | |
import math | |
import numpy as np | |
import skimage.io | |
import matplotlib | |
import matplotlib.pyplot as plt | |
# Root directory of the project | |
ROOT_DIR = os.path.abspath("./Mask_RCNN/") | |
# Import Mask RCNN | |
sys.path.append(ROOT_DIR) # To find local version of the library | |
from mrcnn import utils | |
import mrcnn.model as modellib | |
from mrcnn import visualize | |
# Import COCO config | |
sys.path.append(os.path.join(ROOT_DIR, "samples/coco/")) # find local version | |
import coco | |
%matplotlib inline | |
# Directory to save logs and trained model | |
MODEL_DIR = os.path.join(ROOT_DIR, "logs") | |
# Local path to trained weights file | |
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5") | |
# Download COCO trained weights from Releases if needed | |
if not os.path.exists(COCO_MODEL_PATH): | |
utils.download_trained_weights(COCO_MODEL_PATH) | |
# Directory of images to run detection on | |
IMAGE_DIR = os.path.join(ROOT_DIR, "images") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Processing 1 images | |
image shape: (425, 640, 3) min: 0.00000 max: 255.00000 uint8 | |
molded_images shape: (1, 1024, 1024, 3) min: -123.70000 max: 151.10000 float64 | |
image_metas shape: (1, 93) min: 0.00000 max: 1024.00000 float64 | |
anchors shape: (1, 261888, 4) min: -0.35390 max: 1.29134 float32 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# register this function, so JS code could call this | |
output.register_callback('notebook.run_algo', run_algo) | |
# put the JS code in cell and run it | |
take_photo() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load a random image from the images folder | |
file_names = next(os.walk(IMAGE_DIR))[2] | |
image = skimage.io.imread(os.path.join(IMAGE_DIR, random.choice(file_names))) | |
# 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']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from IPython.display import display, Javascript | |
from google.colab.output import eval_js | |
from base64 import b64decode | |
def take_photo(filename='photo.jpg', quality=0.8): | |
js = Javascript(''' | |
// ... | |
// JavaScript code here <<== | |
// ... | |
''') | |
# make the provided HTML, part of the cell | |
display(js) | |
# call the takePhoto() JavaScript function | |
data = eval_js('takePhoto({})'.format(quality)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment