From 89915f8aaefd46d861328e99d27c67ad93cb2ad2 Mon Sep 17 00:00:00 2001 From: Imendin Date: Sun, 15 Dec 2024 12:07:05 +0100 Subject: [PATCH] Used retina --- .gitignore | 3 ++- README.md | 4 ++++ app.py | 43 +++++++++++++++++++++++++------------------ requirements.txt | 5 ++++- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index b694934..18b5dce 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.venv \ No newline at end of file +.venv +.conda \ No newline at end of file diff --git a/README.md b/README.md index c6c9a17..b75a19c 100644 --- a/README.md +++ b/README.md @@ -22,4 +22,8 @@ services: volumes: - ${PWD}/app_data/:/app/data/ networks: {} +``` + +```python +pip3 install -r requirements.txt ``` \ No newline at end of file diff --git a/app.py b/app.py index b5554d0..1aa76c4 100644 --- a/app.py +++ b/app.py @@ -3,8 +3,9 @@ import time import os import requests import json +from retinaface import RetinaFace -treashold = float(os.getenv('FACE_TRASHOLD', '0.5')) +treashold = float(os.getenv('FACE_TRASHOLD', '0.9')) sever = os.getenv('API_ROOT', 'localhost:8000') root_api_url = 'http://{}/local'.format(sever) base_path = '/app/data/' @@ -18,7 +19,6 @@ try: print("LOOP Started !!!", flush=True) r = requests.get('{}/anonymize'.format(root_api_url)) - face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') # print(r.text, flush=True) response = r.json() @@ -26,36 +26,43 @@ try: print(str(detection['id']) + " Anonymization Started", flush=True) img_path = "{}{}".format(base_path, detection['source_image_path']) - + #print(img_path, flush=True) #Skip Non existing files if (not os.path.exists(img_path)): continue img = cv2.imread(img_path) - gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) - faces, rejects, weights = face_cascade.detectMultiScale3(gray, 1.1, 4, outputRejectLevels = 1) + faces = RetinaFace.detect_faces(img, treashold) #Count faces is null dont anonimize file print(str(len(faces)) + " Faces Found" , flush=True) - if (len(faces) < 0): + if (len(faces) <= 0): + data = { + "id": detection['id'], + "source_image_path": img_path.replace(base_path,""), + "face_found": 0, + } + response = requests.post('{}/anonymize'.format(root_api_url), json=data) + print(str(detection['id']) + " Anonymized 0 faces" , flush=True) continue - + i = 0 - for (x, y, w, h) in faces: - confidence = float(weights[i]) + for (key) in faces: + confidence = faces[key]["score"] print(str(confidence) + " Faces Confidence" , flush=True) - - if (confidence > treashold): - face_roi = img[y:y+h, x:x+w] - blurred_face = cv2.GaussianBlur(face_roi, (99, 99), 50) - img[y:y+h, x:x+w] = blurred_face - print("Faces Anonymized" , flush=True) - text = "{}".format(weights[i]) - #cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2) - #cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2) + x1, y1 = min(faces[key]["facial_area"][0], faces[key]["facial_area"][2]) - 2, min(faces[key]["facial_area"][1], faces[key]["facial_area"][3]) - 2 + x2, y2 = max(faces[key]["facial_area"][0], faces[key]["facial_area"][2]) + 2, max(faces[key]["facial_area"][1], faces[key]["facial_area"][3]) + 2 + x1, x2 = max(x1, 0), min(x2, img.shape[1]) + y1, y2 = max(y1, 0), min(y2, img.shape[0]) + face_roi = img[y1:y2, x1:x2] + blurred_face = cv2.GaussianBlur(face_roi, (99, 99), 50) + img[y1:y2, x1:x2] = blurred_face + print("Faces Anonymized" , flush=True) i = i +1 + #cv2.imshow("test_img", img) + #cv2.waitKey(0) extension = img_path[img_path.index(".")+1:] new_img_path = img_path diff --git a/requirements.txt b/requirements.txt index d397baf..067e002 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,5 @@ opencv-python -requests \ No newline at end of file +numpy +requests +retina-face +tf-keras \ No newline at end of file