python - Kaggle检测上的人脸级联功能不起作用
问题描述
我可以质疑为什么 Kaggle 中的 face haarcascade 不起作用吗?它显示图片,但不会覆盖表面上用于掩码检测的正方形。我正在从事一个项目,该项目涉及识别照片中戴或不戴口罩的人的表情。尽管如此,我经常遇到耳朵的问题。这些输入也可以在 Kaggle 的目录中找到。我是 kaggle 的新用户,最近才发现它,这就是我不熟悉该网站的原因。我也是 Python 新手,呵呵,我想知道它为什么不运行。我们感谢您的帮助!
import tensorflow as tf
import keras
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import load_img
import numpy as np
import argparse
import cv2
import os
import matplotlib.pyplot as plt
%matplotlib inline
model = tf.keras.models.load_model("../input/my-modelh5/my_model.h5")
images=['../input/example/example_01.png', '../input/example/example_02.png','../input/example/example_03.png' ]
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +'haarcascade_frontalface_default.xml')
img = images[0] # Add path here
img = plt.imread(img,format='8UC1')
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
gray = np.array(gray, dtype='uint8')
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
# Draw the rectangle around each face
for (x, y, w, h) in faces:
face = img[y:y+h, x:x+w]
face = cv2.resize(face, (224, 224))
face = img_to_array(face)
face = preprocess_input(face)
face = np.expand_dims(face, axis=0)
(mask, withoutMask) = model.predict(face)[0]
mask = mask*100
withoutMask = withoutMask*100
font = cv2.FONT_HERSHEY_SIMPLEX
# Getting Text Size in pixel
print("Image Width: " , w)
textSize = cv2.getTextSize(text="No Mask: " + str("%.2f" % round(mask, 2)), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, thickness=3)
print("Text Width: " , textSize[0][0])
if mask > withoutMask:
cv2.putText(img,
text = "Mask: " + str("%.2f" % round(mask, 2)),
org = (x-5,y-15),
fontFace=font,
fontScale = (2*w)/textSize[0][0],
color = (0, 255, 0),
thickness = 3,
lineType = cv2.LINE_AA)
cv2.rectangle(img, (x, y), (x+w, y+h), (0,255,0), 5)
else:
cv2.putText(img,
text = "No Mask: " + str("%.2f" % round(withoutMask, 2)),
org = (x-5,y-15),
fontFace=font,
fontScale = (1.8*w)/textSize[0][0],
color = (255, 0, 0),
thickness = 3,
lineType = cv2.LINE_AA)
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 5)
# Display
plt.imshow(img)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
解决方案
推荐阅读
- python - 在排序一个列表期间将不同列表中的相关元素保持在一起
- c# - 如何对通用列表对象进行排序
- javascript - 向和从 js 服务器发送数据的基本方法?
- json - 使用 Pyspark 处理 JSON 结构
- amazon-web-services - 为什么角色假设应该在 lambda 内部完成?
- html - 如何将数值绑定到 CSS 以创建时间线?
- docker - 如何为 docker compose 环境变量设置运行时变量
- javascript - 仅在 Javascript 中具有不同段落的动态 Div
- tsql - 表格模型中的测量列未在浏览器中显示正确的值
- javascript - 调用此函数时如何获取返回值?