Skip to content

Commit

Permalink
Update remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
mickey9801 committed Oct 5, 2019
1 parent 04621a2 commit 251a75b
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 62 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Face recognition sample with opencv-python.

```bash
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install python3-opencv
sudo apt-get install python3-opencv python3-picamera python3-numpy python3-pil
```
* DB Browser for SQLite

Expand Down
26 changes: 17 additions & 9 deletions detector_picam.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# detector_webcam.py
# Finding the person in front of the camera is anyone who stored in database
# Using Pi Camera v2 module (single threading)
#
# Project: Face Recognition using OpenCV and Raspberry Pi
# Ref: https://www.pytorials.com/face-recognition-using-opencv-part-3/
# By: Mickey Chan @ 2019

# Import required modules
import cv2
Expand All @@ -19,7 +25,7 @@
print("Please train the data first")
exit(0)

# Setup GPIO for unlock LED
# Setup GPIO for door lock
relayPin = 26
GPIO.setmode(GPIO.BCM)
GPIO.setup(relayPin, GPIO.OUT)
Expand All @@ -37,24 +43,24 @@
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(640, 480))

# Setup Classifier for detect face
# Setup Classifier for detecting face
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# Setup LBPH recognizer for face recognition
recognizer = cv2.face.createLBPHFaceRecognizer() # or LBPHFaceRecognizer_create()
# Load training data
recognizer.load(fname) # read() for LBPHFaceRecognizer_create()
recognizer.load(fname) # change to read() for LBPHFaceRecognizer_create()

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# Turn off unlock LED when timeout
# Lock the door again when timeout
if time.time() - lastUnlockedAt > unlockDuration:
GPIO.output(relayPin, 0)

frame = frame.array
# Detect face
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, scaleFactor = 1.5, minNeighbors = 5)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Convert captured frame to grayscale
faces = faceCascade.detectMultiScale(gray, scaleFactor = 1.3, minNeighbors = 5) # Detect face(s) inside the frame
for (x, y, w, h) in faces:
# Try to recognize the face a
# Try to recognize the face using recognizer
roiGray = gray[y:y+h, x:x+w]
id_, conf = recognizer.predict(roiGray)
print(id_, conf)
Expand All @@ -63,14 +69,15 @@
# retrieve the user name from database,
# draw a rectangle around the face,
# print the name of the user and
# light up the unlock LED for 5 secord
# unlock the door for 5 secords
if conf <= 70:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# retrieve user name from database
db.execute("SELECT `name` FROM `users` WHERE `id` = (?);", (id_,))
result = db.fetchall()
name = result[0][0]

# You may do anything below for detected user, e.g. unlock the door
GPIO.output(relayPin, 1) # Unlock
lastUnlockedAt = time.time()
print("[Unlock] " + name + " (" + str(conf) + ")")
Expand All @@ -82,7 +89,8 @@
#cv2.putText(frame, 'No Match', (x+2,y+h-5), font, 1, (0,0,255), 2)

cv2.imshow("Face Recognizer", frame)
rawCapture.truncate(0)
rawCapture.truncate(0) # Clear frame buffer for next frame

# Press ESC or 'q' to quit the program
key = cv2.waitKey(1) & 0xff
if key == 27 or key == ord('q'):
Expand Down
25 changes: 16 additions & 9 deletions detector_webcam.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# detector_webcam.py
# Finding the person in front of the camera is anyone who stored in database
# Using USB webcam or IP Cam (single threading)
#
# Project: Face Recognition using OpenCV and Raspberry Pi
# Ref: https://www.pytorials.com/face-recognition-using-opencv-part-3/
# By: Mickey Chan @ 2019

# Import required modules
import cv2
Expand All @@ -17,7 +23,7 @@
print("Please train the data first")
exit(0)

# Setup GPIO for unlock LED
# Setup GPIO for door lock
relayPin = 26
GPIO.setmode(GPIO.BCM)
GPIO.setup(relayPin, GPIO.OUT)
Expand All @@ -36,15 +42,15 @@
vSource = 0 # first USB webcam
vStream = cv2.VideoCapture(vSource)

# Setup Classifier for detect face
# Setup Classifier for detecting face
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# Setup LBPH recognizer for face recognition
recognizer = cv2.face.createLBPHFaceRecognizer() # or LBPHFaceRecognizer_create()
# Load training data
recognizer.load(fname) # read() for LBPHFaceRecognizer_create()
recognizer.load(fname) # change to read() for LBPHFaceRecognizer_create()

while vStream.isOpened():
# Turn off unlock LED when timeout
# Lock the door again when timeout
if time.time() - lastUnlockedAt > unlockDuration:
GPIO.output(relayPin, 0)

Expand All @@ -56,11 +62,11 @@
lastDetectedAt = time.time()

# Detect face
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, scaleFactor = 1.1, minNeighbors = 2)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Convert captured frame to grayscale
faces = faceCascade.detectMultiScale(gray, scaleFactor = 1.3, minNeighbors = 5) # Detect face(s) inside the frame

for (x, y, w, h) in faces:
# Try to recognize the face a
# Try to recognize the face using recognizer
roiGray = gray[y:y+h, x:x+w]
id_, conf = recognizer.predict(roiGray)
print(id_, conf)
Expand All @@ -69,14 +75,15 @@
# retrieve the user name from database,
# draw a rectangle around the face,
# print the name of the user and
# light up the unlock LED for 5 secord
# unlock the door for 5 secords
if conf <= 70:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# retrieve user name from database
db.execute("SELECT `name` FROM `users` WHERE `id` = (?);", (id_,))
result = db.fetchall()
name = result[0][0]

# You may do anything below for detected user, e.g. unlock the door
GPIO.output(relayPin, 1) # Unlock
lastUnlockedAt = time.time()
print("[Unlock] " + str(id_) + ":" + name + " (" + str(conf) + ")")
Expand All @@ -89,7 +96,7 @@

cv2.imshow("Face Recognizer", frame)

# Press ESC to quit the program
# Press ESC or 'q' to quit the program
key = cv2.waitKey(1) & 0xff
if key == 27 or key == ord('q'):
break
Expand Down
40 changes: 25 additions & 15 deletions recordface_picam.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# recordface_picam.py
# Capture face image of a person for face recognition, using Pi Camera v2 module
#
# Project: Face Recognition using OpenCV and Raspberry Pi
# Ref: https://www.pytorials.com/face-recognition-using-opencv-part-2/
# By: Mickey Chan @ 2019

# Import required modules
import cv2
Expand All @@ -18,41 +23,46 @@
os.makedirs(dirName)
print("DataSet Directory Created")

# Ask for the user's name
name = input("What's his/her Name?")

imgCapture = 30 # Number of face image we have to capture
saveFace = False
frameColor = (0,0,255)
userDir = "User_"
frameColor = (0,0,255) # Frame color for detected face
userDir = "User_" # Prefix of face image directory name
beginTime = 0

# Connect to video source
# Connect to video source: Pi Camera v2
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(640, 480))
rawCapture = PiRGBArray(camera, size=(640, 480)) # A Numpy compatible array for storing catpured frame (frame buffer)

# Setup Classifier for detect face
# Setup Classifier for detecting face
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

# Continuously capture video until collected require amount of face data
count = 1
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): # Read a frame
frame = frame.array;
cv2.putText(frame, "Press 'f' to start face capture", (10, 480-10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,255,255), 2)

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, scaleFactor = 1.5, minNeighbors = 5)
# Find any face in the frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Convert captured frame to grayscale
faces = faceCascade.detectMultiScale(gray, scaleFactor = 1.3, minNeighbors = 5) # Detect face(s) inside the frame
# If found, save captured face image
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x,y), (x+w, y+h), frameColor, 2)
cv2.rectangle(frame, (x,y), (x+w, y+h), frameColor, 2) # Draw a frame surrounding the face
# Save captured face data
if saveFace:
roiGray = gray[y:y+h, x:x+w]
fileName = userDir + "/" + f'{count:02}' + ".jpg"
cv2.imwrite(fileName, roiGray)
#print(fileName)
cv2.imshow("face", roiGray)
count += 1

cv2.imshow('frame', frame)
rawCapture.truncate(0)
cv2.imshow('frame', frame) # Show the video frame
rawCapture.truncate(0) # Clear frame buffer for next frame

# Press 'f' to begin detect,
# Press ESC or 'q' to quit
Expand All @@ -69,8 +79,8 @@
os.makedirs(userDir)
#print("Maked directory: " + userDir)

# Quit face detection when captured 30 images
if count > 30:
# Quit face detection when captured required faces
if count > imgCapture:
break

# Clean up
Expand All @@ -80,7 +90,7 @@
db.execute("INSERT INTO `users` (`name`) VALUES(?)", (name,))
uid = db.lastrowid
print("User ID:" + str(uid))
# Rename temperary directory with UID
# Rename temperary directory with USER ID
newUserDir = os.path.join(dirName, str(uid))
os.rename(userDir, newUserDir);
#print("Renamed user dataset directory name to " + newUserDir)
Expand Down
34 changes: 22 additions & 12 deletions recordface_webcam.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# recordface_webcam.py
# Capture face image of a person for face recognition, using webcam or IP cam
#
# Project: Face Recognition using OpenCV and Raspberry Pi
# Ref: https://www.pytorials.com/face-recognition-using-opencv-part-2/
# By: Mickey Chan @ 2019

# Import required modules
import cv2
Expand All @@ -16,45 +21,50 @@
os.makedirs(dirName)
print("DataSet Directory Created")

# Ask for the user's name
name = input("What's his/her Name?")

imgCapture = 30
imgCapture = 30 # Number of face image we have to capture
saveFace = False
frameColor = (0,0,255)
userDir = "User_"
frameColor = (0,0,255) # Frame color for detected face
userDir = "User_" # Prefix of face image directory name
beginTime = 0

# Connect to video source
#vSource = "rtsp://192.168.1.100:8554/live.sdp" # RTSP URL of IP Cam
vSource = 0 # first USB webcam
vStream = cv2.VideoCapture(vSource)

# Setup Classifier for detect face
# Setup Classifier for detecting face
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

# Continuously capture video until collected require amount of face data
count = 1
frameRate = 5
frameRate = 5 # Frequency for capturing face
prevTime = 0
while vStream.isOpened():
timeElapsed = time.time() - prevTime
ok, frame = vStream.read()
ok, frame = vStream.read() # Read a frame
if not ok: break
cv2.putText(frame, "Press 'f' to start face capture", (10, 480-10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,255,255), 2)

if timeElapsed > 1./frameRate:
prevTime = time.time()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, scaleFactor = 1.1, minNeighbors = 2)
# Find any face in the frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Convert captured frame to grayscale
faces = faceCascade.detectMultiScale(gray, scaleFactor = 1.3, minNeighbors = 5) # Detect face(s) inside the frame
# If found, save captured face image
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x,y), (x+w, y+h), frameColor, 2)
cv2.rectangle(frame, (x,y), (x+w, y+h), frameColor, 2) # Draw a frame surrounding the face
# Save captured face data
if saveFace:
roiGray = gray[y:y+h, x:x+w]
fileName = userDir + "/" + f'{count:02}' + ".jpg"
cv2.imwrite(fileName, roiGray)
cv2.imshow("face", roiGray)
count += 1

cv2.imshow('frame', frame)
cv2.imshow('frame', frame) # Show the video frame
# Press 'f' to begin detect,
# Press ESC or 'q' to quit
key = cv2.waitKey(1) & 0xff
Expand All @@ -69,7 +79,7 @@
if not os.path.exists(userDir):
os.makedirs(userDir)

# Quit face detection when captured 30 images
# Quit face detection when captured required faces
if count > imgCapture:
break

Expand All @@ -80,7 +90,7 @@
db.execute("INSERT INTO `users` (`name`) VALUES(?)", (name,))
uid = db.lastrowid
print("User ID:" + str(uid))
# Rename temperary directory with UID
# Rename temperary directory with USER ID
newUserDir = os.path.join(dirName, str(uid))
os.rename(userDir, newUserDir);
#print("Renamed user dataset directory name to " + newUserDir)
Expand Down
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# setup.py
# Prepare environment for the project
#
# Project: Face Recognition using OpenCV and Raspberry Pi
# Ref: https://www.pytorials.com/face-recognition-using-opencv-part-2/
# By: Mickey Chan @ 2019

# Setup database
# Setup user database
import sqlite3

conn = sqlite3.connect('database.db')
Expand Down
9 changes: 8 additions & 1 deletion threading/Capturer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Capture.py
# Worker object for capturing video frame
#
# Project: Face Recognition using OpenCV and Raspberry Pi
# Ref: https://github.com/nrsyed/computer-vision/tree/master/multithread
# By: Mickey Chan @ 2019

from threading import Thread
import cv2

Expand All @@ -19,7 +26,7 @@ def get(self):
if not self.grabbed:
self.stop()
else:
(self.grabbed, self.frame) = self.stream.read()
(self.grabbed, self.frame) = self.stream.read() # Capture a video frame

self.stream.release()
return
Expand Down
Loading

0 comments on commit 251a75b

Please sign in to comment.