gpt4 book ai didi

python - OpenCV + Raspberry PI 就像一个房间监视器

转载 作者:太空宇宙 更新时间:2023-11-03 23:14:05 24 4
gpt4 key购买 nike

我是 opencv 和 python 的新手,我的项目是关于智能家居的。

我设法在 raspberrypi 上安装了 opencv 并使用了网络摄像头。

我的程序将在以下三种情况下运行。1.有人进房间,检测人脸和人,发送消息“爸爸在1号房间”。2.有人进入房间,检测到人脸而不是人,发送消息“未知人在房间1”3.房间没人,发消息“1号房没人”

场景 1 和 2 我知道如何解决它们,但我卡在 esceanrio 3 中。我试图将检测到的人的名字保存在一个变量中,如果这是空的应该发送消息,但是它对我不起作用。

我使用的代码如下,我遇到的问题在代码的末尾:

import cv2, sys, numpy, os
size = 1
fn_haar = 'haarcascade_frontalface_default.xml'
fn_dir = 'att_faces'

# Part 1: Create fisherRecognizer
print('Training...')

# Create a list of images and a list of corresponding names
(images, lables, names, id) = ([], [], {}, 0)

# Get the folders containing the training data
for (subdirs, dirs, files) in os.walk(fn_dir):

# Loop through each folder named after the subject in the photos
for subdir in dirs:
names[id] = subdir
subjectpath = os.path.join(fn_dir, subdir)

# Loop through each photo in the folder
for filename in os.listdir(subjectpath):

# Skip non-image formates
f_name, f_extension = os.path.splitext(filename)
if(f_extension.lower() not in
['.png','.jpg','.jpeg','.gif','.pgm']):
print("Skipping "+filename+", wrong file type")
continue
path = subjectpath + '/' + filename
lable = id

# Add to training data
images.append(cv2.imread(path, 0))
lables.append(int(lable))
id += 1
(im_width, im_height) = (112, 92)

# Create a Numpy array from the two lists above
(images, lables) = [numpy.array(lis) for lis in [images, lables]]

# OpenCV trains a model from the images
model = cv2.face.createFisherFaceRecognizer()
model.train(images, lables)




# Part 2: Use fisherRecognizer on camera stream
haar_cascade = cv2.CascadeClassifier(fn_haar)
webcam = cv2.VideoCapture(0)
while True:

# Loop until the camera is working
rval = False
while(not rval):
# Put the image from the webcam into 'frame'
(rval, frame) = webcam.read()
if(not rval):
print("Failed to open webcam. Trying again...")

# Flip the image (optional)
frame=cv2.flip(frame,1,0)

# Convert to grayscalel
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Resize to speed up detection (optinal, change size above)
mini = cv2.resize(gray, (int(gray.shape[1] / size), int(gray.shape[0] / size)))

# Detect faces and loop through each one
faces = haar_cascade.detectMultiScale(mini)
for i in range(len(faces)):
face_i = faces[i]

# Coordinates of face after scaling back by `size`
(x, y, w, h) = [v * size for v in face_i]
face = gray[y:y + h, x:x + w]
face_resize = cv2.resize(face, (im_width, im_height))

# Try to recognize the face
prediction = model.predict(face_resize)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)

# [1]
# Write the name of recognized face
cv2.putText(frame,
'%s - %.0f' % (names[prediction[0]],prediction[1]),
(x-10, y-10), cv2.FONT_HERSHEY_PLAIN,1,(0, 255, 0))
face = '%S' % (names[prediction[0]]) #Guardar nombre en variable
#Start to validate the name
if face != "" : #If a name is detected
print(face + "Is in the room..") #Print the name in terminal
elif face == "" : #If a name is not detected
print("The room is empty...") #Print the text in terminal
#This last part is where I have problem, when a face is not detected, the text is not printed in the terminal
# Show the image and check for ESC being pressed
cv2.imshow('OpenCV', frame)
key = cv2.waitKey(10)
if key == 27:
break

我使用的代码基于以下教程:Face Detection

感谢任何帮助,谢谢。问候

最佳答案

如果房间里没有检测到人脸,你的代码就不会进入 for i in range(len(faces)) 循环,顺便说一下,它可以简化为 for我在脸上

因此,for 循环末尾的 else 可以解决您的问题:

for face in faces:
do_stuff(face)
else:
print("room is empty")

关于python - OpenCV + Raspberry PI 就像一个房间监视器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43528665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com