Python更改布尔值

时间:2016-11-29 21:49:19

标签: python opencv boolean

我正在使用raspberi Pi 3.我正在使用opencv编写代码。想法是继续扫描面部直到它找到它。如果它确实result.jpg需要发送到电子邮件。但是目前我的发送声明不会从false变为true。检测到脸部没有问题,我得到了结果图片。但它只是继续循环executeface()。当facenumber为>时,如何将发送从False更改为true 0.

#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

所以我的逻辑是。 executeface - 如果没有面部继续扫描。如果有面部更改发送= true并启动sendresult,结果将通过电子邮件发送。

2 个答案:

答案 0 :(得分:1)

永远不会达到条件if facenumber > 0,因为你在它之前的行中退出了executeface()函数('return facenumber`)。

答案 1 :(得分:1)

它应该可行,您只需使用if send==True重新if executeface():。您将返回变量facenumber,而executeface()函数的其余部分永远不会运行。如果executeface()返回0,则if executeface()将为False,否则为True