2016-11-29 92 views
-2

我正在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,结果通过电子邮件发送。

+0

将返回'send'但你永远不把它分配给什么...尝试'发送= executeface()' –

回答

1

它应该工作是你只是用if executeface():修复send==True。您正在返回变量facenumber,而executeface()函数的其余部分永远不会运行。如果executeface()返回0 if executeface()False否则True

+0

非常感谢你!不能相信我错过了简单的事情!还发送==真正的替换如果executeface():作为一个魅力! – Powisss

1

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

+0

哦,太! –