Skip to content

Commit

Permalink
Merge pull request #11 from Avaiga/to_3_0
Browse files Browse the repository at this point in the history
To 3 0
  • Loading branch information
FlorianJacta authored Aug 30, 2024
2 parents 3dcc1d3 + a5227a5 commit 0f24a84
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,5 @@ dmypy.json
.vscode

# Demo Images
images/
images/
src.zip
18 changes: 13 additions & 5 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from taipy.gui import Gui
from taipy.gui import Gui, notify
from webcam import Webcam
import cv2

Expand All @@ -15,10 +15,11 @@
training_data_folder = Path("images")


def on_action_captured_image(state, id, action, payload):
def on_action_captured_image(state, id, payload):
print("Captured image")
choice = payload["args"][0]
if choice == 0:
notify(state, "i", "Adding image to database...")
# Add image to training data:
img = state.captured_image
file_name = str(uuid.uuid4()) + ".jpg"
Expand All @@ -29,6 +30,7 @@ def on_action_captured_image(state, id, action, payload):
label_file_path = Path(training_data_folder, "data.csv")
with label_file_path.open("a") as f:
f.write(f"{file_name},{label}\n")
notify(state, "s", "Image added to database")

state.captured_image = None
state.captured_label = ""
Expand All @@ -49,6 +51,7 @@ def process_image(state, frame):

# Capture image (actually we consider only the first detected face)
if state.capture_image and len(labeled_images) > 0:
notify(state, "i", "Capturing image...")
img = labeled_images[0][0]
label = labeled_images[0][2]
state.captured_image = cv2.imencode(".jpg", img)[1].tobytes()
Expand All @@ -57,9 +60,9 @@ def process_image(state, frame):
state.capture_image = False


def handle_image(state, action, args, value):
def handle_image(state, action, args):
print("Handling image...")
payload = value["args"][0]
payload = args["args"][0]
bytes = payload["data"]
logging.debug(f"Received data: {len(bytes)}")

Expand All @@ -73,14 +76,17 @@ def handle_image(state, action, args, value):
img = cv2.imread(temp_path, cv2.IMREAD_UNCHANGED)
except cv2.error as e:
logging.error(f"Failed to read image file: {e}")
notify(state, "e", f"Failed to read image file: {e}")
return
process_image(state, img)
# Finish. Tempfile is removed.


def button_retrain_clicked(state):
print("Retraining...")
notify(state, "i", "Retraining...")
train_face_recognizer(training_data_folder)
notify(state, "s", "Retrained!")


if __name__ == "__main__":
Expand All @@ -105,6 +111,8 @@ def button_retrain_clicked(state):
- How to detect and recognize faces in the image in real time using [OpenCV](https://opencv.org/).
Wait for your face to be detected. Then, capture your face, provide your name, and retrain the model.
<br/>
<card|card p-half|part|
Expand Down Expand Up @@ -135,4 +143,4 @@ def button_retrain_clicked(state):

gui = Gui(webcam_md)
gui.add_library(Webcam())
gui.run(port=9090)
gui.run(title='Face Recognition')
2 changes: 1 addition & 1 deletion src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
taipy==2.2
taipy==3.0
opencv-python-headless==4.7.0.72
opencv-contrib-python-headless==4.7.0.72
pillow

0 comments on commit 0f24a84

Please sign in to comment.