2016-11-29 141 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

哦,太! –