Filename: FacialDetection.py Line # Mem usage Increment Line Contents ================================================ 4 94.266 MiB 0.000 MiB @profile 5 def detect(img1): 6 108.688 MiB 14.422 MiB img = cv2.imread(img1) 7 # xml file from https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_alt.xml 8 120.430 MiB 11.742 MiB cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml") 9 396.594 MiB 276.164 MiB rects = cascade.detectMultiScale(img) 10 396.594 MiB 0.000 MiB if len(rects) == 0: 11 return None 12 396.652 MiB 0.059 MiB rects[:, 2:] += rects[:, :2] 13 396.652 MiB 0.000 MiB photos = [] 14 396.652 MiB 0.000 MiB for x1, y1, x2, y2 in rects: 15 396.652 MiB 0.000 MiB photos.append(img[y1:y2,x1:x2]) 16 396.652 MiB 0.000 MiB return photos Filename: AllowedUsers.py Line # Mem usage Increment Line Contents ================================================ 18 92.738 MiB 0.000 MiB @profile 19 def setRecognizers(self): 20 92.738 MiB 0.000 MiB recognizers = [] 21 92.770 MiB 0.031 MiB recognizers.append(cv2.face.createLBPHFaceRecognizer()) 22 94.262 MiB 1.492 MiB for rec in recognizers: 23 94.262 MiB 0.000 MiB rec.train(self.images, self.labels) 24 94.262 MiB 0.000 MiB self.recognizers = recognizers Filename: FacialMatch.py Line # Mem usage Increment Line Contents ================================================ 5 109.660 MiB 0.000 MiB @profile 6 def matchFace(roommate, imageToMatch): 7 #use recognizers to decide if the intruder is a roommate or not 8 #get confidence levels from recognizers with imageToRecognize 9 109.660 MiB 0.000 MiB confidences = [] 10 #imageToMatch is the second 11 109.660 MiB 0.000 MiB recognizers = roommate.getRecognizers() 12 110.082 MiB 0.422 MiB for rec in recognizers: 13 110.043 MiB -0.039 MiB img = cv2.imread(imageToMatch, cv2.IMREAD_GRAYSCALE) 14 110.043 MiB 0.000 MiB result = cv2.face.MinDistancePredictCollector() 15 110.082 MiB 0.039 MiB rec.predict(img, result, 0) 16 110.082 MiB 0.000 MiB print "Facial Match Confidence: " + str(result.getDist()) 17 110.082 MiB 0.000 MiB confidences.append(result.getDist()) 18 19 110.082 MiB 0.000 MiB avgConfidence = 0 20 110.082 MiB 0.000 MiB for c in confidences: 21 110.082 MiB 0.000 MiB avgConfidence = avgConfidence + c 22 110.082 MiB 0.000 MiB avgConfidence = avgConfidence/len(confidences) 23 110.082 MiB 0.000 MiB print "Average Facial Match Confidence: " + str(avgConfidence) 24 #if average confidence rate is good 25 110.082 MiB 0.000 MiB if(avgConfidence>=100): 26 return True 27 else: 28 110.082 MiB 0.000 MiB return False Filename: main.py Line # Mem usage Increment Line Contents ================================================ 22 94.266 MiB 0.000 MiB @profile 23 def run(house): 24 # Take picture 25 94.266 MiB 0.000 MiB print "Taking picture: " 26 94.266 MiB 0.000 MiB os.system("raspistill -o tm.jpg") 27 # If face detected send cropped picture and roommate to FacialMatch method 28 109.598 MiB 15.332 MiB photos = detect("addy.jpg") 29 109.598 MiB 0.000 MiB robber_photos = photos 30 109.598 MiB 0.000 MiB if len(photos) != None: 31 112.918 MiB 3.320 MiB for photo in photos: 32 109.660 MiB -3.258 MiB cv2.imwrite("cropped.jpg", photo) 33 # If face is not the roommate, read input from keypad 34 109.660 MiB 0.000 MiB count = len(house.roommates) 35 109.699 MiB 0.039 MiB for roommate in house.roommates: 36 109.699 MiB 0.000 MiB match = matchFace(roommate,"cropped.jpg") 37 109.699 MiB 0.000 MiB if match == True: 38 robber_photos.remove(photo) 39 print "Roommate Detected: " + str(roommate.name) 40 return 41 109.699 MiB 0.000 MiB if len(robber_photos) == len(photos): #none of roommates go to keypad 42 109.699 MiB 0.000 MiB print "ROBBER: Getting password" 43 # If keypad combination is not correct notify roommate 44 109.699 MiB 0.000 MiB c = Credentials(house.roommates) 45 109.703 MiB 0.004 MiB if c.getInput() == False: 46 109.703 MiB 0.000 MiB for i in range(len(robber_photos)): 47 109.703 MiB 0.000 MiB cv2.imwrite('robber'+str(i)+'.jpg', robber_photos[i]) 48 112.918 MiB 3.215 MiB notify(house, robber_photos) # try once connected to internet 49 112.918 MiB 0.000 MiB print "Notified roommate" 50 112.918 MiB 0.000 MiB os.system("rm robber*.jpg") Filename: main.py Line # Mem usage Increment Line Contents ================================================ 51 109.703 MiB 0.000 MiB @profile 52 # Email Reference: http://masnun.com/2009/12/29/python-sending-a-bunch-of-images-as-email-attachments.html 53 def notify(house, pics): 54 109.707 MiB 0.004 MiB print "Notifying preferred roommate:" 55 109.707 MiB 0.000 MiB msg = MIMEMultipart() 56 109.707 MiB 0.000 MiB msg['Subject'] = 'Intuder at home' 57 109.707 MiB 0.000 MiB msg['from'] = 'informative112233@gmail.com' 58 109.707 MiB 0.000 MiB msg['to'] = house.getPreferredEmail() 59 109.715 MiB 0.008 MiB files = glob.glob(os.path.join(".", 'robber*.jpg')) 60 109.926 MiB 0.211 MiB for file in files: 61 109.719 MiB -0.207 MiB fp = open(file, 'rb') 62 109.926 MiB 0.207 MiB img = MIMEImage(fp.read()) 63 109.926 MiB 0.000 MiB fp.close 64 109.926 MiB 0.000 MiB msg.attach(img) 65 110.082 MiB 0.156 MiB server = smtplib.SMTP('smtp.gmail.com', 587) 66 110.324 MiB 0.242 MiB server.starttls() 67 110.324 MiB 0.000 MiB server.login("informative112233@gmail.com", "Inf0rmati0n") 68 110.629 MiB 0.305 MiB server.sendmail(msg['from'], msg['to'], msg.as_string()) 69 110.629 MiB 0.000 MiB server.quit() 70 ''' 71 server.login("informative112233@gmail.com", "Inf0rmati0n") 72 server.sendmail("informative112233@gmail.com", house.getPreferredEmail(), "Someone is at your house") 73 server.quit()''' 74 110.629 MiB 0.000 MiB client = Client("AC55c543c311787f4088bdda5a2eaea753", "f7ae9ba188928d01e349143430b51a3e"); 75 112.922 MiB 2.293 MiB message = client.messages.create(to="+1"+str(house.getPreferredNumber()),from_="+17204206018",body="intruder") 76 112.922 MiB 0.000 MiB print "Notified roommate" Filename: main.py Line # Mem usage Increment Line Contents ================================================ 78 43.961 MiB 0.000 MiB @profile 79 def train(h): 80 #set roommate images and labels 81 #train the recognizers with the roommate images 82 94.266 MiB 50.305 MiB for roommate in h.roommates: 83 43.961 MiB -50.305 MiB trainingSet = [] 84 43.961 MiB 0.000 MiB trainingSet.append("TrainSet1/test1.jpg") 85 43.961 MiB 0.000 MiB trainingSet.append("TrainSet1/test2.jpg") 86 43.961 MiB 0.000 MiB trainingSet.append("TrainSet1/test3.jpg") 87 43.961 MiB 0.000 MiB trainingSet.append("TrainSet1/test4.jpg") 88 43.961 MiB 0.000 MiB trainingSet.append("TrainSet1/test5.jpg") 89 43.961 MiB 0.000 MiB trainingSet.append("TrainSet1/test6.jpg") 90 43.961 MiB 0.000 MiB trainingSet.append("TrainSet1/test7.jpg") 91 43.961 MiB 0.000 MiB trainingSet.append("TrainSet1/test8.jpg") 92 43.961 MiB 0.000 MiB trainingSet.append("TrainSet1/test9.jpg") 93 43.961 MiB 0.000 MiB trainingSet.append("TrainSet1/test10.jpg") 94 43.961 MiB 0.000 MiB trainingImages = [] 95 43.961 MiB 0.000 MiB labels = [] 96 92.738 MiB 48.777 MiB for path in trainingSet: 97 92.734 MiB -0.004 MiB trainingImages.append(cv2.imread(path, cv2.IMREAD_GRAYSCALE)) 98 92.738 MiB 0.004 MiB labels.append(0) 99 92.738 MiB 0.000 MiB roommate.setImages(trainingImages,labels) 100 94.262 MiB 1.523 MiB roommate.setRecognizers()