From 4926c0877b1eb9cd463684def7db6700c83f1604 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Mon, 12 Nov 2018 12:46:29 +0300 Subject: [PATCH 1/3] refactor (to be tested) --- main.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++ untitled.py | 57 ----------------------------------------------------- 2 files changed, 50 insertions(+), 57 deletions(-) create mode 100644 main.py delete mode 100644 untitled.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..927a26f --- /dev/null +++ b/main.py @@ -0,0 +1,50 @@ + +from time import sleep +from picamera import PiCamera +from io import BytesIO +import cv2 +import numpy as np + +def init(): + print('Initilizing Camera and cascade components..') + camera = PiCamera() + print('[ OK ] Camera') + camera.resolution = (640, 480) + face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') + print('[ OK ] CascadeClassifier') + print('Starting Photo loop..') + start() + +def start(): + while True: + print('Taking photo') + camera.capture('img.jpg') + print('Photo Captured') + img = cv2.imread('img.jpg',1) + gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) + print('Photo Converted') + faces = face_cascade.detectMultiScale(gray, 1.3, 5) + print('Photo Processed') + i = 0; + for (x,y,w,h) in faces: + i = i + 1 + cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) + cv2.imwrite('result.jpg', gray) + roi_gray = gray[y:y+h, x:x+w] + roi_color = img[y:y+h, x:x+w] + print('Drawing on face ', i) + + cv2.imshow('image',img) + cv2.waitKey(100) + if cv2.waitKey(1) & 0xFF == ord('q'): + cv2.destroyAllWindows() + break + + print('Finshed') + +def recognize(): + +def sendOK(): + +init() + diff --git a/untitled.py b/untitled.py deleted file mode 100644 index 28f7a2c..0000000 --- a/untitled.py +++ /dev/null @@ -1,57 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# -# untitled.py -# -# Copyright 2018 -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -# MA 02110-1301, USA. -# -# - -from time import sleep -from picamera import PiCamera -from io import BytesIO -import cv2 -import numpy as np - -camera = PiCamera() -camera.resolution = (640, 480) -face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') - -while True: - print('Taking photo') - camera.capture('img.jpg') - img = cv2.imread('img.jpg',1) - gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) - faces = face_cascade.detectMultiScale(gray, 1.3, 5) - for (x,y,w,h) in faces: - cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) - cv2.imwrite('result.jpg', gray) - roi_gray = gray[y:y+h, x:x+w] - roi_color = img[y:y+h, x:x+w] - print('Drawing face') - print('displying iamge') - cv2.imshow('image',img) - cv2.waitKey(100) - if cv2.waitKey(1) & 0xFF == ord('q'): - cv2.destroyAllWindows() - break - -print('Finshed') - - - From 7a550a20b26fd6d953504ccd75d4520499c76db4 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Mon, 12 Nov 2018 13:26:55 +0300 Subject: [PATCH 2/3] refactor 2 --- main.py | 81 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/main.py b/main.py index 927a26f..25a9481 100644 --- a/main.py +++ b/main.py @@ -6,41 +6,60 @@ import cv2 import numpy as np def init(): + go = true print('Initilizing Camera and cascade components..') - camera = PiCamera() - print('[ OK ] Camera') - camera.resolution = (640, 480) - face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') - print('[ OK ] CascadeClassifier') - print('Starting Photo loop..') - start() + try: + camera = PiCamera() + camera.resolution = (640, 480) + sleep(2) + print('[ OK ] Camera') + except: + print('[ Error ] Can not initialize PiCamera') + go = false + + try: + face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') + sleep(1) + print('[ OK ] CascadeClassifier') + except: + print('[ Error ] Can not load cascade File') + go = false + if (go): + print('Starting Photo loop..') + start(camera) + else: + print('closing') -def start(): +def start(camera): while True: - print('Taking photo') - camera.capture('img.jpg') - print('Photo Captured') - img = cv2.imread('img.jpg',1) - gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) - print('Photo Converted') - faces = face_cascade.detectMultiScale(gray, 1.3, 5) - print('Photo Processed') - i = 0; - for (x,y,w,h) in faces: - i = i + 1 - cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) - cv2.imwrite('result.jpg', gray) - roi_gray = gray[y:y+h, x:x+w] - roi_color = img[y:y+h, x:x+w] - print('Drawing on face ', i) - - cv2.imshow('image',img) - cv2.waitKey(100) - if cv2.waitKey(1) & 0xFF == ord('q'): - cv2.destroyAllWindows() - break + try: + print('Taking photo') + camera.capture('img.jpg') + print('Photo Captured') + img = cv2.imread('img.jpg',1) + gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) + print('Photo Converted') + faces = face_cascade.detectMultiScale(gray, 1.3, 5) + print('Photo Processed') + i = 0; + for (x,y,w,h) in faces: + i = i + 1 + cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) + #cv2.imwrite('result.jpg', gray) + roi_gray = gray[y:y+h, x:x+w] + roi_color = img[y:y+h, x:x+w] + print('Drawing on face ', i) + + cv2.imshow('image',img) + cv2.waitKey(100) + if cv2.waitKey(1) & 0xFF == ord('q'): + cv2.destroyAllWindows() + break - print('Finshed') + except: + print('[ Error ] Unexpected exception') + print('Closing') + break def recognize(): From 14a05c10d89d0bf444f5469aa6bc95a3c4a9cace Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Mon, 12 Nov 2018 15:16:58 +0000 Subject: [PATCH 3/3] Working 0.2 --- main.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index 25a9481..2d94ec4 100644 --- a/main.py +++ b/main.py @@ -6,7 +6,7 @@ import cv2 import numpy as np def init(): - go = true + go = True print('Initilizing Camera and cascade components..') try: camera = PiCamera() @@ -15,7 +15,7 @@ def init(): print('[ OK ] Camera') except: print('[ Error ] Can not initialize PiCamera') - go = false + go = False try: face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') @@ -23,14 +23,14 @@ def init(): print('[ OK ] CascadeClassifier') except: print('[ Error ] Can not load cascade File') - go = false + go = False if (go): print('Starting Photo loop..') - start(camera) + start(camera, face_cascade) else: print('closing') -def start(camera): +def start( camera, face_cascade): while True: try: print('Taking photo') @@ -61,9 +61,5 @@ def start(camera): print('Closing') break -def recognize(): - -def sendOK(): - init()