์๋ ํ์ธ์. ์ค๋๋ง์ ๊ธ์ ์์ฑํฉ๋๋ค.
์ค๋์ ์์ ๋ง ๋๋ ค๋ดค๋ค๋ฉด, ์ค์ ๋ก ํ์ต๋ ๋ชจ๋ธ์ ์ด์ฉํ์ฌ ์ผ๊ตด์ ์ฐพ๋๋ก ํ๊ฒ ์ต๋๋ค.
๋ผ์ฆ๋ฒ ๋ฆฌํ์ด๋ก ์ผ๊ตด์ธ์์ ์ฐพ์๋ณด๋ฉด Haarcascade ์๊ณ ๋ฆฌ์ฆ์ด ๋ง์ด ์ฌ์ฉ๋ฉ๋๋ค.
ํ์ง๋ง ์ค๋์ ์ ๋์จ ์๊ณ ๋ฆฌ์ฆ์ด๊ณ ์ข ๋ ๋น ๋ฅด๊ณ ๋ ์ ํํ๊ฒ ์ธ๊ณต์ง๋ฅ์ผ๋ก ์ผ๊ตด ์ธ์์ ํ ์ ์์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ผ๊ตด์ธ์ ์์ฒด๋ฅผ Edge TPU๊ฐ ์ฒ๋ฆฌํ๊ธฐ ๋๋ฌธ์ ๋ผ์ฆ๋ฒ ๋ฆฌํ์ด์ CPU๋ ๊ฑฐ์ ์ฌ์ฉ๋์ง ์์ต๋๋ค.
๊ทธ๋ฌ๊ธฐ ๋๋ฌธ์ ๋ผ์ฆ๋ฒ ๋ฆฌํ์ด์์ ๋ถ๋ด ์์ด ๊ฐ๋ ฅํ ํจ๊ณผ๋ฅผ ๋ณด์ฌ์ค๋๋ค.
์๋ ๊ธ์ ์ฝ์ด๋ณด์๊ณ ๊ฐ์๋ ๊ฒ์ ์ถ์ฒ๋๋ฆฝ๋๋ค.
coding-yoon.tistory.com/87?category=866905
coding-yoon.tistory.com/88?category=866905
coding-yoon.tistory.com/91?category=866905
[Coral] TensorFlow Lite : Tflite ๋ชจ๋ธ ์์ฑ & Edge TPU๋ก Inferece๊น์ง ๊ฐ๋จํ๊ฒ ๊ฐ๋ฐํ๊ธฐ!
์๋ ํ์ธ์. ๋ง์ฝ, ์ฒ์ ์ด ๊ธ์ ๋ณด์ ๋ค๋ฉด ์๋ ๊ธ์ ๋จผ์ ์ฝ์ด๋ณด์๋ ๊ฒ์ ์ถ์ฒ๋๋ฆฝ๋๋ค. 1ํธ : Coral Board ๋์ ๋ผ์ฆ๋ฒ ๋ฆฌํ์ด๋ฅผ ์ฌ์ฉํ๋ ์ด์ : https://coding-yoon.tistory.com/85?category=866905 2ํธ :..
coding-yoon.tistory.com
์ฐ์ , coral์์ Face detection tflite ํ์ผ์ ๋จผ์ ๋ฐ๊ฒ ์ต๋๋ค.
Models | Coral
Download pre-compiled demo models that are compatible with the Edge TPU, or use the model source files to retrain the models for your own applications
coral.ai
์ ํ์ผ Edge TPU model์ ๋๋ฌ ํ์ผ์ ๋ฐ์ต๋๋ค.
๋คํธ์ํฌ๋ ๊ฒฝ๋ ๋ MobileNet V2์ ๊ฐ์ฒด์ธ์ SSD(Single Shot MultiBox Detection)์ผ๋ก ํ์ต๋ ๋ชจ๋ธ์ ๋๋ค.
๊ถ๊ธํ์๋ฉด ๋ ๋ ผ๋ฌธ์ ์ฐธ๊ณ ํ์๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค.
MobileNetV2: Inverted Residuals and Linear Bottlenecks
In this paper we describe a new mobile architecture, MobileNetV2, that improves the state of the art performance of mobile models on multiple tasks and benchmarks as well as across a spectrum of different model sizes. We also describe efficient ways of app
arxiv.org
SSD: Single Shot MultiBox Detector
We present a method for detecting objects in images using a single deep neural network. Our approach, named SSD, discretizes the output space of bounding boxes into a set of default boxes over different aspect ratios and scales per feature map location. At
arxiv.org
detect.py (google git์์ ๊ตฌํ ์ ์์)
# Lint as: python3
# Copyright 2019 Google LLC
#
# 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.
"""Functions to work with detection models."""
import collections
import numpy as np
Object = collections.namedtuple('Object', ['id', 'score', 'bbox'])
class BBox(collections.namedtuple('BBox', ['xmin', 'ymin', 'xmax', 'ymax'])):
"""Bounding box.
Represents a rectangle which sides are either vertical or horizontal, parallel
to the x or y axis.
"""
__slots__ = ()
@property
def width(self):
"""Returns bounding box width."""
return self.xmax - self.xmin
@property
def height(self):
"""Returns bounding box height."""
return self.ymax - self.ymin
@property
def area(self):
"""Returns bound box area."""
return self.width * self.height
@property
def valid(self):
"""Returns whether bounding box is valid or not.
Valid bounding box has xmin <= xmax and ymin <= ymax which is equivalent to
width >= 0 and height >= 0.
"""
return self.width >= 0 and self.height >= 0
def scale(self, sx, sy):
"""Returns scaled bounding box."""
return BBox(xmin=sx * self.xmin,
ymin=sy * self.ymin,
xmax=sx * self.xmax,
ymax=sy * self.ymax)
def translate(self, dx, dy):
"""Returns translated bounding box."""
return BBox(xmin=dx + self.xmin,
ymin=dy + self.ymin,
xmax=dx + self.xmax,
ymax=dy + self.ymax)
def map(self, f):
"""Returns bounding box modified by applying f for each coordinate."""
return BBox(xmin=f(self.xmin),
ymin=f(self.ymin),
xmax=f(self.xmax),
ymax=f(self.ymax))
@staticmethod
def intersect(a, b):
"""Returns the intersection of two bounding boxes (may be invalid)."""
return BBox(xmin=max(a.xmin, b.xmin),
ymin=max(a.ymin, b.ymin),
xmax=min(a.xmax, b.xmax),
ymax=min(a.ymax, b.ymax))
@staticmethod
def union(a, b):
"""Returns the union of two bounding boxes (always valid)."""
return BBox(xmin=min(a.xmin, b.xmin),
ymin=min(a.ymin, b.ymin),
xmax=max(a.xmax, b.xmax),
ymax=max(a.ymax, b.ymax))
@staticmethod
def iou(a, b):
"""Returns intersection-over-union value."""
intersection = BBox.intersect(a, b)
if not intersection.valid:
return 0.0
area = intersection.area
return area / (a.area + b.area - area)
def input_size(interpreter):
"""Returns input image size as (width, height) tuple."""
_, height, width, _ = interpreter.get_input_details()[0]['shape']
return width, height
def input_tensor(interpreter):
"""Returns input tensor view as numpy array of shape (height, width, 3)."""
tensor_index = interpreter.get_input_details()[0]['index']
return interpreter.tensor(tensor_index)()[0]
def set_input(interpreter, size, resize):
"""Copies a resized and properly zero-padded image to the input tensor.
Args:
interpreter: Interpreter object.
size: original image size as (width, height) tuple.
resize: a function that takes a (width, height) tuple, and returns an RGB
image resized to those dimensions.
Returns:
Actual resize ratio, which should be passed to `get_output` function.
"""
width, height = input_size(interpreter)
w, h = size
scale = min(width / w, height / h)
w, h = int(w * scale), int(h * scale)
tensor = input_tensor(interpreter)
tensor.fill(0) # padding
_, _, channel = tensor.shape
tensor[:h, :w] = np.reshape(resize((w, h)), (h, w, channel))
return scale, scale
def output_tensor(interpreter, i):
"""Returns output tensor view."""
tensor = interpreter.tensor(interpreter.get_output_details()[i]['index'])()
return np.squeeze(tensor)
def get_output(interpreter, score_threshold, image_scale=(1.0, 1.0)):
"""Returns list of detected objects."""
boxes = output_tensor(interpreter, 0)
class_ids = output_tensor(interpreter, 1)
scores = output_tensor(interpreter, 2)
count = int(output_tensor(interpreter, 3))
width, height = input_size(interpreter)
image_scale_x, image_scale_y = image_scale
sx, sy = width / image_scale_x, height / image_scale_y
def make(i):
ymin, xmin, ymax, xmax = boxes[i]
return Object(
id=int(class_ids[i]),
score=float(scores[i]),
bbox=BBox(xmin=xmin,
ymin=ymin,
xmax=xmax,
ymax=ymax).scale(sx, sy).map(int))
return [make(i) for i in range(count) if scores[i] >= score_threshold]
main.py
import detect
import tflite_runtime.interpreter as tflite
import time
from PIL import Image
from PIL import ImageDraw
import cv2
import numpy as np
import time
import os
# .tflite interpreter
interpreter = tflite.Interpreter(
os.path.join(os.getcwd(), "ssd_mobilenet_v2_face_quant_postprocess_edgetpu.tflite"),
experimental_delegates=[tflite.load_delegate('libedgetpu.so.1')]
)
interpreter.allocate_tensors()
# Draws the bounding box and label for each object.
def draw_objects(image, objs):
for obj in objs:
bbox = obj.bbox
cv2.rectangle(image,(bbox.xmin, bbox.ymin), (bbox.xmax, bbox.ymax), (0, 255, 0),2)
bbox_point_w = bbox.xmin + ((bbox.xmax-bbox.xmin) // 2)
bbox_point_h = bbox.ymin + ((bbox.ymax-bbox.ymin) // 2)
cv2.circle(image, (bbox_point_w, bbox.ymax-bbox.ymin), 5, (0,0,255),-1)
cv2.putText(image, text='%d%%' % (obj.score*100), org=(bbox.xmin, bbox.ymin), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1.0, color=(0, 255, 0), thickness=1, lineType=cv2.LINE_AA)
def main():
cap = cv2.VideoCapture(0)
while True:
ret, image = cap.read()
# image reshape
image = cv2.resize(image, dsize=(320, 320), interpolation=cv2.INTER_AREA)
# image BGR to RGB
image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
tensor = detect.input_tensor(interpreter=interpreter)[:, :] = image.copy()
tensor.fill(0) # padding
interpreter.invoke() # start
objs = detect.get_output(interpreter, 0.5, (1.0, 1.0))
if len(image):
draw_objects(image, objs)
image = cv2.cvtColor(image,cv2.COLOR_RGB2BGR)
cv2.imshow('face detector', image)
k = cv2.waitKey(30) & 0xff
if k == 27: # press 'ESC' to quit # ESC exit
break
if __name__ == '__main__':
main()
์ ์ฝ๋๋ฅผ ๋๋ ค๋ณด์๋ฉด, delay๊ฐ ์ ํ ์์ด ๋์ ์ฑ๋ฅ์ผ๋ก ์ผ๊ตด ์ธ์์ ํฉ๋๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ์ผ๊ตด ์ธ์ํ๊ณ ๋ค๋ฅธ ํ๋ก์ธ์ค๋ฅผ ์ถ๊ฐํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ์ผ๊ตด ์ธ์ํ๊ณ ๋ง์คํฌ๋ฅผ ๊ฒ์ถํ๋๋ผ๋
์ ํ delay๊ฐ ์์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ผ๊ตด์ธ์์ ํ๊ธฐ ์ , ์ ์ฒ๋ฆฌ๊ฐ ์์ต๋๋ค.
๋ฅ๋ฌ๋์ผ๋ก ๋ง๋ ๋ชจ๋ธ์ ๋๋ ค๋ณด๋ฉด, ์ค์ ๋ก ์ ์ฉ๋์ง ์๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค.
๋ง๋ค์ด์ง ๋ชจ๋ธ์ ์ฑ๋ฅ์ ๋์ด๊ธฐ ์ํด OpenCV์ ์ ์ฒ๋ฆฌ๋ฅผ ๋ง์ด ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค.
ํ์ง๋ง ์ด ๊ฐ์ ๊ฒฝ์ฐ๋ OpenCV๋ ์ฌ์ด์ฆ๋ฅผ ์ค์ด๊ฑฐ๋, ๋จ์ํ View ์ญํ ๋ง ํ๊ณ ๊ทธ ์ธ ์์ ์ ์ ํ ํ์ง ์์ต๋๋ค.
๋ฅ๋ฌ๋์ ๊ณต๋ถํ์ ๋ค๋ฉด, ์ค์ ๋ก ๋ชจ๋ธ์ ์ ์ฉํด๋ณด์๋ ๊ฒ๋ ๋ง์ ๊ณต๋ถ๋ฅผ ํ๋๋ฐ ๋์์ด ๋ ๊ฒ ๊ฐ์ต๋๋ค.