https://gachonyws.github.io/project/mask-detection/
이분 블로그에 있는 코드를 코랩으로 사용해서 공부해보고있는데 마지막에 실시간으로 마스크를 썻는지 인식을 하던데
밑에 cv2.VideoCapture(0)로 바꿔주면 실시간영상으로 바뀌고 그걸 프레임단위로 읽을 수 있게 바꿔줘야 하는걸로 알고 있는데 실행이 안되더라구요
어떤 코드를 추가해줘야 실행이 가능할까요?
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input from tensorflow.keras.models import load_model import numpy as np import cv2 import matplotlib.pyplot as plt import os cap = cv2.VideoCapture('examples/03.mp4') ret, img = cap.read() fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') out = cv2.VideoWriter('./output.mp4', fourcc, cap.get(cv2.CAP_PROP_FPS), (img.shape[1], img.shape[0])) while cap.isOpened(): ret, img = cap.read() if not ret: break h, w = img.shape[:2] blob = cv2.dnn.blobFromImage(img, scalefactor=1., size=(300, 300), mean=(104., 177., 123.)) facenet.setInput(blob) dets = facenet.forward() result_img = img.copy() for i in range(dets.shape[2]): confidence = dets[0, 0, i, 2] if confidence < 0.5: continue x1 = int(dets[0, 0, i, 3] * w) y1 = int(dets[0, 0, i, 4] * h) x2 = int(dets[0, 0, i, 5] * w) y2 = int(dets[0, 0, i, 6] * h) face = img[y1:y2, x1:x2] face_input = cv2.resize(face, dsize=(224, 224)) face_input = cv2.cvtColor(face_input, cv2.COLOR_BGR2RGB) face_input = preprocess_input(face_input) face_input = np.expand_dims(face_input, axis=0) mask, nomask = model.predict(face_input).squeeze() if mask > nomask: color = (0, 255, 0) label = 'Mask %d%%' % (mask * 100) else: color = (0, 0, 255) label = 'No Mask %d%%' % (nomask * 100) cv2.rectangle(result_img, pt1=(x1, y1), pt2=(x2, y2), thickness=2, color=color, lineType=cv2.LINE_AA) cv2.putText(result_img, text=label, org=(x1, y1 - 10), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.8, color=color, thickness=2, lineType=cv2.LINE_AA) out.write(result_img) #cv2_imshow(result_img) # colab 환경에서 출력 때문에 계속 Busy 상태라 ignore if cv2.waitKey(1) == ord('q'): break out.release() cap.release()
댓글 0