해당 글은 제로베이스데이터스쿨 학습자료를 참고하여 작성되었습니다
net = cv2.dnn.readNet(model, config)
blob = cv2.dnn.blobFromImage(image, scalefactor, size, mean)
net.setInput(blob)
out = net.forward()
import sys
import numpy as np
import cv2
model = './data/opencv_face_detector/res10_300x300_ssd_iter_140000_fp16.caffemodel'
config = './data/opencv_face_detector/deploy.prototxt'
#model = 'opencv_face_detector/opencv_face_detector_uint8.pb'
#config = 'opencv_face_detector/opencv_face_detector.pbtxt'
# 카메라 열기
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print('Camera open failed!')
sys.exit()
# 모델 불러오기
net = cv2.dnn.readNet(model, config)
if net.empty():
print('Net open failed!')
sys.exit()
while True:
# 카메라 읽기
ret, frame = cap.read()
# 이미지 좌우 반전
frame = cv2.flip(frame, 1)
if not ret:
break
# 2차원 -> 4차원 이미지 생성
blob = cv2.dnn.blobFromImage(frame, 1, (300, 300), (104, 177, 123))
# 모델에 이미지 입력
net.setInput(blob)
# 모델 실행
out = net.forward()
detect = out[0, 0, :, :]
(h, w) = frame.shape[:2]
for i in range(detect.shape[0]):
confidence = detect[i, 2]
if confidence < 0.5: # 예측값이 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 = f'Face: {confidence:4.2f}'
cv2.putText(frame, label, (x1, y1 - 1), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 1, cv2.LINE_AA)
cv2.imshow('frame', frame)
if cv2.waitKey(1) == 27: # ESC누르면 종료
break
cap.release()
cv2.destroyAllWindows()
cv2.resize(src, dsize, dst=None, fx=None, y=None, interpolation=None) -> dst
import sys
import numpy as np
import cv2
model = './data/opencv_face_detector/res10_300x300_ssd_iter_140000_fp16.caffemodel'
config = './data/opencv_face_detector/deploy.prototxt'
#model = 'opencv_face_detector/opencv_face_detector_uint8.pb'
#config = 'opencv_face_detector/opencv_face_detector.pbtxt'
# 카메라 열기
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print('Camera open failed!')
sys.exit()
# 모델 불러오기
net = cv2.dnn.readNet(model, config)
if net.empty():
print('Net open failed!')
sys.exit()
while True:
# 카메라 읽기
ret, frame = cap.read()
# 이미지 좌우 반전
frame = cv2.flip(frame, 1)
if not ret:
break
# 2차원 -> 4차원 이미지 생성
blob = cv2.dnn.blobFromImage(frame, 1, (300, 300), (104, 177, 123))
# 모델에 이미지 입력
net.setInput(blob)
# 모델 실행
out = net.forward()
detect = out[0, 0, :, :]
(h, w) = frame.shape[:2]
for i in range(detect.shape[0]):
confidence = detect[i, 2]
if confidence < 0.5: # 예측값이 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)
face_img = frame[y1:y2, x1:x2]
fh, fw = face_img.shape[:2]
# 모자이크 처리
face_img2 = cv2.resize(face_img, (0, 0), fx=1./16, fy=1./16)
cv2.resize(face_img2, (fw, fh), dst=face_img, interpolation=cv2.INTER_NEAREST)
#frame[y1:y2, x1:x2] = cv2.resize(face_img2, (fw, fh), interpolation=cv2.INTER_NEAREST)
# 바운딩 박스 그리기
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0))
# # 예측값 출력
label = f'Face: {confidence:4.2f}'
cv2.putText(frame, label, (x1, y1 - 1), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 1, cv2.LINE_AA)
cv2.imshow('frame', frame)
if cv2.waitKey(1) == 27: # ESC누르면 종료
break
cap.release()
cv2.destroyAllWindows()