2000년대부터 사용되고 있는 심층 신경망의 또 다른 이름
Deep learning is part of a broader family of machine learning methods
based on artificial neural networks. - WikiPedia
cv2.dnn.readNet(model, config=None, framework=None) -> retval
네트워크 입력 블롭(blob) 만들기
cv2.dnn.blobFromImage(image, scalefactor=None, size=None,
mean=None, swapRB=None, crop=None,
ddepth=None) -> retval
네트워크 입력 설정하기
cv2.dnn_Net.setInput(blob, name=None, scalefactor=None, mean=None)
네트워크 순방향 실행 (추론)
cv2.dnn_Net.forward(outputName=None) -> retval
import sys
import numpy as np
import cv2
img = cv2.imread('beagle.jpg')
if img is None:
print('Image load failed!')
exit()
# Load network
net = cv2.dnn.readNet('bvlc_googlenet.caffemodel', 'deploy.prototxt')
if net.empty():
print('Network load failed!')
exit()
# Load class names
classNames = None
with open('classification_classes_ILSVRC2012.txt', 'rt') as f:
classNames = f.read().rstrip('\n').split('\n')
# Inference
inputBlob = cv2.dnn.blobFromImage(img, 1, (224, 224), (104, 117, 123))
net.setInput(inputBlob, 'data')
prob = net.forward()
# Check results & Display
out = prob.flatten()
classId = np.argmax(out)
confidence = out[classId]
text = '%s (%4.2f%%)' % (classNames[classId], confidence * 100)
cv2.putText(img, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 1, cv2.LINE_AA)
cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()
import cv2
model = 'res10_300x300_ssd_iter_140000_fp16.caffemodel'
config = 'deploy.prototxt'
#model = 'opencv_face_detector_uint8.pb'
#config = 'opencv_face_detector.pbtxt'
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print('Camera open failed!')
exit()
net = cv2.dnn.readNet(model, config)
if net.empty():
print('Net open failed!')
exit()
# while True:
# _, frame = cap.read()
# if frame is None:
# break
frame = cv2.imread('mask.jpg')
if frame is None:
print('Image load failed!')
exit()
blob = cv2.dnn.blobFromImage(frame, 1, (300, 300), (104, 177, 123))
net.setInput(blob)
detect = net.forward()
detect = detect[0, 0, :, :]
(h, w) = frame.shape[:2]
for i in range(detect.shape[0]):
confidence = detect[i, 2]
if confidence < 0.5:
break
x1 = int(detect[i, 3] * w)
y1 = int(detect[i, 4] * h)
x2 = int(detect[i, 5] * w)
y2 = int(detect[i, 6] * h)
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0))
label = 'Face: %4.3f' % confidence
cv2.putText(frame, label, (x1, y1 - 1), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 1, cv2.LINE_AA)
cv2.imshow('frame', frame)
cv2.waitKey()
cv2.destroyAllWindows()