# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
#
# 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
#
# http://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.
"""Main script to run the object detection routine."""
import argparse
import sys
import time
import cv2
from tflite_support.task import core
from tflite_support.task import processor
from tflite_support.task import vision
import utils
def run(model: str, camera_id: int, width: int, height: int, num_threads: int,
enable_edgetpu: bool) -> None:
"""Continuously run inference on images acquired from the camera.
Args:
model: Name of the TFLite object detection model.
camera_id: The camera id to be passed to OpenCV.
width: The width of the frame captured from the camera.
height: The height of the frame captured from the camera.
num_threads: The number of CPU threads to run the model.
enable_edgetpu: True/False whether the model is a EdgeTPU model.
"""
# Variables to calculate FPS
counter, fps = 0, 0
start_time = time.time()
# Start capturing video input from the camera
cap = cv2.VideoCapture(camera_id)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
# Visualization parameters
row_size = 20 # pixels
left_margin = 24 # pixels
text_color = (0, 0, 255) # red
font_size = 1
font_thickness = 1
fps_avg_frame_count = 10
# Initialize the object detection model
base_options = core.BaseOptions(
file_name=model, use_coral=True, num_threads=num_threads)
detection_options = processor.DetectionOptions(
max_results=3, score_threshold=0.3)
options = vision.ObjectDetectorOptions(
base_options=base_options, detection_options=detection_options)
detector = vision.ObjectDetector.create_from_options(options)
# Continuously capture images from the camera and run inference
while cap.isOpened():
success, image = cap.read()
if not success:
sys.exit(
'ERROR: Unable to read from webcam. Please verify your webcam settings.'
)
counter += 1
image = cv2.flip(image, 1)
# Convert the image from BGR to RGB as required by the TFLite model.
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Create a TensorImage object from the RGB image.
input_tensor = vision.TensorImage.create_from_array(rgb_image)
# Run object detection estimation using the model.
detection_result = detector.detect(input_tensor)
# Draw keypoints and edges on input image
image = utils.visualize(image, detection_result)
# Calculate the FPS
if counter % fps_avg_frame_count == 0:
end_time = time.time()
fps = fps_avg_frame_count / (end_time - start_time)
start_time = time.time()
# Show the FPS
fps_text = 'FPS = {:.1f}'.format(fps)
text_location = (left_margin, row_size)
cv2.putText(image, fps_text, text_location, cv2.FONT_HERSHEY_PLAIN,
font_size, text_color, font_thickness)
# Stop the program if the ESC key is pressed.
if cv2.waitKey(1) == 27:
break
cv2.imshow('object_detector', image)
cap.release()
cv2.destroyAllWindows()
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'--model',
help='Path of the object detection model.',
required=False,
default='efficientdet_lite0.tflite')
parser.add_argument(
'--cameraId', help='Id of camera.', required=False, type=int, default=0)
parser.add_argument(
'--frameWidth',
help='Width of frame to capture from camera.',
required=False,
type=int,
default=640)
parser.add_argument(
'--frameHeight',
help='Height of frame to capture from camera.',
required=False,
type=int,
default=480)
parser.add_argument(
'--numThreads',
help='Number of CPU threads to run the model.',
required=False,
type=int,
default=4)
parser.add_argument(
'--enableEdgeTPU',
help='Whether to run the model on EdgeTPU.',
action='store_true',
required=False,
default=False)
args = parser.parse_args()
run(args.model, int(args.cameraId), args.frameWidth, args.frameHeight,
int(args.numThreads), bool(args.enableEdgeTPU))
if __name__ == '__main__':
main()
PROBLEMSTELLUNG & KI-LÖSUNG
DAS PROBLEM: MÄUSE IM HAUS
Mit dieser Katzenklappe präsentiere ich eine innovative Lösung für ein häufiges Problem: Mäuse, die durch eine Tür ins Haus gelangen.
Viele Katzenbesitzer kennen das Problem: Die Katze kommt von draußen zurück und hat eine Maus im Maul. Die Maus wird dann im Haus freigelassen oder gar verspeist, was zu unangenehmen Gerüchen und Hygieneproblemen führen kann.
DIE LÖSUNG: TÜR MIT KÜNSTLICHER INTELLIGENZ
Ich habe eine Tür mit künstlicher Intelligenz entwickelt, die mithilfe von TensorFlow trainiert wurde.
Das Besondere an dieser Tür ist, dass sie erkennt, ob die Katze eine Maus im Maul hat. Wenn dies der Fall ist, bleibt die Tür geschlossen und die Katze wird nicht ins Haus gelassen. Die Tür öffnet sich nur dann, wenn die Katze keine Maus bei sich trägt.
DIE TECHNIK & DAS ERGEBNIS
DIE TECHNIK: RASPBERRY PI, GOOGLE CORAL & 3D-DRUCK
Ich habe einen Raspberry Pi verwendet, um die Tür zu steuern, und für die Rechenleistung der künstlichen Intelligenz habe ich Google Coral genutzt.
Google Coral ist ein kleines Gerät, das speziell für maschinelles Lernen optimiert ist. Es kann TensorFlow-Modelle ausführen und Bilder in Echtzeit analysieren.
Außerdem habe ich die Tür mit einem 3D-Drucker hergestellt und zeige euch, wie das funktioniert.
SAUBERE & MÄUSEFREIE WOHNUNG
Schaut euch dieses innovative Projekt an, um mehr darüber zu erfahren! Ich zeige euch Schritt für Schritt, wie ihr eure eigene intelligente Katzenklappe bauen könnt.
Mit dieser Tür könnt ihr eure Wohnung vor unerwünschten Mäusen schützen und eurer Katze trotzdem den Zugang zum Freien ermöglichen.
CODE UND DOWNLOADS
Alle trainierten Modelle, Inferenz-Skripte, Quelltexte und das einsatzbereite Betriebssystem-Image.