Created
January 31, 2020 15:04
-
-
Save Namburger/9a4fb9b7c092541654a38f4dc7de067f to your computer and use it in GitHub Desktop.
Doing object detection with 2 cameras with the Coral Dev Board
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
# Copyright 2019 Google LLC | |
# | |
# Changes made by Nam Vu 01/20/2020 | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# https://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
r"""A demo for object detection. | |
Model and Label File taken from here: | |
https://github.com/google-coral/edgetpu/tree/master/test_data | |
$ wget https://github.com/google-coral/edgetpu/raw/master/test_data/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite | |
$ curl https://raw.githubusercontent.com/google-coral/edgetpu/master/test_data/coco_labels.txt > coco_labels.txt | |
$ python3 object_detection.py --model mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite --label coco_labels.txt | |
""" | |
import argparse | |
import platform | |
import subprocess | |
from edgetpu.detection.engine import DetectionEngine | |
from edgetpu.utils import dataset_utils | |
import cv2 | |
import numpy as np | |
from PIL import Image | |
from PIL import ImageDraw | |
def do_detection(engine, img, k=3): | |
# Run inference. | |
ans = engine.detect_with_image( | |
img, | |
threshold=0.05, | |
keep_aspect_ratio=False, | |
relative_coord=False, | |
top_k=k) | |
return ans | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
'--model', | |
help='Path of the detection model, it must be a SSD model with postprocessing operator.', | |
required=True) | |
parser.add_argument('--label', help='Path of the labels file.') | |
parser.add_argument('--top_k', help='Top number to results to return.') | |
args = parser.parse_args() | |
# Initialize engine. | |
engine = DetectionEngine(args.model) | |
labels = dataset_utils.read_label_file(args.label) if args.label else None | |
top_k = args.top_k if args.top_k else 3 | |
# capturing videos with cv2 | |
cap = cv2.VideoCapture(0) | |
cap2 = cv2.VideoCapture(1) | |
# looping through all frames | |
frame_num = 0 | |
while True: | |
print('<<<----------------------------------------->>>') | |
print('Frame number: ', frame_num) | |
ret, cv2img = cap.read() | |
ret2, cv2img2 = cap2.read() | |
cv2img = cv2.cvtColor(cv2img, cv2.COLOR_BGR2RGB) | |
cv2img2 = cv2.cvtColor(cv2img2, cv2.COLOR_BGR2RGB) | |
img = Image.fromarray(cv2img) | |
img2 = Image.fromarray(cv2img2) | |
draw = ImageDraw.Draw(img) | |
draw2 = ImageDraw.Draw(img2) | |
ans = do_detection(engine, img) | |
ans2 = do_detection(engine, img2) | |
# results from cam1 | |
if ans: | |
print('----------------CAM1-------------------') | |
for obj in ans: | |
if labels: | |
print(labels[obj.label_id]) | |
print('score = ', obj.score) | |
box = obj.bounding_box.flatten().tolist() | |
print('box=', box) | |
draw.rectangle(box, outline='red') | |
else: | |
print('No object detected!') | |
# results from cam2 | |
if ans2: | |
print('----------------CAM2-------------------') | |
for obj in ans2: | |
if labels: | |
print(labels[obj.label_id]) | |
print('score = ', obj.score) | |
box = obj.bounding_box.flatten().tolist() | |
print('box=', box) | |
draw2.rectangle(box, outline='red') | |
else: | |
print('No object detected!') | |
new_cv2img = np.array(img.convert('RGB')) | |
new_cv2img = new_cv2img[:, :, ::-1].copy() # RGB to BGR | |
cv2.imshow('Object Detection from CAM1', new_cv2img) | |
new_cv2img2 = np.array(img2.convert('RGB')) | |
new_cv2img2 = new_cv2img2[:, :, ::-1].copy() # RGB to BGR | |
cv2.imshow('Object Detection from CAM2', new_cv2img2) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
frame_num += 1 | |
print('\n') | |
cap.release() | |
cap2.release() | |
cv2.destroyAllWindows() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment