我正在raspberi Pi上工作3.我正在用opencv编写代码。想法是继续扫描脸部,直到找到它。如果结果.jpg需要发送到电子邮件。但是目前我的发送语句不会从false更改为true。脸部检测没有问题,我得到的结果图片。但它只是循环executeface()。当facenumber> 0时,我如何获得发送从False变为true。Python更改布尔值
#FOR FACE
import io
import picamera
import cv2
import numpy
import sched, time
import time
#FOR EMAIL
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
send=False
def sendresult():
print "SENDING THE MAIL"
def executeface():
print "starting execute face"
#Create a memory stream so photos doesn't need to be saved in a file
stream = io.BytesIO()
#Get the picture (low resolution, so it should be quite fast)
#Here you can also specify other parameters (e.g.:rotate the image)
with picamera.PiCamera() as camera:
camera.resolution = (320, 240)
camera.capture(stream, format='jpeg')
#Convert the picture into a numpy array
buff = numpy.fromstring(stream.getvalue(), dtype=numpy.uint8)
#Now creates an OpenCV image
image = cv2.imdecode(buff, 1)
#Load a cascade file for detecting faces
face_cascade = cv2.CascadeClassifier('/home/pi/haarcascade_frontalface_alt.xml')
#Convert to grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
#Look for faces in the image using the loaded cascade file
faces = face_cascade.detectMultiScale(gray, 1.1, 5)
print "Found "+str(len(faces))+" face(s)"
facenumber = int(str(len(faces)))
#print "send value ="+send
#Save the result image
cv2.imwrite('result.jpg',image)
print "WROTE RESULT"
return facenumber
if facenumber > 0:
send=True
return send
while send is False:
executeface()
if send==True:
print "execute sendresult"
break
所以我的逻辑是。执行面 - 如果没有脸保持扫描。如果存在脸部变化,则发送= true并开始sendresult,结果通过电子邮件发送。
将返回'send'但你永远不把它分配给什么...尝试'发送= executeface()' –