Skip to content

Instantly share code, notes, and snippets.

View AyaanZaveri's full-sized avatar
🚀

Ayaan AyaanZaveri

🚀
  • Toronto, Ontario
  • 08:44 (UTC -05:00)
View GitHub Profile
#TensorFlow 2.5
#Takes about a minute
!pip install -U tensorflow>=2.5
#Import
import os
import pathlib
import matplotlib
import matplotlib.pyplot as plt
# @title Load Models
def load_image_into_numpy_array(path):
"""Load an image from file into a numpy array.
Puts image into numpy array to feed into tensorflow graph.
Note that by convention we put it into a numpy array with shape
(height, width, channels), where channels=3 for RGB.
Args:
#Mount Drive
from google.colab import drive
drive.mount('/content/drive')
#Clone The Files For The Model
!git clone --depth 1 https://github.com/AyaanZaveri/models
#Use Proxima Nova Font
!mv "/content/models/research/object_detection/Proxima Nova Bold.ttf" /usr/share/fonts/truetype
#Install Packages
%%bash
sudo apt install -y protobuf-compiler
cd models/research/
protoc object_detection/protos/*.proto --python_out=.
cp object_detection/packages/tf2/setup.py .
python -m pip install .
#@title Model Selection { display-mode: "form", run: "auto" }
model_display_name = 'EfficientDet D4 1024x1024' # @param ['CenterNet HourGlass104 512x512','CenterNet HourGlass104 Keypoints 512x512','CenterNet HourGlass104 1024x1024','CenterNet HourGlass104 Keypoints 1024x1024','CenterNet Resnet50 V1 FPN 512x512','CenterNet Resnet50 V1 FPN Keypoints 512x512','CenterNet Resnet101 V1 FPN 512x512','CenterNet Resnet50 V2 512x512','CenterNet Resnet50 V2 Keypoints 512x512','EfficientDet D0 512x512','EfficientDet D1 640x640','EfficientDet D2 768x768','EfficientDet D3 896x896','EfficientDet D4 1024x1024','EfficientDet D5 1280x1280','EfficientDet D6 1280x1280','EfficientDet D7 1536x1536','SSD MobileNet v2 320x320','SSD MobileNet V1 FPN 640x640','SSD MobileNet V2 FPNLite 320x320','SSD MobileNet V2 FPNLite 640x640','SSD ResNet50 V1 FPN 640x640 (RetinaNet50)','SSD ResNet50 V1 FPN 1024x1024 (RetinaNet50)','SSD ResNet101 V1 FPN 640x640 (RetinaNet101)','SSD ResNet101 V1 FPN 1024x1024 (RetinaNet101)','SSD ResNet152 V1 FPN 640x
print('Loading Model...')
hub_model = hub.load(model_handle)
print('Model Loaded!')
#Download The Image
!wget -O myfile.jpg.tmp "https://images.unsplash.com/photo-1595801185745-c35f30d63934?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2250&q=80" && mv myfile.jpg{.tmp,} && mv /content/myfile.jpg "/content/"
#Set Image Path
image_path = "/content/myfile.jpg"
image_np = load_image_into_numpy_array(image_path)
#Show Image
plt.figure(figsize=(24,32))
plt.imshow(image_np[0])
#Detection
results = hub_model(image_np)
result = {key:value.numpy() for key,value in results.items()}
label_id_offset = 0
image_np_with_detections = image_np.copy()
# Use keypoints if available in detections
keypoints, keypoint_scores = None, None
if 'detection_keypoints' in result:
keypoints = result['detection_keypoints'][0]
keypoint_scores = result['detection_keypoint_scores'][0]
viz_utils.visualize_boxes_and_labels_on_image_array(