main cpy
This commit is contained in:
parent
dc3d283d5d
commit
e495e951cb
32 changed files with 308 additions and 63 deletions
BIN
face_recognition/face_recog_old/face_recog_2/boom.jpg
Normal file
BIN
face_recognition/face_recog_old/face_recog_2/boom.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 148 KiB |
15
face_recognition/face_recog_old/face_recog_2/cv_test.py
Normal file
15
face_recognition/face_recog_old/face_recog_2/cv_test.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
import cv2
|
||||
|
||||
cam = cv2.VideoCapture(0)
|
||||
|
||||
while True:
|
||||
check, frame = cam.read()
|
||||
|
||||
cv2.imshow('video', frame)
|
||||
|
||||
key = cv2.waitKey(1)
|
||||
if key == 27:
|
||||
break
|
||||
|
||||
cam.release()
|
||||
cv2.destroyAllWindows()
|
36
face_recognition/face_recog_old/face_recog_2/datacollect.py
Normal file
36
face_recognition/face_recog_old/face_recog_2/datacollect.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
import cv2
|
||||
import os
|
||||
|
||||
video = cv2.VideoCapture(0)
|
||||
|
||||
facedectect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
||||
|
||||
count = 0
|
||||
|
||||
nameID = str(input("Enter Your Name: ")).lower()
|
||||
|
||||
path = 'images/' + nameID
|
||||
isExist = os.path.exists(path)
|
||||
|
||||
|
||||
if isExist:
|
||||
print("Name Already Taken")
|
||||
nameID = str(input("Enter Your Name Again"))
|
||||
else:
|
||||
os.makedirs(path)
|
||||
|
||||
while True:
|
||||
ret, frame = video.read()
|
||||
faces = facedectect.detectMultiScale(frame,1.3,5)
|
||||
for x,y,w,h in faces:
|
||||
count = count+1
|
||||
name = './images/' + nameID + '/' + str(count) + '.jpg'
|
||||
print("Creating Images............" + name)
|
||||
cv2.imwrite(name, frame[y:y+h,x:x+w])
|
||||
cv2.rectangle(frame, (x,y), (x+w, y+h), (0,255,0), 3)
|
||||
cv2.imshow("WindowFrame", frame)
|
||||
k = cv2.waitKey(1)
|
||||
if k == ord('q'):
|
||||
break
|
||||
video.release()
|
||||
cv2.destroyAllWindows()
|
102
face_recognition/face_recog_old/face_recog_2/face_main.py
Normal file
102
face_recognition/face_recog_old/face_recog_2/face_main.py
Normal file
|
@ -0,0 +1,102 @@
|
|||
import face_recognition as face
|
||||
import numpy as np
|
||||
import cv2
|
||||
|
||||
#ORIGINAL_CODE_CREDIT: https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
|
||||
|
||||
#ดึงวิดีโอตัวอย่างเข้ามา, ถ้าต้องการใช้webcamให้ใส่เป็น
|
||||
video_capture = cv2.VideoCapture(0)
|
||||
|
||||
#ใบหน้าคนที่ต้องการรู้จำเป็นreference #คนที่1
|
||||
boom_image = face.load_image_file("boom.jpg")
|
||||
boom_face_encoding = face.face_encodings(boom_image)[0]
|
||||
|
||||
tat_image = face.load_image_file("tat.jpg")
|
||||
tat_face_encoding = face.face_encodings(tat_image)[0]
|
||||
|
||||
'''
|
||||
#ใบหน้าคนที่ต้องการรู้จำเป็นreference #คนที่2
|
||||
oat_image = face.load_image_file("oat.jpg")
|
||||
oat_face_encoding = face.face_encodings(oat_image)[0]
|
||||
'''
|
||||
|
||||
|
||||
#ประกาศตัวแปร
|
||||
face_locations = []
|
||||
face_encodings = []
|
||||
face_names = []
|
||||
face_percent = []
|
||||
#ตัวแปรนี้ใช้สำหรับคิดเฟรมเว้นเฟรมเพื่อเพิ่มfps
|
||||
process_this_frame = True
|
||||
|
||||
known_face_encodings = [boom_face_encoding, tat_face_encoding]
|
||||
known_face_names = ["BOOM", "TAT"]
|
||||
|
||||
#loopคำนวณแต่ละเฟรมของวิดีโอ
|
||||
while True:
|
||||
#อ่านค่าแต่ละเฟรมจากวิดีโอ
|
||||
ret, frame = video_capture.read()
|
||||
if ret:
|
||||
#ลดขนาดสองเท่าเพื่อเพิ่มfps
|
||||
small_frame = cv2.resize(frame, (0,0), fx=0.5,fy=0.5)
|
||||
#เปลี่ยน bgrเป็น rgb
|
||||
#rgb_small_frame = small_frame[:,:,::-1]
|
||||
rgb_small_frame = np.ascontiguousarray(small_frame[:, :, ::-1])
|
||||
|
||||
face_names = []
|
||||
face_percent = []
|
||||
|
||||
if process_this_frame:
|
||||
#ค้นหาตำแหน่งใบหน้าในเฟรม
|
||||
face_locations = face.face_locations(rgb_small_frame)
|
||||
#นำใบหน้ามาหาfeaturesต่างๆที่เป็นเอกลักษณ์
|
||||
face_encodings = face.face_encodings(rgb_small_frame, face_locations)
|
||||
|
||||
#เทียบแต่ละใบหน้า
|
||||
for face_encoding in face_encodings:
|
||||
face_distances = face.face_distance(known_face_encodings, face_encoding)
|
||||
best = np.argmin(face_distances)
|
||||
face_percent_value = 1-face_distances[best]
|
||||
|
||||
#กรองใบหน้าที่ความมั่นใจ50% ปล.สามารถลองเปลี่ยนได้
|
||||
if face_percent_value >= 0.4:
|
||||
name = known_face_names[best]
|
||||
percent = round(face_percent_value*100,2)
|
||||
face_percent.append(percent)
|
||||
else:
|
||||
name = "UNKNOWN"
|
||||
face_percent.append(0)
|
||||
face_names.append(name)
|
||||
|
||||
#วาดกล่องและtextเมื่อแสดงผลออกมาออกมา
|
||||
for (top,right,bottom, left), name, percent in zip(face_locations, face_names, face_percent):
|
||||
top*= 2
|
||||
right*= 2
|
||||
bottom*= 2
|
||||
left*= 2
|
||||
|
||||
if name == "UNKNOWN":
|
||||
color = [46,2,209]
|
||||
else:
|
||||
color = [255,102,51]
|
||||
|
||||
cv2.rectangle(frame, (left,top), (right,bottom), color, 2)
|
||||
cv2.rectangle(frame, (left-1, top -30), (right+1,top), color, cv2.FILLED)
|
||||
cv2.rectangle(frame, (left-1, bottom), (right+1,bottom+30), color, cv2.FILLED)
|
||||
font = cv2.FONT_HERSHEY_DUPLEX
|
||||
cv2.putText(frame, name, (left+6, top-6), font, 0.6, (255,255,255), 1)
|
||||
cv2.putText(frame, "MATCH: "+str(percent)+"%", (left+6, bottom+23), font, 0.6, (255,255,255), 1)
|
||||
|
||||
|
||||
#สลับค่าเป็นค่าตรงข้ามเพื่อให้คิดเฟรมเว้นเฟรม
|
||||
process_this_frame = not process_this_frame
|
||||
|
||||
#แสดงผลลัพท์ออกมา
|
||||
cv2.imshow("Video", frame)
|
||||
if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||
break
|
||||
|
||||
|
||||
#ล้างค่าต่างๆเมื่อปิดโปรแกรม
|
||||
video_capture.release()
|
||||
cv2.destroyAllWindows()
|
105
face_recognition/face_recog_old/face_recog_2/face_main_2.py
Normal file
105
face_recognition/face_recog_old/face_recog_2/face_main_2.py
Normal file
|
@ -0,0 +1,105 @@
|
|||
import face_recognition
|
||||
import cv2
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
|
||||
# other example, but it includes some basic performance tweaks to make things run a lot faster:
|
||||
# 1. Process each video frame at 1/4 resolution (though still display it at full resolution)
|
||||
# 2. Only detect faces in every other frame of video.
|
||||
|
||||
# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
|
||||
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
|
||||
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
|
||||
|
||||
# Get a reference to webcam #0 (the default one)
|
||||
video_capture = cv2.VideoCapture(0)
|
||||
|
||||
# Load a sample picture and learn how to recognize it.
|
||||
PUN_image = face_recognition.load_image_file("PUN.jpg")
|
||||
PUN_face_encoding = face_recognition.face_encodings(PUN_image)[0]
|
||||
|
||||
# Load a second sample picture and learn how to recognize it.
|
||||
TAT_image = face_recognition.load_image_file("tat.jpg")
|
||||
TAT_face_encoding = face_recognition.face_encodings(TAT_image)[0]
|
||||
|
||||
# Create arrays of known face encodings and their names
|
||||
known_face_encodings = [
|
||||
PUN_face_encoding,
|
||||
TAT_face_encoding
|
||||
]
|
||||
known_face_names = [
|
||||
"PUN",
|
||||
"TAT"
|
||||
]
|
||||
|
||||
# Initialize some variables
|
||||
face_locations = []
|
||||
face_encodings = []
|
||||
face_names = []
|
||||
process_this_frame = True
|
||||
|
||||
while True:
|
||||
# Grab a single frame of video
|
||||
ret, frame = video_capture.read()
|
||||
|
||||
# Only process every other frame of video to save time
|
||||
if process_this_frame:
|
||||
# Resize frame of video to 1/4 size for faster face recognition processing
|
||||
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
|
||||
|
||||
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
|
||||
rgb_small_frame = np.ascontiguousarray(small_frame[:, :, ::-1])
|
||||
|
||||
# Find all the faces and face encodings in the current frame of video
|
||||
face_locations = face_recognition.face_locations(rgb_small_frame)
|
||||
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
|
||||
|
||||
face_names = []
|
||||
for face_encoding in face_encodings:
|
||||
# See if the face is a match for the known face(s)
|
||||
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
|
||||
name = "Unknown"
|
||||
|
||||
# # If a match was found in known_face_encodings, just use the first one.
|
||||
# if True in matches:
|
||||
# first_match_index = matches.index(True)
|
||||
# name = known_face_names[first_match_index]
|
||||
|
||||
# Or instead, use the known face with the smallest distance to the new face
|
||||
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
|
||||
best_match_index = np.argmin(face_distances)
|
||||
if matches[best_match_index]:
|
||||
name = known_face_names[best_match_index]
|
||||
|
||||
face_names.append(name)
|
||||
|
||||
process_this_frame = not process_this_frame
|
||||
|
||||
|
||||
# Display the results
|
||||
for (top, right, bottom, left), name in zip(face_locations, face_names):
|
||||
# Scale back up face locations since the frame we detected in was scaled to 1/4 size
|
||||
top *= 4
|
||||
right *= 4
|
||||
bottom *= 4
|
||||
left *= 4
|
||||
|
||||
# Draw a box around the face
|
||||
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
|
||||
|
||||
# Draw a label with a name below the face
|
||||
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
|
||||
font = cv2.FONT_HERSHEY_DUPLEX
|
||||
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
|
||||
|
||||
# Display the resulting image
|
||||
cv2.imshow('Video', frame)
|
||||
|
||||
# Hit 'q' on the keyboard to quit!
|
||||
if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||
break
|
||||
|
||||
# Release handle to the webcam
|
||||
video_capture.release()
|
||||
cv2.destroyAllWindows()
|
File diff suppressed because it is too large
Load diff
BIN
face_recognition/face_recog_old/face_recog_2/tat.jpg
Normal file
BIN
face_recognition/face_recog_old/face_recog_2/tat.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 124 KiB |
65
face_recognition/face_recog_old/face_recog_2/test.py
Normal file
65
face_recognition/face_recog_old/face_recog_2/test.py
Normal file
|
@ -0,0 +1,65 @@
|
|||
import tensorflow as tf
|
||||
from tensorflow import keras
|
||||
import numpy as np
|
||||
import cv2
|
||||
from keras.models import load_model
|
||||
import numpy as np
|
||||
|
||||
facedectect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
||||
|
||||
|
||||
cap = cv2.VideoCapture(0)
|
||||
cap.set(3,640)
|
||||
cap.set(4,480)
|
||||
font = cv2.FONT_HERSHEY_COMPLEX
|
||||
|
||||
|
||||
model = load_model('keras_model.h5', compile= False)
|
||||
def get_classname(classNo):
|
||||
if classNo == 0:
|
||||
return "Siwat"
|
||||
elif classNo == 1:
|
||||
return "Sukatat"
|
||||
elif classNo == 2:
|
||||
return "Sorapat"
|
||||
|
||||
while True:
|
||||
sucess, imgOriginal = cap.read()
|
||||
faces = facedectect.detectMultiScale(imgOriginal,1.3,5)
|
||||
for x,y,w,h in faces:
|
||||
crop_img = imgOriginal[y:y+h, x:x+h]
|
||||
img = cv2.resize(crop_img, (224,224))
|
||||
img = img.reshape(1, 224, 224, 3)
|
||||
prediction = model.predict(img)
|
||||
classIndex = np.argmax(prediction,axis=1)
|
||||
#classIndex = model.predict(img)
|
||||
probabilityValue = np.amax(prediction)
|
||||
|
||||
if classIndex == 0:
|
||||
cv2.rectangle(imgOriginal, (x,y), (x+w, y+h), (0,255,0), 2)
|
||||
cv2.rectangle(imgOriginal, (x,y-40), (x+w, y), (0,255,0), -2)
|
||||
cv2.putText(imgOriginal, str(get_classname(classIndex)), (x,y-10), font, 0.75, (0,255,0), 2, cv2.LINE_AA, False)
|
||||
print(str(get_classname(classIndex)))
|
||||
elif classIndex == 1:
|
||||
cv2.rectangle(imgOriginal, (x,y), (x+w, y+h), (0,255,0), 2)
|
||||
cv2.rectangle(imgOriginal, (x,y-40), (x+w, y), (0,255,0), -2)
|
||||
cv2.putText(imgOriginal, str(get_classname(classIndex)), (x,y-10), font, 0.75, (0,255,0), 2, cv2.LINE_AA, False)
|
||||
print(str(get_classname(classIndex)))
|
||||
elif classIndex == 2:
|
||||
cv2.rectangle(imgOriginal, (x,y), (x+w, y+h), (0,255,0), 2)
|
||||
cv2.rectangle(imgOriginal, (x,y-40), (x+w, y), (0,255,0), -2)
|
||||
cv2.putText(imgOriginal, str(get_classname(classIndex)), (x,y-10), font, 0.75, (0,255,0), 2, cv2.LINE_AA, False)
|
||||
print(str(get_classname(classIndex)))
|
||||
|
||||
cv2.putText(imgOriginal, str(round(probabilityValue*100, 2))+ "%", (180,75), font ,0.75, (0,255,0), 2, cv2.LINE_AA, False)
|
||||
cv2.imshow("Result", imgOriginal)
|
||||
k = cv2.waitKey(1)
|
||||
if k == ord('q'):
|
||||
break
|
||||
video.release()
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue