Opencv Deep learning
# Opencv Deep learning Tutorial
# https://github.com/opencv/opencv/wiki/Deep-Learning-in-OpenCV
# Caffe Model Zoo : github.com/BVLC/caffe
## 모델 파일 : dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel
## 설정 파일 : github.com/BVLC/caffe/blob/master/models/bvlc_googlenet/deploy.prototxt
# ONNX Model Zoo : github.com/onnx/models
# 모델파일: https://github.com/onnx/models/tree/master/vision/classification/inception_and_googlenet/googlenet
# 클래스 이름 파일 : github.com/opencv/opencv/blob/4.1.0/samples/data/dnn/
# readNet(model, config)
# model, config, dataset
# https://cocodataset.org/#home
# 실행순서
# cv2.dnn.readNet(model, config)-> ret, 객체생성
# blobFromImage(image, scalefactor, size, mean, swapRB, crop) -> retval
# scalefactor: Multiply by factor
# image has BGR ordering and swapRB is true.
##############################################################################################
import numpy as np
import cv2
import sys
import os
import glob
img = cv2.imread('./opencv_face_detector/fig/sunglass.png')
## Tensoflow
model='./face_detector/opencv_face_detector_uint8.pb'
config = './face_detector/opencv_face_detector.pbtxt.txt'
# cap = cv2.VideoCapture(0) #카메라일때
cap = cv2.VideoCapture('/Users/winviva/Downloads/movie/인질/인질 Hostage Missing Celebrity.2021.1080p.FHDRip.H264.AAC-NonDRM.mp4')
if not cap.isOpened():
print('vid faile')
sys.exit()
face_net = cv2.dnn.readNet(model, config)
if face_net.empty():
print('net fail')
sys.exit()
while True:
ret, img = cap.read()
if not ret:
print('vid fail')
break
##################
blob = cv2.dnn.blobFromImage(img,1,(300,300),(104, 117, 123),swapRB=False)
face_net.setInput(blob)
out = face_net.forward()
# print(out.shape)
detect = out[0,0,:,:]
# print(detect.shape)
h, w = img.shape[:2]
threshold = 0.5
for i in range(detect.shape[0]):
confidence = detect[i,2]
if confidence > 0.5:
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(img, (x1,y1),(x2,y2),(0,0,255),2)
# text = f'Face: {confidence*100:4.2f}%'
text = 'Face: {}%'.format(round(confidence*100,2))
cv2.putText(img, text, (x1,y1-3), cv2.FONT_HERSHEY_COMPLEX,1,(255,255,0), 1, cv2.LINE_AA)
cv2.imshow('image', img)
if cv2.waitKey(20)== 27:
break
cap.release()
cv2.waitKey(1)
cv2.destroyAllWindows()
cv2.waitKey(1)